diff options
Diffstat (limited to '')
-rw-r--r-- | nodejs/automation/agent.ts | 30 | ||||
-rw-r--r-- | nodejs/automation/contracting.ts | 9 | ||||
-rw-r--r-- | nodejs/automation/trading.ts | 63 |
3 files changed, 99 insertions, 3 deletions
diff --git a/nodejs/automation/agent.ts b/nodejs/automation/agent.ts index 2aecef5..a6506c5 100644 --- a/nodejs/automation/agent.ts +++ b/nodejs/automation/agent.ts @@ -17,6 +17,7 @@ enum states { start_running_contracts_with_the_command_ship = 0, visit_all_shipyards, visit_all_markets, + send_probes_to_all_shipyards, } export async function run(): Promise<void> { @@ -41,6 +42,8 @@ export async function run(): Promise<void> { await visit_all_markets(); state++; continue; + case states.send_probes_to_all_shipyards: + await send_probes_to_all_shipyards(); state++; continue; default: @@ -54,6 +57,33 @@ export async function run(): Promise<void> { } } +async function send_probes_to_all_shipyards(): Promise<void> { + outer: while(true) { + const shipyardWaypoints = await trait(getShips()[0].nav.systemSymbol, 'SHIPYARD'); + let candidates: Array<Waypoint> = []; + for (const w of shipyardWaypoints) { + if (is_there_a_ship_at_this_waypoint(w)) continue; + candidates.push(w); + } + if (candidates.length === 0) return; + // if we do not have enough probes, we buy some + if (candidates.length - 1 >= getShips().length - 2) { + const probe = await purchaseShip('SHIP_PROBE'); + const probeWaypoint = await waypoint(probe.nav.waypointSymbol); + await probe.navigate(candidates[0]); + continue outer; + } + // otherwise we find the closest ones from a shipyard + const probes = getShips().slice(2); + let probesWaypoints: Array<Waypoint> = []; + for (const p of probes) { + probesWaypoints.push(await waypoint(p.nav.waypointSymbol)); + } + const next = sortByDistanceFrom(candidates[0], probesWaypoints)[0].data; + await probes.filter(p => p.nav.waypointSymbol === next.symbol)[0].navigate(candidates[0]); + } +} + async function visit_all_markets(): Promise<void> { if (await are_we_done_visiting_all_markets()) return; // send all our probes except the starting one to map the system's markets diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts index 80569ef..d043d66 100644 --- a/nodejs/automation/contracting.ts +++ b/nodejs/automation/contracting.ts @@ -1,7 +1,8 @@ +import * as autoTrading from './trading.ts'; import { debugLog } from '../lib/api.ts'; import { Ship } from '../lib/ships.ts'; -import * as mining from './mining.js'; -import * as selling from './selling.js'; +import * as mining from './mining.ts'; +import * as selling from './selling.ts'; import { Contract, getContracts } from '../lib/contracts.ts'; import * as libSystems from '../lib/systems.ts'; import * as systems from '../lib/systems.ts'; @@ -105,7 +106,9 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void } } if (buyingPoint === "") { - throw `runTradeProcurement failed, no market exports or exchanges ${wantedCargo}`; + debugLog(`runTradeProcurement failed, no market exports or exchanges ${wantedCargo}`); + //await autoTrading.run(ship); + throw "not implemented"; } // go buy what we need await ship.navigate(await libSystems.waypoint(buyingPoint)); diff --git a/nodejs/automation/trading.ts b/nodejs/automation/trading.ts new file mode 100644 index 0000000..897bd46 --- /dev/null +++ b/nodejs/automation/trading.ts @@ -0,0 +1,63 @@ +//import events from 'events'; +// +//const bus = new events.EventEmitter(); + +import { debugLog } from '../lib/api.ts'; +import { Ship } from '../lib/ships.ts'; +import { market, trait, waypoint } from '../lib/systems.ts'; +import { Waypoint } from '../lib/types.ts'; +import { + distance, + sortByDistanceFrom, + sortByPrice, + whatCanBeTradedAt, +} from '../lib/utils.ts'; + +async function navigate_to_nearest_exporting_waypoint(ship: Ship): Promise<void> { + const shipWaypoint = await waypoint(ship.nav.waypointSymbol); + const marketplaceWaypoints = await trait(ship.nav.systemSymbol, 'MARKETPLACE'); + const candidates: Array<Waypoint> = []; + for (const w of marketplaceWaypoints) { + const marketplaceData = await market(w); + if (!marketplaceData.exports) continue; + candidates.push(w); + } + const next = sortByDistanceFrom(shipWaypoint, candidates)[0].data; + await ship.navigate(next); +} + +export async function run(ship: Ship): Promise<void> { + while (true) { + const shipWaypoint = await waypoint(ship.nav.waypointSymbol); + const marketplaceData = await market(shipWaypoint); + if (marketplaceData.exports.length === 0) { + await navigate_to_nearest_exporting_waypoint(ship); + continue; + } + const marketplaceWaypoints = await trait(ship.nav.systemSymbol, 'MARKETPLACE'); + const candidates: Array<{price: number, tradeVolume: number, symbol: string, waypoint: Waypoint}> = []; + for (const w of marketplaceWaypoints) { + const data = await market(w); + const trades = marketplaceData.exports.filter(g => data.imports.some(h => g.symbol === h.symbol)); + if (!trades) continue; + const exports = marketplaceData.tradeGoods.filter(g => trades.some(h => g.symbol === h.symbol)); + const imports = data.tradeGoods.filter(g => trades.some(h => g.symbol === h.symbol)); + for (const e of exports) { + const i = imports.filter(g => g.symbol === e.symbol)[0]; + const price = i.sellPrice - e.purchasePrice - distance(w, shipWaypoint); + candidates.push({price: price, tradeVolume: e.tradeVolume, symbol: e.symbol, waypoint: w}); + debugLog({distance: distance(w, shipWaypoint), purchasePrice: e.purchasePrice, sellPrice: i.sellPrice, tradeVolume: e.tradeVolume, symbol: e.symbol, waypoint: w}); + } + } + sortByPrice(candidates); + debugLog(candidates[0]); + throw "STOP"; + while(!ship.isFull()) { + await ship.purchase(candidates[0].symbol, Math.min(candidates[0].tradeVolume, ship.cargo.capacity - ship.cargo.units)); + } + await ship.navigate(candidates[0].waypoint); + while (!ship.isEmpty()) { + await ship.sell(candidates[0].symbol, Math.min(candidates[0].tradeVolume, ship.cargo.inventory.filter(i => i.symbol === candidates[0].symbol)[0].units)); + } + } +} |