From 234770b611df32178382b557df396db220070a7f Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 5 Apr 2024 00:42:30 +0200 Subject: [node] Big Ships lib refactoring --- nodejs/lib/contracts.ts | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) (limited to 'nodejs/lib/contracts.ts') diff --git a/nodejs/lib/contracts.ts b/nodejs/lib/contracts.ts index 2b6f877..833d434 100644 --- a/nodejs/lib/contracts.ts +++ b/nodejs/lib/contracts.ts @@ -1,20 +1,24 @@ -import { Agent } from '../model/agent.ts'; -import { APIError } from '../model/api.ts'; -import { Cargo } from '../model/cargo.ts'; -import { Contract } from '../model/contract.ts'; -import { Ship } from '../model/ship.ts'; +import { + Agent, + Cargo, + Contract, +} from './types.ts'; +import { + APIError, + debugLog, + send, + sendPaginated, +} from './api.ts'; +import { Ship } from './ships.ts'; import * as dbAgents from '../database/agents.ts'; import * as dbContracts from '../database/contracts.ts'; -import * as api from './api.ts'; -import * as dbShips from '../database/ships.ts'; -import * as libShips from '../lib/ships.ts'; export async function accept(contract: Contract): Promise { contract = dbContracts.getContract(contract.id); if (contract.accepted) return contract; - const response = await api.send<{agent: Agent, contract: Contract, type: ''}>({endpoint: `/my/contracts/${contract.id}/accept`, method: 'POST'}); + const response = await send<{agent: Agent, contract: Contract, type: ''}>({endpoint: `/my/contracts/${contract.id}/accept`, method: 'POST'}); if (response.error) { - api.debugLog(response); + debugLog(response); throw response; } dbAgents.setAgent(response.data.agent); @@ -23,7 +27,7 @@ export async function accept(contract: Contract): Promise { } export async function getContracts(): Promise> { - const response = await api.sendPaginated({endpoint: '/my/contracts'}); + const response = await sendPaginated({endpoint: '/my/contracts'}); response.forEach(contract => dbContracts.setContract(contract)); return response; } @@ -32,9 +36,9 @@ export async function getContract(contract: Contract): Promise { try { return dbContracts.getContract(contract.id); } catch {} - const response = await api.send({endpoint: `/my/contracts/${contract.id}`}); + const response = await send({endpoint: `/my/contracts/${contract.id}`}); if (response.error) { - api.debugLog(response); + debugLog(response); throw response; } dbContracts.setContract(response.data); @@ -43,15 +47,14 @@ export async function getContract(contract: Contract): Promise { export async function deliver(contract: Contract, ship: Ship): Promise { contract = dbContracts.getContract(contract.id); - ship = dbShips.getShip(ship.symbol); if (contract.terms.deliver[0].unitsRequired <= contract.terms.deliver[0].unitsFulfilled) { return await fulfill(contract); } const tradeSymbol = contract.terms.deliver[0].tradeSymbol; let units = 0; ship.cargo.inventory.forEach(i => {if (i.symbol === tradeSymbol) units = i.units; }); - await libShips.dock(ship); // we need to be docked to deliver - const response = await api.send<{contract: Contract, cargo: Cargo}>({ endpoint: `/my/contracts/${contract.id}/deliver`, method: 'POST', payload: { + await ship.dock(); // we need to be docked to deliver + const response = await send<{contract: Contract, cargo: Cargo}>({ endpoint: `/my/contracts/${contract.id}/deliver`, method: 'POST', payload: { shipSymbol: ship.symbol, tradeSymbol: tradeSymbol, units: units, @@ -61,12 +64,12 @@ export async function deliver(contract: Contract, ship: Ship): Promise case 4509: // contract delivery terms have been met return await fulfill(contract); default: // yet unhandled error - api.debugLog(response); + debugLog(response); throw response; } } dbContracts.setContract(response.data.contract); - dbShips.setShipCargo(ship.symbol, response.data.cargo); + ship.cargo = response.data.cargo; if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) { return await fulfill(response.data.contract); } @@ -76,9 +79,9 @@ export async function deliver(contract: Contract, ship: Ship): Promise export async function fulfill(contract: Contract): Promise { contract = dbContracts.getContract(contract.id); if (contract.fulfilled) return contract; - const response = await api.send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${contract.id}/fulfill`, method: 'POST'}); + const response = await send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${contract.id}/fulfill`, method: 'POST'}); if (response.error) { - api.debugLog(response); + debugLog(response); throw response; } dbAgents.setAgent(response.data.agent); -- cgit v1.2.3