summaryrefslogtreecommitdiff
path: root/nodejs/automation
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-13 23:45:45 +0200
committerJulien Dessaux2024-05-13 23:45:45 +0200
commitde0251bc22d554e2ace4d2d3d061dc1054656bcd (patch)
tree7e0f00f2ded551a9904a772c5cf6f37a2c06bb61 /nodejs/automation
parent[node] improved sql migrations code (diff)
downloadspacetraders-de0251bc22d554e2ace4d2d3d061dc1054656bcd.tar.gz
spacetraders-de0251bc22d554e2ace4d2d3d061dc1054656bcd.tar.bz2
spacetraders-de0251bc22d554e2ace4d2d3d061dc1054656bcd.zip
[node] Big Contracts lib refactoring
Diffstat (limited to 'nodejs/automation')
-rw-r--r--nodejs/automation/contracting.ts12
-rw-r--r--nodejs/automation/init.ts5
-rw-r--r--nodejs/automation/mining.ts4
3 files changed, 7 insertions, 14 deletions
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<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");
diff --git a/nodejs/automation/init.ts b/nodejs/automation/init.ts
index 1e8ea72..28163b7 100644
--- a/nodejs/automation/init.ts
+++ b/nodejs/automation/init.ts
@@ -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);
}
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);