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 ++++++++------- 3 files changed, 27 insertions(+), 23 deletions(-) (limited to 'nodejs/automation') 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; } } -- cgit v1.2.3