[node] multiple contracting fixes and some more refactoring
This commit is contained in:
parent
eeaa64b5ed
commit
3e80bc8a4d
6 changed files with 65 additions and 46 deletions
|
@ -1,29 +1,35 @@
|
||||||
import { Contract } from '../lib/types.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 * as dbContracts from '../database/contracts.ts';
|
||||||
import * as contracts 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 * as utils from '../lib/utils.ts';
|
import {
|
||||||
|
sortByDistanceFrom,
|
||||||
|
} from '../lib/utils.ts';
|
||||||
|
|
||||||
export async function run(ship: Ship): Promise<void> {
|
export async function run(ship: Ship): Promise<void> {
|
||||||
while(true) { // we use the fact that there can only be at most one active contract at a time
|
const contracts = await libContracts.getContracts();
|
||||||
const contracts = dbContracts.getContracts().filter(c => !c.fulfilled);
|
const active = contracts.filter(function(c) {
|
||||||
let contract: Contract;
|
if (c.fulfilled) return false;
|
||||||
if (contracts.length === 0) {
|
const deadline = new Date(c.terms.deadline).getTime();
|
||||||
contract = await ship.negotiate();
|
const now = new Date().getTime();
|
||||||
} else {
|
return deadline > now;
|
||||||
contract = contracts[0];
|
});
|
||||||
}
|
for (const contract of active) {
|
||||||
await runOne(contract, ship);
|
await runOne(contract, ship);
|
||||||
await ship.negotiate();
|
}
|
||||||
|
while(true) {
|
||||||
|
await runOne(await ship.negotiate(), ship);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runOne(contract: Contract, ship: Ship): Promise<void> {
|
async function runOne(contract: Contract, ship: Ship): Promise<void> {
|
||||||
await contracts.accept(contract);
|
debugLog(contract);
|
||||||
|
await libContracts.accept(contract);
|
||||||
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$/)) {
|
||||||
|
@ -52,7 +58,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 contracts.deliver(contract, ship);
|
contract = await libContracts.deliver(contract, ship);
|
||||||
if (contract.fulfilled) return;
|
if (contract.fulfilled) return;
|
||||||
}
|
}
|
||||||
await ship.navigate(asteroid);
|
await ship.navigate(asteroid);
|
||||||
|
@ -73,20 +79,7 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void
|
||||||
// make sure we are not carrying useless stuff
|
// make sure we are not carrying useless stuff
|
||||||
await selling.sell(ship, wantedCargo);
|
await selling.sell(ship, wantedCargo);
|
||||||
// go buy what we need
|
// go buy what we need
|
||||||
const rawMarkets = await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE');
|
const markets = sortByDistanceFrom(ship.nav.route.destination, await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE'));
|
||||||
// sorted by distance from where we are
|
|
||||||
const markets = rawMarkets.map(function (m) { return {
|
|
||||||
data: m,
|
|
||||||
distance: (m.x - ship.nav.route.destination.x) ** 2 + (m.y - ship.nav.route.destination.y) ** 2,
|
|
||||||
}});
|
|
||||||
markets.sort(function(a, b) {
|
|
||||||
if (a.distance < b.distance) {
|
|
||||||
return -1;
|
|
||||||
} else if (a.distance > b.distance) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
});
|
|
||||||
// check from the closest one that exports what we need
|
// check from the closest one that exports what we need
|
||||||
let buyingPoint: string = "";
|
let buyingPoint: string = "";
|
||||||
outer: for (let i = 0; i < markets.length; i++) {
|
outer: for (let i = 0; i < markets.length; i++) {
|
||||||
|
@ -105,7 +98,7 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void
|
||||||
const waypoint = await libSystems.waypoint(markets[i].data.symbol);
|
const waypoint = await libSystems.waypoint(markets[i].data.symbol);
|
||||||
const market = await libSystems.market(waypoint);
|
const market = await libSystems.market(waypoint);
|
||||||
for (let j = 0; j < market.exchange.length; j++) {
|
for (let j = 0; j < market.exchange.length; j++) {
|
||||||
if (market.exports[j].symbol === wantedCargo) {
|
if (market.exchange[j].symbol === wantedCargo) {
|
||||||
buyingPoint = market.symbol;
|
buyingPoint = market.symbol;
|
||||||
break outer;
|
break outer;
|
||||||
}
|
}
|
||||||
|
@ -124,7 +117,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 contracts.deliver(contract, ship);
|
contract = await libContracts.deliver(contract, ship);
|
||||||
if (contract.fulfilled) return;
|
if (contract.fulfilled) return;
|
||||||
}
|
}
|
||||||
console.log("runTradeProcurement not implemented");
|
console.log("runTradeProcurement not implemented");
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
import * as dbMarkets from '../database/markets.ts';
|
import * as dbMarkets from '../database/markets.ts';
|
||||||
import * as libSystems from '../lib/systems.ts';
|
import * as libSystems from '../lib/systems.ts';
|
||||||
import { categorizeCargo } from '../lib/utils.ts';
|
import {
|
||||||
|
categorizeCargo,
|
||||||
|
sortByDistanceFrom,
|
||||||
|
} from '../lib/utils.ts';
|
||||||
import { Ship } from '../lib/ships.ts';
|
import { Ship } from '../lib/ships.ts';
|
||||||
import {
|
import {
|
||||||
CargoManifest,
|
CargoManifest,
|
||||||
|
@ -26,21 +29,7 @@ export async function sell(ship: Ship, good: string): Promise<Ship> {
|
||||||
return ship;
|
return ship;
|
||||||
}
|
}
|
||||||
// we need to move somewhere else to sell our remaining goods
|
// we need to move somewhere else to sell our remaining goods
|
||||||
// first we look into markets in our system
|
const markets = sortByDistanceFrom(ship.nav.route.destination, await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE'));
|
||||||
const rawMarkets = await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE');
|
|
||||||
// sorted by distance from where we are
|
|
||||||
const markets = rawMarkets.map(function (m) { return {
|
|
||||||
data: m,
|
|
||||||
distance: (m.x - ship.nav.route.destination.x) ** 2 + (m.y - ship.nav.route.destination.y) ** 2,
|
|
||||||
}});
|
|
||||||
markets.sort(function(a, b) {
|
|
||||||
if (a.distance < b.distance) {
|
|
||||||
return -1;
|
|
||||||
} else if (a.distance > b.distance) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
});
|
|
||||||
// check from the closest one if they import what we need to sell
|
// check from the closest one if they import what we need to sell
|
||||||
for (let i = 0; i < markets.length; i++) {
|
for (let i = 0; i < markets.length; i++) {
|
||||||
const waypoint = await libSystems.waypoint(markets[i].data.symbol);
|
const waypoint = await libSystems.waypoint(markets[i].data.symbol);
|
||||||
|
|
|
@ -61,6 +61,10 @@ export async function deliver(contract: Contract, ship: Ship): Promise<Contract>
|
||||||
}});
|
}});
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
switch(response.error.code) {
|
switch(response.error.code) {
|
||||||
|
case 4503: // contract has expired
|
||||||
|
// TODO sell cargo? the next trading loop should take care of it by itself
|
||||||
|
contract.fulfilled = true;
|
||||||
|
return contract;
|
||||||
case 4509: // contract delivery terms have been met
|
case 4509: // contract delivery terms have been met
|
||||||
return await fulfill(contract);
|
return await fulfill(contract);
|
||||||
default: // yet unhandled error
|
default: // yet unhandled error
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
import { Cooldown } from './types.ts';
|
import { Cooldown } from './types.ts';
|
||||||
|
|
||||||
|
export type ContractDeadlineExpired = {
|
||||||
|
contractId: string;
|
||||||
|
deadline: Date;
|
||||||
|
};
|
||||||
|
|
||||||
export type MarketTradeVolumeError = {
|
export type MarketTradeVolumeError = {
|
||||||
waypointSymbol: string;
|
waypointSymbol: string;
|
||||||
tradeSymbol: string;
|
tradeSymbol: string;
|
||||||
|
|
|
@ -130,6 +130,7 @@ export class Ship {
|
||||||
this.nav.status = 'IN_ORBIT'; // we arrive in orbit
|
this.nav.status = 'IN_ORBIT'; // we arrive in orbit
|
||||||
}
|
}
|
||||||
async negotiate(): Promise<Contract> {
|
async negotiate(): Promise<Contract> {
|
||||||
|
await this.dock();
|
||||||
const response = await send<{contract: Contract}>({endpoint: `/my/ships/${this.symbol}/negotiate/contract`, method: 'POST'});
|
const response = await send<{contract: Contract}>({endpoint: `/my/ships/${this.symbol}/negotiate/contract`, method: 'POST'});
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
switch(response.error.code) {
|
switch(response.error.code) {
|
||||||
|
@ -142,6 +143,7 @@ export class Ship {
|
||||||
throw response;
|
throw response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
dbContracts.setContract(response.data.contract);
|
||||||
return response.data.contract;
|
return response.data.contract;
|
||||||
}
|
}
|
||||||
async orbit(): Promise<void> {
|
async orbit(): Promise<void> {
|
||||||
|
@ -180,6 +182,7 @@ export class Ship {
|
||||||
dbAgents.setAgent(response.data.agent);
|
dbAgents.setAgent(response.data.agent);
|
||||||
}
|
}
|
||||||
async refuel(): Promise<void> {
|
async refuel(): Promise<void> {
|
||||||
|
if (this.fuel.current === this.fuel.capacity) return;
|
||||||
// TODO check if our current waypoint has a marketplace (and sells fuel)?
|
// TODO check if our current waypoint has a marketplace (and sells fuel)?
|
||||||
await this.dock();
|
await this.dock();
|
||||||
const response = await send<{agent: Agent, fuel: Fuel}>({endpoint: `/my/ships/${this.symbol}/refuel`, method: 'POST'}); // TODO transaction field
|
const response = await send<{agent: Agent, fuel: Fuel}>({endpoint: `/my/ships/${this.symbol}/refuel`, method: 'POST'}); // TODO transaction field
|
||||||
|
|
|
@ -5,6 +5,11 @@ export type CategorizedCargo = {
|
||||||
goods: CargoManifest;
|
goods: CargoManifest;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type Point = {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
};
|
||||||
|
|
||||||
// cargo is a ship.cargo object, want is an optional symbol
|
// cargo is a ship.cargo object, want is an optional symbol
|
||||||
export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo {
|
export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo {
|
||||||
const wanted = cargo.inventory.filter(i => i.symbol === want || i.symbol === 'ANTIMATTER');
|
const wanted = cargo.inventory.filter(i => i.symbol === want || i.symbol === 'ANTIMATTER');
|
||||||
|
@ -20,6 +25,26 @@ export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo {
|
||||||
return {wanted: wobj, goods: gobj};
|
return {wanted: wobj, goods: gobj};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function distance(a: Point, b: Point) {
|
||||||
|
return Math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sortByDistanceFrom<T extends Point>(a: Point, points: Array<T>): Array<{data: T, distance: number}>{
|
||||||
|
let result = points.map(function (m) { return {
|
||||||
|
data: m,
|
||||||
|
distance: distance(a, m),
|
||||||
|
}});
|
||||||
|
result.sort(function(a, b) {
|
||||||
|
if (a.distance < b.distance) {
|
||||||
|
return -1;
|
||||||
|
} else if (a.distance > b.distance) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
export function systemFromWaypoint(waypoint: string): string {
|
export function systemFromWaypoint(waypoint: string): string {
|
||||||
return waypoint.split('-').slice(0,2).join('-');
|
return waypoint.split('-').slice(0,2).join('-');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue