diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/agent.js | 34 | ||||
-rw-r--r-- | lib/ships.js | 16 | ||||
-rw-r--r-- | lib/systems.js | 19 |
3 files changed, 14 insertions, 55 deletions
diff --git a/lib/agent.js b/lib/agent.js deleted file mode 100644 index bf27e85..0000000 --- a/lib/agent.js +++ /dev/null @@ -1,34 +0,0 @@ -import { registerAgent } from '../database/config.js'; - -// This function inits the database in case we have an already registered game -export function init(symbol, faction, token) { - registerAgent(symbol, faction, token); - // TODO ships - // TODO contract - // TODO agent -} - -// This function registers then inits the database -export function register(symbol, faction) { - fetch( - 'https://api.spacetraders.io/v2/register', - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - symbol: symbol, - faction: faction, - }), - }) - .then(response => response.json()) - .then(response => { - console.log(JSON.stringify(response, null, 2)); - init(symbol, faction, response.data.token); - // TODO ship - // TODO contract - // TODO agent - }) - .catch(err => console.error(err)); -} diff --git a/lib/ships.js b/lib/ships.js index 109054c..55ee585 100644 --- a/lib/ships.js +++ b/lib/ships.js @@ -12,12 +12,14 @@ export async function extract(ctx) { default: // yet unhandled error throw response; } + } else { + await api.sleep(response.data.cooldown.remainingSeconds*1000); } return response; } export async function dock(ctx) { - const response = await api.send({endpoint: `/my/ships/${ctx.ship}/dock`, method: 'POST'}); + const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/dock`, method: 'POST'}); if (response.error !== undefined) { switch(response.error.code) { case 4214: // ship is in transit @@ -48,7 +50,17 @@ export async function negotiate(ctx) { } export async function orbit(ctx) { - return await api.send({endpoint: `/my/ships/${ctx.ship}/orbit`, method: 'POST'}); + const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/orbit`, method: 'POST'}); + if (response.error !== undefined) { + switch(response.error.code) { + case 4214: // ship is in transit + await api.sleep(response.error.data.secondsToArrival * 1000); + return orbit(ctx); + default: // yet unhandled error + throw response; + } + } + return response; } export async function purchase(ctx) { diff --git a/lib/systems.js b/lib/systems.js index 3cc7c7a..fcbf41a 100644 --- a/lib/systems.js +++ b/lib/systems.js @@ -1,25 +1,6 @@ import * as api from './api.js'; import * as db from '../database/systems.js'; -// Retrieves all systems information, should be called only once after registering -export async function init(ctx) { - if (db.isInit()) { - return; - } - for (let page=1; true; ++page) { - const response = await api.send({endpoint: `/systems?limit=20&page=${page}`, priority:100}); - if (response.error !== undefined) { - throw response; - } - response.data.forEach(system => db.setSystem(system)); - if (response.meta.total <= response.meta.limit * page) { - break; - } - } - console.log('Finished retrieving all systems information'); - db.init(); -} - // Retrieves a shipyard's information for ctx.symbol export async function shipyard(ctx) { const systemSymbol = ctx.symbol.match(/([^-]+-[^-]+)/)[1]; // TODO generalise this extraction |