summaryrefslogtreecommitdiff
path: root/lib/ships.js
blob: 6eefd870b63377d4811d90f7ef561813a83305a7 (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
import * as api from './api.js';

export async function extract(ctx) {
	const response = await api.send({endpoint: `/my/ships/${ctx.ship}/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 extract(ctx);
		case 4228: // ship is full
			return null;
		default: // yet unhandled error
			throw response;
		}
	}
	return response;
}

export async function dock(ctx) {
	const response = await api.send({endpoint: `/my/ships/${ctx.ship}/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 dock(ctx);
		default: // yet unhandled error
			throw response;
		}
	}
	return response;
}

export async function navigate(ctx) {
	const response = await api.send({endpoint: `/my/ships/${ctx.ship}/navigate`, method: 'POST', payload: { waypointSymbol: ctx.waypoint }});
	const delay = new Date(response.data.nav.route.arrival) - new Date();
	await api.sleep(delay);
}

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

export async function purchase(ctx) {
	return await api.send({endpoint: '/my/ships', method: 'POST', payload: {
		shipType: ctx.shipType,
		waypointSymbol: ctx.waypoint,
	}});
}

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

export async function sell(ctx) {
	return await api.send({endpoint: `/my/ships/${ctx.ship}/sell`, method: 'POST', payload: { symbol: ctx.good, units: ctx.units }});
}

export async function ship(ctx) {
	return await api.send({endpoint: `/my/ships/${ctx.ship}`});
}