summaryrefslogtreecommitdiff
path: root/nodejs/lib/contracts.js
blob: 5c2e970ac5db07e28d15fe152692d669e958080d (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
import * as dbContracts from '../database/contracts.js';
import * as api from './api.js';
import * as dbShips from '../database/ships.js';
import * as libShips from '../lib/ships.js';

export async function accept(ctx) {
	const contract = dbContracts.getContract(ctx.id);
	if (contract.accepted) {
		return;
	}
	await api.send({endpoint: `/my/contracts/${ctx.id}/accept`, method: 'POST'});
	contract.accepted = true;
	dbContracts.setContract(contract);
}

export async function contracts() {
	const contracts = await api.send({endpoint: '/my/contracts', page: 1});
	contracts.forEach(contract => dbContracts.setContract(contract));
	return contracts;
}

export async function deliver(ctx) {
	await libShips.dock(ctx);
	const response = await api.send({ endpoint: `/my/contracts/${ctx.contract}/deliver`, method: 'POST', payload: {
		shipSymbol: ctx.symbol,
		tradeSymbol: ctx.good,
		units: ctx.units,
	}});
	if (response.error !== undefined) {
		throw response;
	}
	dbShips.setShipCargo(ctx.symbol, response.data.cargo);
	// TODO update contract delivered units
	// TODO track credits
}

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