diff options
author | Julien Dessaux | 2023-07-01 23:13:13 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-07-01 23:13:13 +0200 |
commit | 36cc33f9e96a38ecea98ac8d26275b4828347d80 (patch) | |
tree | 653dcea7e656ec815fc0a1fa5664a6b89abccaa3 /nodejs/automation | |
parent | Fixed prepared statements (diff) | |
download | spacetraders-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/automation')
-rw-r--r-- | nodejs/automation/automation.js | 26 | ||||
-rw-r--r-- | nodejs/automation/contracting.js | 55 | ||||
-rw-r--r-- | nodejs/automation/exploration.js | 21 | ||||
-rw-r--r-- | nodejs/automation/mining.js | 34 |
4 files changed, 136 insertions, 0 deletions
diff --git a/nodejs/automation/automation.js b/nodejs/automation/automation.js new file mode 100644 index 0000000..3184c2f --- /dev/null +++ b/nodejs/automation/automation.js @@ -0,0 +1,26 @@ +import * as dbConfig from '../database/config.js'; +import * as dbShips from '../database/ships.js'; +import * as exploration from './exploration.js'; + +// This function registers then inits the database +export async function register(symbol, faction) { + const response = await fetch('https://api.spacetraders.io/v2/register', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + symbol: symbol, + faction: faction, + }), + }); + const json = await response.json(); + console.log(JSON.stringify(response, null, 2)); + if (response.error !== undefined) { + throw response; + } + dbConfig.registerAgent(json.data); + exploration.init(); + dbShips.setShip(json.data.ship); + // TODO contract +} diff --git a/nodejs/automation/contracting.js b/nodejs/automation/contracting.js new file mode 100644 index 0000000..525253c --- /dev/null +++ b/nodejs/automation/contracting.js @@ -0,0 +1,55 @@ +import * as mining from './mining.js'; +import * as dbShips from '../database/ships.js'; +import * as api from '../lib/api.js'; +import * as contracts from '../lib/contracts.js'; +import * as ships from '../lib/ships.js'; +import * as systems from '../lib/systems.js'; + +export async function auto(ctx) { + let ship = dbShips.getShip(ctx.symbol); + // Fetch our contracts in the system the ship currently is in + let cs = await contracts.contracts(); + cs = cs.data.filter(c => c.terms.deliver[0].destinationSymbol.startsWith(ship.nav.systemSymbol)); + if (cs === []) throw `No contract at ${ctx.symbol}'s location`; + let contract = cs[0]; + if (!contract.accepted) { + console.log(new Date(), `accepting contract ${contract.id}`); + await contracts.accept({contract: contract.id}); + } + const good = contract.terms.deliver[0].tradeSymbol; + const deliveryPoint = contract.terms.deliver[0].destinationSymbol; + const asteroidFields = await systems.type({symbol: ship.nav.systemSymbol, type: 'ASTEROID_FIELD'}); + const asteroidField = asteroidFields[0].symbol; + while (true) { + ship = dbShips.getShip(ctx.symbol); + // If we are in transit, we wait until we arrive + const delay = new Date(ship.nav.route.arrival) - new Date(); + if (delay > 0) await api.sleep(delay); + // Then it depends on where we are + let goodCargo = ship.cargo.inventory.filter(i => i.symbol === good)[0]; + // the switch makes this 'resumable' + switch (ship.nav.waypointSymbol) { + case asteroidField: + let response = await mining.mineUntilFullOf({good: good, symbol: ctx.symbol}); + await ships.navigate({symbol: ctx.symbol, waypoint: deliveryPoint}); + break; + case deliveryPoint: + await ships.dock({symbol: ctx.symbol}); + await ships.refuel({symbol: ctx.symbol}); + if (goodCargo !== undefined) { + console.log(`delivering ${goodCargo.units} of ${good}`); + await contracts.deliver({contract: contract.id, symbol: ctx.symbol, good: good, units: goodCargo.units }); + } + await ships.navigate({symbol: ctx.symbol, waypoint: asteroidField}); + await ships.dock({symbol: ctx.symbol}); + await ships.refuel({symbol: ctx.symbol}); + await ships.orbit({symbol: ctx.symbol}); + break; + default: + await ships.navigate({symbol: ctx.symbol, waypoint: asteroidField}); + await ships.dock({symbol: ctx.symbol}); + await ships.refuel({symbol: ctx.symbol}); + await ships.orbit({symbol: ctx.symbol}); + } + } +} diff --git a/nodejs/automation/exploration.js b/nodejs/automation/exploration.js new file mode 100644 index 0000000..b35efe0 --- /dev/null +++ b/nodejs/automation/exploration.js @@ -0,0 +1,21 @@ +import * as db from '../database/systems.js'; +import * as api from '../lib/api.js'; + +// Retrieves all systems information, should be called only once after registering +export async function init() { + 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(); +} diff --git a/nodejs/automation/mining.js b/nodejs/automation/mining.js new file mode 100644 index 0000000..f35af36 --- /dev/null +++ b/nodejs/automation/mining.js @@ -0,0 +1,34 @@ +import * as dbShips from '../database/ships.js'; +import * as api from '../lib/api.js'; +import * as ships from '../lib/ships.js'; + +// example ctx { good: 'SILVER_ORE', symbol: 'ADYXAX-2' } +// returns the number of units of the good the ship holds +export async function mineUntilFullOf(ctx) { + while(true) { + let cargo = await mineUntilFull({symbol: ctx.symbol}); + let good = cargo.inventory.filter(i => i.symbol === ctx.good)[0]; + const antimatter = cargo.inventory.filter(i => i.symbol === 'ANTIMATTER')[0]; + const junk = cargo.inventory.filter(i => i.symbol !== ctx.good && i.symbol !== 'ANTIMATTER'); + if ((good?.units ?? 0) + (antimatter?.units ?? 0) >= cargo.capacity * 0.9) { // > 90% full of the valuable goods + return good.units; + } else { // we are full but need to sell junk + for (let i=0; i<junk.length; ++i) { + await ships.sell({symbol: ctx.symbol, good: junk[i].symbol, units: junk[i].units}); + } + } + } +} + +// example ctx { symbol: 'ADYXAX-2' } +// extract the ship's cargo contents when more than 80% full then returns the ships cargo object +async function mineUntilFull(ctx) { + while(true) { + const ship = dbShips.getShip(ctx.symbol); + if (ship.cargo.units >= ship.cargo.capacity * 0.9) return ship.cargo; + if (await ships.extract({symbol: ctx.symbol}) === null) + ship = await ship(ctx); // refresh the ships status from the server just in case + } +} + +// TODO surveying the asteroid field |