Many new api calls
This commit is contained in:
parent
e52378e03f
commit
d79a4485a6
6 changed files with 75 additions and 8 deletions
|
@ -9,7 +9,7 @@ let queue = new PriorityQueue(); // a priority queue to hold api calls we want t
|
|||
|
||||
// send takes a request object as argument and an optional context ctx
|
||||
// example request: {
|
||||
// endpoint: the url endpoint to call,
|
||||
// endpoint: the path part of the url to call,
|
||||
// method: HTTP method for `fetch` call, defaults to 'GET',
|
||||
// payload: optional json object that will be send along with the request,
|
||||
// priority: optional priority value (defaults to 10, lower than 10 means the message will be sent faster)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import * as api from './api.js';
|
||||
|
||||
export async function accept(ctx) {
|
||||
return await api.send({endpoint: `/my/contracts/${ctx.id}/accept`, method: 'POST'});
|
||||
return await api.send({endpoint: `/my/contracts/${ctx.contract}/accept`, method: 'POST'});
|
||||
}
|
||||
|
||||
export async function contracts() {
|
||||
|
@ -15,3 +15,7 @@ export async function deliver(ctx) {
|
|||
units: ctx.units,
|
||||
}});
|
||||
}
|
||||
|
||||
export async function fulfill(ctx) {
|
||||
return await api.send({ endpoint: `/my/contracts/${ctx.contract}/fulfill`, method: 'POST'});
|
||||
}
|
||||
|
|
15
lib/ships.js
15
lib/ships.js
|
@ -30,10 +30,21 @@ export async function dock(ctx) {
|
|||
return response;
|
||||
}
|
||||
|
||||
export async function jump(ctx) {
|
||||
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 delay = new Date(response.data.nav.route.arrival) - new Date();
|
||||
await api.sleep(delay);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function negotiate(ctx) {
|
||||
return await api.send({endpoint: `/my/ships/${ctx.ship}/negotiate/contract`, method: 'POST'});
|
||||
}
|
||||
|
||||
export async function orbit(ctx) {
|
||||
|
@ -58,3 +69,7 @@ export async function sell(ctx) {
|
|||
export async function ship(ctx) {
|
||||
return await api.send({endpoint: `/my/ships/${ctx.ship}`});
|
||||
}
|
||||
|
||||
export async function survey(ctx) {
|
||||
return await api.send({endpoint: `/my/ships/${ctx.ship}/survey`, method: 'POST'});
|
||||
}
|
||||
|
|
|
@ -3,18 +3,18 @@ import * as db from '../database/systems.js';
|
|||
|
||||
// Retrieves a list of waypoints that have a specific ctx.trait like a SHIPYARD or a MARKETPLACE in the system ctx.symbol
|
||||
export async function trait(ctx) {
|
||||
const s = await getSystem(ctx);
|
||||
const s = await system(ctx);
|
||||
return s.filter(s => s.traits.some(t => t.symbol === ctx.trait));
|
||||
}
|
||||
|
||||
// Retrieves a list of waypoints that have a specific ctx.type like ASTEROID_FIELD in the system ctx.symbol
|
||||
export async function type(ctx, response) {
|
||||
const s = await getSystem(ctx);
|
||||
const s = await system(ctx);
|
||||
return s.filter(s => s.type === ctx.type);
|
||||
}
|
||||
|
||||
// Retrieves the system's information for ctx.symbol and cache it in the database
|
||||
async function getSystem(ctx) {
|
||||
export async function system(ctx) {
|
||||
let s = db.getSystem(ctx.symbol);
|
||||
if (s === null) {
|
||||
const response = await api.send({endpoint: `/systems/${ctx.symbol}/waypoints?limit=20&page=1`});
|
||||
|
@ -34,3 +34,16 @@ async function getSystem(ctx) {
|
|||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
export async function systems(ctx) {
|
||||
const response = await api.send({endpoint: `/systems?limit=20&page=1`});
|
||||
// TODO pagination
|
||||
return response;
|
||||
}
|
||||
|
||||
// Retrieves a shipyard's information for ctx.symbol
|
||||
export async function shipyard(ctx) {
|
||||
const systemSymbol = ctx.symbol.match(/([^-]+-[^-]+)/)[1]; // TODO generalise this extraction
|
||||
console.log(systemSymbol);
|
||||
return await api.send({endpoint: `/systems/${systemSymbol}/waypoints/${ctx.symbol}/shipyard`});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue