[node] Big Contracts lib refactoring
This commit is contained in:
parent
1f6daef018
commit
de0251bc22
7 changed files with 88 additions and 139 deletions
|
@ -1,10 +1,8 @@
|
||||||
import { debugLog } from '../lib/api.ts';
|
import { debugLog } from '../lib/api.ts';
|
||||||
import { Ship } from '../lib/ships.ts';
|
import { Ship } from '../lib/ships.ts';
|
||||||
import { Contract } from '../lib/types.ts';
|
|
||||||
import * as mining from './mining.js';
|
import * as mining from './mining.js';
|
||||||
import * as selling from './selling.js';
|
import * as selling from './selling.js';
|
||||||
import * as dbContracts from '../database/contracts.ts';
|
import { Contract, getContracts } from '../lib/contracts.ts';
|
||||||
import * as libContracts from '../lib/contracts.ts';
|
|
||||||
import * as libSystems from '../lib/systems.ts';
|
import * as libSystems from '../lib/systems.ts';
|
||||||
import * as systems from '../lib/systems.ts';
|
import * as systems from '../lib/systems.ts';
|
||||||
import {
|
import {
|
||||||
|
@ -12,7 +10,7 @@ import {
|
||||||
} from '../lib/utils.ts';
|
} from '../lib/utils.ts';
|
||||||
|
|
||||||
export async function run(ship: Ship): Promise<void> {
|
export async function run(ship: Ship): Promise<void> {
|
||||||
const contracts = await libContracts.getContracts();
|
const contracts = await getContracts();
|
||||||
const active = contracts.filter(function(c) {
|
const active = contracts.filter(function(c) {
|
||||||
if (c.fulfilled) return false;
|
if (c.fulfilled) return false;
|
||||||
const deadline = new Date(c.terms.deadline).getTime();
|
const deadline = new Date(c.terms.deadline).getTime();
|
||||||
|
@ -29,7 +27,7 @@ export async function run(ship: Ship): Promise<void> {
|
||||||
|
|
||||||
async function runOne(contract: Contract, ship: Ship): Promise<void> {
|
async function runOne(contract: Contract, ship: Ship): Promise<void> {
|
||||||
debugLog(contract);
|
debugLog(contract);
|
||||||
await libContracts.accept(contract);
|
await contract.accept();
|
||||||
switch(contract.type) {
|
switch(contract.type) {
|
||||||
case 'PROCUREMENT':
|
case 'PROCUREMENT':
|
||||||
//if (contract.terms.deliver[0].tradeSymbol.match(/_ORE$/)) {
|
//if (contract.terms.deliver[0].tradeSymbol.match(/_ORE$/)) {
|
||||||
|
@ -58,7 +56,7 @@ async function runOreProcurement(contract: Contract, ship: Ship): Promise<void>
|
||||||
break;
|
break;
|
||||||
case deliveryPoint.symbol:
|
case deliveryPoint.symbol:
|
||||||
if (goodCargo !== undefined) { // we could be here if a client restart happens right after selling before we navigate away
|
if (goodCargo !== undefined) { // we could be here if a client restart happens right after selling before we navigate away
|
||||||
contract = await libContracts.deliver(contract, ship);
|
await contract.deliver(ship);
|
||||||
if (contract.fulfilled) return;
|
if (contract.fulfilled) return;
|
||||||
}
|
}
|
||||||
await ship.navigate(asteroid);
|
await ship.navigate(asteroid);
|
||||||
|
@ -117,7 +115,7 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void
|
||||||
await ship.purchase(wantedCargo, units);
|
await ship.purchase(wantedCargo, units);
|
||||||
// then make a delivery
|
// then make a delivery
|
||||||
await ship.navigate(deliveryPoint);
|
await ship.navigate(deliveryPoint);
|
||||||
contract = await libContracts.deliver(contract, ship);
|
await contract.deliver(ship);
|
||||||
if (contract.fulfilled) return;
|
if (contract.fulfilled) return;
|
||||||
}
|
}
|
||||||
console.log("runTradeProcurement not implemented");
|
console.log("runTradeProcurement not implemented");
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
import * as dbAgents from '../database/agents.ts';
|
import * as dbAgents from '../database/agents.ts';
|
||||||
import * as db from '../database/db.ts';
|
import * as db from '../database/db.ts';
|
||||||
import * as dbContracts from '../database/contracts.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,
|
Agent,
|
||||||
Contract,
|
|
||||||
} from '../lib/types.ts';
|
} from '../lib/types.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';
|
||||||
|
|
||||||
|
@ -31,7 +30,6 @@ 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 libContracts.getContracts();
|
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
throw json;
|
throw json;
|
||||||
|
@ -40,5 +38,4 @@ 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);
|
dbAgents.addAgent(json.data.agent);
|
||||||
dbContracts.setContract(json.data.contract);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
import * as selling from './selling.js';
|
import * as selling from './selling.js';
|
||||||
import * as dbContracts from '../database/contracts.js';
|
import { Contract } from '../lib/contracts.js';
|
||||||
import { Ship } from '../lib/ships.js';
|
import { Ship } from '../lib/ships.js';
|
||||||
import {
|
import {
|
||||||
Contract,
|
|
||||||
Waypoint,
|
Waypoint,
|
||||||
} from '../lib/types.ts';
|
} from '../lib/types.ts';
|
||||||
import { categorizeCargo } from '../lib/utils.ts';
|
import { categorizeCargo } from '../lib/utils.ts';
|
||||||
|
@ -11,7 +10,6 @@ export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroid:
|
||||||
// TODO find a good asteroid
|
// TODO find a good asteroid
|
||||||
while(true) {
|
while(true) {
|
||||||
await mineUntilFull(ship);
|
await mineUntilFull(ship);
|
||||||
contract = dbContracts.getContract(contract.id);
|
|
||||||
const deliver = contract.terms.deliver[0];
|
const deliver = contract.terms.deliver[0];
|
||||||
const cargo = categorizeCargo(ship.cargo, deliver.tradeSymbol);
|
const cargo = categorizeCargo(ship.cargo, deliver.tradeSymbol);
|
||||||
const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0);
|
const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0);
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
import { Contract } from '../lib/types.ts';
|
|
||||||
import { DbData, db } from './db.ts';
|
|
||||||
|
|
||||||
const addContractStatement = db.prepare(`INSERT INTO contracts(data) VALUES (json(?));`);
|
|
||||||
const getContractStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'id' = ?;`);
|
|
||||||
const getContractsStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'fulfilled' = false;`);
|
|
||||||
const updateContractStatement = db.prepare(`UPDATE contracts SET data = json(:data) WHERE data->>'id' = :id;`);
|
|
||||||
|
|
||||||
export function getContract(id: string): Contract {
|
|
||||||
const data = getContractStatement.get(id) as DbData|undefined;
|
|
||||||
if (!data) throw `invalid id ${id} in getContract database call`;
|
|
||||||
return JSON.parse(data.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getContracts(): Array<Contract> {
|
|
||||||
const data = getContractsStatement.all() as Array<DbData>;
|
|
||||||
return data.map(contractData => JSON.parse(contractData.data));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function setContract(data: Contract): void {
|
|
||||||
const changes = updateContractStatement.run({
|
|
||||||
data: JSON.stringify(data),
|
|
||||||
id: data.id,
|
|
||||||
}).changes;
|
|
||||||
if (changes === 0) addContractStatement.run(JSON.stringify(data));
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
import {
|
import {
|
||||||
Agent,
|
Agent,
|
||||||
Cargo,
|
Cargo,
|
||||||
Contract,
|
|
||||||
} from './types.ts';
|
} from './types.ts';
|
||||||
import {
|
import {
|
||||||
APIError,
|
APIError,
|
||||||
|
@ -11,84 +10,92 @@ import {
|
||||||
} from './api.ts';
|
} from './api.ts';
|
||||||
import { Ship } from './ships.ts';
|
import { Ship } from './ships.ts';
|
||||||
import * as dbAgents from '../database/agents.ts';
|
import * as dbAgents from '../database/agents.ts';
|
||||||
import * as dbContracts from '../database/contracts.ts';
|
|
||||||
|
|
||||||
export async function accept(contract: Contract): Promise<Contract> {
|
|
||||||
contract = dbContracts.getContract(contract.id);
|
|
||||||
if (contract.accepted) return contract;
|
|
||||||
const response = await send<{agent: Agent, contract: Contract, type: ''}>({endpoint: `/my/contracts/${contract.id}/accept`, method: 'POST'});
|
|
||||||
if (response.error) {
|
|
||||||
debugLog(response);
|
|
||||||
throw response;
|
|
||||||
}
|
|
||||||
dbAgents.setAgent(response.data.agent);
|
|
||||||
dbContracts.setContract(response.data.contract);
|
|
||||||
return response.data.contract;
|
|
||||||
}
|
|
||||||
|
|
||||||
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'});
|
||||||
response.forEach(contract => dbContracts.setContract(contract));
|
return response.map(contract => new Contract(contract));
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getContract(contract: Contract): Promise<Contract> {
|
export class Contract {
|
||||||
try {
|
accepted: boolean;
|
||||||
return dbContracts.getContract(contract.id);
|
deadlineToAccept: Date;
|
||||||
} catch {}
|
expiration: Date;
|
||||||
const response = await send<Contract>({endpoint: `/my/contracts/${contract.id}`});
|
factionSymbol: string;
|
||||||
if (response.error) {
|
fulfilled: boolean;
|
||||||
debugLog(response);
|
id: string;
|
||||||
throw response;
|
terms: {
|
||||||
|
deadline: Date;
|
||||||
|
payment: {
|
||||||
|
onAccepted: number;
|
||||||
|
onFulfilled: number;
|
||||||
|
},
|
||||||
|
deliver: Array<{
|
||||||
|
tradeSymbol: string;
|
||||||
|
destinationSymbol: string;
|
||||||
|
unitsRequired: number;
|
||||||
|
unitsFulfilled: number;
|
||||||
|
}>;
|
||||||
|
};
|
||||||
|
type: string;
|
||||||
|
constructor(contract: Contract) {
|
||||||
|
this.accepted = contract.accepted;
|
||||||
|
this.deadlineToAccept = contract.deadlineToAccept;
|
||||||
|
this.expiration = contract.expiration;
|
||||||
|
this.factionSymbol = contract.factionSymbol;
|
||||||
|
this.fulfilled = contract.fulfilled;
|
||||||
|
this.id = contract.id;
|
||||||
|
this.terms = contract.terms;
|
||||||
|
this.type = contract.type;
|
||||||
}
|
}
|
||||||
dbContracts.setContract(response.data);
|
async accept(): Promise<void> {
|
||||||
return response.data;
|
if (this.accepted) return;
|
||||||
}
|
const response = await send<{agent: Agent, contract: Contract, type: ''}>({endpoint: `/my/contracts/${this.id}/accept`, method: 'POST'});
|
||||||
|
if (response.error) {
|
||||||
export async function deliver(contract: Contract, ship: Ship): Promise<Contract> {
|
debugLog(response);
|
||||||
contract = dbContracts.getContract(contract.id);
|
throw response;
|
||||||
if (contract.terms.deliver[0].unitsRequired <= contract.terms.deliver[0].unitsFulfilled) {
|
}
|
||||||
return await fulfill(contract);
|
dbAgents.setAgent(response.data.agent);
|
||||||
}
|
}
|
||||||
const tradeSymbol = contract.terms.deliver[0].tradeSymbol;
|
async deliver(ship: Ship): Promise<void> {
|
||||||
let units = 0;
|
const unitsRemaining = this.terms.deliver[0].unitsRequired - this.terms.deliver[0].unitsFulfilled;
|
||||||
ship.cargo.inventory.forEach(i => {if (i.symbol === tradeSymbol) units = i.units; });
|
if (unitsRemaining <= 0) return await this.fulfill();
|
||||||
await ship.dock(); // we need to be docked to deliver
|
const tradeSymbol = this.terms.deliver[0].tradeSymbol;
|
||||||
const response = await send<{contract: Contract, cargo: Cargo}>({ endpoint: `/my/contracts/${contract.id}/deliver`, method: 'POST', payload: {
|
let units = 0;
|
||||||
shipSymbol: ship.symbol,
|
ship.cargo.inventory.forEach(i => {if (i.symbol === tradeSymbol) units = i.units; });
|
||||||
tradeSymbol: tradeSymbol,
|
if (units === 0) return;
|
||||||
units: units,
|
if (units > unitsRemaining) units = unitsRemaining;
|
||||||
}});
|
await ship.dock(); // we need to be docked to deliver
|
||||||
if (response.error) {
|
const response = await send<{contract: Contract, cargo: Cargo}>({ endpoint: `/my/contracts/${this.id}/deliver`, method: 'POST', payload: {
|
||||||
switch(response.error.code) {
|
shipSymbol: ship.symbol,
|
||||||
case 4503: // contract has expired
|
tradeSymbol: tradeSymbol,
|
||||||
// TODO sell cargo? the next trading loop should take care of it by itself
|
units: units,
|
||||||
contract.fulfilled = true;
|
}});
|
||||||
return contract;
|
if (response.error) {
|
||||||
case 4509: // contract delivery terms have been met
|
switch(response.error.code) {
|
||||||
return await fulfill(contract);
|
case 4503: // contract has expired
|
||||||
default: // yet unhandled error
|
// TODO sell cargo? the next trading loop should take care of it by itself
|
||||||
debugLog(response);
|
this.fulfilled = true;
|
||||||
throw response;
|
return;
|
||||||
|
case 4509: // contract delivery terms have been met
|
||||||
|
return await this.fulfill();
|
||||||
|
default: // yet unhandled error
|
||||||
|
debugLog(response);
|
||||||
|
throw response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ship.cargo = response.data.cargo;
|
||||||
|
if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) {
|
||||||
|
return await this.fulfill();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dbContracts.setContract(response.data.contract);
|
async fulfill(): Promise<void> {
|
||||||
ship.cargo = response.data.cargo;
|
if (this.terms.deliver[0].unitsRequired > this.terms.deliver[0].unitsFulfilled) return;
|
||||||
if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) {
|
if (this.fulfilled) return;
|
||||||
return await fulfill(response.data.contract);
|
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);
|
||||||
}
|
}
|
||||||
return response.data.contract;
|
};
|
||||||
}
|
|
||||||
|
|
||||||
export async function fulfill(contract: Contract): Promise<Contract> {
|
|
||||||
contract = dbContracts.getContract(contract.id);
|
|
||||||
if (contract.fulfilled) return contract;
|
|
||||||
const response = await send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${contract.id}/fulfill`, method: 'POST'});
|
|
||||||
if (response.error) {
|
|
||||||
debugLog(response);
|
|
||||||
throw response;
|
|
||||||
}
|
|
||||||
dbAgents.setAgent(response.data.agent);
|
|
||||||
dbContracts.setContract(response.data.contract);
|
|
||||||
return response.data.contract;
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,11 +10,11 @@ import {
|
||||||
ShipIsStillOnCooldownError,
|
ShipIsStillOnCooldownError,
|
||||||
ShipRequiresMoreFuelForNavigationError,
|
ShipRequiresMoreFuelForNavigationError,
|
||||||
} from './errors.ts';
|
} from './errors.ts';
|
||||||
|
import { Contract } from './contracts.ts';
|
||||||
import * as libSystems from './systems.ts';
|
import * as libSystems from './systems.ts';
|
||||||
import {
|
import {
|
||||||
Agent,
|
Agent,
|
||||||
Cargo,
|
Cargo,
|
||||||
Contract,
|
|
||||||
Cooldown,
|
Cooldown,
|
||||||
Fuel,
|
Fuel,
|
||||||
Nav,
|
Nav,
|
||||||
|
@ -25,7 +25,6 @@ import {
|
||||||
shortestPath,
|
shortestPath,
|
||||||
} from './utils.ts';
|
} from './utils.ts';
|
||||||
import * as dbAgents from '../database/agents.ts';
|
import * as dbAgents from '../database/agents.ts';
|
||||||
import * as dbContracts from '../database/contracts.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});
|
||||||
|
@ -154,8 +153,7 @@ export class Ship {
|
||||||
throw response;
|
throw response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dbContracts.setContract(response.data.contract);
|
return new Contract(response.data.contract);
|
||||||
return response.data.contract;
|
|
||||||
}
|
}
|
||||||
async orbit(): Promise<void> {
|
async orbit(): Promise<void> {
|
||||||
if (this.nav.status === 'IN_ORBIT') return;
|
if (this.nav.status === 'IN_ORBIT') return;
|
||||||
|
|
|
@ -30,29 +30,6 @@ export type Chart = {
|
||||||
submittedOn: Date;
|
submittedOn: Date;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Contract = {
|
|
||||||
id: string;
|
|
||||||
factionSymbol: string;
|
|
||||||
type: string;
|
|
||||||
terms: {
|
|
||||||
deadline: Date;
|
|
||||||
payment: {
|
|
||||||
onAccepted: number;
|
|
||||||
onFulfilled: number;
|
|
||||||
},
|
|
||||||
deliver: Array<{
|
|
||||||
tradeSymbol: string;
|
|
||||||
destinationSymbol: string;
|
|
||||||
unitsRequired: number;
|
|
||||||
unitsFulfilled: number;
|
|
||||||
}>;
|
|
||||||
};
|
|
||||||
accepted: boolean;
|
|
||||||
fulfilled: boolean;
|
|
||||||
expiration: Date;
|
|
||||||
deadlineToAccept: Date;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type Cooldown = {
|
export type Cooldown = {
|
||||||
shipSymbol: string;
|
shipSymbol: string;
|
||||||
totalSeconds: number;
|
totalSeconds: number;
|
||||||
|
|
Loading…
Add table
Reference in a new issue