From a34e4fbed4b6c2eb2440361178bc6cf213e6657c Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 31 Mar 2024 09:44:49 +0200 Subject: [node] implemented basic procurement trading loop --- nodejs/automation/contracting.ts | 148 ++++++++++++++++++++++++++++----------- 1 file changed, 108 insertions(+), 40 deletions(-) (limited to 'nodejs/automation/contracting.ts') diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts index 7fc8877..02715c8 100644 --- a/nodejs/automation/contracting.ts +++ b/nodejs/automation/contracting.ts @@ -7,64 +7,132 @@ import * as dbShips from '../database/ships.ts'; import * as api from '../lib/api.ts'; import * as contracts from '../lib/contracts.ts'; import * as libShips from '../lib/ships.ts'; +import * as libSystems from '../lib/systems.ts'; import * as systems from '../lib/systems.ts'; import * as utils from '../lib/utils.ts'; -export async function init() { - const cs = dbContracts.getContracts().filter(c => !c.fulfilled); - cs.forEach(contract => run(contract)); +export async function init(): Promise { + const ship = dbShips.getShips()[0]; // This should always be the command ship + 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 libShips.negotiate(ship); + } else { + contract = contracts[0]; + } + await run(contract, ship); + await libShips.negotiate(ship); + } } -async function run(contract: Contract) { +async function run(contract: Contract, ship: Ship): Promise { await contracts.accept(contract); - const contractSystem = utils.systemFromWaypoint(contract.terms.deliver[0].destinationSymbol); - let ships = dbShips.getShipsAt(contractSystem); - ships = ships.filter(ship => ship.registration.role !== 'SATELLITE'); // filter out probes - switch(contract.type) { case 'PROCUREMENT': - await runProcurement(contract, ships); + if (contract.terms.deliver[0].tradeSymbol.match(/_ORE$/)) { + await runOreProcurement(contract, ship); + } else { + await runTradeProcurement(contract, ship); + } break; default: throw `Handling of contract type ${contract.type} is not implemented yet`; } } -async function runProcurement(contract: Contract, ships: Array) { - // TODO check if contract is fulfilled! +async function runOreProcurement(contract: Contract, ship: Ship): Promise { const wantedCargo = contract.terms.deliver[0].tradeSymbol; const deliveryPoint = contract.terms.deliver[0].destinationSymbol; - const asteroids = await systems.type(ships[0].nav.systemSymbol, 'ENGINEERED_ASTEROID'); + const asteroids = await systems.type(ship.nav.systemSymbol, 'ENGINEERED_ASTEROID'); const asteroidSymbol = asteroids[0].symbol; - ships.forEach(async function(ship) { - while (true) { - while (!contract.fulfilled) { - ship = dbShips.getShip(ship.symbol); - let goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0] - // If we are in transit, we wait until we arrive - const delay = new Date(ship.nav.route.arrival).getTime() - new Date().getTime(); - if (delay > 0) await api.sleep(delay); - // Then it depends on where we are - switch (ship.nav.waypointSymbol) { - case asteroidSymbol: - await mining.mineUntilFullFor(contract, ship, asteroidSymbol); - await libShips.navigate(ship, deliveryPoint); - break; - case deliveryPoint: - 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) break; - } - await libShips.navigate(ship, asteroidSymbol); - break; - default: - if (await libShips.isFull(ship)) { - await selling.sell(ship, wantedCargo); - } - await libShips.navigate(ship, asteroidSymbol); + while (!contract.fulfilled) { + ship = dbShips.getShip(ship.symbol); + 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); + await libShips.navigate(ship, deliveryPoint); + break; + case deliveryPoint: + 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 libShips.navigate(ship, asteroidSymbol); + break; + default: + await selling.sell(ship, wantedCargo); + await libShips.navigate(ship, asteroidSymbol); + } + } +} + +async function runTradeProcurement(contract: Contract, ship: Ship): Promise { + const deliver = contract.terms.deliver[0]; + const deliveryPoint = deliver.destinationSymbol; + const wantedCargo = deliver.tradeSymbol; + while (!contract.fulfilled) { + ship = dbShips.getShip(ship.symbol); + const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0] + // make sure we are not carrying useless stuff + await selling.sell(ship, wantedCargo); + // go buy what we need + 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; + }); + // check from the closest one that exports what we need + let buyingPoint: string = null; + outer: for (let i = 0; i < markets.length; i++) { + const waypointSymbol = markets[i].data.symbol; + const market = await libSystems.market(waypointSymbol); + for (let j = 0; j < market.exports; j++) { + if (market.exports[j].symbol === wantedCargo) { + buyingPoint = market.symbol; + break outer; } } - contract = await libShips.negotiate(ship); // TODO that's not correct, the procurement type can change } - }); + // if we did not find an exporting market we look for an exchange + if (buyingPoint === null) { + outer: for (let i = 0; i < markets.length; i++) { + const waypointSymbol = markets[i].data.symbol; + const market = await libSystems.market(waypointSymbol); + for (let j = 0; j < market.exchanges; j++) { + if (market.exports[j].symbol === wantedCargo) { + buyingPoint = market.symbol; + break outer; + } + } + } + } + if (buyingPoint === null) { + throw `runTradeProcurement failed, no market exports or exchanges ${wantedCargo}`; + } + // go buy what we need + await libShips.navigate(ship, buyingPoint); + const units = Math.min( + deliver.unitsRequired - deliver.unitsFulfilled, + ship.cargo.capacity - ship.cargo.units, + ); + await libShips.buy(ship, wantedCargo, units); + // then make a delivery + await libShips.navigate(ship, deliveryPoint); + contract = await contracts.deliver(contract, ship); + if (contract.fulfilled) return; + } + console.log("runTradeProcurement not implemented"); + throw "not implemented"; } -- cgit v1.2.3