1
0
Fork 0

[node] waypoints usage refactoring

This commit is contained in:
Julien Dessaux 2024-04-06 10:55:11 +02:00
parent 234770b611
commit eeaa64b5ed
Signed by: adyxax
GPG key ID: F92E51B86E07177E
5 changed files with 43 additions and 33 deletions

View file

@ -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,

View file

@ -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);
}
}

View file

@ -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;
}
}