summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJulien Dessaux2023-06-15 01:02:03 +0200
committerJulien Dessaux2023-06-15 01:02:03 +0200
commit93e6e02bcafaac0a57b7c1d98dca5ba52f32684b (patch)
tree965134bd28dab9acf6c313a78ea1099e60126406 /lib
parentFixed api errors and some async issues (diff)
downloadspacetraders-93e6e02bcafaac0a57b7c1d98dca5ba52f32684b.tar.gz
spacetraders-93e6e02bcafaac0a57b7c1d98dca5ba52f32684b.tar.bz2
spacetraders-93e6e02bcafaac0a57b7c1d98dca5ba52f32684b.zip
Began implementing surveying
Diffstat (limited to 'lib')
-rw-r--r--lib/ships.js30
1 files changed, 28 insertions, 2 deletions
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;
}