summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJulien Dessaux2023-06-08 09:21:05 +0200
committerJulien Dessaux2023-06-08 09:21:05 +0200
commitda567a11eef2b3536d58b44741e91988b1c33598 (patch)
tree36ab4332efbf88f9f41730ff5ab7ced67a95fa1f /lib
parentUse waypoints function where relevant (diff)
downloadspacetraders-da567a11eef2b3536d58b44741e91988b1c33598.tar.gz
spacetraders-da567a11eef2b3536d58b44741e91988b1c33598.tar.bz2
spacetraders-da567a11eef2b3536d58b44741e91988b1c33598.zip
Implement a bit more autonomy for the ships
Diffstat (limited to '')
-rw-r--r--lib/contracts.js2
-rw-r--r--lib/ships.js25
2 files changed, 22 insertions, 5 deletions
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 }});