summaryrefslogtreecommitdiff
path: root/nodejs
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-21 00:26:17 +0200
committerJulien Dessaux2024-05-21 00:26:17 +0200
commitd77558b8def94fd1e1efecc3cfebb7dd3b6d6ae3 (patch)
treeec1c660af2badb2df4e407d81e16a5a297b4773d /nodejs
parentFixed pathing function for probes (diff)
downloadspacetraders-d77558b8def94fd1e1efecc3cfebb7dd3b6d6ae3.tar.gz
spacetraders-d77558b8def94fd1e1efecc3cfebb7dd3b6d6ae3.tar.bz2
spacetraders-d77558b8def94fd1e1efecc3cfebb7dd3b6d6ae3.zip
[node] implement agent automation that visits all shipyards with the starting probe
Diffstat (limited to 'nodejs')
-rw-r--r--nodejs/automation/agent.ts66
-rw-r--r--nodejs/automation/contracting.ts2
-rw-r--r--nodejs/lib/ships.ts17
-rw-r--r--nodejs/lib/systems.ts6
-rw-r--r--nodejs/lib/utils.ts2
-rwxr-xr-xnodejs/main.ts20
6 files changed, 101 insertions, 12 deletions
diff --git a/nodejs/automation/agent.ts b/nodejs/automation/agent.ts
new file mode 100644
index 0000000..429c83f
--- /dev/null
+++ b/nodejs/automation/agent.ts
@@ -0,0 +1,66 @@
+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 { market, shipyard, trait, waypoint } from '../lib/systems.ts';
+import { Waypoint } from '../lib/types.ts';
+import {
+ distance,
+ sortByDistanceFrom,
+} from '../lib/utils.ts';
+
+const bus = new events.EventEmitter(); // a bus to notify the agent to start purchasing ships
+let running = false;
+let state = 0;
+enum states {
+ start_running_contracts_with_the_command_ship = 0,
+ visit_all_shipyards,
+}
+
+export async function run(): Promise<void> {
+ if (running) {
+ throw 'refusing to start a second agent processor';
+ }
+ running = true;
+ state = 0;
+ try {
+ while(true) {
+ const ships = getShips();
+ switch(state) {
+ case states.start_running_contracts_with_the_command_ship:
+ //await autoContracting.run(ships[0]);
+ state++;
+ continue;
+ case states.visit_all_shipyards:
+ await visit_all_shipyards(ships[1]);
+ state++;
+ continue;
+ default:
+ debugLog('No more agent processor states implemented, exiting!')
+ return;
+ }
+ }
+ } catch (e) {
+ running = false;
+ throw e;
+ }
+}
+
+async function visit_all_shipyards(probe: Ship) {
+ const probeWaypoint = await waypoint(probe.nav.waypointSymbol);
+ const shipyardWaypoints = await trait(probe.nav.systemSymbol, 'SHIPYARD');
+ let candidates: Array<Waypoint> = [];
+ for (const w of shipyardWaypoints) {
+ const shipyardData = await shipyard(w);
+ if (shipyardData.ships) continue;
+ candidates.push(w);
+ }
+ const nexts = sortByDistanceFrom(probeWaypoint, candidates).map(o => o.data);
+ for (const next of nexts) {
+ await probe.navigate(next);
+ await market(next);
+ await shipyard(next);
+ }
+}
diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts
index 2215b2c..80569ef 100644
--- a/nodejs/automation/contracting.ts
+++ b/nodejs/automation/contracting.ts
@@ -33,7 +33,7 @@ async function runOne(contract: Contract, ship: Ship): Promise<void> {
//if (contract.terms.deliver[0].tradeSymbol.match(/_ORE$/)) {
// await runOreProcurement(contract, ship);
//} else {
- await runTradeProcurement(contract, ship);
+ await runTradeProcurement(contract, ship);
//}
break;
default:
diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts
index e4875f8..7daae71 100644
--- a/nodejs/lib/ships.ts
+++ b/nodejs/lib/ships.ts
@@ -88,6 +88,22 @@ export class Ship {
await sleep(response.data.cooldown.remainingSeconds*1000);
return this.cargo;
}
+ //async flightMode(mode: string): Promise<void> {
+ // if (this.nav.flightMode === mode) return;
+ // const response = await send<nav>({endpoint: `/my/ships/${this.symbol}/nav`, method: 'PATCH', payload: { flightmode: mode }});
+ // if (response.error) {
+ // switch(response.error.code) {
+ // case 4214:
+ // const sicite = response.error.data as ShipIsCurrentlyInTransitError;
+ // await sleep(sicite.secondsToArrival * 1000);
+ // return await this.flightMode(mode);
+ // default: // yet unhandled error
+ // debugLog(response);
+ // throw response;
+ // }
+ // }
+ // this.nav = response.data;
+ //}
isFull(): boolean {
return this.cargo.units >= this.cargo.capacity * 0.9;
}
@@ -105,6 +121,7 @@ export class Ship {
}
private async navigateTo(symbol: string): Promise<void> {
await this.orbit();
+ //if (this.fuel.capacity === 0) this.flightMode('BURN');
const response = await send<{fuel: Fuel, nav: Nav}>({endpoint: `/my/ships/${this.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: symbol }}); // TODO events field
if (response.error) {
switch(response.error.code) {
diff --git a/nodejs/lib/systems.ts b/nodejs/lib/systems.ts
index 6f0a830..f07f92b 100644
--- a/nodejs/lib/systems.ts
+++ b/nodejs/lib/systems.ts
@@ -13,13 +13,13 @@ import {
Waypoint,
} from './types.ts'
import {
- isThereAShipAtThisWaypoint,
+ is_there_a_ship_at_this_waypoint,
systemFromWaypoint,
} from './utils.ts';
export async function market(waypoint: Waypoint): Promise<Market> {
const data = dbMarkets.getMarketAtWaypoint(waypoint.symbol);
- if (data && (data.tradeGoods || !isThereAShipAtThisWaypoint(waypoint))) { return data; }
+ if (data && (data.tradeGoods || !is_there_a_ship_at_this_waypoint(waypoint))) { return data; }
const systemSymbol = systemFromWaypoint(waypoint.symbol);
let response = await send<Market>({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint.symbol}/market`});
if (response.error) {
@@ -32,7 +32,7 @@ export async function market(waypoint: Waypoint): Promise<Market> {
export async function shipyard(waypoint: Waypoint): Promise<Shipyard> {
const data = dbShipyards.get(waypoint.symbol);
- if (data && (data.ships || !isThereAShipAtThisWaypoint(waypoint))) { return data; }
+ if (data && (data.ships || !is_there_a_ship_at_this_waypoint(waypoint))) { return data; }
const systemSymbol = systemFromWaypoint(waypoint.symbol);
const response = await send<Shipyard>({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint.symbol}/shipyard`});
if (response.error) {
diff --git a/nodejs/lib/utils.ts b/nodejs/lib/utils.ts
index 24aa3b4..6d051ea 100644
--- a/nodejs/lib/utils.ts
+++ b/nodejs/lib/utils.ts
@@ -40,7 +40,7 @@ export function distance(a: Point, b: Point) {
return Math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2);
}
-export function isThereAShipAtThisWaypoint(waypoint: Waypoint): boolean {
+export function is_there_a_ship_at_this_waypoint(waypoint: Waypoint): boolean {
return getShips().some(s => s.nav.waypointSymbol === waypoint.symbol);
}
diff --git a/nodejs/main.ts b/nodejs/main.ts
index c4f205d..cb819c7 100755
--- a/nodejs/main.ts
+++ b/nodejs/main.ts
@@ -1,17 +1,23 @@
-import * as autoContracting from './automation/contracting.ts';
+import * as autoAgent from './automation/agent.ts';
//import * as autoExploring from './automation/exploration.ts';
import * as autoInit from './automation/init.ts';
import { getAgent } from './lib/agent.ts';
-import { getShips } from './lib/ships.ts';
-import { debugLog, send } from './lib/api.ts';
+import { debugLog } from './lib/api.ts';
//debugLog(await send({endpoint: '/'}));
await autoInit.init();
+
debugLog(getAgent());
-debugLog(await getAgent().purchaseShip());
+await autoAgent.run();
+
+//import { market, trait } from './lib/systems.ts';
+//const ws = await trait(ships[0].nav.systemSymbol, 'SHIPYARD');
+//debugLog(ws);
-//const ships = getShips();
-//await autoContracting.run(ships[0]); // dedicate the command ship to running contracts
-//autoExploring.init();
+//for (let w of ws) {
+// debugLog(await market(w));
+//}
+//
+//await ships[0].navigate(await waypoint('X1-GR47-I59'));