diff options
Diffstat (limited to 'lib/ships.js')
-rw-r--r-- | lib/ships.js | 63 |
1 files changed, 43 insertions, 20 deletions
diff --git a/lib/ships.js b/lib/ships.js index e536e73..9a57c12 100644 --- a/lib/ships.js +++ b/lib/ships.js @@ -1,35 +1,58 @@ import * as api from './api.js'; -export function extract(ctx) { - console.log(`${ctx.ship}: extracting`); - api.send({endpoint: `/my/ships/${ctx.ship}/extract`, method: 'POST', next: ctx.next}); +export async function extract(ctx) { + const response = await api.send({endpoint: `/my/ships/${ctx.ship}/extract`, method: 'POST'}); + if (response.error !== undefined) { + switch(response.error.code) { + case 4000: // ship is on cooldown + await api.sleep(response.error.data.remainingSeconds * 1000); + return extract(ctx); + case 4228: // ship is full + return null; + default: // yet unhandled error + throw response; + } + } + return response; } -export function dock(ctx) { - console.log(`${ctx.ship}: docking`); - api.send({endpoint: `/my/ships/${ctx.ship}/dock`, method: 'POST', next: ctx.next}); +export async function dock(ctx) { + const response = await api.send({endpoint: `/my/ships/${ctx.ship}/dock`, method: 'POST'}); + if (response.error !== undefined) { + switch(response.error.code) { + case 4214: // ship is in transit + await api.sleep(response.error.data.secondsToArrival * 1000); + return dock(ctx); + default: // yet unhandled error + throw response; + } + } + return response; } -export function navigate(ctx) { - console.log(`${ctx.ship}: navigating to ${ctx.waypoint}`); - api.send({endpoint: `/my/ships/${ctx.ship}/navigate`, method: 'POST', payload: { waypointSymbol: ctx.waypoint }, next: ctx.next}); +export async function navigate(ctx) { + return await api.send({endpoint: `/my/ships/${ctx.ship}/navigate`, method: 'POST', payload: { waypointSymbol: ctx.waypoint }}); } -export function orbit(ctx) { - console.log(`${ctx.ship}: orbiting`); - api.send({endpoint: `/my/ships/${ctx.ship}/orbit`, method: 'POST', next: ctx.next}); +export async function orbit(ctx) { + return await api.send({endpoint: `/my/ships/${ctx.ship}/orbit`, method: 'POST'}); } -export function refuel(ctx) { - console.log(`${ctx.ship}: refueling`); - api.send({endpoint: `/my/ships/${ctx.ship}/refuel`, method: 'POST', next: ctx.next}); +export async function purchase(ctx) { + return await api.send({endpoint: '/my/ships', method: 'POST', payload: { + shipType: ctx.shipType, + waypointSymbol: ctx.waypoint, + }}); } -export function sell(ctx) { - console.log(`${ctx.ship}: selling ${ctx.units} of ${ctx.good}`); - api.send({endpoint: `/my/ships/${ctx.ship}/sell`, method: 'POST', payload: { symbol: ctx.good, units: ctx.units }, next: ctx.next}); +export async function refuel(ctx) { + return await api.send({endpoint: `/my/ships/${ctx.ship}/refuel`, method: 'POST'}); } -export function ship(ctx) { - api.send({endpoint: `/my/ships/${ctx.ship}`, next: ctx.next}); +export async function sell(ctx) { + return await api.send({endpoint: `/my/ships/${ctx.ship}/sell`, method: 'POST', payload: { symbol: ctx.good, units: ctx.units }}); +} + +export async function ship(ctx) { + return await api.send({endpoint: `/my/ships/${ctx.ship}`}); } |