diff options
author | Julien Dessaux | 2024-04-05 00:42:30 +0200 |
---|---|---|
committer | Julien Dessaux | 2024-04-07 23:01:52 +0200 |
commit | 234770b611df32178382b557df396db220070a7f (patch) | |
tree | 8c768846716eaf892ff75abe84d0f5c00e8519ff /nodejs/automation/contracting.ts | |
parent | [node] Fixed basic procurement trading loop (diff) | |
download | spacetraders-234770b611df32178382b557df396db220070a7f.tar.gz spacetraders-234770b611df32178382b557df396db220070a7f.tar.bz2 spacetraders-234770b611df32178382b557df396db220070a7f.zip |
[node] Big Ships lib refactoring
Diffstat (limited to 'nodejs/automation/contracting.ts')
-rw-r--r-- | nodejs/automation/contracting.ts | 32 |
1 files changed, 13 insertions, 19 deletions
diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts index fa9a4c4..96f9106 100644 --- a/nodejs/automation/contracting.ts +++ b/nodejs/automation/contracting.ts @@ -1,32 +1,28 @@ -import { Contract } from '../model/contract.ts'; -import { Ship } from '../model/ship.ts'; +import { Contract } from '../lib/types.ts'; +import { Ship } from '../lib/ships.ts'; import * as mining from './mining.js'; import * as selling from './selling.js'; import * as dbContracts from '../database/contracts.ts'; -import * as dbShips from '../database/ships.ts'; -import * as api from '../lib/api.ts'; import * as contracts from '../lib/contracts.ts'; -import * as libShips from '../lib/ships.ts'; import * as libSystems from '../lib/systems.ts'; import * as systems from '../lib/systems.ts'; import * as utils from '../lib/utils.ts'; -export async function init(): Promise<void> { - const ship = dbShips.getShips()[0]; // This should always be the command ship +export async function run(ship: Ship): Promise<void> { while(true) { // we use the fact that there can only be at most one active contract at a time const contracts = dbContracts.getContracts().filter(c => !c.fulfilled); let contract: Contract; if (contracts.length === 0) { - contract = await libShips.negotiate(ship); + contract = await ship.negotiate(); } else { contract = contracts[0]; } - await run(contract, ship); - await libShips.negotiate(ship); + await runOne(contract, ship); + await ship.negotiate(); } } -async function run(contract: Contract, ship: Ship): Promise<void> { +async function runOne(contract: Contract, ship: Ship): Promise<void> { await contracts.accept(contract); switch(contract.type) { case 'PROCUREMENT': @@ -47,24 +43,23 @@ async function runOreProcurement(contract: Contract, ship: Ship): Promise<void> const asteroids = await systems.type(ship.nav.systemSymbol, 'ENGINEERED_ASTEROID'); const asteroidSymbol = asteroids[0].symbol; while (!contract.fulfilled) { - ship = dbShips.getShip(ship.symbol); const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0] // what we do depends on where we are switch (ship.nav.waypointSymbol) { case asteroidSymbol: await mining.mineUntilFullFor(contract, ship, asteroidSymbol); - await libShips.navigate(ship, deliveryPoint); + await ship.navigate(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) return; } - await libShips.navigate(ship, asteroidSymbol); + await ship.navigate(asteroidSymbol); break; default: await selling.sell(ship, wantedCargo); - await libShips.navigate(ship, asteroidSymbol); + await ship.navigate(asteroidSymbol); } } } @@ -74,7 +69,6 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void const deliveryPoint = deliver.destinationSymbol; const wantedCargo = deliver.tradeSymbol; while (!contract.fulfilled) { - ship = dbShips.getShip(ship.symbol); const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0] // make sure we are not carrying useless stuff await selling.sell(ship, wantedCargo); @@ -122,14 +116,14 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void throw `runTradeProcurement failed, no market exports or exchanges ${wantedCargo}`; } // go buy what we need - await libShips.navigate(ship, buyingPoint); + await ship.navigate(buyingPoint); const units = Math.min( deliver.unitsRequired - deliver.unitsFulfilled, ship.cargo.capacity - ship.cargo.units, ); - await libShips.buy(ship, wantedCargo, units); + await ship.purchase(wantedCargo, units); // then make a delivery - await libShips.navigate(ship, deliveryPoint); + await ship.navigate(deliveryPoint); contract = await contracts.deliver(contract, ship); if (contract.fulfilled) return; } |