summaryrefslogtreecommitdiff
path: root/nodejs/automation
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--nodejs/automation/contracting.ts54
-rw-r--r--nodejs/automation/init.ts9
-rw-r--r--nodejs/automation/mining.ts17
3 files changed, 44 insertions, 36 deletions
diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts
index ab3c606..7fc8877 100644
--- a/nodejs/automation/contracting.ts
+++ b/nodejs/automation/contracting.ts
@@ -11,7 +11,7 @@ import * as systems from '../lib/systems.ts';
import * as utils from '../lib/utils.ts';
export async function init() {
- const cs = dbContracts.getContracts();
+ const cs = dbContracts.getContracts().filter(c => !c.fulfilled);
cs.forEach(contract => run(contract));
}
@@ -37,34 +37,34 @@ async function runProcurement(contract: Contract, ships: Array<Ship>) {
const asteroids = await systems.type(ships[0].nav.systemSymbol, 'ENGINEERED_ASTEROID');
const asteroidSymbol = asteroids[0].symbol;
ships.forEach(async function(ship) {
- while (!contract.fulfilled) {
- ship = dbShips.getShip(ship.symbol) as Ship;
- let goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
- // If we are in transit, we wait until we arrive
- const delay = new Date(ship.nav.route.arrival).getTime() - new Date().getTime();
- if (delay > 0) await api.sleep(delay);
- // Then it depends on where we are
- switch (ship.nav.waypointSymbol) {
- case asteroidSymbol:
- await mining.mineUntilFullOf(wantedCargo, ship, asteroidSymbol);
- await libShips.navigate(ship, deliveryPoint);
- break;
- case deliveryPoint:
- if (goodCargo !== undefined) { // we could be here if a client restart happens right after selling before we navigate away
- console.log(`delivering ${goodCargo.units} of ${wantedCargo}`);
- contract = await contracts.deliver(contract, ship);
- if (contract.fulfilled) break;
- }
- await libShips.navigate(ship, asteroidSymbol);
- break;
- default:
- if (libShips.isFull(ship)) {
- await selling.sell(ship, wantedCargo);
- } else {
+ while (true) {
+ while (!contract.fulfilled) {
+ ship = dbShips.getShip(ship.symbol);
+ let goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
+ // If we are in transit, we wait until we arrive
+ const delay = new Date(ship.nav.route.arrival).getTime() - new Date().getTime();
+ if (delay > 0) await api.sleep(delay);
+ // Then it depends on where we are
+ switch (ship.nav.waypointSymbol) {
+ case asteroidSymbol:
+ await mining.mineUntilFullFor(contract, ship, asteroidSymbol);
+ await libShips.navigate(ship, deliveryPoint);
+ break;
+ case deliveryPoint:
+ if (goodCargo !== undefined) { // we could be here if a client restart happens right after selling before we navigate away
+ contract = await contracts.deliver(contract, ship);
+ if (contract.fulfilled) break;
+ }
await libShips.navigate(ship, asteroidSymbol);
- }
+ break;
+ default:
+ if (await libShips.isFull(ship)) {
+ await selling.sell(ship, wantedCargo);
+ }
+ await libShips.navigate(ship, asteroidSymbol);
+ }
}
+ contract = await libShips.negotiate(ship); // TODO that's not correct, the procurement type can change
}
- // TODO repurpose the ship
});
}
diff --git a/nodejs/automation/init.ts b/nodejs/automation/init.ts
index 2850fac..74f42fb 100644
--- a/nodejs/automation/init.ts
+++ b/nodejs/automation/init.ts
@@ -29,8 +29,11 @@ export async function init(): Promise<void> {
if (json.error !== undefined) {
switch(json.error?.code) {
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened
- await libContracts.contracts();
- await libShips.ships();
+ // TODO await agents.agents();
+ const contracts = await libContracts.getContracts();
+ const ongoing = contracts.filter(c => !c.fulfilled);
+ const ships = await libShips.getShips();
+ if (ongoing.length === 0) libShips.negotiate(ships[0]);
return;
default:
throw json;
@@ -42,5 +45,5 @@ export async function init(): Promise<void> {
dbContracts.setContract(json.data.contract);
dbShips.setShip(json.data.ship);
// Temporary fix to fetch the data on the startup probe
- await libShips.ships();
+ await libShips.getShips();
}
diff --git a/nodejs/automation/mining.ts b/nodejs/automation/mining.ts
index b9db7d4..272c39a 100644
--- a/nodejs/automation/mining.ts
+++ b/nodejs/automation/mining.ts
@@ -1,21 +1,26 @@
import * as selling from './selling.js';
+import * as dbContracts from '../database/contracts.js';
import * as dbShips from '../database/ships.js';
import * as api from '../lib/api.js';
import * as libShips from '../lib/ships.js';
import * as utils from '../lib/utils.js';
+import { Contract } from '../model/contract.ts';
import { Ship } from '../model/ship.ts';
-export async function mineUntilFullOf(good: string, ship: Ship, asteroidSymbol: string): Promise<void> {
+export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroidSymbol: string): Promise<void> {
// TODO find a good asteroid
while(true) {
await mineUntilFull(ship);
- ship = dbShips.getShip(ship.symbol) as Ship;
- const cargo = utils.categorizeCargo(ship.cargo, good);
+ contract = dbContracts.getContract(contract.id);
+ const deliver = contract.terms.deliver[0];
+ ship = dbShips.getShip(ship.symbol);
+ const cargo = utils.categorizeCargo(ship.cargo, deliver.tradeSymbol);
const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0);
- // > 90% full of the valuable goods ?
- if (wantedUnits >= ship.cargo.capacity * 0.9) return ship;
+ // > 90% full of the valuable goods ? Or just have enough for the contract?
+ if (wantedUnits >= ship.cargo.capacity * 0.9
+ || cargo.wanted[deliver.tradeSymbol] >= deliver.unitsRequired - deliver.unitsFulfilled) return;
// we are full but need to sell junk
- await selling.sell(ship, good);
+ await selling.sell(ship, deliver.tradeSymbol);
await libShips.navigate(ship, asteroidSymbol);
}
}