summaryrefslogtreecommitdiff
path: root/nodejs/lib/contracts.ts
blob: f9ba212b8b4688493f72055abec602c6825c79cf (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
import {
	Cargo,
} from './types.ts';
import {
	APIError,
	debugLog,
	send,
	sendPaginated,
} from './api.ts';
import { Agent, setAgent } from './agent.ts';
import { Ship } from './ships.ts';

export async function getContracts(): Promise<Array<Contract>> {
	const response = await sendPaginated<Contract>({endpoint: '/my/contracts'});
	return response.map(contract => new Contract(contract));
}

export class Contract {
	accepted: boolean;
	deadlineToAccept: Date;
	expiration: Date;
	factionSymbol: string;
	fulfilled: boolean;
	id: string;
	terms: {
		deadline: Date;
		payment: {
			onAccepted: number;
			onFulfilled: number;
		},
		deliver: Array<{
			tradeSymbol: string;
			destinationSymbol: string;
			unitsRequired: number;
			unitsFulfilled: number;
		}>;
	};
	type: string;
	constructor(contract: Contract) {
		this.accepted = contract.accepted;
		this.deadlineToAccept = contract.deadlineToAccept;
		this.expiration = contract.expiration;
		this.factionSymbol = contract.factionSymbol;
		this.fulfilled = contract.fulfilled;
		this.id = contract.id;
		this.terms = contract.terms;
		this.type = contract.type;
	}
	async accept(): Promise<void> {
		if (this.accepted) return;
		const response = await send<{agent: Agent, contract: Contract, type: ''}>({endpoint: `/my/contracts/${this.id}/accept`, method: 'POST'});
		if (response.error) {
			debugLog(response);
			throw response;
		}
		this.accepted = response.data.contract.accepted;
		this.terms = response.data.contract.terms;
		setAgent(response.data.agent);
	}
	async deliver(ship: Ship): Promise<void> {
		const unitsRemaining = this.terms.deliver[0].unitsRequired - this.terms.deliver[0].unitsFulfilled;
		if (unitsRemaining <= 0) return await this.fulfill();
		const tradeSymbol = this.terms.deliver[0].tradeSymbol;
		let units = 0;
		ship.cargo.inventory.forEach(i => {if (i.symbol === tradeSymbol) units = i.units; });
		if (units === 0) return;
		if (units > unitsRemaining) units = unitsRemaining;
		await ship.dock(); // we need to be docked to deliver
		const response = await send<{contract: Contract, cargo: Cargo}>({ endpoint: `/my/contracts/${this.id}/deliver`, method: 'POST', payload: {
			shipSymbol: ship.symbol,
			tradeSymbol: tradeSymbol,
			units: units,
		}});
		if (response.error) {
			switch(response.error.code) {
				case 4503: // contract has expired
					// TODO sell cargo? the next trading loop should take care of it by itself
					this.fulfilled = true;
					return;
				case 4509: // contract delivery terms have been met
					return await this.fulfill();
				default: // yet unhandled error
					debugLog(response);
					throw response;
			}
		}
		this.terms = response.data.contract.terms;
		ship.cargo = response.data.cargo;
		if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) {
			return await this.fulfill();
		}
	}
	async fulfill(): Promise<void> {
		if (this.terms.deliver[0].unitsRequired < this.terms.deliver[0].unitsFulfilled) return;
		if (this.fulfilled) return;
		const response = await send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${this.id}/fulfill`, method: 'POST'});
		if (response.error) {
			debugLog(response);
			throw response;
		}
		setAgent(response.data.agent);
		this.fulfilled = true;
		this.terms = response.data.contract.terms;
	}
};