summaryrefslogtreecommitdiff
path: root/nodejs/lib/ships.js
blob: d75d78ebe350256ec4cd315e481e025b2ece75a4 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import * as api from './api.js';
import * as dbConfig from '../database/config.js';
import * as dbShips from '../database/ships.js';
import * as dbSurveys from '../database/surveys.js';
import * as systems from '../lib/systems.js';

export async function extract(ctx) {
	const ship = dbShips.getShip(ctx.symbol);
	const asteroidFields = await systems.type({symbol: ship.nav.systemSymbol, type: 'ASTEROID_FIELD'});
	// TODO if there are multiple fields, find the closest one?
	await navigate({symbol: ctx.symbol, waypoint: asteroidFields[0].symbol});
	await orbit(ctx);
	// TODO handle surveying?
	const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/extract`, method: 'POST'});
	if (response.error !== undefined) {
		switch(response.error.code) {
		case 4000: // ship is on cooldown
			await api.sleep(response.error.data.cooldown.remainingSeconds  * 1000);
			return await extract(ctx);
		case 4228: // ship is full
			return null;
		default: // yet unhandled error
			throw response;
		}
	} else {
		dbShips.setShipCargo(ctx.symbol, response.data.cargo);
		await api.sleep(response.data.cooldown.remainingSeconds*1000);
	}
	return response;
}

export async function dock(ctx) {
	const ship = dbShips.getShip(ctx.symbol);
	if (ship.nav.status === 'DOCKED') {
		return null;
	}
	const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/dock`, method: 'POST'});
	if (response.error !== undefined) {
		switch(response.error.code) {
		case 4214: // ship is in transit
			await api.sleep(response.error.data.secondsToArrival  * 1000);
			return await dock(ctx);
		default: // yet unhandled error
			throw response;
		}
	}
	dbShips.setShipNav(ctx.symbol, response.data.nav);
	return response;
}

function hasMount(shipSymbol, mountSymbol) {
	const ship = dbShips.getShip(shipSymbol);
	return ship.mounts.filter(s => s.symbol === mountSymbol).length > 0;
}

export async function jump(ctx) {
	// TODO
	const response = await api.send({endpoint: `/my/ships/${ctx.ship}/jump`, method: 'POST', payload: { systemSymbol: ctx.system }});
	await api.sleep(response.data.cooldown.remainingSeconds*1000);
	return response;
}

export async function navigate(ctx) {
	const ship = dbShips.getShip(ctx.symbol);
	if (ship.nav.waypointSymbol === ctx.waypoint) {
		return await orbit(ctx);
	}
	await orbit(ctx);
	// TODO if we do not have enough fuel, make a stop to refuel along the way or drift to the destination
	const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: ctx.waypoint }});
	if (response.error !== undefined) {
		switch(response.error.code) {
		case 4214: // ship is in transit
			await api.sleep(response.error.data.secondsToArrival  * 1000);
			return await navigate(ctx);
		default: // yet unhandled error
			throw response;
		}
	}
	dbShips.setShipFuel(ctx.symbol, response.data.fuel);
	dbShips.setShipNav(ctx.symbol, response.data.nav);
	const delay = new Date(response.data.nav.route.arrival) - new Date();
	await api.sleep(delay);
	response.data.nav.status = 'IN_ORBIT';
	dbShips.setShipNav(ctx.symbol, response.data.nav);
	await refuel(ctx);
	return response;
}

export async function negotiate(ctx) {
	// TODO
	return await api.send({endpoint: `/my/ships/${ctx.ship}/negotiate/contract`, method: 'POST'});
}

export async function orbit(ctx) {
	const ship = dbShips.getShip(ctx.symbol);
	if (ship.nav.status === 'IN_ORBIT') {
		return null;
	}
	const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/orbit`, method: 'POST'});
	if (response.error !== undefined) {
		switch(response.error.code) {
		case 4214: // ship is in transit
			await api.sleep(response.error.data.secondsToArrival  * 1000);
			return await orbit(ctx);
		default: // yet unhandled error
			throw response;
		}
	}
	dbShips.setShipNav(ctx.symbol, response.data.nav);
	return response;
}

export async function purchase(ctx) {
	const response = await api.send({endpoint: '/my/ships', method: 'POST', payload: {
		shipType: ctx.shipType,
		waypointSymbol: ctx.waypoint,
	}});
	if (response.error !== undefined) {
		throw response;
	}
	dbShips.setShip(response.data.ship);
	return response.data;
}

export async function refuel(ctx) {
	const ship = dbShips.getShip(ctx.symbol);
	if (ship.fuel.current >= ship.fuel.capacity * 0.9) {
		return null;
	}
	// TODO check if our current waypoint has a marketplace (and sells fuel)?
	await dock(ctx);
	const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/refuel`, method: 'POST'});
	if (response.error !== undefined) {
		throw response;
	}
	dbShips.setShipFuel(ctx.symbol, response.data.fuel);
	// TODO track credits
	return response;
}

export async function sell(ctx) {
	// TODO check if our current waypoint has a marketplace (and sells fuel)?
	await dock(ctx);
	const ship = dbShips.getShip(ctx.symbol);
	const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/sell`, method: 'POST', payload: { symbol: ctx.good, units: ctx.units }});
	if (response.error !== undefined) {
		throw response;
	}
	dbShips.setShipCargo(ctx.symbol, response.data.cargo);
	// TODO track credits
	return response;
}

export async function ship(ctx) {
	const response = await api.send({endpoint: `/my/ships/${ctx.symbol}`});
	if (response.error !== undefined) {
		throw response;
	}
	dbShips.setShip(response.data);
	return response;
}

export async function survey(ctx) {
	if (!hasMount(ctx.symbol, 'MOUNT_SURVEYOR_I')) { // we check if a surveyor is mounted on the ship
		return null;
	}
	const ship = dbShips.getShip(ctx.symbol);
	const asteroidFields = await systems.type({symbol: ship.nav.systemSymbol, type: 'ASTEROID_FIELD'});
	// TODO if there are multiple fields, find the closest one?
	await navigate({symbol: ctx.symbol, waypoint: asteroidFields[0].symbol});
	await orbit(ctx);
	const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/survey`, method: 'POST'});
	api.debugLog(response);
	if (response.error !== undefined) {
		switch(response.error.code) {
		case 4000: // ship is on cooldown
			await api.sleep(response.error.data.cooldown.remainingSeconds  * 1000);
			return await survey(ctx);
		default: // yet unhandled error
			throw response;
		}
	}
	dbSurveys.set(response.data.surveys[0]);
	await api.sleep(response.data.cooldown.remainingSeconds*1000);
	return response;
}