Began implementing surveying
This commit is contained in:
parent
ec5d09586f
commit
93e6e02bca
4 changed files with 54 additions and 2 deletions
6
database/003_surveys.sql
Normal file
6
database/003_surveys.sql
Normal file
|
@ -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'));
|
|
@ -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(
|
||||
|
|
19
database/surveys.js
Normal file
19
database/surveys.js
Normal file
|
@ -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));
|
||||
}
|
30
lib/ships.js
30
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue