From d668eac4a63a9aa98c3efff395faa23cfcea1c1b Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 21 Mar 2024 17:08:37 +0100 Subject: [node] begin the great typescript rewrite --- nodejs/lib/contracts.ts | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 nodejs/lib/contracts.ts (limited to 'nodejs/lib/contracts.ts') diff --git a/nodejs/lib/contracts.ts b/nodejs/lib/contracts.ts new file mode 100644 index 0000000..fb62813 --- /dev/null +++ b/nodejs/lib/contracts.ts @@ -0,0 +1,74 @@ +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 * 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 { + if (contract.accepted) return contract; + const response = await api.send<{agent: Agent, contract: Contract, type: ''}>({endpoint: `/my/contracts/${contract.id}/accept`, method: 'POST'}); + if ('apiError' in response) { + api.debugLog(response); + throw response; + } + dbAgents.setAgent(response.agent); + dbContracts.setContract(response.contract); + return response.contract; +} + +export async function contracts(): Promise> { + const response = await api.sendPaginated({endpoint: '/my/contracts', page: 1}); + if ('apiError' in response) { + api.debugLog(response); + throw response; + } + response.forEach(contract => dbContracts.setContract(contract)); + return response; +} + +export async function deliver(contract: Contract, ship: Ship): Promise { + 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; }); + ship = 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: { + shipSymbol: ship.symbol, + tradeSymbol: tradeSymbol, + units: units, + }}); + if ('apiError' in response) { + switch(response.code) { + case 4509: // contract delivery terms have been met + return await fulfill(contract); + default: // yet unhandled error + api.debugLog(response); + throw response; + } + } + dbContracts.setContract(response.contract); + dbShips.setShipCargo(ship.symbol, response.cargo); + if(response.contract.terms.deliver[0].unitsRequired >= response.contract.terms.deliver[0].unitsFulfilled) { + return await fulfill(contract); + } + return response.contract; +} + +export async function fulfill(contract: Contract): Promise { + if (contract.fulfilled) return contract; + const response = await api.send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${contract.id}/fulfill`, method: 'POST'}); + if ('apiError' in response) { + api.debugLog(response); + throw response; + } + dbAgents.setAgent(response.agent); + dbContracts.setContract(response.contract); + return response.contract; +} -- cgit v1.2.3