[node] implement ship purchasing
This commit is contained in:
parent
b1627bf0d7
commit
09537408a5
2 changed files with 47 additions and 1 deletions
|
@ -3,7 +3,7 @@ import events from 'events';
|
||||||
import * as autoContracting from './contracting.ts';
|
import * as autoContracting from './contracting.ts';
|
||||||
import { debugLog, send, sleep } from '../lib/api.ts';
|
import { debugLog, send, sleep } from '../lib/api.ts';
|
||||||
import { getAgent } from '../lib/agent.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 { market, shipyard, trait, waypoint } from '../lib/systems.ts';
|
||||||
import { Waypoint } from '../lib/types.ts';
|
import { Waypoint } from '../lib/types.ts';
|
||||||
import {
|
import {
|
||||||
|
|
|
@ -22,6 +22,7 @@ import {
|
||||||
Waypoint,
|
Waypoint,
|
||||||
} from './types.ts';
|
} from './types.ts';
|
||||||
import {
|
import {
|
||||||
|
is_there_a_ship_at_this_waypoint,
|
||||||
shortestPath,
|
shortestPath,
|
||||||
} from './utils.ts';
|
} from './utils.ts';
|
||||||
|
|
||||||
|
@ -251,3 +252,48 @@ export async function initShips(): Promise<void> {
|
||||||
}
|
}
|
||||||
myShips = response.data.map(ship => new Ship(ship));
|
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;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue