summaryrefslogtreecommitdiff
path: root/nodejs/automation/agent.ts
blob: 59799f52b8f6d6ae90ca64a082656ce46c2eb9be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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, purchaseShip, 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,
	send_the_starting_probe_to_a_shipyard_that_sells_probes,
}

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:
					// TODO await autoContracting.run(ships[0]);
					state++;
					continue;
				case states.visit_all_shipyards:
					await visit_all_shipyards(ships[1]);
					state++;
					continue;
				case states.send_the_starting_probe_to_a_shipyard_that_sells_probes:
					await send_the_starting_probe_to_a_shipyard_that_sells_probes(ships[1]);
					state++;
					continue;
				default:
					debugLog('No more agent processor states implemented, exiting!')
					return;
			}
		}
	} catch (e) {
		running = false;
		throw e;
	}
}

async function send_the_starting_probe_to_a_shipyard_that_sells_probes(probe: Ship) {
	const probeWaypoint = await waypoint(probe.nav.waypointSymbol);
	const myShipyard = await shipyard(probeWaypoint);
	if (myShipyard.shipTypes.some(t => t.type === 'SHIP_PROBE')) return;
	// our starting probe is not at a shipyard that sells probes, let's move
	const shipyardWaypoints = await trait(probe.nav.systemSymbol, 'SHIPYARD');
	let candidates: Array<{price: number, waypoint: Waypoint}> = [];
	for (const w of shipyardWaypoints) {
		const shipyardData = await shipyard(w);
		const probeData = shipyardData.ships.filter(t => t.type === 'SHIP_PROBE');
		if (probeData.length === 0) continue;
		candidates.push({price: probeData[0].purchasePrice, waypoint: w });
	};
	candidates.sort(function(a, b) {
		if (a.price < b.price) {
			return -1;
		} else if (a.price > b.price) {
			return 1;
		}
		return 0;
	});
	await probe.navigate(candidates[0].waypoint);
}

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);
	}
}