summaryrefslogtreecommitdiff
path: root/nodejs
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-15 23:49:33 +0200
committerJulien Dessaux2024-05-15 23:49:33 +0200
commit6504e44ffa97965e47e893b55621d2d04003d519 (patch)
tree18b416bdd03988b5e2215e8e4c1771b06e7f4084 /nodejs
parent[node] updated dependencies (diff)
downloadspacetraders-6504e44ffa97965e47e893b55621d2d04003d519.tar.gz
spacetraders-6504e44ffa97965e47e893b55621d2d04003d519.tar.bz2
spacetraders-6504e44ffa97965e47e893b55621d2d04003d519.zip
[node] Added agent class, and fixed contract updates
Diffstat (limited to 'nodejs')
-rw-r--r--nodejs/automation/init.ts8
-rw-r--r--nodejs/database/agents.ts20
-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
6 files changed, 61 insertions, 44 deletions
diff --git a/nodejs/automation/init.ts b/nodejs/automation/init.ts
index 28163b7..7ec1252 100644
--- a/nodejs/automation/init.ts
+++ b/nodejs/automation/init.ts
@@ -1,12 +1,9 @@
-import * as dbAgents from '../database/agents.ts';
import * as db from '../database/db.ts';
import * as dbTokens from '../database/tokens.ts';
import {
Response,
} from '../lib/api.ts';
-import {
- Agent,
-} from '../lib/types.ts';
+import { Agent, initAgent, setAgent } from '../lib/agent.ts';
import { Contract } from '../lib/contracts.ts';
import { Ship } from '../lib/ships.ts';
import * as libContracts from '../lib/contracts.ts';
@@ -30,6 +27,7 @@ export async function init(): Promise<void> {
switch(json.error?.code) {
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened
// TODO await agents.agents();
+ await initAgent();
return;
default:
throw json;
@@ -37,5 +35,5 @@ export async function init(): Promise<void> {
}
db.reset();
dbTokens.addToken(json.data.token);
- dbAgents.addAgent(json.data.agent);
+ setAgent(json.data.agent);
}
diff --git a/nodejs/database/agents.ts b/nodejs/database/agents.ts
deleted file mode 100644
index 5221dc7..0000000
--- a/nodejs/database/agents.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { Agent } from '../lib/types.ts';
-import { DbData, db } from './db.ts';
-
-const addAgentStatement = db.prepare(`INSERT INTO agents(data) VALUES (json(?));`);
-const getAgentStatement = db.prepare(`SELECT data FROM agents;`);
-const setAgentStatement = db.prepare(`UPDATE agents SET data = json(?);`);
-
-export function addAgent(agent: Agent): void {
- addAgentStatement.run(JSON.stringify(agent));
-}
-
-export function getAgent(): Agent|null {
- const data = getAgentStatement.get() as DbData|undefined;
- if (!data) return null;
- return JSON.parse(data.data);
-}
-
-export function setAgent(agent: Agent): void {
- setAgentStatement.run(JSON.stringify(agent));
-}
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;