summaryrefslogtreecommitdiff
path: root/nodejs/lib/agent.ts
blob: ee7200c551e3eeea416391f88ba8f3faa9d4f780 (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
import { debugLog, send } from './api.ts';

export class Agent {
	accountId: string;
	credits: number;
	headquarters: string;
	shipCount: number;
	startingFaction: string;
	symbol: string;
	constructor() {
		this.accountId = "";
		this.credits = 0;
		this.headquarters = "";
		this.shipCount = 0;
		this.startingFaction = "";
		this.symbol = "";
	}
	set(agent: Agent) {
		this.accountId = agent.accountId;
		this.credits = agent.credits;
		this.headquarters = agent.headquarters;
		this.shipCount = agent.shipCount;
		this.startingFaction = agent.startingFaction;
		this.symbol = agent.symbol;
	}
};

let myAgent : Agent = new Agent();

export function getAgent(): Agent {
	return myAgent;
}

export async function initAgent(): Promise<void> {
	const response = await send<Agent>({endpoint: `/my/agent`, page: 1});
	if (response.error) {
		debugLog(response);
		throw response;
	}
	myAgent.set(response.data);
}

export function setAgent(agent: Agent): void {
	myAgent.set(agent);
}