From de0251bc22d554e2ace4d2d3d061dc1054656bcd Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 13 May 2024 23:45:45 +0200 Subject: [node] Big Contracts lib refactoring --- nodejs/automation/contracting.ts | 12 +++++------- nodejs/automation/init.ts | 5 +---- nodejs/automation/mining.ts | 4 +--- 3 files changed, 7 insertions(+), 14 deletions(-) (limited to 'nodejs/automation') diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts index 9ac5d2f..4648b0d 100644 --- a/nodejs/automation/contracting.ts +++ b/nodejs/automation/contracting.ts @@ -1,10 +1,8 @@ import { debugLog } from '../lib/api.ts'; import { Ship } from '../lib/ships.ts'; -import { Contract } from '../lib/types.ts'; import * as mining from './mining.js'; import * as selling from './selling.js'; -import * as dbContracts from '../database/contracts.ts'; -import * as libContracts from '../lib/contracts.ts'; +import { Contract, getContracts } from '../lib/contracts.ts'; import * as libSystems from '../lib/systems.ts'; import * as systems from '../lib/systems.ts'; import { @@ -12,7 +10,7 @@ import { } from '../lib/utils.ts'; export async function run(ship: Ship): Promise { - const contracts = await libContracts.getContracts(); + const contracts = await getContracts(); const active = contracts.filter(function(c) { if (c.fulfilled) return false; const deadline = new Date(c.terms.deadline).getTime(); @@ -29,7 +27,7 @@ export async function run(ship: Ship): Promise { async function runOne(contract: Contract, ship: Ship): Promise { debugLog(contract); - await libContracts.accept(contract); + await contract.accept(); switch(contract.type) { case 'PROCUREMENT': //if (contract.terms.deliver[0].tradeSymbol.match(/_ORE$/)) { @@ -58,7 +56,7 @@ async function runOreProcurement(contract: Contract, ship: Ship): Promise break; case deliveryPoint.symbol: if (goodCargo !== undefined) { // we could be here if a client restart happens right after selling before we navigate away - contract = await libContracts.deliver(contract, ship); + await contract.deliver(ship); if (contract.fulfilled) return; } await ship.navigate(asteroid); @@ -117,7 +115,7 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise { switch(json.error?.code) { case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened // TODO await agents.agents(); - await libContracts.getContracts(); return; default: throw json; @@ -40,5 +38,4 @@ export async function init(): Promise { db.reset(); dbTokens.addToken(json.data.token); dbAgents.addAgent(json.data.agent); - dbContracts.setContract(json.data.contract); } diff --git a/nodejs/automation/mining.ts b/nodejs/automation/mining.ts index cdfcb78..e7ba62f 100644 --- a/nodejs/automation/mining.ts +++ b/nodejs/automation/mining.ts @@ -1,8 +1,7 @@ import * as selling from './selling.js'; -import * as dbContracts from '../database/contracts.js'; +import { Contract } from '../lib/contracts.js'; import { Ship } from '../lib/ships.js'; import { - Contract, Waypoint, } from '../lib/types.ts'; import { categorizeCargo } from '../lib/utils.ts'; @@ -11,7 +10,6 @@ export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroid: // TODO find a good asteroid while(true) { await mineUntilFull(ship); - contract = dbContracts.getContract(contract.id); const deliver = contract.terms.deliver[0]; const cargo = categorizeCargo(ship.cargo, deliver.tradeSymbol); const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0); -- cgit v1.2.3