summaryrefslogtreecommitdiff
path: root/nodejs/lib
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-01 23:13:13 +0200
committerJulien Dessaux2023-07-01 23:13:13 +0200
commit36cc33f9e96a38ecea98ac8d26275b4828347d80 (patch)
tree653dcea7e656ec815fc0a1fa5664a6b89abccaa3 /nodejs/lib
parentFixed prepared statements (diff)
downloadspacetraders-36cc33f9e96a38ecea98ac8d26275b4828347d80.tar.gz
spacetraders-36cc33f9e96a38ecea98ac8d26275b4828347d80.tar.bz2
spacetraders-36cc33f9e96a38ecea98ac8d26275b4828347d80.zip
Moved the nodejs agent to its own subfolder to make room for my haskell agent
Diffstat (limited to 'nodejs/lib')
-rw-r--r--nodejs/lib/api.js140
-rw-r--r--nodejs/lib/contracts.js26
-rw-r--r--nodejs/lib/priority_queue.js39
-rw-r--r--nodejs/lib/ships.js187
-rw-r--r--nodejs/lib/systems.js68
5 files changed, 460 insertions, 0 deletions
diff --git a/nodejs/lib/api.js b/nodejs/lib/api.js
new file mode 100644
index 0000000..667ef37
--- /dev/null
+++ b/nodejs/lib/api.js
@@ -0,0 +1,140 @@
+import * as fs from 'fs';
+import * as events from 'events';
+
+import { getToken } from '../database/config.js';
+import { PriorityQueue } from './priority_queue.js';
+
+// queue processor module variables
+const bus = new events.EventEmitter(); // a bus to notify the queue processor to start processing messages
+let busy = false; // true if we are already sending api requests.
+let backoffSeconds = 0;
+let running = false;
+// other module variables
+let headers = undefined; // a file scope 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.
+
+// a single queue processor should be running at any time, otherwise there will be trouble!
+async function queue_processor() {
+ if (running) {
+ throw 'refusing to start a second queue processor';
+ }
+ running = true;
+ while(true) {
+ try {
+ if (backoffSeconds > 0) {
+ await sleep(backoffSeconds * 1000);
+ backoffSeconds = 0;
+ }
+ if (queue.isEmpty()) {
+ busy = false;
+ await new Promise(resolve => bus.once('send', resolve));
+ busy = true;
+ }
+ const before = new Date();
+ await send_this(queue.dequeue().element);
+ const duration = new Date() - before;
+ if (duration < 400) { // 333 should work, but 400 should still allow some manual requests to go through during development
+ await sleep(400 - duration);
+ }
+ } catch (e) {
+ running = false;
+ throw e;
+ }
+ }
+}
+queue_processor();
+
+// send takes a request object as argument and an optional context ctx
+// example request: {
+// endpoint: the path part of the url to call,
+// method: HTTP method for `fetch` call, defaults to 'GET',
+// payload: optional json object that will be send along with the request,
+// priority: optional priority value (defaults to 10, lower than 10 means the message will be sent faster)
+// }
+export function send(request, ctx) {
+ return new Promise((resolve, reject) => {
+ let data = {
+ ctx: ctx,
+ 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) {
+ if (headers === undefined) {
+ const token = getToken();
+ if (token === null) {
+ throw 'Could not get token from the database. Did you init or register yet?';
+ }
+ headers = {
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${token}`
+ };
+ }
+ let options = {
+ headers: headers,
+ };
+ if (data.request.method !== undefined) {
+ options['method'] = data.request.method;
+ }
+ if (data.request.payload !== undefined) {
+ options['body'] = JSON.stringify(data.request.payload);
+ }
+ fs.writeFileSync('log', JSON.stringify({event: 'send', date: new Date(), data: data}) + '\n', {flag: 'a+'});
+ try {
+ let response = await fetch(`https://api.spacetraders.io/v2${data.request.endpoint}`, options);
+ response = await response.json();
+ switch(response.error?.code) {
+ //case 401: // TODO 401 means a server reset happened
+ // TODO reject all promises in queue
+ // close database file
+ // rm database file
+ // logrotate
+ // spawnSync
+ // break;
+ case 429: // 429 means rate limited, let's hold back as instructed
+ backoffSeconds = response.error.data.retryAfter;
+ queue.enqueue(data, 1);
+ break;
+ case 503: // 503 means maintenance mode, let's hold back for 1 minute
+ backoffSeconds = 60;
+ queue.enqueue(data, 1);
+ break;
+ default: // no error!
+ fs.writeFileSync('log', JSON.stringify({event: 'response', date: new Date(), data: response}) + '\n', {flag: 'a+'});
+ return data.resolve(response);
+ }
+ } catch (err) {
+ fs.writeFileSync('log', JSON.stringify({event: 'error', date: new Date(), data: err}) + '\n', {flag: 'a+'});
+ switch(err.cause?.code) {
+ case 'EAI_AGAIN': // DNS lookup timed out error, let's hold back for 5 seconds
+ backoffSeconds = 5;
+ queue.enqueue(data, 1);
+ break;
+ case 'ECONNRESET':
+ queue.enqueue(data, 1);
+ break;
+ case 'UND_ERR_CONNECT_TIMEOUT':
+ queue.enqueue(data, 1);
+ break;
+ default:
+ data.reject(response);
+ }
+ }
+}
+
+export function debugLog(ctx) {
+ console.log(`--- ${Date()} -----------------------------------------------------------------------------`);
+ console.log(JSON.stringify(ctx, null, 2));
+}
+
+export function sleep(delay) {
+ return new Promise((resolve) => setTimeout(resolve, delay))
+}
diff --git a/nodejs/lib/contracts.js b/nodejs/lib/contracts.js
new file mode 100644
index 0000000..316e181
--- /dev/null
+++ b/nodejs/lib/contracts.js
@@ -0,0 +1,26 @@
+import * as api from './api.js';
+import * as dbShips from '../database/ships.js';
+
+export async function accept(ctx) {
+ return await api.send({endpoint: `/my/contracts/${ctx.contract}/accept`, method: 'POST'});
+}
+
+export async function contracts() {
+ return await api.send({endpoint: '/my/contracts'});
+}
+
+export async function deliver(ctx) {
+ const response = await api.send({ endpoint: `/my/contracts/${ctx.contract}/deliver`, method: 'POST', payload: {
+ shipSymbol: ctx.symbol,
+ tradeSymbol: ctx.good,
+ units: ctx.units,
+ }});
+ if (response.error !== undefined) {
+ throw response;
+ }
+ dbShips.setShipCargo(ctx.symbol, response.data.cargo);
+}
+
+export async function fulfill(ctx) {
+ return await api.send({ endpoint: `/my/contracts/${ctx.contract}/fulfill`, method: 'POST'});
+}
diff --git a/nodejs/lib/priority_queue.js b/nodejs/lib/priority_queue.js
new file mode 100644
index 0000000..da526b2
--- /dev/null
+++ b/nodejs/lib/priority_queue.js
@@ -0,0 +1,39 @@
+export class QElement {
+ constructor(element, priority) {
+ this.element = element;
+ this.priority = priority;
+ }
+}
+
+export class PriorityQueue {
+ constructor(elt) {
+ this.items = [];
+ if (elt !== undefined) {
+ this.enqueue(elt, 0);
+ }
+ }
+
+ enqueue(element, priority) {
+ let qElement = new QElement(element, priority);
+
+ for (let i = 0; i < this.items.length; ++i) {
+ if (this.items[i].priority > qElement.priority) {
+ this.items.splice(i, 0, qElement);
+ return;
+ }
+ }
+ this.items.push(qElement);
+ }
+ dequeue() {
+ return this.items.shift(); // we would use pop to get the highest priority, shift() gives us the lowest priority
+ }
+ front() {
+ return this.items[0];
+ }
+ rear() {
+ return this.items[this.items.length - 1];
+ }
+ isEmpty() {
+ return this.items.length === 0;
+ }
+}
diff --git a/nodejs/lib/ships.js b/nodejs/lib/ships.js
new file mode 100644
index 0000000..d75d78e
--- /dev/null
+++ b/nodejs/lib/ships.js
@@ -0,0 +1,187 @@
+import * as api from './api.js';
+import * as dbConfig from '../database/config.js';
+import * as dbShips from '../database/ships.js';
+import * as dbSurveys from '../database/surveys.js';
+import * as systems from '../lib/systems.js';
+
+export async function extract(ctx) {
+ const ship = dbShips.getShip(ctx.symbol);
+ const asteroidFields = await systems.type({symbol: ship.nav.systemSymbol, type: 'ASTEROID_FIELD'});
+ // TODO if there are multiple fields, find the closest one?
+ await navigate({symbol: ctx.symbol, waypoint: asteroidFields[0].symbol});
+ await orbit(ctx);
+ // TODO handle surveying?
+ const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/extract`, method: 'POST'});
+ if (response.error !== undefined) {
+ switch(response.error.code) {
+ case 4000: // ship is on cooldown
+ await api.sleep(response.error.data.cooldown.remainingSeconds * 1000);
+ return await extract(ctx);
+ case 4228: // ship is full
+ return null;
+ default: // yet unhandled error
+ throw response;
+ }
+ } else {
+ dbShips.setShipCargo(ctx.symbol, response.data.cargo);
+ await api.sleep(response.data.cooldown.remainingSeconds*1000);
+ }
+ return response;
+}
+
+export async function dock(ctx) {
+ const ship = dbShips.getShip(ctx.symbol);
+ if (ship.nav.status === 'DOCKED') {
+ return null;
+ }
+ 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
+ await api.sleep(response.error.data.secondsToArrival * 1000);
+ return await dock(ctx);
+ default: // yet unhandled error
+ throw response;
+ }
+ }
+ dbShips.setShipNav(ctx.symbol, response.data.nav);
+ return response;
+}
+
+function hasMount(shipSymbol, mountSymbol) {
+ const ship = dbShips.getShip(shipSymbol);
+ return ship.mounts.filter(s => s.symbol === mountSymbol).length > 0;
+}
+
+export async function jump(ctx) {
+ // TODO
+ const response = await api.send({endpoint: `/my/ships/${ctx.ship}/jump`, method: 'POST', payload: { systemSymbol: ctx.system }});
+ await api.sleep(response.data.cooldown.remainingSeconds*1000);
+ return response;
+}
+
+export async function navigate(ctx) {
+ const ship = dbShips.getShip(ctx.symbol);
+ if (ship.nav.waypointSymbol === ctx.waypoint) {
+ return await orbit(ctx);
+ }
+ await orbit(ctx);
+ // 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/${ctx.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: ctx.waypoint }});
+ if (response.error !== undefined) {
+ switch(response.error.code) {
+ case 4214: // ship is in transit
+ await api.sleep(response.error.data.secondsToArrival * 1000);
+ return await navigate(ctx);
+ default: // yet unhandled error
+ throw response;
+ }
+ }
+ dbShips.setShipFuel(ctx.symbol, response.data.fuel);
+ dbShips.setShipNav(ctx.symbol, response.data.nav);
+ const delay = new Date(response.data.nav.route.arrival) - new Date();
+ await api.sleep(delay);
+ response.data.nav.status = 'IN_ORBIT';
+ dbShips.setShipNav(ctx.symbol, response.data.nav);
+ await refuel(ctx);
+ return response;
+}
+
+export async function negotiate(ctx) {
+ // TODO
+ return await api.send({endpoint: `/my/ships/${ctx.ship}/negotiate/contract`, method: 'POST'});
+}
+
+export async function orbit(ctx) {
+ const ship = dbShips.getShip(ctx.symbol);
+ if (ship.nav.status === 'IN_ORBIT') {
+ return null;
+ }
+ 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 await orbit(ctx);
+ default: // yet unhandled error
+ throw response;
+ }
+ }
+ dbShips.setShipNav(ctx.symbol, response.data.nav);
+ return response;
+}
+
+export async function purchase(ctx) {
+ const response = await api.send({endpoint: '/my/ships', method: 'POST', payload: {
+ shipType: ctx.shipType,
+ waypointSymbol: ctx.waypoint,
+ }});
+ if (response.error !== undefined) {
+ throw response;
+ }
+ dbShips.setShip(response.data.ship);
+ return response.data;
+}
+
+export async function refuel(ctx) {
+ const ship = dbShips.getShip(ctx.symbol);
+ if (ship.fuel.current >= ship.fuel.capacity * 0.9) {
+ return null;
+ }
+ // TODO check if our current waypoint has a marketplace (and sells fuel)?
+ await dock(ctx);
+ const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/refuel`, method: 'POST'});
+ if (response.error !== undefined) {
+ throw response;
+ }
+ dbShips.setShipFuel(ctx.symbol, response.data.fuel);
+ // TODO track credits
+ return response;
+}
+
+export async function sell(ctx) {
+ // TODO check if our current waypoint has a marketplace (and sells fuel)?
+ await dock(ctx);
+ const ship = dbShips.getShip(ctx.symbol);
+ const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/sell`, method: 'POST', payload: { symbol: ctx.good, units: ctx.units }});
+ if (response.error !== undefined) {
+ throw response;
+ }
+ dbShips.setShipCargo(ctx.symbol, response.data.cargo);
+ // TODO track credits
+ return response;
+}
+
+export async function ship(ctx) {
+ const response = await api.send({endpoint: `/my/ships/${ctx.symbol}`});
+ if (response.error !== undefined) {
+ throw response;
+ }
+ dbShips.setShip(response.data);
+ return response;
+}
+
+export async function survey(ctx) {
+ if (!hasMount(ctx.symbol, 'MOUNT_SURVEYOR_I')) { // we check if a surveyor is mounted on the ship
+ return null;
+ }
+ const ship = dbShips.getShip(ctx.symbol);
+ const asteroidFields = await systems.type({symbol: ship.nav.systemSymbol, type: 'ASTEROID_FIELD'});
+ // TODO if there are multiple fields, find the closest one?
+ await navigate({symbol: ctx.symbol, waypoint: asteroidFields[0].symbol});
+ await orbit(ctx);
+ const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/survey`, method: 'POST'});
+ api.debugLog(response);
+ if (response.error !== undefined) {
+ switch(response.error.code) {
+ case 4000: // ship is on cooldown
+ await api.sleep(response.error.data.cooldown.remainingSeconds * 1000);
+ return await survey(ctx);
+ default: // yet unhandled error
+ throw response;
+ }
+ }
+ dbSurveys.set(response.data.surveys[0]);
+ await api.sleep(response.data.cooldown.remainingSeconds*1000);
+ return response;
+}
diff --git a/nodejs/lib/systems.js b/nodejs/lib/systems.js
new file mode 100644
index 0000000..e03da6c
--- /dev/null
+++ b/nodejs/lib/systems.js
@@ -0,0 +1,68 @@
+import * as api from './api.js';
+import * as db from '../database/systems.js';
+
+// Retrieves a shipyard's information for ctx.symbol
+export async function shipyard(ctx) {
+ const systemSymbol = ctx.symbol.match(/([^-]+-[^-]+)/)[1]; // TODO generalise this extraction
+ console.log(systemSymbol);
+ return await api.send({endpoint: `/systems/${systemSymbol}/waypoints/${ctx.symbol}/shipyard`});
+}
+
+// Retrieves the system's information for ctx.symbol and caches it in the database
+export async function system(ctx) {
+ let s = db.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;
+ db.setSystem(s);
+ }
+ return s;
+}
+
+// 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 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 the system's information for ctx.symbol and caches it in the database
+export async function waypoints(ctx) {
+ await system(ctx);
+ let updated = db.getSystemUpdated(ctx.symbol);
+ // 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;
+ }
+ }
+ db.setSystemWaypoints(ctx.symbol, waypoints);
+ return waypoints;
+ }
+ return db.getSystem(ctx.symbol).waypoints;
+}