summaryrefslogtreecommitdiff
path: root/nodejs/lib
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--nodejs/lib/api.ts54
-rw-r--r--nodejs/lib/contracts.ts36
-rw-r--r--nodejs/lib/ships.ts58
-rw-r--r--nodejs/lib/systems.ts118
4 files changed, 119 insertions, 147 deletions
diff --git a/nodejs/lib/api.ts b/nodejs/lib/api.ts
index 08c6c71..f023ccf 100644
--- a/nodejs/lib/api.ts
+++ b/nodejs/lib/api.ts
@@ -1,9 +1,10 @@
-import * as fs from 'fs';
-import * as events from 'events';
+import fs from 'fs';
+import events from 'events';
-import { APIError, Request, RequestPromise, Response } from '../model/api.ts';
-import { getToken } from '../database/tokens.ts';
import { PriorityQueue } from './priority_queue.ts';
+import { getToken } from '../database/tokens.ts';
+import { APIError, Request, RequestPromise, Response } from '../model/api.ts';
+import { RateLimitError } from '../model/errors.ts';
// queue processor module variables
const bus = new events.EventEmitter(); // a bus to notify the queue processor to start processing messages
@@ -14,7 +15,7 @@ let running = false;
let headers: {[key:string]:string}|null = null; // a file scoped variable so that we only evaluate these once.
let queue = new PriorityQueue(); // a priority queue to hold api calls we want to send, allows for throttling.
-async function queue_processor() {
+async function queue_processor(): Promise<void> {
if (running) {
throw 'refusing to start a second queue processor';
}
@@ -45,19 +46,29 @@ async function queue_processor() {
}
queue_processor();
-export async function send<T>(request: Request): Promise<T|APIError> {
- const response = await send_one<T>(request);
- if (response.error) return response.error;
- return response.data;
+export async function send<T>(request: Request): Promise<Response<T>> {
+ return new Promise((resolve, reject) => {
+ const data: RequestPromise<T> = {
+ reject: reject,
+ request: request,
+ resolve: resolve,
+ };
+ queue.enqueue(data, request.priority ? request.priority : 10);
+ if (!busy) {
+ bus.emit('send'); // the queue was previously empty, let's wake up the queue_processor
+ }
+ });
}
-export async function sendPaginated<T>(request: Request): Promise<Array<T>|APIError> {
+export async function sendPaginated<T>(request: Request): Promise<Array<T>> {
if (request.page === undefined) request.page = 1;
let ret: Array<T> = [];
while (true) {
- const response = await send_one<T>(request);
+ const response = await send<T>(request);
if (response.meta === undefined) {
throw {"message": "paginated request did not return a meta block", "request": request, "response": response};
+ } else if (response.error) {
+ throw {"message": "paginated request returned an error", "request": request, "response": response};
}
ret = ret.concat(response.data);
if (response.meta.limit * response.meta.page >= response.meta.total) {
@@ -67,22 +78,8 @@ export async function sendPaginated<T>(request: Request): Promise<Array<T>|APIEr
}
}
-function send_one<T>(request: Request): Promise<Response<T>> {
- return new Promise((resolve, reject) => {
- const data: RequestPromise<T> = {
- reject: reject,
- request: request,
- resolve: resolve,
- };
- queue.enqueue(data, request.priority ? request.priority : 10);
- if (!busy) {
- bus.emit('send'); // the queue was previously empty, let's wake up the queue_processor
- }
- });
-}
-
// send_this take a data object as argument built in the send function above
-async function send_this(data: RequestPromise<unknown>) {
+async function send_this(data: RequestPromise<unknown>): Promise<void> {
if (headers === null) {
const token = getToken();
if (token === null) {
@@ -119,7 +116,8 @@ async function send_this(data: RequestPromise<unknown>) {
// spawnSync?
// break;
case 429: // 429 means rate limited, let's hold back as instructed
- backoffSeconds = json.error.data.retryAfter;
+ const errorData = json.error.data as RateLimitError;
+ backoffSeconds = errorData.retryAfter;
queue.enqueue(data, 1);
break;
case 503: // 503 means maintenance mode, let's hold back for 1 minute
@@ -150,7 +148,7 @@ async function send_this(data: RequestPromise<unknown>) {
}
}
-export function debugLog(ctx: any) {
+export function debugLog(ctx: any): void {
console.log(`--- ${Date()} -----------------------------------------------------------------------------`);
console.log(JSON.stringify(ctx, null, 2));
}
diff --git a/nodejs/lib/contracts.ts b/nodejs/lib/contracts.ts
index fb62813..0adb5f6 100644
--- a/nodejs/lib/contracts.ts
+++ b/nodejs/lib/contracts.ts
@@ -12,21 +12,17 @@ import * as libShips from '../lib/ships.ts';
export async function accept(contract: Contract): Promise<Contract> {
if (contract.accepted) return contract;
const response = await api.send<{agent: Agent, contract: Contract, type: ''}>({endpoint: `/my/contracts/${contract.id}/accept`, method: 'POST'});
- if ('apiError' in response) {
+ if (response.error) {
api.debugLog(response);
throw response;
}
- dbAgents.setAgent(response.agent);
- dbContracts.setContract(response.contract);
- return response.contract;
+ dbAgents.setAgent(response.data.agent);
+ dbContracts.setContract(response.data.contract);
+ return response.data.contract;
}
export async function contracts(): Promise<Array<Contract>> {
- const response = await api.sendPaginated<Contract>({endpoint: '/my/contracts', page: 1});
- if ('apiError' in response) {
- api.debugLog(response);
- throw response;
- }
+ const response = await api.sendPaginated<Contract>({endpoint: '/my/contracts'});
response.forEach(contract => dbContracts.setContract(contract));
return response;
}
@@ -44,8 +40,8 @@ export async function deliver(contract: Contract, ship: Ship): Promise<Contract>
tradeSymbol: tradeSymbol,
units: units,
}});
- if ('apiError' in response) {
- switch(response.code) {
+ if (response.error) {
+ switch(response.error.code) {
case 4509: // contract delivery terms have been met
return await fulfill(contract);
default: // yet unhandled error
@@ -53,22 +49,22 @@ export async function deliver(contract: Contract, ship: Ship): Promise<Contract>
throw response;
}
}
- dbContracts.setContract(response.contract);
- dbShips.setShipCargo(ship.symbol, response.cargo);
- if(response.contract.terms.deliver[0].unitsRequired >= response.contract.terms.deliver[0].unitsFulfilled) {
- return await fulfill(contract);
+ dbContracts.setContract(response.data.contract);
+ dbShips.setShipCargo(ship.symbol, response.data.cargo);
+ if(response.data.contract.terms.deliver[0].unitsRequired >= response.data.contract.terms.deliver[0].unitsFulfilled) {
+ return await fulfill(response.data.contract);
}
- return response.contract;
+ return response.data.contract;
}
export async function fulfill(contract: Contract): Promise<Contract> {
if (contract.fulfilled) return contract;
const response = await api.send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${contract.id}/fulfill`, method: 'POST'});
- if ('apiError' in response) {
+ if (response.error) {
api.debugLog(response);
throw response;
}
- dbAgents.setAgent(response.agent);
- dbContracts.setContract(response.contract);
- return response.contract;
+ dbAgents.setAgent(response.data.agent);
+ dbContracts.setContract(response.data.contract);
+ return response.data.contract;
}
diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts
index 2e04dd4..955824e 100644
--- a/nodejs/lib/ships.ts
+++ b/nodejs/lib/ships.ts
@@ -2,19 +2,20 @@ import { Response } from '../model/api.ts';
import { Agent } from '../model/agent.ts';
import { Cargo } from '../model/cargo.ts';
import { Cooldown, Fuel, Nav, Ship } from '../model/ship.ts';
-import * as api from './api.js';
+import * as api from './api.ts';
import * as dbAgents from '../database/agents.ts';
-import * as dbShips from '../database/ships.js';
-import * as dbSurveys from '../database/surveys.js';
-import * as systems from '../lib/systems.js';
+import * as dbShips from '../database/ships.ts';
+//import * as dbSurveys from '../database/surveys.ts';
+import * as systems from '../lib/systems.ts';
export async function dock(ship: Ship): Promise<Ship> {
if (ship.nav.status === 'DOCKED') return ship;
- const response = await api.send({endpoint: `/my/ships/${ship.symbol}/dock`, method: 'POST'}) as Response<{nav: Nav}>;
- if (response.error !== undefined) {
+ const response = await api.send<{nav: Nav}>({endpoint: `/my/ships/${ship.symbol}/dock`, method: 'POST'});
+ if (response.error) {
switch(response.error.code) {
case 4214: // ship is in transit
- await api.sleep(response.error.data.secondsToArrival * 1000);
+ const errorData = response.error.data as { secondsToArrival: number};
+ await api.sleep(errorData.secondsToArrival * 1000);
return await dock(ship);
default: // yet unhandled error
api.debugLog(response);
@@ -33,11 +34,12 @@ export async function extract(ship: Ship): Promise<Ship> {
//await navigate({symbol: ctx.symbol, waypoint: asteroidFields[0].symbol});
ship = await orbit(ship);
// TODO handle surveying?
- const response = await api.send({endpoint: `/my/ships/${ship.symbol}/extract`, method: 'POST'}) as Response<{cooldown: Cooldown, cargo: Cargo}>; // TODO extraction and events api response fields cf https://spacetraders.stoplight.io/docs/spacetraders/b3931d097608d-extract-resources
- if (response.error !== undefined) {
+ const response = await api.send<{cooldown: Cooldown, cargo: Cargo}>({endpoint: `/my/ships/${ship.symbol}/extract`, method: 'POST'}); // TODO extraction and events api response fields cf https://spacetraders.stoplight.io/docs/spacetraders/b3931d097608d-extract-resources
+ if (response.error) {
switch(response.error.code) {
case 4000: // ship is on cooldown
- await api.sleep(response.error.data.cooldown.remainingSeconds * 1000);
+ const errorData = response.error.data as {cooldown: Cooldown};
+ await api.sleep(errorData.cooldown.remainingSeconds * 1000);
return await extract(ship);
case 4228: // ship is full
return ship;
@@ -69,11 +71,12 @@ export async function navigate(ship: Ship, waypoint: string): Promise<Ship> {
if (ship.nav.waypointSymbol === waypoint) return ship;
ship = await orbit(ship);
// TODO if we do not have enough fuel, make a stop to refuel along the way or drift to the destination
- const response = await api.send({endpoint: `/my/ships/${ship.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: waypoint }}) as Response<{fuel: Fuel, nav: Nav}>; // TODO events field
- if (response.error !== undefined) {
+ const response = await api.send<{fuel: Fuel, nav: Nav}>({endpoint: `/my/ships/${ship.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: waypoint }}); // TODO events field
+ if (response.error) {
switch(response.error.code) {
case 4214: // ship is in transit
- await api.sleep(response.error.data.secondsToArrival * 1000);
+ const errorData = response.error.data as { secondsToArrival: number};
+ await api.sleep(errorData.secondsToArrival * 1000);
return await navigate(ship, waypoint);
default: // yet unhandled error
api.debugLog(response);
@@ -89,8 +92,8 @@ export async function navigate(ship: Ship, waypoint: string): Promise<Ship> {
response.data.nav.status = 'IN_ORBIT'; // we arrive in orbit
dbShips.setShipNav(ship.symbol, response.data.nav);
ship.nav = response.data.nav
- ship = await refuel(ship);
- return ship;
+ // TODO only refuel at the start of a journey, if we do not have enough OR if the destination does not sell fuel?
+ return await refuel(ship);
}
//export async function negotiate(ctx) {
@@ -100,11 +103,12 @@ export async function navigate(ship: Ship, waypoint: string): Promise<Ship> {
export async function orbit(ship: Ship): Promise<Ship> {
if (ship.nav.status === 'IN_ORBIT') return ship;
- const response = await api.send({endpoint: `/my/ships/${ship.symbol}/orbit`, method: 'POST'}) as Response<{nav: Nav}>;
- if (response.error !== undefined) {
+ const response = await api.send<{nav: Nav}>({endpoint: `/my/ships/${ship.symbol}/orbit`, method: 'POST'});
+ if (response.error) {
switch(response.error.code) {
case 4214: // ship is in transit
- await api.sleep(response.error.data.secondsToArrival * 1000);
+ const errorData = response.error.data as { secondsToArrival: number};
+ await api.sleep(errorData.secondsToArrival * 1000);
return await orbit(ship);
default: // yet unhandled error
throw response;
@@ -131,8 +135,8 @@ export async function refuel(ship: Ship): Promise<Ship> {
if (ship.fuel.current >= ship.fuel.capacity * 0.9) return ship;
// TODO check if our current waypoint has a marketplace (and sells fuel)?
ship = await dock(ship);
- const response = await api.send({endpoint: `/my/ships/${ship.symbol}/refuel`, method: 'POST'}) as Response<{agent: Agent, fuel: Fuel}>; // TODO transaction field
- if (response.error !== undefined) {
+ const response = await api.send<{agent: Agent, fuel: Fuel}>({endpoint: `/my/ships/${ship.symbol}/refuel`, method: 'POST'}); // TODO transaction field
+ if (response.error) {
api.debugLog(response);
throw response;
}
@@ -143,12 +147,12 @@ export async function refuel(ship: Ship): Promise<Ship> {
}
export async function sell(ship: Ship, tradeSymbol: string): Promise<Ship> {
- // TODO check if our current waypoint has a marketplace?
+ // TODO check if our current waypoint has a marketplace and buys tradeSymbol?
ship = await dock(ship);
let units = 0;
ship.cargo.inventory.forEach(i => {if (i.symbol === tradeSymbol) units = i.units; });
- const response = await api.send({endpoint: `/my/ships/${ship.symbol}/sell`, method: 'POST', payload: { symbol: tradeSymbol, units: units }}) as Response<{agent: Agent, cargo: Cargo}>; // TODO transaction field
- if (response.error !== undefined) {
+ const response = await api.send<{agent: Agent, cargo: Cargo}>({endpoint: `/my/ships/${ship.symbol}/sell`, method: 'POST', payload: { symbol: tradeSymbol, units: units }}); // TODO transaction field
+ if (response.error) {
api.debugLog(response);
throw response;
}
@@ -159,8 +163,8 @@ export async function sell(ship: Ship, tradeSymbol: string): Promise<Ship> {
}
export async function ships(): Promise<Array<Ship>> {
- const response = await api.send({endpoint: `/my/ships`, page: 1}) as Response<Array<Ship>>;
- if (response.error !== undefined) {
+ const response = await api.send<Array<Ship>>({endpoint: `/my/ships`, page: 1});
+ if (response.error) {
api.debugLog(response);
throw response;
}
@@ -169,8 +173,8 @@ export async function ships(): Promise<Array<Ship>> {
}
export async function ship(ship: Ship): Promise<Ship> {
- const response = await api.send({endpoint: `/my/ships/${ship.symbol}`}) as Response<Ship>;
- if (response.error !== undefined) {
+ const response = await api.send<Ship>({endpoint: `/my/ships/${ship.symbol}`});
+ if (response.error) {
api.debugLog(response);
throw response;
}
diff --git a/nodejs/lib/systems.ts b/nodejs/lib/systems.ts
index 94ebab7..dc938bb 100644
--- a/nodejs/lib/systems.ts
+++ b/nodejs/lib/systems.ts
@@ -1,86 +1,60 @@
-import * as api from './api.js';
-import * as dbMarkets from '../database/markets.js';
-import * as dbShips from '../database/ships.js';
-import * as dbSystems from '../database/systems.js';
-import * as utils from './utils.js';
+import * as api from './api.ts';
+import * as dbMarkets from '../database/markets.ts';
+import * as dbShips from '../database/ships.ts';
+import * as dbSystems from '../database/systems.ts';
+import { Market } from '../model/market.ts'
+import { System, Waypoint } from '../model/system.ts'
+import * as utils from './utils.ts';
-// Retrieves a marketplace's market data for waypointSymbol
-export async function market(waypointSymbol: string) {
+export async function market(waypointSymbol: string): Promise<Market> {
const data = dbMarkets.getMarketAtWaypoint(waypointSymbol);
- if (data === null) {
- if (dbShips.getShipsAt(waypointSymbol) === null) {
- return null;
- }
- const systemSymbol = utils.systemFromWaypoint(waypointSymbol);
- let d = await api.send({endpoint: `/systems/${systemSymbol}/waypoints/${waypointSymbol}/market`});
- delete d.data.transactions;
- dbMarkets.setMarket(d.data);
- return d;
- }
- return data;
+ if (data) { return data; }
+ const systemSymbol = utils.systemFromWaypoint(waypointSymbol);
+ let response = await api.send<Market>({endpoint: `/systems/${systemSymbol}/waypoints/${waypointSymbol}/market`});
+ if (response.error) {
+ api.debugLog(response);
+ throw response;
+ }
+ dbMarkets.setMarket(response.data);
+ return response.data;
}
-// Retrieves a shipyard's information for ctx.symbol
-export async function shipyard(ctx) {
- const systemSymbol = utils.systemFromWaypoint(ctx.symbol);
- return await api.send({endpoint: `/systems/${systemSymbol}/waypoints/${ctx.symbol}/shipyard`});
-}
+//export async function shipyard(waypoint: string): Promise<unknown> {
+// // TODO database caching
+// const systemSymbol = utils.systemFromWaypoint(waypoint);
+// return await api.send({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint}/shipyard`});
+//}
-// Retrieves the system's information for ctx.symbol and caches it in the database
-export async function system(ctx) {
- let s = dbSystems.getSystem(ctx.symbol);
- if (s === null) {
- const response = await api.send({endpoint: `/systems/${ctx.symbol}`});
- if (response.error !== undefined) {
- switch(response.error.code) {
- case 404:
- throw `Error retrieving info for system ${ctx.symbol}: ${response.error.message}`;
- default: // yet unhandled error
- throw response;
- }
- }
- s = response.data;
- dbSystems.setSystem(s);
+export async function system(symbol: string): Promise<System> {
+ let data = dbSystems.getSystem(symbol);
+ if (data) { return data; }
+ const response = await api.send<System>({endpoint: `/systems/${symbol}`});
+ if (response.error) {
+ api.debugLog(response);
+ throw response;
}
- return s;
+ dbSystems.setSystem(response.data);
+ return response.data;
}
-// Retrieves a list of waypoints that have a specific ctx.trait like a SHIPYARD or a MARKETPLACE in the system ctx.symbol
-export async function trait(ctx) {
- const w = await waypoints(ctx);
- return w.filter(s => s.traits.some(t => t.symbol === ctx.trait));
+// Retrieves a list of waypoints that have a specific trait like a SHIPYARD or a MARKETPLACE
+export async function trait(system: string, trait: string): Promise<Array<Waypoint>> {
+ const ws = await waypoints(system);
+ return ws.filter(w => w.traits.some(t => t.symbol === trait));
}
-// Retrieves a list of waypoints that have a specific ctx.type like ASTEROID_FIELD in the system ctx.symbol
-export async function type(ctx, response) {
- const w = await waypoints(ctx);
- return w.filter(s => s.type === ctx.type);
+// Retrieves a list of waypoints that have a specific type like ASTEROID_FIELD
+export async function type(system: string, typeSymbol: string): Promise<Array<Waypoint>> {
+ const ws = await waypoints(system);
+ return ws.filter(s => s.type === typeSymbol);
}
-// Retrieves the system's information for ctx.symbol and caches it in the database
-export async function waypoints(ctx) {
- await system(ctx);
- let updated = dbSystems.getSystemUpdated(ctx.symbol);
+export async function waypoints(systemSymbol: string): Promise<Array<Waypoint>> {
+ const s = await system(systemSymbol);
+ const updated = dbSystems.getSystemUpdated(systemSymbol);
// TODO handle uncharted systems
- if (updated === null) {
- let waypoints = [];
- for (let page=1; true; ++page) {
- const response = await api.send({endpoint: `/systems/${ctx.symbol}/waypoints?limit=20&page=${page}`, priority: 98});
- if (response.error !== undefined) {
- switch(response.error.code) {
- case 404:
- throw `Error retrieving waypoints for system ${ctx.symbol}: ${response.error.message}`;
- default: // yet unhandled error
- throw response;
- }
- }
- waypoints = waypoints.concat(response.data);
- if (response.meta.total <= response.meta.limit * page) {
- break;
- }
- }
- dbSystems.setSystemWaypoints(ctx.symbol, waypoints);
- return waypoints;
- }
- return dbSystems.getSystem(ctx.symbol).waypoints;
+ if (updated) return s.waypoints;
+ const waypoints = await api.sendPaginated<Waypoint>({endpoint: `/systems/${systemSymbol}/waypoints`});
+ dbSystems.setSystemWaypoints(systemSymbol, waypoints);
+ return waypoints;
}