1
0
Fork 0

Moved the nodejs agent to its own subfolder to make room for my haskell agent

This commit is contained in:
Julien Dessaux 2023-07-01 23:13:13 +02:00
parent 0bc0df0891
commit 36cc33f9e9
Signed by: adyxax
GPG key ID: F92E51B86E07177E
22 changed files with 0 additions and 0 deletions

140
nodejs/lib/api.js Normal file
View file

@ -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))
}

26
nodejs/lib/contracts.js Normal file
View file

@ -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'});
}

View file

@ -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;
}
}

187
nodejs/lib/ships.js Normal file
View file

@ -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;
}

68
nodejs/lib/systems.js Normal file
View file

@ -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;
}