1
0
Fork 0

[node] Big Ships lib refactoring

This commit is contained in:
Julien Dessaux 2024-04-05 00:42:30 +02:00
parent a58394230d
commit 234770b611
Signed by: adyxax
GPG key ID: F92E51B86E07177E
27 changed files with 500 additions and 589 deletions

View file

@ -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> {
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<Contract> {
}
export async function getContracts(): Promise<Array<Contract>> {
const response = await api.sendPaginated<Contract>({endpoint: '/my/contracts'});
const response = await sendPaginated<Contract>({endpoint: '/my/contracts'});
response.forEach(contract => dbContracts.setContract(contract));
return response;
}
@ -32,9 +36,9 @@ export async function getContract(contract: Contract): Promise<Contract> {
try {
return dbContracts.getContract(contract.id);
} catch {}
const response = await api.send<Contract>({endpoint: `/my/contracts/${contract.id}`});
const response = await send<Contract>({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<Contract> {
export async function deliver(contract: Contract, ship: Ship): Promise<Contract> {
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<Contract>
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<Contract>
export async function fulfill(contract: Contract): Promise<Contract> {
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);