Fixing requests to server

agent_bryte
AmorimDSJM ago%!(EXTRA string=2 years)
parent 67e2dbf213
commit 8962158b46
  1. 3
      index.html
  2. 92
      js/main.js

@ -1,3 +1,4 @@
<!-- © 2023 Entreprise SpaceTarders -->
<!DOCTYPE html>
<header>
<meta charset="UTF-8">
@ -9,6 +10,6 @@
<body>
<div class="leaderboard">
<button class="btn-test"></button>
</div>
</body>

@ -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(`
<p>${agent.credits}</p>
<p>${agent.symbol}</p>
<p>${agent.headquarters}</p>
<p>${agent.startingFaction}</p>
<p>${agent.shipCount}</p>
`);
});
}
})
$(document).ready(function() {
$(".btn-test").on("click", () => {
getAllAgents();
});
});
Loading…
Cancel
Save