From 6504e44ffa97965e47e893b55621d2d04003d519 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 15 May 2024 23:49:33 +0200 Subject: [node] Added agent class, and fixed contract updates --- nodejs/automation/init.ts | 8 +++----- nodejs/database/agents.ts | 20 -------------------- nodejs/lib/agent.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ nodejs/lib/contracts.ts | 14 +++++++++----- nodejs/lib/ships.ts | 9 ++++----- nodejs/lib/types.ts | 9 --------- 6 files changed, 61 insertions(+), 44 deletions(-) delete mode 100644 nodejs/database/agents.ts create mode 100644 nodejs/lib/agent.ts (limited to 'nodejs') 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 { 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 { } 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 { + const response = await send({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> { const response = await sendPaginated({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 { 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 { - 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> { const response = await send>({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 { 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 { // 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; -- cgit v1.2.3