From 8962158b46b345a681b9306c975b91b8cb26d821 Mon Sep 17 00:00:00 2001 From: AmorimDSJM Date: Fri, 1 Dec 2023 16:31:19 +0100 Subject: [PATCH] Fixing requests to server --- index.html | 3 +- js/main.js | 92 +++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 65 insertions(+), 30 deletions(-) diff --git a/index.html b/index.html index 4bbba0b..a94f17a 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,4 @@ +
@@ -9,6 +10,6 @@
- +
\ No newline at end of file diff --git a/js/main.js b/js/main.js index 9ddc4b0..4fec37c 100644 --- a/js/main.js +++ b/js/main.js @@ -1,5 +1,7 @@ +/* © 2023 Entreprise SpaceTarders */ 'use strict' - +let agents = []; + // ------------------- Create Agent ------------------- function createAgent(faction, symbol){ @@ -22,11 +24,6 @@ function createAgent(faction, symbol){ return response.data; }); } - - -$(document).ready(function() { -//Let agent = createAgent("COSMIC","agent_bryte"); -}) // ------------------- Get Agent ------------------- @@ -34,10 +31,16 @@ $(document).ready(function() { // ------------------- List Agent (Leaderboard) ------------------- +function sortAgentByCredits(a1, a2) { + if(a1.credits < a2.credits) + return -1 + if(a1.credits > a2.credits) + return 1 + return 0 +} - - -function listAgent(page = 1, limit = 20, agents = []){ +var meta = null +function listAgent(page, limit = 20){ const settings = { async: true, crossDomain: true, @@ -49,31 +52,62 @@ function listAgent(page = 1, limit = 20, agents = []){ data : { page: page, limit: limit + }, + success: function(response) { + meta = response.meta + agents = agents.concat(response.data); + console.log("Actioni") + if (response.meta.page == page) drawAgents(); + else setTimeout(10000, listAgent(page, limit)); } }; + $.ajax(settings); +} +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} - $.ajax(settings).done(function (response) { - const agents = response.data - console.log(response.meta) - agents.sort((a1, a2) => { - if(a1.credits < a2.credits) - return -1 - if(a1.credits > a2.credits) - return 1 - return 0 - }) - agents.reverse() - console.log(agents); - let nbTour = agents.Length; - for(i = 0; i < nbTour; i++) - { - let agents = listAgent(i, 20, false) +async function getAllAgents() { + const limit = 20 + const settings = { + async: true, + crossDomain: true, + url: 'https://api.spacetraders.io/v2/agents', + method: 'GET', + headers: { + Accept: 'application/json' + }, + data : { + page: 1, + limit: limit + }, + success: function(response) { + meta = response.meta + const total = Math.ceil(meta.total / limit); + listAgent(1, limit) } - }) + }; + $.ajax(settings); } -$(document).ready(function() { +function drawAgents() { + $(".leaderboard").html(""); + agents.sort(sortAgentByCredits); + agents.reverse(); + agents.forEach(agent => { + $(".leaderboard").append(` +

${agent.credits}

+

${agent.symbol}

+

${agent.headquarters}

+

${agent.startingFaction}

+

${agent.shipCount}

+ `); + }); +} -}) - \ No newline at end of file +$(document).ready(function() { + $(".btn-test").on("click", () => { + getAllAgents(); + }); +}); \ No newline at end of file