1
0
Fork 0

Use the database cache for mining and contracting automations

This commit is contained in:
Julien Dessaux 2023-06-12 23:40:07 +02:00
parent e18c5b1fdf
commit 28b3a695dc
Signed by: adyxax
GPG key ID: F92E51B86E07177E
3 changed files with 25 additions and 13 deletions

View file

@ -1,14 +1,15 @@
import * as mining from './mining.js'; import * as mining from './mining.js';
import * as dbShips from '../database/ships.js';
import * as api from '../lib/api.js'; import * as api from '../lib/api.js';
import * as contracts from '../lib/contracts.js'; import * as contracts from '../lib/contracts.js';
import * as ships from '../lib/ships.js'; import * as ships from '../lib/ships.js';
import * as systems from '../lib/systems.js'; import * as systems from '../lib/systems.js';
export async function auto(ctx) { export async function auto(ctx) {
let ship = await ships.ship({symbol: ctx.symbol}); let ship = dbShips.getShip(ctx.symbol);
// Fetch our contracts in the system the ship currently is in // Fetch our contracts in the system the ship currently is in
let cs = await contracts.contracts(); let cs = await contracts.contracts();
cs = cs.data.filter(c => c.terms.deliver[0].destinationSymbol.startsWith(ship.data.nav.systemSymbol)); cs = cs.data.filter(c => c.terms.deliver[0].destinationSymbol.startsWith(ship.nav.systemSymbol));
if (cs === []) throw `No contract at ${ctx.symbol}'s location`; if (cs === []) throw `No contract at ${ctx.symbol}'s location`;
let contract = cs[0]; let contract = cs[0];
if (!contract.accepted) { if (!contract.accepted) {
@ -17,17 +18,17 @@ export async function auto(ctx) {
} }
const good = contract.terms.deliver[0].tradeSymbol; const good = contract.terms.deliver[0].tradeSymbol;
const deliveryPoint = contract.terms.deliver[0].destinationSymbol; const deliveryPoint = contract.terms.deliver[0].destinationSymbol;
const asteroidFields = await systems.type({symbol: ship.data.nav.systemSymbol, type: 'ASTEROID_FIELD'}); const asteroidFields = await systems.type({symbol: ship.nav.systemSymbol, type: 'ASTEROID_FIELD'});
const asteroidField = asteroidFields[0].symbol; const asteroidField = asteroidFields[0].symbol;
while (true) { while (true) {
ship = await ships.ship({symbol: ctx.symbol}); // TODO we should not need to fetch this ship = dbShips.getShip(ctx.symbol);
// If we are in transit, we wait until we arrive // If we are in transit, we wait until we arrive
const delay = new Date(ship.data.nav.route.arrival) - new Date(); const delay = new Date(ship.nav.route.arrival) - new Date();
if (delay > 0) await api.sleep(delay); if (delay > 0) await api.sleep(delay);
// Then it depends on where we are // Then it depends on where we are
let goodCargo = ship.data.cargo.inventory.filter(i => i.symbol === good)[0]; let goodCargo = ship.cargo.inventory.filter(i => i.symbol === good)[0];
// the switch makes this 'resumable' // the switch makes this 'resumable'
switch (ship.data.nav.waypointSymbol) { switch (ship.nav.waypointSymbol) {
case asteroidField: case asteroidField:
let response = await mining.mineUntilFullOf({good: good, symbol: ctx.symbol}); let response = await mining.mineUntilFullOf({good: good, symbol: ctx.symbol});
await ships.navigate({symbol: ctx.symbol, waypoint: deliveryPoint}); await ships.navigate({symbol: ctx.symbol, waypoint: deliveryPoint});
@ -35,15 +36,20 @@ export async function auto(ctx) {
case deliveryPoint: case deliveryPoint:
await ships.dock({symbol: ctx.symbol}); await ships.dock({symbol: ctx.symbol});
await ships.refuel({symbol: ctx.symbol}); await ships.refuel({symbol: ctx.symbol});
if (goodCargo !== undefined) {
console.log(`delivering ${goodCargo.units} of ${good}`); console.log(`delivering ${goodCargo.units} of ${good}`);
await contracts.deliver({contract: contract.id, symbol: ctx.symbol, good: good, units: goodCargo.units }); await contracts.deliver({contract: contract.id, symbol: ctx.symbol, good: good, units: goodCargo.units });
}
await ships.navigate({symbol: ctx.symbol, waypoint: asteroidField}); await ships.navigate({symbol: ctx.symbol, waypoint: asteroidField});
await ships.dock({symbol: ctx.symbol}); await ships.dock({symbol: ctx.symbol});
await ships.refuel({symbol: ctx.symbol}); await ships.refuel({symbol: ctx.symbol});
await ships.orbit({symbol: ctx.symbol}); await ships.orbit({symbol: ctx.symbol});
break; break;
default: default:
throw `where is the ship?`; await ships.navigate({symbol: ctx.symbol, waypoint: asteroidField});
await ships.dock({symbol: ctx.symbol});
await ships.refuel({symbol: ctx.symbol});
await ships.orbit({symbol: ctx.symbol});
} }
} }
} }

View file

@ -25,8 +25,9 @@ export async function mineUntilFullOf(ctx) {
async function mineUntilFull(ctx) { async function mineUntilFull(ctx) {
while(true) { while(true) {
const ship = dbShips.getShip(ctx.symbol); const ship = dbShips.getShip(ctx.symbol);
if (ship.cargo.units >= ship.cargo.capacity * 0.8) return ship.cargo; if (ship.cargo.units >= ship.cargo.capacity * 0.9) return ship.cargo;
await ships.extract({symbol: ctx.symbol}); if (await ships.extract({symbol: ctx.symbol}) === null)
ship = await ship(ctx); // refresh the ships status from the server just in case
} }
} }

View file

@ -1,4 +1,5 @@
import * as api from './api.js'; import * as api from './api.js';
import * as dbShips from '../database/ships.js';
export async function accept(ctx) { export async function accept(ctx) {
return await api.send({endpoint: `/my/contracts/${ctx.contract}/accept`, method: 'POST'}); return await api.send({endpoint: `/my/contracts/${ctx.contract}/accept`, method: 'POST'});
@ -9,11 +10,15 @@ export async function contracts() {
} }
export async function deliver(ctx) { export async function deliver(ctx) {
return await api.send({ endpoint: `/my/contracts/${ctx.contract}/deliver`, method: 'POST', payload: { const response = await api.send({ endpoint: `/my/contracts/${ctx.contract}/deliver`, method: 'POST', payload: {
shipSymbol: ctx.symbol, shipSymbol: ctx.symbol,
tradeSymbol: ctx.good, tradeSymbol: ctx.good,
units: ctx.units, units: ctx.units,
}}); }});
if (response.error !== undefined) {
throw response;
}
dbShips.setShipCargo(ctx.symbol, response.data.cargo);
} }
export async function fulfill(ctx) { export async function fulfill(ctx) {