Implement a bit more autonomy for the ships
This commit is contained in:
parent
4d5384aa43
commit
da567a11ee
5 changed files with 51 additions and 40 deletions
25
lib/ships.js
25
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 }});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue