From c91a3b6f72192ccc5008d83561e68365d5be55a5 Mon Sep 17 00:00:00 2001 From: Makaci Michael Gabriel Date: Fri, 24 Nov 2023 16:11:34 +0100 Subject: [PATCH] Ajout createAgent, getAgent, listAgents, getPublicAgent, getWaypoint, getMarket et correction de listSystems et de listWaypointsInSystems --- .gitignore | 2 - index.html | 16 +++++++ scripts/agent.js | 84 +++++++++++++++++++++++++++++++++ scripts/main.js | 29 ++++++++++++ scripts/systems.js | 113 +++++++++++++++++++++++++++++++++++++++++++++ systems.js | 72 ----------------------------- 6 files changed, 242 insertions(+), 74 deletions(-) create mode 100644 index.html create mode 100644 scripts/agent.js create mode 100644 scripts/main.js create mode 100644 scripts/systems.js delete mode 100644 systems.js diff --git a/.gitignore b/.gitignore index 76b6561..e69de29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +0,0 @@ -index.html -script.js \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..6e64a39 --- /dev/null +++ b/index.html @@ -0,0 +1,16 @@ + + + + + + + + + SpaceTraders + + + diff --git a/scripts/agent.js b/scripts/agent.js new file mode 100644 index 0000000..b2f971c --- /dev/null +++ b/scripts/agent.js @@ -0,0 +1,84 @@ +export async function createAgent(symbol, faction) { + let agent; + + await $.ajax("https://api.spacetraders.io/v2/register", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + data: JSON.stringify({ + symbol: symbol, + faction: faction, + }), + success: function (response) { + agent = response.data; + }, + error: function (error) { + console.log(error); + }, + }); + + return agent; +} + +export async function getAgent(token) { + let agent; + + await $.ajax("https://api.spacetraders.io/v2/my/agent", { + method: "GET", + headers: { + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + success: function (response) { + agent = response.data; + }, + error: function (error) { + console.log(error); + }, + }); + + return agent; +} + +export async function listAgents(limit, page) { + let agents; + + await $.ajax("https://api.spacetraders.io/v2/agents", { + method: "GET", + headers: { + Accept: "application/json", + }, + data: { + limit: limit, + page: page, + }, + success: function (response) { + agents = response.data; + }, + error: function (error) { + console.log(error); + }, + }); + + return agents; +} + +export async function getPublicAgent(agentSymbol) { + let agent; + + await $.ajax(`https://api.spacetraders.io/v2/agents/${agentSymbol}`, { + method: "GET", + headers: { + Accept: "application/json", + }, + success: function (response) { + agent = response.data; + }, + error: function (error) { + console.log(error); + }, + }); + + return agent; +} diff --git a/scripts/main.js b/scripts/main.js new file mode 100644 index 0000000..e459b9d --- /dev/null +++ b/scripts/main.js @@ -0,0 +1,29 @@ +"use strict"; + +import { createAgent } from "./agent.js"; +import { getAgent } from "./agent.js"; +import { listAgents } from "./agent.js"; +import { getPublicAgent } from "./agent.js"; + +import { listSystems } from "./systems.js"; +import { getSystem } from "./systems.js"; +import { listWaypointsInSystem } from "./systems.js"; +import { getWaypoint } from "./systems.js"; +import { getMarket } from "./systems.js"; + +const token = + "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiVEVTVDUzNTYiLCJ2ZXJzaW9uIjoidjIuMS4yIiwicmVzZXRfZGF0ZSI6IjIwMjMtMTEtMTgiLCJpYXQiOjE3MDA4MzEzOTEsInN1YiI6ImFnZW50LXRva2VuIn0.H4C3rNwgaBf6ych4txV7WO3jwt-ZAsb6jWSnQ3EMZfO7BgVbUW00a3uMtiQ7qCBuZ91YnmtUL8PZSRnR1RzCAjUd6Y64Kwt8cARgSZ56a08zIreXQ66WsXpm-pVXHKlrD7LeA9sHzZGhD9yADnghbJmCy6UoiYhgr8I7OwL9EIf-nb3B5l2UTchiNHTNmKjsggycQDDaK2yCcKXhy6rro8-ptogU5QFFYiIbshCiEos4Sc-CHbKci-DQpzWb9FcsNntb1PdZcm2hhjGDi4KD8Q1Ccvd-m1vTH4SwV1xt66tT5SMMuBA7nblsC2DlNV682PZi27XcpibMIyWoSQ938w"; + +// const systemSym = "X1-KD70"; +// const waypointSym = "X1-KD70-AA1X"; + +$(document).ready(async function () { + let systemSymbol = await listSystems(1, 1); + systemSymbol = systemSymbol[0].symbol; + + let waypointSymbol = await listWaypointsInSystem(1, 1, systemSymbol); + waypointSymbol = waypointSymbol[0].symbol; + + let market = await getMarket(systemSymbol, waypointSymbol, token); + console.log(market); +}); diff --git a/scripts/systems.js b/scripts/systems.js new file mode 100644 index 0000000..c5d3587 --- /dev/null +++ b/scripts/systems.js @@ -0,0 +1,113 @@ +export async function listSystems(limit, page) { + let systems; + + await $.ajax("https://api.spacetraders.io/v2/systems/", { + method: "GET", + headers: { + Accept: "application/json", + }, + data: { + limit: limit, + page: page, + }, + success: function (response) { + systems = response.data; + }, + error: function (error) { + console.log(error); + }, + }); + + return systems; +} + +export async function getSystem(systemSymbol) { + let system; + + await $.ajax(`https://api.spacetraders.io/v2/systems/${systemSymbol}`, { + method: "GET", + headers: { + Accept: "application/json", + }, + success: function (response) { + system = response.data; + }, + error: function (error) { + console.log(error); + }, + }); + + return system; +} + +export async function listWaypointsInSystem(limit, page, systemSymbol) { + let waypoints; + + await $.ajax( + `https://api.spacetraders.io/v2/systems/${systemSymbol}/waypoints`, + { + method: "GET", + headers: { + Accept: "application/json", + }, + data: { + limit: limit, + page: page, + }, + success: function (response) { + waypoints = response.data; + }, + error: function (error) { + console.log(error); + }, + } + ); + + return waypoints; +} + +export async function getWaypoint(systemSymbol, waypointSymbol) { + let waypoint; + + await $.ajax( + `https://api.spacetraders.io/v2/systems/${systemSymbol}/waypoints/${waypointSymbol}`, + { + method: "GET", + headers: { + Accept: "application/json", + }, + success: function (response) { + waypoint = response.data; + }, + error: function (error) { + console.log(error); + }, + } + ); + + return waypoint; +} + +export async function getMarket(systemSymbol, waypointSymbol, token) { + let market; + + await $.ajax( + `https://api.spacetraders.io/v2/systems/X1-KD70/waypoints/X1-KD70-AA1X/market`, + { + method: "GET", + headers: { + Accept: "application/json", + Authorization: `Bearer ${token}`, + }, + success: function (response) { + console.log(response); + market = response; + }, + error: function (error) { + console.log(error); + }, + } + ); + + return market; +} diff --git a/systems.js b/systems.js deleted file mode 100644 index d542c64..0000000 --- a/systems.js +++ /dev/null @@ -1,72 +0,0 @@ -export async function listSystems(limit, pages){ - let systems = []; - - for(let page = 1; page <= pages; page++){ - await $.ajax('https://api.spacetraders.io/v2/systems/', { - method: 'GET', - headers: { - Accept: 'application/json', - }, - data: { - limit: limit, - page: page - }, - success: function(response){ - response.data.forEach(sytem => { - systems.push(sytem); - }); - }, - error: function(error){ - console.log(error); - } - }); - } - - return systems; -} - -export async function getSystem(systemSymbol){ - let system; - - await $.ajax(`https://api.spacetraders.io/v2/systems/${systemSymbol}`, { - method: 'GET', - headers: { - Accept: 'application/json' - }, - success: function(response){ - system = response.data; - }, - error: function(error){ - console.log(error); - } - }); - - return system; -} - -export async function listWaypointsInSystem(limit, pages, systemSymbol){ - let waypoints = []; - - for(let page = 1; page <= pages; page++){ - await $.ajax(`https://api.spacetraders.io/v2/systems/${systemSymbol}/waypoints`, { - method: 'GET', - headers: { - Accept: 'application/json', - }, - data: { - limit: limit, - page: page - }, - success: function(response){ - response.data.forEach(waypoint => { - waypoints.push(waypoint); - }); - }, - error: function(error){ - console.log(error); - } - }); - } - - return waypoints; -} \ No newline at end of file