summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2023-06-12 23:40:07 +0200
committerJulien Dessaux2023-06-12 23:40:07 +0200
commit28b3a695dce34b5adafb0139978de63271f75180 (patch)
treeb7d1df0ef4d8cb2b5e910bc8e14c2f4045e95a1e
parentTake requests time in consideration for rate limiting calculations (diff)
downloadspacetraders-28b3a695dce34b5adafb0139978de63271f75180.tar.gz
spacetraders-28b3a695dce34b5adafb0139978de63271f75180.tar.bz2
spacetraders-28b3a695dce34b5adafb0139978de63271f75180.zip
Use the database cache for mining and contracting automations
-rw-r--r--automation/contracting.js26
-rw-r--r--automation/mining.js5
-rw-r--r--lib/contracts.js7
3 files changed, 25 insertions, 13 deletions
diff --git a/automation/contracting.js b/automation/contracting.js
index c76caff..525253c 100644
--- a/automation/contracting.js
+++ b/automation/contracting.js
@@ -1,14 +1,15 @@
import * as mining from './mining.js';
+import * as dbShips from '../database/ships.js';
import * as api from '../lib/api.js';
import * as contracts from '../lib/contracts.js';
import * as ships from '../lib/ships.js';
import * as systems from '../lib/systems.js';
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
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`;
let contract = cs[0];
if (!contract.accepted) {
@@ -17,17 +18,17 @@ export async function auto(ctx) {
}
const good = contract.terms.deliver[0].tradeSymbol;
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;
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
- 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);
// 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'
- switch (ship.data.nav.waypointSymbol) {
+ switch (ship.nav.waypointSymbol) {
case asteroidField:
let response = await mining.mineUntilFullOf({good: good, symbol: ctx.symbol});
await ships.navigate({symbol: ctx.symbol, waypoint: deliveryPoint});
@@ -35,15 +36,20 @@ export async function auto(ctx) {
case deliveryPoint:
await ships.dock({symbol: ctx.symbol});
await ships.refuel({symbol: ctx.symbol});
- console.log(`delivering ${goodCargo.units} of ${good}`);
- await contracts.deliver({contract: contract.id, symbol: ctx.symbol, good: good, units: goodCargo.units });
+ if (goodCargo !== undefined) {
+ console.log(`delivering ${goodCargo.units} of ${good}`);
+ await contracts.deliver({contract: contract.id, symbol: ctx.symbol, good: good, units: goodCargo.units });
+ }
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});
break;
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});
}
}
}
diff --git a/automation/mining.js b/automation/mining.js
index 393a98c..f35af36 100644
--- a/automation/mining.js
+++ b/automation/mining.js
@@ -25,8 +25,9 @@ export async function mineUntilFullOf(ctx) {
async function mineUntilFull(ctx) {
while(true) {
const ship = dbShips.getShip(ctx.symbol);
- if (ship.cargo.units >= ship.cargo.capacity * 0.8) return ship.cargo;
- await ships.extract({symbol: ctx.symbol});
+ if (ship.cargo.units >= ship.cargo.capacity * 0.9) return ship.cargo;
+ if (await ships.extract({symbol: ctx.symbol}) === null)
+ ship = await ship(ctx); // refresh the ships status from the server just in case
}
}
diff --git a/lib/contracts.js b/lib/contracts.js
index abf32b3..316e181 100644
--- a/lib/contracts.js
+++ b/lib/contracts.js
@@ -1,4 +1,5 @@
import * as api from './api.js';
+import * as dbShips from '../database/ships.js';
export async function accept(ctx) {
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) {
- 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,
tradeSymbol: ctx.good,
units: ctx.units,
}});
+ if (response.error !== undefined) {
+ throw response;
+ }
+ dbShips.setShipCargo(ctx.symbol, response.data.cargo);
}
export async function fulfill(ctx) {