From 93e6e02bcafaac0a57b7c1d98dca5ba52f32684b Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 15 Jun 2023 01:02:03 +0200 Subject: Began implementing surveying --- lib/ships.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'lib/ships.js') diff --git a/lib/ships.js b/lib/ships.js index 12cbd7d..d75d78e 100644 --- a/lib/ships.js +++ b/lib/ships.js @@ -1,6 +1,7 @@ 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) { @@ -47,6 +48,11 @@ export async function dock(ctx) { 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 }}); @@ -156,6 +162,26 @@ export async function ship(ctx) { } export async function survey(ctx) { - // TODO - return await api.send({endpoint: `/my/ships/${ctx.symbol}/survey`, method: 'POST'}); + 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; } -- cgit v1.2.3