summaryrefslogtreecommitdiff
path: root/nodejs/automation/init.ts
blob: 74f42fb7b447a151be282c1d7630ec5041aa6fcd (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
import * as dbAgents from '../database/agents.ts';
import * as db from '../database/db.ts';
import * as dbContracts from '../database/contracts.ts';
import * as dbShips from '../database/ships.ts';
import * as dbTokens from '../database/tokens.ts';
import { Agent } from '../model/agent.ts';
import { Response } from '../model/api.ts';
import { Contract } from '../model/contract.ts';
import { Ship } from '../model/ship.ts';
import * as api from '../lib/api.ts';
import * as libContracts from '../lib/contracts.ts';
import * as libShips from '../lib/ships.ts';

const symbol = process.env.NODE_ENV === 'test' ? 'ADYXAX-0' : 'ADYXAX-JS';

// This function registers then inits the database
export async function init(): Promise<void> {
	const response = await fetch('https://api.spacetraders.io/v2/register', {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json',
		},
		body: JSON.stringify({
			symbol: symbol,
			faction: "COSMIC",
		}),
	});
    const json = await response.json() as Response<{agent: Agent, contract: Contract, ship: Ship, token: string}>;
	if (json.error !== undefined) {
		switch(json.error?.code) {
			case 4111:  // 4111 means the agent symbol has already been claimed so no server reset happened
				// TODO await agents.agents();
				const contracts = await libContracts.getContracts();
				const ongoing = contracts.filter(c => !c.fulfilled);
				const ships = await libShips.getShips();
				if (ongoing.length === 0) libShips.negotiate(ships[0]);
				return;
			default:
				throw json;
		}
	}
	db.reset();
	dbTokens.addToken(json.data.token);
	dbAgents.addAgent(json.data.agent);
	dbContracts.setContract(json.data.contract);
	dbShips.setShip(json.data.ship);
	// Temporary fix to fetch the data on the startup probe
	await libShips.getShips();
}