1
0
Fork 0

[node] Big Contracts lib refactoring

This commit is contained in:
Julien Dessaux 2024-05-13 23:45:45 +02:00
parent 1f6daef018
commit de0251bc22
Signed by: adyxax
GPG key ID: F92E51B86E07177E
7 changed files with 88 additions and 139 deletions

View file

@ -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<void> {
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<void> {
async function runOne(contract: Contract, ship: Ship): Promise<void> {
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<void>
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<void
await ship.purchase(wantedCargo, units);
// then make a delivery
await ship.navigate(deliveryPoint);
contract = await libContracts.deliver(contract, ship);
await contract.deliver(ship);
if (contract.fulfilled) return;
}
console.log("runTradeProcurement not implemented");

View file

@ -1,14 +1,13 @@
import * as dbAgents from '../database/agents.ts';
import * as db from '../database/db.ts';
import * as dbContracts from '../database/contracts.ts';
import * as dbTokens from '../database/tokens.ts';
import {
Response,
} from '../lib/api.ts';
import {
Agent,
Contract,
} from '../lib/types.ts';
import { Contract } from '../lib/contracts.ts';
import { Ship } from '../lib/ships.ts';
import * as libContracts from '../lib/contracts.ts';
@ -31,7 +30,6 @@ export async function init(): Promise<void> {
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<void> {
db.reset();
dbTokens.addToken(json.data.token);
dbAgents.addAgent(json.data.agent);
dbContracts.setContract(json.data.contract);
}

View file

@ -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);