From eeaa64b5ed54cee8f4ffc85f96178e9799c1a8ac Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 6 Apr 2024 10:55:11 +0200 Subject: [node] waypoints usage refactoring --- nodejs/automation/contracting.ts | 26 +++++++++++++------------- nodejs/automation/mining.ts | 9 ++++++--- nodejs/automation/selling.ts | 15 ++++++++------- nodejs/lib/ships.ts | 10 +++++----- nodejs/lib/systems.ts | 16 +++++++++++----- 5 files changed, 43 insertions(+), 33 deletions(-) diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts index 96f9106..93a9a0e 100644 --- a/nodejs/automation/contracting.ts +++ b/nodejs/automation/contracting.ts @@ -39,34 +39,34 @@ async function runOne(contract: Contract, ship: Ship): Promise { async function runOreProcurement(contract: Contract, ship: Ship): Promise { const wantedCargo = contract.terms.deliver[0].tradeSymbol; - const deliveryPoint = contract.terms.deliver[0].destinationSymbol; + const deliveryPoint = await libSystems.waypoint(contract.terms.deliver[0].destinationSymbol); const asteroids = await systems.type(ship.nav.systemSymbol, 'ENGINEERED_ASTEROID'); - const asteroidSymbol = asteroids[0].symbol; + const asteroid = await systems.waypoint(asteroids[0].symbol); while (!contract.fulfilled) { const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0] // what we do depends on where we are switch (ship.nav.waypointSymbol) { - case asteroidSymbol: - await mining.mineUntilFullFor(contract, ship, asteroidSymbol); + case asteroid.symbol: + await mining.mineUntilFullFor(contract, ship, asteroid); await ship.navigate(deliveryPoint); break; - case deliveryPoint: + 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); if (contract.fulfilled) return; } - await ship.navigate(asteroidSymbol); + await ship.navigate(asteroid); break; default: await selling.sell(ship, wantedCargo); - await ship.navigate(asteroidSymbol); + await ship.navigate(asteroid); } } } async function runTradeProcurement(contract: Contract, ship: Ship): Promise { const deliver = contract.terms.deliver[0]; - const deliveryPoint = deliver.destinationSymbol; + const deliveryPoint = await libSystems.waypoint(deliver.destinationSymbol); const wantedCargo = deliver.tradeSymbol; while (!contract.fulfilled) { const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0] @@ -90,8 +90,8 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise { +export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroid: Waypoint): Promise { // TODO find a good asteroid while(true) { await mineUntilFull(ship); @@ -17,7 +20,7 @@ export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroidS || cargo.wanted[deliver.tradeSymbol] >= deliver.unitsRequired - deliver.unitsFulfilled) return; // we are full but need to sell junk await selling.sell(ship, deliver.tradeSymbol); - await ship.navigate(asteroidSymbol); + await ship.navigate(asteroid); } } diff --git a/nodejs/automation/selling.ts b/nodejs/automation/selling.ts index bc586fe..6cb7d32 100644 --- a/nodejs/automation/selling.ts +++ b/nodejs/automation/selling.ts @@ -10,10 +10,11 @@ import { // example ctx { ship: {XXX}, keep: 'SILVER_ORE' } export async function sell(ship: Ship, good: string): Promise { outer: while(true) { + const waypoint = await libSystems.waypoint(ship.nav.waypointSymbol); // first lets see what we want to sell let cargo = categorizeCargo(ship.cargo, good); // get the marketdata from our location - const market = await libSystems.market(ship.nav.waypointSymbol); + const market = await libSystems.market(waypoint); // can we sell anything here? const goods = whatCanBeTradedAt(cargo.goods, market.imports.concat(market.exchange)); for (let i = 0; i < goods.length; i++) { @@ -42,22 +43,22 @@ export async function sell(ship: Ship, good: string): Promise { }); // check from the closest one if they import what we need to sell for (let i = 0; i < markets.length; i++) { - const waypointSymbol = markets[i].data.symbol; - const market = await libSystems.market(waypointSymbol); + const waypoint = await libSystems.waypoint(markets[i].data.symbol); + const market = await libSystems.market(waypoint); // if we have no data on the market we need to go there and see // and if we have data and can sell there we need to go too if (market === null || whatCanBeTradedAt(cargo.goods, market.imports).length > 0) { - await ship.navigate(waypointSymbol); + await ship.navigate(waypoint); continue outer; } } // check from the closest one if they exchange what we need to sell for (let i = 0; i < markets.length; i++) { - const waypointSymbol = markets[i].data.symbol; - const market = await libSystems.market(waypointSymbol); + const waypoint = await libSystems.waypoint(markets[i].data.symbol); + const market = await libSystems.market(waypoint); // if we can sell there we need to go if (whatCanBeTradedAt(cargo.goods, market.exchange).length > 0) { - await ship.navigate(waypointSymbol); + await ship.navigate(waypoint); continue outer; } } diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts index 7221596..187a87b 100644 --- a/nodejs/lib/ships.ts +++ b/nodejs/lib/ships.ts @@ -18,6 +18,7 @@ import { Fuel, Nav, Registration, + Waypoint, } from './types.ts'; import * as dbAgents from '../database/agents.ts'; @@ -96,14 +97,13 @@ export class Ship { isFull(): boolean { return this.cargo.units >= this.cargo.capacity * 0.9; } - async navigate(waypointSymbol: string): Promise { - if (this.nav.waypointSymbol === waypointSymbol) return; - const d = + async navigate(waypoint: Waypoint): Promise { + if (this.nav.waypointSymbol === waypoint.symbol) return; // TODO compute fuel consumption and refuel if we do not have enough OR if the destination does not sell fuel? await this.refuel(); await this.orbit(); // TODO if we do not have enough fuel, make a stop to refuel along the way or drift to the destination - const response = await send<{fuel: Fuel, nav: Nav}>({endpoint: `/my/ships/${this.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: waypointSymbol }}); // TODO events field + const response = await send<{fuel: Fuel, nav: Nav}>({endpoint: `/my/ships/${this.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: waypoint.symbol }}); // TODO events field if (response.error) { switch(response.error.code) { case 4203: // not enough fuel @@ -117,7 +117,7 @@ export class Ship { case 4214: const sicite = response.error.data as ShipIsCurrentlyInTransitError; await sleep(sicite.secondsToArrival * 1000); - return await this.navigate(waypointSymbol); + return await this.navigate(waypoint); default: // yet unhandled error debugLog(response); throw response; diff --git a/nodejs/lib/systems.ts b/nodejs/lib/systems.ts index 97aa6e3..d4b3be5 100644 --- a/nodejs/lib/systems.ts +++ b/nodejs/lib/systems.ts @@ -8,15 +8,15 @@ import * as dbSystems from '../database/systems.ts'; import { Market, System, - Waypoint + Waypoint, } from './types.ts' import { systemFromWaypoint } from './utils.ts'; -export async function market(waypointSymbol: string): Promise { - const data = dbMarkets.getMarketAtWaypoint(waypointSymbol); +export async function market(waypoint: Waypoint): Promise { + const data = dbMarkets.getMarketAtWaypoint(waypoint.symbol); if (data) { return data; } - const systemSymbol = systemFromWaypoint(waypointSymbol); - let response = await send({endpoint: `/systems/${systemSymbol}/waypoints/${waypointSymbol}/market`}); + const systemSymbol = systemFromWaypoint(waypoint.symbol); + let response = await send({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint.symbol}/market`}); if (response.error) { debugLog(response); throw response; @@ -55,6 +55,12 @@ export async function type(system: string, typeSymbol: string): Promise s.type === typeSymbol); } +export async function waypoint(waypointSymbol: string): Promise { + const systemSymbol = systemFromWaypoint(waypointSymbol); + const w = await waypoints(systemSymbol); + return w.filter(w => w.symbol === waypointSymbol)[0]; +} + export async function waypoints(systemSymbol: string): Promise> { const s = await system(systemSymbol); const updated = dbSystems.getSystemUpdated(systemSymbol); -- cgit v1.2.3