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 | |
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')
-rw-r--r-- | nodejs/automation/contracting.ts | 32 | ||||
-rw-r--r-- | nodejs/automation/exploration.ts | 2 | ||||
-rw-r--r-- | nodejs/automation/init.ts | 20 | ||||
-rw-r--r-- | nodejs/automation/mining.ts | 18 | ||||
-rw-r--r-- | nodejs/automation/selling.ts | 25 |
5 files changed, 40 insertions, 57 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; } diff --git a/nodejs/automation/exploration.ts b/nodejs/automation/exploration.ts index 67fbfe4..9ea0a5b 100644 --- a/nodejs/automation/exploration.ts +++ b/nodejs/automation/exploration.ts @@ -1,6 +1,6 @@ import db from '../database/db.ts'; import * as dbSystems from '../database/systems.ts'; -import { System } from '../model/system.ts'; +import { System } from '../lib/types.ts'; import * as api from '../lib/api.ts'; export async function init(): Promise<void> { diff --git a/nodejs/automation/init.ts b/nodejs/automation/init.ts index 79c5e39..1e8ea72 100644 --- a/nodejs/automation/init.ts +++ b/nodejs/automation/init.ts @@ -1,16 +1,16 @@ import * as dbAgents from '../database/agents.ts'; import * as db from '../database/db.ts'; import * as dbContracts from '../database/contracts.ts'; -import * as dbShips from '../database/ships.ts'; import * as dbTokens from '../database/tokens.ts'; -import { Agent } from '../model/agent.ts'; -import { Response } from '../model/api.ts'; -import { Contract } from '../model/contract.ts'; -import { Ship } from '../model/ship.ts'; -import * as api from '../lib/api.ts'; -//import * as agents from '../lib/angent.ts'; +import { + Response, +} from '../lib/api.ts'; +import { + Agent, + Contract, +} from '../lib/types.ts'; +import { Ship } from '../lib/ships.ts'; import * as libContracts from '../lib/contracts.ts'; -import * as libShips from '../lib/ships.ts'; const symbol = process.env.NODE_ENV === 'test' ? 'ADYXAX-0' : 'ADYXAX-JS'; @@ -32,7 +32,6 @@ export async function init(): Promise<void> { case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened // TODO await agents.agents(); await libContracts.getContracts(); - await libShips.getShips(); return; default: throw json; @@ -42,7 +41,4 @@ export async function init(): Promise<void> { dbTokens.addToken(json.data.token); dbAgents.addAgent(json.data.agent); dbContracts.setContract(json.data.contract); - dbShips.setShip(json.data.ship); - // Temporary fix to fetch the data on the startup probe - await libShips.getShips(); } diff --git a/nodejs/automation/mining.ts b/nodejs/automation/mining.ts index 272c39a..07fa19b 100644 --- a/nodejs/automation/mining.ts +++ b/nodejs/automation/mining.ts @@ -1,11 +1,8 @@ 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'; +import { Ship } from '../lib/ships.js'; +import { Contract } from '../lib/types.ts'; +import { categorizeCargo } from '../lib/utils.ts'; export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroidSymbol: string): Promise<void> { // TODO find a good asteroid @@ -13,21 +10,20 @@ export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroidS await mineUntilFull(ship); 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 cargo = categorizeCargo(ship.cargo, deliver.tradeSymbol); const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0); // > 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, deliver.tradeSymbol); - await libShips.navigate(ship, asteroidSymbol); + await ship.navigate(asteroidSymbol); } } async function mineUntilFull(ship: Ship): Promise<void> { - while (!libShips.isFull(ship)) { - await libShips.extract(ship); + while (!ship.isFull()) { + await ship.extract(); } } diff --git a/nodejs/automation/selling.ts b/nodejs/automation/selling.ts index 04b0e9d..bc586fe 100644 --- a/nodejs/automation/selling.ts +++ b/nodejs/automation/selling.ts @@ -1,29 +1,26 @@ import * as dbMarkets from '../database/markets.ts'; -import * as dbShips from '../database/ships.ts'; -import * as api from '../lib/api.ts'; -import * as libShips from '../lib/ships.ts'; import * as libSystems from '../lib/systems.ts'; -import * as utils from '../lib/utils.ts'; -import { CargoManifest } from '../model/cargo.ts'; -import { CommonThing } from '../model/common.ts'; -import { Ship } from '../model/ship.ts'; +import { categorizeCargo } from '../lib/utils.ts'; +import { Ship } from '../lib/ships.ts'; +import { + CargoManifest, + CommonThing, +} from '../lib/types.ts'; // example ctx { ship: {XXX}, keep: 'SILVER_ORE' } export async function sell(ship: Ship, good: string): Promise<Ship> { outer: while(true) { - ship = dbShips.getShip(ship.symbol); // first lets see what we want to sell - let cargo = utils.categorizeCargo(ship.cargo, good); + let cargo = categorizeCargo(ship.cargo, good); // get the marketdata from our location const market = await libSystems.market(ship.nav.waypointSymbol); // can we sell anything here? const goods = whatCanBeTradedAt(cargo.goods, market.imports.concat(market.exchange)); for (let i = 0; i < goods.length; i++) { - await libShips.sell(ship, goods[i].symbol); + await ship.sell(goods[i].symbol); }; // are we done selling everything we can? - ship = dbShips.getShip(ship.symbol); - cargo = utils.categorizeCargo(ship.cargo, good); + cargo = categorizeCargo(ship.cargo, good); if (Object.keys(cargo.goods).length === 0) { return ship; } @@ -50,7 +47,7 @@ export async function sell(ship: Ship, good: string): Promise<Ship> { // if we have no data on the market we need to go there and see // and if we have data and can sell there we need to go too if (market === null || whatCanBeTradedAt(cargo.goods, market.imports).length > 0) { - await libShips.navigate(ship, waypointSymbol); + await ship.navigate(waypointSymbol); continue outer; } } @@ -60,7 +57,7 @@ export async function sell(ship: Ship, good: string): Promise<Ship> { const market = await libSystems.market(waypointSymbol); // if we can sell there we need to go if (whatCanBeTradedAt(cargo.goods, market.exchange).length > 0) { - await libShips.navigate(ship, waypointSymbol); + await ship.navigate(waypointSymbol); continue outer; } } |