diff options
author | Julien Dessaux | 2023-06-02 00:00:09 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-06-02 00:00:09 +0200 |
commit | 7008c91c6fb74f7106c4d9903849d3f1fc17b2f1 (patch) | |
tree | a180768c196025bb4b4e5b27c81a3e1a521bb40d /lib | |
parent | Another big refactoring (diff) | |
download | spacetraders-7008c91c6fb74f7106c4d9903849d3f1fc17b2f1.tar.gz spacetraders-7008c91c6fb74f7106c4d9903849d3f1fc17b2f1.tar.bz2 spacetraders-7008c91c6fb74f7106c4d9903849d3f1fc17b2f1.zip |
Improved ships lib with ships db caching
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ships.js | 69 |
1 files changed, 62 insertions, 7 deletions
diff --git a/lib/ships.js b/lib/ships.js index 55ee585..23f272d 100644 --- a/lib/ships.js +++ b/lib/ships.js @@ -1,7 +1,12 @@ import * as api from './api.js'; +import * as dbConfig from '../database/config.js'; +import * as dbShips from '../database/ships.js'; export async function extract(ctx) { - const response = await api.send({endpoint: `/my/ships/${ctx.ship}/extract`, method: 'POST'}); + // TODO check if our current waypoint has an asteroid field? + await orbit(ctx); + const ship = dbShips.getShip(ctx.symbol); + const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/extract`, method: 'POST'}); if (response.error !== undefined) { switch(response.error.code) { case 4000: // ship is on cooldown @@ -13,12 +18,17 @@ export async function extract(ctx) { throw response; } } else { + dbShips.setShipCargo(ctx.symbol, response.data.cargo); await api.sleep(response.data.cooldown.remainingSeconds*1000); } return response; } export async function dock(ctx) { + const ship = dbShips.getShip(ctx.symbol); + if (ship.nav.status === 'DOCKED') { + return null; + } const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/dock`, method: 'POST'}); if (response.error !== undefined) { switch(response.error.code) { @@ -29,27 +39,41 @@ export async function dock(ctx) { throw response; } } + dbShips.setShipNav(ctx.symbol, response.data.nav); return response; } export async function jump(ctx) { + // TODO const response = await api.send({endpoint: `/my/ships/${ctx.ship}/jump`, method: 'POST', payload: { systemSymbol: ctx.system }}); await api.sleep(response.data.cooldown.remainingSeconds*1000); return response; } export async function navigate(ctx) { - const response = await api.send({endpoint: `/my/ships/${ctx.ship}/navigate`, method: 'POST', payload: { waypointSymbol: ctx.waypoint }}); + const ship = dbShips.getShip(ctx.symbol); + if (ship.nav.waypointSymbol === ctx.waypoint) { + return null; + } + await orbit(ctx); + const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: ctx.waypoint }}); + 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); return response; } export async function negotiate(ctx) { + // TODO return await api.send({endpoint: `/my/ships/${ctx.ship}/negotiate/contract`, method: 'POST'}); } export async function orbit(ctx) { + const ship = dbShips.getShip(ctx.symbol); + if (ship.nav.status === 'IN_ORBIT') { + return null; + } const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/orbit`, method: 'POST'}); if (response.error !== undefined) { switch(response.error.code) { @@ -60,28 +84,59 @@ export async function orbit(ctx) { throw response; } } + dbShips.setShipNav(ctx.symbol, response.data.nav); return response; } export async function purchase(ctx) { - return await api.send({endpoint: '/my/ships', method: 'POST', payload: { + const response = await api.send({endpoint: '/my/ships', method: 'POST', payload: { shipType: ctx.shipType, waypointSymbol: ctx.waypoint, }}); + if (response.error !== undefined) { + throw response; + } + dbShips.setShip(response.data.ship); } export async function refuel(ctx) { - return await api.send({endpoint: `/my/ships/${ctx.ship}/refuel`, method: 'POST'}); + // 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; + } + await dock(ctx); + const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/refuel`, method: 'POST'}); + if (response.error !== undefined) { + throw response; + } + dbShips.setShipFuel(ctx.symbol, response.data.fuel); + // TODO track credits + return response; } export async function sell(ctx) { - return await api.send({endpoint: `/my/ships/${ctx.ship}/sell`, method: 'POST', payload: { symbol: ctx.good, units: ctx.units }}); + 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 }}); + if (response.error !== undefined) { + throw response; + } + dbShips.setShipCargo(ctx.symbol, response.data.cargo); + // TODO track credits + return response; } export async function ship(ctx) { - return await api.send({endpoint: `/my/ships/${ctx.ship}`}); + const response = await api.send({endpoint: `/my/ships/${ctx.symbol}`}); + if (response.error !== undefined) { + throw response; + } + dbShips.setShip(response.data); + return response; } export async function survey(ctx) { - return await api.send({endpoint: `/my/ships/${ctx.ship}/survey`, method: 'POST'}); + // TODO + return await api.send({endpoint: `/my/ships/${ctx.symbol}/survey`, method: 'POST'}); } |