From da567a11eef2b3536d58b44741e91988b1c33598 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 8 Jun 2023 09:21:05 +0200 Subject: Implement a bit more autonomy for the ships --- lib/contracts.js | 2 +- lib/ships.js | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/contracts.js b/lib/contracts.js index a118ae4..abf32b3 100644 --- a/lib/contracts.js +++ b/lib/contracts.js @@ -10,7 +10,7 @@ export async function contracts() { export async function deliver(ctx) { return await api.send({ endpoint: `/my/contracts/${ctx.contract}/deliver`, method: 'POST', payload: { - shipSymbol: ctx.ship, + shipSymbol: ctx.symbol, tradeSymbol: ctx.good, units: ctx.units, }}); diff --git a/lib/ships.js b/lib/ships.js index c1bba08..1e92cf3 100644 --- a/lib/ships.js +++ b/lib/ships.js @@ -1,11 +1,15 @@ import * as api from './api.js'; import * as dbConfig from '../database/config.js'; import * as dbShips from '../database/ships.js'; +import * as systems from '../lib/systems.js'; export async function extract(ctx) { - // TODO check if our current waypoint has an asteroid field? - await orbit(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) { @@ -53,16 +57,27 @@ export async function jump(ctx) { export async function navigate(ctx) { const ship = dbShips.getShip(ctx.symbol); if (ship.nav.waypointSymbol === ctx.waypoint) { - return null; + 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 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; } @@ -99,14 +114,15 @@ export async function purchase(ctx) { throw response; } dbShips.setShip(response.data.ship); + return response.data; } export async function refuel(ctx) { - // TODO check if our current waypoint has a marketplace (and sells fuel)? 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) { @@ -118,6 +134,7 @@ export async function refuel(ctx) { } 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 }}); -- cgit v1.2.3