From 3e80bc8a4d3127d17dbc3f52301d33a79e53a980 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 6 Apr 2024 21:36:42 +0200 Subject: [node] multiple contracting fixes and some more refactoring --- nodejs/automation/contracting.ts | 53 +++++++++++++++++----------------------- nodejs/automation/selling.ts | 21 ++++------------ nodejs/lib/contracts.ts | 4 +++ nodejs/lib/errors.ts | 5 ++++ nodejs/lib/ships.ts | 3 +++ nodejs/lib/utils.ts | 25 +++++++++++++++++++ 6 files changed, 65 insertions(+), 46 deletions(-) diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts index 93a9a0e..93c2f8e 100644 --- a/nodejs/automation/contracting.ts +++ b/nodejs/automation/contracting.ts @@ -1,29 +1,35 @@ -import { Contract } from '../lib/types.ts'; +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 contracts from '../lib/contracts.ts'; +import * as libContracts from '../lib/contracts.ts'; import * as libSystems from '../lib/systems.ts'; import * as systems from '../lib/systems.ts'; -import * as utils from '../lib/utils.ts'; +import { + sortByDistanceFrom, +} from '../lib/utils.ts'; export async function run(ship: Ship): Promise { - while(true) { // we use the fact that there can only be at most one active contract at a time - const contracts = dbContracts.getContracts().filter(c => !c.fulfilled); - let contract: Contract; - if (contracts.length === 0) { - contract = await ship.negotiate(); - } else { - contract = contracts[0]; - } + const contracts = await libContracts.getContracts(); + const active = contracts.filter(function(c) { + if (c.fulfilled) return false; + const deadline = new Date(c.terms.deadline).getTime(); + const now = new Date().getTime(); + return deadline > now; + }); + for (const contract of active) { await runOne(contract, ship); - await ship.negotiate(); + } + while(true) { + await runOne(await ship.negotiate(), ship); } } async function runOne(contract: Contract, ship: Ship): Promise { - await contracts.accept(contract); + debugLog(contract); + await libContracts.accept(contract); switch(contract.type) { case 'PROCUREMENT': if (contract.terms.deliver[0].tradeSymbol.match(/_ORE$/)) { @@ -52,7 +58,7 @@ async function runOreProcurement(contract: Contract, ship: Ship): Promise 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 contracts.deliver(contract, ship); + contract = await libContracts.deliver(contract, ship); if (contract.fulfilled) return; } await ship.navigate(asteroid); @@ -73,20 +79,7 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise b.distance) { - return 1; - } - return 0; - }); + const markets = sortByDistanceFrom(ship.nav.route.destination, await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE')); // check from the closest one that exports what we need let buyingPoint: string = ""; outer: for (let i = 0; i < markets.length; i++) { @@ -105,7 +98,7 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise { return ship; } // we need to move somewhere else to sell our remaining goods - // first we look into markets in our system - const rawMarkets = await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE'); - // sorted by distance from where we are - const markets = rawMarkets.map(function (m) { return { - data: m, - distance: (m.x - ship.nav.route.destination.x) ** 2 + (m.y - ship.nav.route.destination.y) ** 2, - }}); - markets.sort(function(a, b) { - if (a.distance < b.distance) { - return -1; - } else if (a.distance > b.distance) { - return 1; - } - return 0; - }); + const markets = sortByDistanceFrom(ship.nav.route.destination, await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE')); // check from the closest one if they import what we need to sell for (let i = 0; i < markets.length; i++) { const waypoint = await libSystems.waypoint(markets[i].data.symbol); diff --git a/nodejs/lib/contracts.ts b/nodejs/lib/contracts.ts index 833d434..0582cc7 100644 --- a/nodejs/lib/contracts.ts +++ b/nodejs/lib/contracts.ts @@ -61,6 +61,10 @@ export async function deliver(contract: Contract, ship: Ship): Promise }}); if (response.error) { switch(response.error.code) { + case 4503: // contract has expired + // TODO sell cargo? the next trading loop should take care of it by itself + contract.fulfilled = true; + return contract; case 4509: // contract delivery terms have been met return await fulfill(contract); default: // yet unhandled error diff --git a/nodejs/lib/errors.ts b/nodejs/lib/errors.ts index f9dca89..c560ee8 100644 --- a/nodejs/lib/errors.ts +++ b/nodejs/lib/errors.ts @@ -1,5 +1,10 @@ import { Cooldown } from './types.ts'; +export type ContractDeadlineExpired = { + contractId: string; + deadline: Date; +}; + export type MarketTradeVolumeError = { waypointSymbol: string; tradeSymbol: string; diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts index 187a87b..67dca00 100644 --- a/nodejs/lib/ships.ts +++ b/nodejs/lib/ships.ts @@ -130,6 +130,7 @@ export class Ship { this.nav.status = 'IN_ORBIT'; // we arrive in orbit } async negotiate(): Promise { + await this.dock(); const response = await send<{contract: Contract}>({endpoint: `/my/ships/${this.symbol}/negotiate/contract`, method: 'POST'}); if (response.error) { switch(response.error.code) { @@ -142,6 +143,7 @@ export class Ship { throw response; } } + dbContracts.setContract(response.data.contract); return response.data.contract; } async orbit(): Promise { @@ -180,6 +182,7 @@ export class Ship { dbAgents.setAgent(response.data.agent); } async refuel(): Promise { + if (this.fuel.current === this.fuel.capacity) return; // TODO check if our current waypoint has a marketplace (and sells fuel)? await this.dock(); const response = await send<{agent: Agent, fuel: Fuel}>({endpoint: `/my/ships/${this.symbol}/refuel`, method: 'POST'}); // TODO transaction field diff --git a/nodejs/lib/utils.ts b/nodejs/lib/utils.ts index 39378e9..480e7a4 100644 --- a/nodejs/lib/utils.ts +++ b/nodejs/lib/utils.ts @@ -5,6 +5,11 @@ export type CategorizedCargo = { goods: CargoManifest; }; +type Point = { + x: number; + y: number; +}; + // cargo is a ship.cargo object, want is an optional symbol export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo { const wanted = cargo.inventory.filter(i => i.symbol === want || i.symbol === 'ANTIMATTER'); @@ -20,6 +25,26 @@ export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo { return {wanted: wobj, goods: gobj}; } +export function distance(a: Point, b: Point) { + return Math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2); +} + +export function sortByDistanceFrom(a: Point, points: Array): Array<{data: T, distance: number}>{ + let result = points.map(function (m) { return { + data: m, + distance: distance(a, m), + }}); + result.sort(function(a, b) { + if (a.distance < b.distance) { + return -1; + } else if (a.distance > b.distance) { + return 1; + } + return 0; + }); + return result; +} + export function systemFromWaypoint(waypoint: string): string { return waypoint.split('-').slice(0,2).join('-'); } -- cgit v1.2.3