summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2024-04-06 10:55:11 +0200
committerJulien Dessaux2024-04-07 23:01:52 +0200
commiteeaa64b5ed54cee8f4ffc85f96178e9799c1a8ac (patch)
tree4c9ebcfd01773bb6c34e66e1325cf40739dae17f
parent[node] Big Ships lib refactoring (diff)
downloadspacetraders-eeaa64b5ed54cee8f4ffc85f96178e9799c1a8ac.tar.gz
spacetraders-eeaa64b5ed54cee8f4ffc85f96178e9799c1a8ac.tar.bz2
spacetraders-eeaa64b5ed54cee8f4ffc85f96178e9799c1a8ac.zip
[node] waypoints usage refactoring
-rw-r--r--nodejs/automation/contracting.ts26
-rw-r--r--nodejs/automation/mining.ts9
-rw-r--r--nodejs/automation/selling.ts15
-rw-r--r--nodejs/lib/ships.ts10
-rw-r--r--nodejs/lib/systems.ts16
5 files changed, 43 insertions, 33 deletions
diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts
index 96f9106..93a9a0e 100644
--- a/nodejs/automation/contracting.ts
+++ b/nodejs/automation/contracting.ts
@@ -39,34 +39,34 @@ async function runOne(contract: Contract, ship: Ship): Promise<void> {
async function runOreProcurement(contract: Contract, ship: Ship): Promise<void> {
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 asteroidSymbol = asteroids[0].symbol;
+ const asteroid = await systems.waypoint(asteroids[0].symbol);
while (!contract.fulfilled) {
const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
// what we do depends on where we are
switch (ship.nav.waypointSymbol) {
- case asteroidSymbol:
- await mining.mineUntilFullFor(contract, ship, asteroidSymbol);
+ case asteroid.symbol:
+ await mining.mineUntilFullFor(contract, ship, asteroid);
await ship.navigate(deliveryPoint);
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
contract = await contracts.deliver(contract, ship);
if (contract.fulfilled) return;
}
- await ship.navigate(asteroidSymbol);
+ await ship.navigate(asteroid);
break;
default:
await selling.sell(ship, wantedCargo);
- await ship.navigate(asteroidSymbol);
+ await ship.navigate(asteroid);
}
}
}
async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void> {
const deliver = contract.terms.deliver[0];
- const deliveryPoint = deliver.destinationSymbol;
+ const deliveryPoint = await libSystems.waypoint(deliver.destinationSymbol);
const wantedCargo = deliver.tradeSymbol;
while (!contract.fulfilled) {
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
let buyingPoint: string = "";
outer: for (let i = 0; i < markets.length; i++) {
- const waypointSymbol = markets[i].data.symbol;
- const market = await libSystems.market(waypointSymbol);
+ const waypoint = await libSystems.waypoint(markets[i].data.symbol);
+ const market = await libSystems.market(waypoint);
for (let j = 0; j < market.exports.length; j++) {
if (market.exports[j].symbol === wantedCargo) {
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 (buyingPoint === "") {
outer: for (let i = 0; i < markets.length; i++) {
- const waypointSymbol = markets[i].data.symbol;
- const market = await libSystems.market(waypointSymbol);
+ const waypoint = await libSystems.waypoint(markets[i].data.symbol);
+ const market = await libSystems.market(waypoint);
for (let j = 0; j < market.exchange.length; j++) {
if (market.exports[j].symbol === wantedCargo) {
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}`;
}
// go buy what we need
- await ship.navigate(buyingPoint);
+ await ship.navigate(await libSystems.waypoint(buyingPoint));
const units = Math.min(
deliver.unitsRequired - deliver.unitsFulfilled,
ship.cargo.capacity - ship.cargo.units,
diff --git a/nodejs/automation/mining.ts b/nodejs/automation/mining.ts
index 07fa19b..cdfcb78 100644
--- a/nodejs/automation/mining.ts
+++ b/nodejs/automation/mining.ts
@@ -1,10 +1,13 @@
import * as selling from './selling.js';
import * as dbContracts from '../database/contracts.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';
-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
while(true) {
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;
// we are full but need to sell junk
await selling.sell(ship, deliver.tradeSymbol);
- await ship.navigate(asteroidSymbol);
+ await ship.navigate(asteroid);
}
}
diff --git a/nodejs/automation/selling.ts b/nodejs/automation/selling.ts
index bc586fe..6cb7d32 100644
--- a/nodejs/automation/selling.ts
+++ b/nodejs/automation/selling.ts
@@ -10,10 +10,11 @@ import {
// example ctx { ship: {XXX}, keep: 'SILVER_ORE' }
export async function sell(ship: Ship, good: string): Promise<Ship> {
outer: while(true) {
+ const waypoint = await libSystems.waypoint(ship.nav.waypointSymbol);
// first lets see what we want to sell
let cargo = categorizeCargo(ship.cargo, good);
// 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?
const goods = whatCanBeTradedAt(cargo.goods, market.imports.concat(market.exchange));
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
for (let i = 0; i < markets.length; i++) {
- const waypointSymbol = markets[i].data.symbol;
- const market = await libSystems.market(waypointSymbol);
+ const waypoint = await libSystems.waypoint(markets[i].data.symbol);
+ const market = await libSystems.market(waypoint);
// 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
if (market === null || whatCanBeTradedAt(cargo.goods, market.imports).length > 0) {
- await ship.navigate(waypointSymbol);
+ await ship.navigate(waypoint);
continue outer;
}
}
// check from the closest one if they exchange what we need to sell
for (let i = 0; i < markets.length; i++) {
- const waypointSymbol = markets[i].data.symbol;
- const market = await libSystems.market(waypointSymbol);
+ const waypoint = await libSystems.waypoint(markets[i].data.symbol);
+ const market = await libSystems.market(waypoint);
// if we can sell there we need to go
if (whatCanBeTradedAt(cargo.goods, market.exchange).length > 0) {
- await ship.navigate(waypointSymbol);
+ await ship.navigate(waypoint);
continue outer;
}
}
diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts
index 7221596..187a87b 100644
--- a/nodejs/lib/ships.ts
+++ b/nodejs/lib/ships.ts
@@ -18,6 +18,7 @@ import {
Fuel,
Nav,
Registration,
+ Waypoint,
} from './types.ts';
import * as dbAgents from '../database/agents.ts';
@@ -96,14 +97,13 @@ export class Ship {
isFull(): boolean {
return this.cargo.units >= this.cargo.capacity * 0.9;
}
- async navigate(waypointSymbol: string): Promise<void> {
- if (this.nav.waypointSymbol === waypointSymbol) return;
- const d =
+ async navigate(waypoint: Waypoint): Promise<void> {
+ if (this.nav.waypointSymbol === waypoint.symbol) return;
// 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.orbit();
// 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) {
switch(response.error.code) {
case 4203: // not enough fuel
@@ -117,7 +117,7 @@ export class Ship {
case 4214:
const sicite = response.error.data as ShipIsCurrentlyInTransitError;
await sleep(sicite.secondsToArrival * 1000);
- return await this.navigate(waypointSymbol);
+ return await this.navigate(waypoint);
default: // yet unhandled error
debugLog(response);
throw response;
diff --git a/nodejs/lib/systems.ts b/nodejs/lib/systems.ts
index 97aa6e3..d4b3be5 100644
--- a/nodejs/lib/systems.ts
+++ b/nodejs/lib/systems.ts
@@ -8,15 +8,15 @@ import * as dbSystems from '../database/systems.ts';
import {
Market,
System,
- Waypoint
+ Waypoint,
} from './types.ts'
import { systemFromWaypoint } from './utils.ts';
-export async function market(waypointSymbol: string): Promise<Market> {
- const data = dbMarkets.getMarketAtWaypoint(waypointSymbol);
+export async function market(waypoint: Waypoint): Promise<Market> {
+ const data = dbMarkets.getMarketAtWaypoint(waypoint.symbol);
if (data) { return data; }
- const systemSymbol = systemFromWaypoint(waypointSymbol);
- let response = await send<Market>({endpoint: `/systems/${systemSymbol}/waypoints/${waypointSymbol}/market`});
+ const systemSymbol = systemFromWaypoint(waypoint.symbol);
+ let response = await send<Market>({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint.symbol}/market`});
if (response.error) {
debugLog(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);
}
+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>> {
const s = await system(systemSymbol);
const updated = dbSystems.getSystemUpdated(systemSymbol);