[node] waypoints usage refactoring
This commit is contained in:
parent
234770b611
commit
eeaa64b5ed
5 changed files with 43 additions and 33 deletions
|
@ -39,34 +39,34 @@ async function runOne(contract: Contract, ship: Ship): Promise<void> {
|
||||||
|
|
||||||
async function runOreProcurement(contract: Contract, ship: Ship): Promise<void> {
|
async function runOreProcurement(contract: Contract, ship: Ship): Promise<void> {
|
||||||
const wantedCargo = contract.terms.deliver[0].tradeSymbol;
|
const wantedCargo = contract.terms.deliver[0].tradeSymbol;
|
||||||
const deliveryPoint = contract.terms.deliver[0].destinationSymbol;
|
const deliveryPoint = await libSystems.waypoint(contract.terms.deliver[0].destinationSymbol);
|
||||||
const asteroids = await systems.type(ship.nav.systemSymbol, 'ENGINEERED_ASTEROID');
|
const asteroids = await systems.type(ship.nav.systemSymbol, 'ENGINEERED_ASTEROID');
|
||||||
const asteroidSymbol = asteroids[0].symbol;
|
const asteroid = await systems.waypoint(asteroids[0].symbol);
|
||||||
while (!contract.fulfilled) {
|
while (!contract.fulfilled) {
|
||||||
const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
|
const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
|
||||||
// what we do depends on where we are
|
// what we do depends on where we are
|
||||||
switch (ship.nav.waypointSymbol) {
|
switch (ship.nav.waypointSymbol) {
|
||||||
case asteroidSymbol:
|
case asteroid.symbol:
|
||||||
await mining.mineUntilFullFor(contract, ship, asteroidSymbol);
|
await mining.mineUntilFullFor(contract, ship, asteroid);
|
||||||
await ship.navigate(deliveryPoint);
|
await ship.navigate(deliveryPoint);
|
||||||
break;
|
break;
|
||||||
case deliveryPoint:
|
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 contracts.deliver(contract, ship);
|
||||||
if (contract.fulfilled) return;
|
if (contract.fulfilled) return;
|
||||||
}
|
}
|
||||||
await ship.navigate(asteroidSymbol);
|
await ship.navigate(asteroid);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
await selling.sell(ship, wantedCargo);
|
await selling.sell(ship, wantedCargo);
|
||||||
await ship.navigate(asteroidSymbol);
|
await ship.navigate(asteroid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void> {
|
async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void> {
|
||||||
const deliver = contract.terms.deliver[0];
|
const deliver = contract.terms.deliver[0];
|
||||||
const deliveryPoint = deliver.destinationSymbol;
|
const deliveryPoint = await libSystems.waypoint(deliver.destinationSymbol);
|
||||||
const wantedCargo = deliver.tradeSymbol;
|
const wantedCargo = deliver.tradeSymbol;
|
||||||
while (!contract.fulfilled) {
|
while (!contract.fulfilled) {
|
||||||
const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
|
const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
|
||||||
|
@ -90,8 +90,8 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void
|
||||||
// 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++) {
|
||||||
const waypointSymbol = markets[i].data.symbol;
|
const waypoint = await libSystems.waypoint(markets[i].data.symbol);
|
||||||
const market = await libSystems.market(waypointSymbol);
|
const market = await libSystems.market(waypoint);
|
||||||
for (let j = 0; j < market.exports.length; j++) {
|
for (let j = 0; j < market.exports.length; j++) {
|
||||||
if (market.exports[j].symbol === wantedCargo) {
|
if (market.exports[j].symbol === wantedCargo) {
|
||||||
buyingPoint = market.symbol;
|
buyingPoint = market.symbol;
|
||||||
|
@ -102,8 +102,8 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void
|
||||||
// if we did not find an exporting market we look for an exchange
|
// if we did not find an exporting market we look for an exchange
|
||||||
if (buyingPoint === "") {
|
if (buyingPoint === "") {
|
||||||
outer: for (let i = 0; i < markets.length; i++) {
|
outer: for (let i = 0; i < markets.length; i++) {
|
||||||
const waypointSymbol = markets[i].data.symbol;
|
const waypoint = await libSystems.waypoint(markets[i].data.symbol);
|
||||||
const market = await libSystems.market(waypointSymbol);
|
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.exports[j].symbol === wantedCargo) {
|
||||||
buyingPoint = market.symbol;
|
buyingPoint = market.symbol;
|
||||||
|
@ -116,7 +116,7 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void
|
||||||
throw `runTradeProcurement failed, no market exports or exchanges ${wantedCargo}`;
|
throw `runTradeProcurement failed, no market exports or exchanges ${wantedCargo}`;
|
||||||
}
|
}
|
||||||
// go buy what we need
|
// go buy what we need
|
||||||
await ship.navigate(buyingPoint);
|
await ship.navigate(await libSystems.waypoint(buyingPoint));
|
||||||
const units = Math.min(
|
const units = Math.min(
|
||||||
deliver.unitsRequired - deliver.unitsFulfilled,
|
deliver.unitsRequired - deliver.unitsFulfilled,
|
||||||
ship.cargo.capacity - ship.cargo.units,
|
ship.cargo.capacity - ship.cargo.units,
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
import * as selling from './selling.js';
|
import * as selling from './selling.js';
|
||||||
import * as dbContracts from '../database/contracts.js';
|
import * as dbContracts from '../database/contracts.js';
|
||||||
import { Ship } from '../lib/ships.js';
|
import { Ship } from '../lib/ships.js';
|
||||||
import { Contract } from '../lib/types.ts';
|
import {
|
||||||
|
Contract,
|
||||||
|
Waypoint,
|
||||||
|
} from '../lib/types.ts';
|
||||||
import { categorizeCargo } from '../lib/utils.ts';
|
import { categorizeCargo } from '../lib/utils.ts';
|
||||||
|
|
||||||
export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroidSymbol: string): Promise<void> {
|
export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroid: Waypoint): Promise<void> {
|
||||||
// TODO find a good asteroid
|
// TODO find a good asteroid
|
||||||
while(true) {
|
while(true) {
|
||||||
await mineUntilFull(ship);
|
await mineUntilFull(ship);
|
||||||
|
@ -17,7 +20,7 @@ export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroidS
|
||||||
|| cargo.wanted[deliver.tradeSymbol] >= deliver.unitsRequired - deliver.unitsFulfilled) return;
|
|| cargo.wanted[deliver.tradeSymbol] >= deliver.unitsRequired - deliver.unitsFulfilled) return;
|
||||||
// we are full but need to sell junk
|
// we are full but need to sell junk
|
||||||
await selling.sell(ship, deliver.tradeSymbol);
|
await selling.sell(ship, deliver.tradeSymbol);
|
||||||
await ship.navigate(asteroidSymbol);
|
await ship.navigate(asteroid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,10 +10,11 @@ import {
|
||||||
// example ctx { ship: {XXX}, keep: 'SILVER_ORE' }
|
// example ctx { ship: {XXX}, keep: 'SILVER_ORE' }
|
||||||
export async function sell(ship: Ship, good: string): Promise<Ship> {
|
export async function sell(ship: Ship, good: string): Promise<Ship> {
|
||||||
outer: while(true) {
|
outer: while(true) {
|
||||||
|
const waypoint = await libSystems.waypoint(ship.nav.waypointSymbol);
|
||||||
// first lets see what we want to sell
|
// first lets see what we want to sell
|
||||||
let cargo = categorizeCargo(ship.cargo, good);
|
let cargo = categorizeCargo(ship.cargo, good);
|
||||||
// get the marketdata from our location
|
// get the marketdata from our location
|
||||||
const market = await libSystems.market(ship.nav.waypointSymbol);
|
const market = await libSystems.market(waypoint);
|
||||||
// can we sell anything here?
|
// can we sell anything here?
|
||||||
const goods = whatCanBeTradedAt(cargo.goods, market.imports.concat(market.exchange));
|
const goods = whatCanBeTradedAt(cargo.goods, market.imports.concat(market.exchange));
|
||||||
for (let i = 0; i < goods.length; i++) {
|
for (let i = 0; i < goods.length; i++) {
|
||||||
|
@ -42,22 +43,22 @@ export async function sell(ship: Ship, good: string): Promise<Ship> {
|
||||||
});
|
});
|
||||||
// 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 waypointSymbol = markets[i].data.symbol;
|
const waypoint = await libSystems.waypoint(markets[i].data.symbol);
|
||||||
const market = await libSystems.market(waypointSymbol);
|
const market = await libSystems.market(waypoint);
|
||||||
// if we have no data on the market we need to go there and see
|
// if we have no data on the market we need to go there and see
|
||||||
// and if we have data and can sell there we need to go too
|
// and if we have data and can sell there we need to go too
|
||||||
if (market === null || whatCanBeTradedAt(cargo.goods, market.imports).length > 0) {
|
if (market === null || whatCanBeTradedAt(cargo.goods, market.imports).length > 0) {
|
||||||
await ship.navigate(waypointSymbol);
|
await ship.navigate(waypoint);
|
||||||
continue outer;
|
continue outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// check from the closest one if they exchange what we need to sell
|
// check from the closest one if they exchange what we need to sell
|
||||||
for (let i = 0; i < markets.length; i++) {
|
for (let i = 0; i < markets.length; i++) {
|
||||||
const waypointSymbol = markets[i].data.symbol;
|
const waypoint = await libSystems.waypoint(markets[i].data.symbol);
|
||||||
const market = await libSystems.market(waypointSymbol);
|
const market = await libSystems.market(waypoint);
|
||||||
// if we can sell there we need to go
|
// if we can sell there we need to go
|
||||||
if (whatCanBeTradedAt(cargo.goods, market.exchange).length > 0) {
|
if (whatCanBeTradedAt(cargo.goods, market.exchange).length > 0) {
|
||||||
await ship.navigate(waypointSymbol);
|
await ship.navigate(waypoint);
|
||||||
continue outer;
|
continue outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import {
|
||||||
Fuel,
|
Fuel,
|
||||||
Nav,
|
Nav,
|
||||||
Registration,
|
Registration,
|
||||||
|
Waypoint,
|
||||||
} from './types.ts';
|
} from './types.ts';
|
||||||
import * as dbAgents from '../database/agents.ts';
|
import * as dbAgents from '../database/agents.ts';
|
||||||
|
|
||||||
|
@ -96,14 +97,13 @@ export class Ship {
|
||||||
isFull(): boolean {
|
isFull(): boolean {
|
||||||
return this.cargo.units >= this.cargo.capacity * 0.9;
|
return this.cargo.units >= this.cargo.capacity * 0.9;
|
||||||
}
|
}
|
||||||
async navigate(waypointSymbol: string): Promise<void> {
|
async navigate(waypoint: Waypoint): Promise<void> {
|
||||||
if (this.nav.waypointSymbol === waypointSymbol) return;
|
if (this.nav.waypointSymbol === waypoint.symbol) return;
|
||||||
const d =
|
|
||||||
// TODO compute fuel consumption and refuel if we do not have enough OR if the destination does not sell fuel?
|
// TODO compute fuel consumption and refuel if we do not have enough OR if the destination does not sell fuel?
|
||||||
await this.refuel();
|
await this.refuel();
|
||||||
await this.orbit();
|
await this.orbit();
|
||||||
// TODO if we do not have enough fuel, make a stop to refuel along the way or drift to the destination
|
// TODO if we do not have enough fuel, make a stop to refuel along the way or drift to the destination
|
||||||
const response = await send<{fuel: Fuel, nav: Nav}>({endpoint: `/my/ships/${this.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: waypointSymbol }}); // TODO events field
|
const response = await send<{fuel: Fuel, nav: Nav}>({endpoint: `/my/ships/${this.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: waypoint.symbol }}); // TODO events field
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
switch(response.error.code) {
|
switch(response.error.code) {
|
||||||
case 4203: // not enough fuel
|
case 4203: // not enough fuel
|
||||||
|
@ -117,7 +117,7 @@ export class Ship {
|
||||||
case 4214:
|
case 4214:
|
||||||
const sicite = response.error.data as ShipIsCurrentlyInTransitError;
|
const sicite = response.error.data as ShipIsCurrentlyInTransitError;
|
||||||
await sleep(sicite.secondsToArrival * 1000);
|
await sleep(sicite.secondsToArrival * 1000);
|
||||||
return await this.navigate(waypointSymbol);
|
return await this.navigate(waypoint);
|
||||||
default: // yet unhandled error
|
default: // yet unhandled error
|
||||||
debugLog(response);
|
debugLog(response);
|
||||||
throw response;
|
throw response;
|
||||||
|
|
|
@ -8,15 +8,15 @@ import * as dbSystems from '../database/systems.ts';
|
||||||
import {
|
import {
|
||||||
Market,
|
Market,
|
||||||
System,
|
System,
|
||||||
Waypoint
|
Waypoint,
|
||||||
} from './types.ts'
|
} from './types.ts'
|
||||||
import { systemFromWaypoint } from './utils.ts';
|
import { systemFromWaypoint } from './utils.ts';
|
||||||
|
|
||||||
export async function market(waypointSymbol: string): Promise<Market> {
|
export async function market(waypoint: Waypoint): Promise<Market> {
|
||||||
const data = dbMarkets.getMarketAtWaypoint(waypointSymbol);
|
const data = dbMarkets.getMarketAtWaypoint(waypoint.symbol);
|
||||||
if (data) { return data; }
|
if (data) { return data; }
|
||||||
const systemSymbol = systemFromWaypoint(waypointSymbol);
|
const systemSymbol = systemFromWaypoint(waypoint.symbol);
|
||||||
let response = await send<Market>({endpoint: `/systems/${systemSymbol}/waypoints/${waypointSymbol}/market`});
|
let response = await send<Market>({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint.symbol}/market`});
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
debugLog(response);
|
debugLog(response);
|
||||||
throw response;
|
throw response;
|
||||||
|
@ -55,6 +55,12 @@ export async function type(system: string, typeSymbol: string): Promise<Array<Wa
|
||||||
return ws.filter(s => s.type === typeSymbol);
|
return ws.filter(s => s.type === typeSymbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function waypoint(waypointSymbol: string): Promise<Waypoint> {
|
||||||
|
const systemSymbol = systemFromWaypoint(waypointSymbol);
|
||||||
|
const w = await waypoints(systemSymbol);
|
||||||
|
return w.filter(w => w.symbol === waypointSymbol)[0];
|
||||||
|
}
|
||||||
|
|
||||||
export async function waypoints(systemSymbol: string): Promise<Array<Waypoint>> {
|
export async function waypoints(systemSymbol: string): Promise<Array<Waypoint>> {
|
||||||
const s = await system(systemSymbol);
|
const s = await system(systemSymbol);
|
||||||
const updated = dbSystems.getSystemUpdated(systemSymbol);
|
const updated = dbSystems.getSystemUpdated(systemSymbol);
|
||||||
|
|
Loading…
Add table
Reference in a new issue