[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
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 {
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue