summaryrefslogtreecommitdiff
path: root/nodejs/automation
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/automation
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/automation')
-rw-r--r--nodejs/automation/agent.ts66
-rw-r--r--nodejs/automation/contracting.ts2
2 files changed, 67 insertions, 1 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: