summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-22 01:08:27 +0200
committerJulien Dessaux2024-05-22 01:08:27 +0200
commit09537408a5eef07b3b069045b5268b80617e7b18 (patch)
tree2eed42c56dde531f9f12367e03856e74b1601ef9
parent[node] implement automation next step: sending the startup probe to a shipyar... (diff)
downloadspacetraders-09537408a5eef07b3b069045b5268b80617e7b18.tar.gz
spacetraders-09537408a5eef07b3b069045b5268b80617e7b18.tar.bz2
spacetraders-09537408a5eef07b3b069045b5268b80617e7b18.zip
[node] implement ship purchasing
-rw-r--r--nodejs/automation/agent.ts2
-rw-r--r--nodejs/lib/ships.ts46
2 files changed, 47 insertions, 1 deletions
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<void> {
}
myShips = response.data.map(ship => new Ship(ship));
}
+
+export async function purchaseShip(shipType: string): Promise<Ship> {
+ 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;
+}