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 --- database/003_surveys.sql | 6 ++++++ database/db.js | 1 + database/surveys.js | 19 +++++++++++++++++++ lib/ships.js | 30 ++++++++++++++++++++++++++++-- 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 database/003_surveys.sql create mode 100644 database/surveys.js diff --git a/database/003_surveys.sql b/database/003_surveys.sql new file mode 100644 index 0000000..0548796 --- /dev/null +++ b/database/003_surveys.sql @@ -0,0 +1,6 @@ +CREATE TABLE surveys ( + id INTEGER PRIMARY KEY, + data JSON NOT NULL +); +CREATE INDEX surveys_data_symbol on surveys (json_extract(data, '$.symbol')); +CREATE INDEX surveys_data_expiration on surveys (json_extract(data, '$.expiration')); diff --git a/database/db.js b/database/db.js index a6d5174..0fdbcfe 100644 --- a/database/db.js +++ b/database/db.js @@ -5,6 +5,7 @@ const allMigrations = [ 'database/000_init.sql', 'database/001_systems.sql', 'database/002_ships.sql', + 'database/003_surveys.sql', ]; const db = new Database( diff --git a/database/surveys.js b/database/surveys.js new file mode 100644 index 0000000..60c85b2 --- /dev/null +++ b/database/surveys.js @@ -0,0 +1,19 @@ +import db from './db.js'; + +const deleteExpiredSurveysStatement = db.prepare(`DELETE FROM surveys WHERE data->>'expiration' < ?;`); +const getSurveysStatement = db.prepare(`SELECT data FROM surveys WHERE data->>'symbol' = ?;`); +const setSurveysStatement = db.prepare(`INSERT INTO surveys(data) VALUES (json(?));`); + +export function deleteExpired() { + return deleteExpiredSurveysStatement.run(new Date().toISOString()).changes; +} + +export function get(symbol) { + deleteExpired(); + return getSurveysStatement.all(symbol); +} + +export function set(survey) { + deleteExpired(); + return setSurveysStatement.run(JSON.stringify(survey)); +} 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