From 09537408a5eef07b3b069045b5268b80617e7b18 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 22 May 2024 01:08:27 +0200 Subject: [node] implement ship purchasing --- nodejs/automation/agent.ts | 2 +- nodejs/lib/ships.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/nodejs/automation/agent.ts b/nodejs/automation/agent.ts index 9ee0ea8..59799f5 100644 --- a/nodejs/automation/agent.ts +++ b/nodejs/automation/agent.ts @@ -3,7 +3,7 @@ import events from 'events'; import * as autoContracting from './contracting.ts'; import { debugLog, send, sleep } from '../lib/api.ts'; import { getAgent } from '../lib/agent.ts'; -import { getShips, Ship } from '../lib/ships.ts'; +import { getShips, purchaseShip, Ship } from '../lib/ships.ts'; import { market, shipyard, trait, waypoint } from '../lib/systems.ts'; import { Waypoint } from '../lib/types.ts'; import { diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts index 7daae71..0e3605f 100644 --- a/nodejs/lib/ships.ts +++ b/nodejs/lib/ships.ts @@ -22,6 +22,7 @@ import { Waypoint, } from './types.ts'; import { + is_there_a_ship_at_this_waypoint, shortestPath, } from './utils.ts'; @@ -251,3 +252,48 @@ export async function initShips(): Promise { } myShips = response.data.map(ship => new Ship(ship)); } + +export async function purchaseShip(shipType: string): Promise { + const shipyardWaypoints = await libSystems.trait(getShips()[0].nav.systemSymbol, 'SHIPYARD'); + // backup candidates exist in case we do not have a probe in orbit of a + // shipyard selling ${shipType} + let backupCandidates: Array<{price: number, waypoint: Waypoint}> = []; + let candidates: Array<{price: number, waypoint: Waypoint}> = []; + for (const w of shipyardWaypoints) { + const shipyardData = await libSystems.shipyard(w); + const data = shipyardData.ships.filter(t => t.type === shipType); + if (data.length === 0) continue; + backupCandidates.push({price: data[0].purchasePrice, waypoint: w }); + if (!is_there_a_ship_at_this_waypoint(w)) continue; + candidates.push({price: data[0].purchasePrice, waypoint: w }); + } + let needsNavigate = false; + if (candidates.length === 0) { + if (backupCandidates.length === 0) throw `no shipyards sell ships of type ${shipType}`; + candidates = backupCandidates; + needsNavigate = true; + } + candidates.sort(function(a, b) { + if (a.price < b.price) { + return -1; + } else if (a.price > b.price) { + return 1; + } + return 0; + }); + if (needsNavigate) { + // we did not have a probe in orbit of a shipyard selling ${shipType} + // yet, must be early game buying our second probe so let's move the + // starting probe in position + await getShips()[1].navigate(candidates[0].waypoint); + } + const response = await send<{agent: Agent, ship: Ship}>({endpoint: `/my/ships`, method: 'POST', payload: {shipType: shipType, waypointSymbol: candidates[0].waypoint.symbol}}); // TODO transaction field + if (response.error) { + debugLog(response); + throw response; + } + setAgent(response.data.agent); + const ship = new Ship(response.data.ship); + myShips.push(ship); + return ship; +} -- cgit v1.2.3