From d79a4485a6b1a3ae6b647d94b45ee22a515441f2 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 29 May 2023 23:25:09 +0200 Subject: Many new api calls --- automation/contract.js | 2 +- lib/api.js | 2 +- lib/contracts.js | 6 +++++- lib/ships.js | 15 +++++++++++++++ lib/systems.js | 19 ++++++++++++++++--- main.js | 39 +++++++++++++++++++++++++++++++++++++-- 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/automation/contract.js b/automation/contract.js index bf428e9..aebb265 100644 --- a/automation/contract.js +++ b/automation/contract.js @@ -13,7 +13,7 @@ export async function auto(ctx) { let contract = cs[0]; if (!contract.accepted) { console.log(new Date(), `accepting contract ${contract.id}`); - await contracts.accept({id: contract.id}); + await contracts.accept({contract: contract.id}); } const good = contract.terms.deliver[0].tradeSymbol; const deliveryPoint = contract.terms.deliver[0].destinationSymbol; diff --git a/lib/api.js b/lib/api.js index 1519888..a531980 100644 --- a/lib/api.js +++ b/lib/api.js @@ -9,7 +9,7 @@ let queue = new PriorityQueue(); // a priority queue to hold api calls we want t // send takes a request object as argument and an optional context ctx // example request: { -// endpoint: the url endpoint to call, +// 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) diff --git a/lib/contracts.js b/lib/contracts.js index 9494595..a118ae4 100644 --- a/lib/contracts.js +++ b/lib/contracts.js @@ -1,7 +1,7 @@ import * as api from './api.js'; export async function accept(ctx) { - return await api.send({endpoint: `/my/contracts/${ctx.id}/accept`, method: 'POST'}); + return await api.send({endpoint: `/my/contracts/${ctx.contract}/accept`, method: 'POST'}); } export async function contracts() { @@ -15,3 +15,7 @@ export async function deliver(ctx) { units: ctx.units, }}); } + +export async function fulfill(ctx) { + return await api.send({ endpoint: `/my/contracts/${ctx.contract}/fulfill`, method: 'POST'}); +} diff --git a/lib/ships.js b/lib/ships.js index 6eefd87..109054c 100644 --- a/lib/ships.js +++ b/lib/ships.js @@ -30,10 +30,21 @@ export async function dock(ctx) { return response; } +export async function jump(ctx) { + 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 response = await api.send({endpoint: `/my/ships/${ctx.ship}/navigate`, method: 'POST', payload: { waypointSymbol: ctx.waypoint }}); const delay = new Date(response.data.nav.route.arrival) - new Date(); await api.sleep(delay); + return response; +} + +export async function negotiate(ctx) { + return await api.send({endpoint: `/my/ships/${ctx.ship}/negotiate/contract`, method: 'POST'}); } export async function orbit(ctx) { @@ -58,3 +69,7 @@ export async function sell(ctx) { export async function ship(ctx) { return await api.send({endpoint: `/my/ships/${ctx.ship}`}); } + +export async function survey(ctx) { + return await api.send({endpoint: `/my/ships/${ctx.ship}/survey`, method: 'POST'}); +} diff --git a/lib/systems.js b/lib/systems.js index ad354e4..4b480de 100644 --- a/lib/systems.js +++ b/lib/systems.js @@ -3,18 +3,18 @@ import * as db from '../database/systems.js'; // 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 s = await getSystem(ctx); + const s = await system(ctx); return s.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 s = await getSystem(ctx); + const s = await system(ctx); return s.filter(s => s.type === ctx.type); } // Retrieves the system's information for ctx.symbol and cache it in the database -async function getSystem(ctx) { +export async function system(ctx) { let s = db.getSystem(ctx.symbol); if (s === null) { const response = await api.send({endpoint: `/systems/${ctx.symbol}/waypoints?limit=20&page=1`}); @@ -34,3 +34,16 @@ async function getSystem(ctx) { } return s; } + +export async function systems(ctx) { + const response = await api.send({endpoint: `/systems?limit=20&page=1`}); + // TODO pagination + return response; +} + +// 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`}); +} diff --git a/main.js b/main.js index e6a516f..92aca26 100755 --- a/main.js +++ b/main.js @@ -1,4 +1,5 @@ import * as autoContract from './automation/contract.js'; +import * as autoMining from './automation/mining.js'; import * as agent from './lib/agent.js'; import * as api from './lib/api.js'; import * as contracts from './lib/contracts.js'; @@ -12,12 +13,16 @@ my-agent Fetch your agent's status. register [symbol] [faction] Registers your agent then inits the database ships.ship [ship_symbol] Retrieve a ship's status. ships Retrieve all of your ships. +status Servers' status`); } switch(process.argv[2]) { case 'autoContractForShip': await autoContract.auto({ship: process.argv[3]}); break; +case 'autoMiningForShip': + await autoMining.mineUntilFullOf({ship: process.argv[3], good: 'NON_EXISTENT'}); + break; case 'init': if (process.argv[3] !== undefined && process.argv[4] !== undefined && process.argv[5] !== undefined) { agent.init(process.argv[3], process.argv[4], process.argv[5]); @@ -38,14 +43,20 @@ case 'register': case 'ships': api.debugLog(await api.send({endpoint: '/my/ships'})); break; +case 'status': + api.debugLog(await api.send({endpoint: '/'})); + break; default: // wip and manual actions switch(process.argv[2]) { + case 'contracts.accept': + api.debugLog(await contracts.accept({contract: process.argv[3]})); + break; case 'contracts.contracts': api.debugLog(await contracts.contracts()); break; - case 'contracts.accept': - api.debugLog(await contracts.accept({id: process.argv[3]})); + case 'contracts.fulfill': + api.debugLog(await contracts.fulfill({contract: process.argv[3]})); break; case 'ships.dock': api.debugLog(await ships.dock({ship: process.argv[3]})); @@ -53,9 +64,18 @@ default: case 'ships.extract': api.debugLog(await ships.extract({ship: process.argv[3]})); break; + case 'ships.jump': + api.debugLog(await ships.jump({ship: process.argv[3], system: process.argv[4]})); + break; //case 'market': // api.send({endpoint: `/systems/${process.argv[3]}/waypoints/${process.argv[4]}/market`}); // break; + case 'ships.navigate': + api.debugLog(await ships.navigate({ship: process.argv[3], waypoint: process.argv[4]})); + break; + case 'ships.negotiate': + api.debugLog(await ships.negotiate({ship: process.argv[3]})); + break; case 'ships.navigate': api.debugLog(await ships.navigate({ship: process.argv[3], waypoint: process.argv[4]})); break; @@ -74,12 +94,27 @@ default: case 'ships.ship': api.debugLog(await ships.ship({ship: process.argv[3]})); break; + case 'ships.survey': + api.debugLog(await ships.survey({ship: process.argv[3]})); + break; case 'systems.asteroids': api.debugLog(await systems.type({symbol: process.argv[3], type: 'ASTEROID_FIELD'})); break; + case 'systems.jumpGate': + api.debugLog(await systems.type({symbol: process.argv[3], type: 'JUMP_GATE'})); + break; + case 'systems.shipyard': + api.debugLog(await systems.shipyard({symbol: process.argv[3]})); + break; case 'systems.shipyards': api.debugLog(await systems.trait({symbol: process.argv[3], trait: 'SHIPYARD'})); break; + case 'systems.system': + api.debugLog(await systems.system({symbol: process.argv[3]})); + break; + case 'systems.systems': + api.debugLog(await systems.systems()); + break; default: usage(); } -- cgit v1.2.3