[node] Added agent class, and fixed contract updates
This commit is contained in:
parent
9d48b58d7f
commit
6504e44ffa
6 changed files with 61 additions and 44 deletions
|
@ -1,12 +1,9 @@
|
||||||
import * as dbAgents from '../database/agents.ts';
|
|
||||||
import * as db from '../database/db.ts';
|
import * as db from '../database/db.ts';
|
||||||
import * as dbTokens from '../database/tokens.ts';
|
import * as dbTokens from '../database/tokens.ts';
|
||||||
import {
|
import {
|
||||||
Response,
|
Response,
|
||||||
} from '../lib/api.ts';
|
} from '../lib/api.ts';
|
||||||
import {
|
import { Agent, initAgent, setAgent } from '../lib/agent.ts';
|
||||||
Agent,
|
|
||||||
} from '../lib/types.ts';
|
|
||||||
import { Contract } from '../lib/contracts.ts';
|
import { Contract } from '../lib/contracts.ts';
|
||||||
import { Ship } from '../lib/ships.ts';
|
import { Ship } from '../lib/ships.ts';
|
||||||
import * as libContracts from '../lib/contracts.ts';
|
import * as libContracts from '../lib/contracts.ts';
|
||||||
|
@ -30,6 +27,7 @@ export async function init(): Promise<void> {
|
||||||
switch(json.error?.code) {
|
switch(json.error?.code) {
|
||||||
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened
|
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened
|
||||||
// TODO await agents.agents();
|
// TODO await agents.agents();
|
||||||
|
await initAgent();
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
throw json;
|
throw json;
|
||||||
|
@ -37,5 +35,5 @@ export async function init(): Promise<void> {
|
||||||
}
|
}
|
||||||
db.reset();
|
db.reset();
|
||||||
dbTokens.addToken(json.data.token);
|
dbTokens.addToken(json.data.token);
|
||||||
dbAgents.addAgent(json.data.agent);
|
setAgent(json.data.agent);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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));
|
|
||||||
}
|
|
45
nodejs/lib/agent.ts
Normal file
45
nodejs/lib/agent.ts
Normal file
|
@ -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);
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
import {
|
import {
|
||||||
Agent,
|
|
||||||
Cargo,
|
Cargo,
|
||||||
} from './types.ts';
|
} from './types.ts';
|
||||||
import {
|
import {
|
||||||
|
@ -8,8 +7,8 @@ import {
|
||||||
send,
|
send,
|
||||||
sendPaginated,
|
sendPaginated,
|
||||||
} from './api.ts';
|
} from './api.ts';
|
||||||
|
import { Agent, setAgent } from './agent.ts';
|
||||||
import { Ship } from './ships.ts';
|
import { Ship } from './ships.ts';
|
||||||
import * as dbAgents from '../database/agents.ts';
|
|
||||||
|
|
||||||
export async function getContracts(): Promise<Array<Contract>> {
|
export async function getContracts(): Promise<Array<Contract>> {
|
||||||
const response = await sendPaginated<Contract>({endpoint: '/my/contracts'});
|
const response = await sendPaginated<Contract>({endpoint: '/my/contracts'});
|
||||||
|
@ -54,7 +53,9 @@ export class Contract {
|
||||||
debugLog(response);
|
debugLog(response);
|
||||||
throw 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> {
|
async deliver(ship: Ship): Promise<void> {
|
||||||
const unitsRemaining = this.terms.deliver[0].unitsRequired - this.terms.deliver[0].unitsFulfilled;
|
const unitsRemaining = this.terms.deliver[0].unitsRequired - this.terms.deliver[0].unitsFulfilled;
|
||||||
|
@ -83,19 +84,22 @@ export class Contract {
|
||||||
throw response;
|
throw response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.terms = contract.terms;
|
||||||
ship.cargo = response.data.cargo;
|
ship.cargo = response.data.cargo;
|
||||||
if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) {
|
if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) {
|
||||||
return await this.fulfill();
|
return await this.fulfill();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async fulfill(): Promise<void> {
|
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;
|
if (this.fulfilled) return;
|
||||||
const response = await send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${this.id}/fulfill`, method: 'POST'});
|
const response = await send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${this.id}/fulfill`, method: 'POST'});
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
debugLog(response);
|
debugLog(response);
|
||||||
throw response;
|
throw response;
|
||||||
}
|
}
|
||||||
dbAgents.setAgent(response.data.agent);
|
setAgent(response.data.agent);
|
||||||
|
this.fulfilled = true;
|
||||||
|
this.terms = contract.terms;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,10 +10,10 @@ import {
|
||||||
ShipIsStillOnCooldownError,
|
ShipIsStillOnCooldownError,
|
||||||
ShipRequiresMoreFuelForNavigationError,
|
ShipRequiresMoreFuelForNavigationError,
|
||||||
} from './errors.ts';
|
} from './errors.ts';
|
||||||
|
import { Agent, setAgent } from './agent.ts';
|
||||||
import { Contract } from './contracts.ts';
|
import { Contract } from './contracts.ts';
|
||||||
import * as libSystems from './systems.ts';
|
import * as libSystems from './systems.ts';
|
||||||
import {
|
import {
|
||||||
Agent,
|
|
||||||
Cargo,
|
Cargo,
|
||||||
Cooldown,
|
Cooldown,
|
||||||
Fuel,
|
Fuel,
|
||||||
|
@ -24,7 +24,6 @@ import {
|
||||||
import {
|
import {
|
||||||
shortestPath,
|
shortestPath,
|
||||||
} from './utils.ts';
|
} from './utils.ts';
|
||||||
import * as dbAgents from '../database/agents.ts';
|
|
||||||
|
|
||||||
export async function getShips(): Promise<Array<Ship>> {
|
export async function getShips(): Promise<Array<Ship>> {
|
||||||
const response = await send<Array<Ship>>({endpoint: `/my/ships`, page: 1});
|
const response = await send<Array<Ship>>({endpoint: `/my/ships`, page: 1});
|
||||||
|
@ -188,7 +187,7 @@ export class Ship {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.cargo = response.data.cargo;
|
this.cargo = response.data.cargo;
|
||||||
dbAgents.setAgent(response.data.agent);
|
setAgent(response.data.agent);
|
||||||
}
|
}
|
||||||
async refuel(): Promise<void> {
|
async refuel(): Promise<void> {
|
||||||
if (this.fuel.current === this.fuel.capacity) return;
|
if (this.fuel.current === this.fuel.capacity) return;
|
||||||
|
@ -200,7 +199,7 @@ export class Ship {
|
||||||
throw response;
|
throw response;
|
||||||
}
|
}
|
||||||
this.fuel = response.data.fuel;
|
this.fuel = response.data.fuel;
|
||||||
dbAgents.setAgent(response.data.agent);
|
setAgent(response.data.agent);
|
||||||
}
|
}
|
||||||
async sell(tradeSymbol: string, maybeUnits?: number): Promise<Cargo> {
|
async sell(tradeSymbol: string, maybeUnits?: number): Promise<Cargo> {
|
||||||
// TODO check if our current waypoint has a marketplace and buys tradeSymbol?
|
// TODO check if our current waypoint has a marketplace and buys tradeSymbol?
|
||||||
|
@ -225,7 +224,7 @@ export class Ship {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.cargo = response.data.cargo;
|
this.cargo = response.data.cargo;
|
||||||
dbAgents.setAgent(response.data.agent);
|
setAgent(response.data.agent);
|
||||||
return this.cargo;
|
return this.cargo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,3 @@
|
||||||
export type Agent = {
|
|
||||||
accountId: string;
|
|
||||||
credits: number;
|
|
||||||
headquarters: string;
|
|
||||||
shipCount: number;
|
|
||||||
startingFaction: string;
|
|
||||||
symbol: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type CommonThing = {
|
export type CommonThing = {
|
||||||
description: string;
|
description: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
|
Loading…
Add table
Reference in a new issue