summaryrefslogtreecommitdiff
path: root/nodejs/lib
diff options
context:
space:
mode:
Diffstat (limited to 'nodejs/lib')
-rw-r--r--nodejs/lib/agent.ts45
-rw-r--r--nodejs/lib/contracts.ts14
-rw-r--r--nodejs/lib/ships.ts9
-rw-r--r--nodejs/lib/types.ts9
4 files changed, 58 insertions, 19 deletions
diff --git a/nodejs/lib/agent.ts b/nodejs/lib/agent.ts
new file mode 100644
index 0000000..ee7200c
--- /dev/null
+++ b/nodejs/lib/agent.ts
@@ -0,0 +1,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);
+}
diff --git a/nodejs/lib/contracts.ts b/nodejs/lib/contracts.ts
index 1b54178..009c853 100644
--- a/nodejs/lib/contracts.ts
+++ b/nodejs/lib/contracts.ts
@@ -1,5 +1,4 @@
import {
- Agent,
Cargo,
} from './types.ts';
import {
@@ -8,8 +7,8 @@ import {
send,
sendPaginated,
} from './api.ts';
+import { Agent, setAgent } from './agent.ts';
import { Ship } from './ships.ts';
-import * as dbAgents from '../database/agents.ts';
export async function getContracts(): Promise<Array<Contract>> {
const response = await sendPaginated<Contract>({endpoint: '/my/contracts'});
@@ -54,7 +53,9 @@ export class Contract {
debugLog(response);
throw response;
}
- dbAgents.setAgent(response.data.agent);
+ this.accepted = contract.accepted;
+ this.terms = contract.terms;
+ setAgent(response.data.agent);
}
async deliver(ship: Ship): Promise<void> {
const unitsRemaining = this.terms.deliver[0].unitsRequired - this.terms.deliver[0].unitsFulfilled;
@@ -83,19 +84,22 @@ export class Contract {
throw response;
}
}
+ this.terms = 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.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;
}
- dbAgents.setAgent(response.data.agent);
+ setAgent(response.data.agent);
+ this.fulfilled = true;
+ this.terms = contract.terms;
}
};
diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts
index 8df3aea..920cc21 100644
--- a/nodejs/lib/ships.ts
+++ b/nodejs/lib/ships.ts
@@ -10,10 +10,10 @@ import {
ShipIsStillOnCooldownError,
ShipRequiresMoreFuelForNavigationError,
} from './errors.ts';
+import { Agent, setAgent } from './agent.ts';
import { Contract } from './contracts.ts';
import * as libSystems from './systems.ts';
import {
- Agent,
Cargo,
Cooldown,
Fuel,
@@ -24,7 +24,6 @@ import {
import {
shortestPath,
} from './utils.ts';
-import * as dbAgents from '../database/agents.ts';
export async function getShips(): Promise<Array<Ship>> {
const response = await send<Array<Ship>>({endpoint: `/my/ships`, page: 1});
@@ -188,7 +187,7 @@ export class Ship {
}
}
this.cargo = response.data.cargo;
- dbAgents.setAgent(response.data.agent);
+ setAgent(response.data.agent);
}
async refuel(): Promise<void> {
if (this.fuel.current === this.fuel.capacity) return;
@@ -200,7 +199,7 @@ export class Ship {
throw response;
}
this.fuel = response.data.fuel;
- dbAgents.setAgent(response.data.agent);
+ setAgent(response.data.agent);
}
async sell(tradeSymbol: string, maybeUnits?: number): Promise<Cargo> {
// TODO check if our current waypoint has a marketplace and buys tradeSymbol?
@@ -225,7 +224,7 @@ export class Ship {
}
}
this.cargo = response.data.cargo;
- dbAgents.setAgent(response.data.agent);
+ setAgent(response.data.agent);
return this.cargo;
}
}
diff --git a/nodejs/lib/types.ts b/nodejs/lib/types.ts
index e4d750f..4f15d70 100644
--- a/nodejs/lib/types.ts
+++ b/nodejs/lib/types.ts
@@ -1,12 +1,3 @@
-export type Agent = {
- accountId: string;
- credits: number;
- headquarters: string;
- shipCount: number;
- startingFaction: string;
- symbol: string;
-};
-
export type CommonThing = {
description: string;
name: string;