1
0
Fork 0

Updated the lib for the new api

This commit is contained in:
Julien Dessaux 2023-05-25 01:20:56 +02:00
parent 9963ab79b7
commit b1157af9cd
Signed by: adyxax
GPG key ID: F92E51B86E07177E
5 changed files with 227 additions and 93 deletions

View file

@ -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}`});
}