1
0
Fork 0

[javascript] Implement the selling loop of the contracting automation

This commit is contained in:
Julien Dessaux 2024-03-18 01:16:01 +01:00
parent 1b1df83ffd
commit fcbd9cf56c
Signed by: adyxax
GPG key ID: F92E51B86E07177E
5 changed files with 110 additions and 16 deletions

View file

@ -1,22 +1,21 @@
import * as selling from './selling.js';
import * as dbShips from '../database/ships.js';
import * as api from '../lib/api.js';
import * as ships from '../lib/ships.js';
import * as libShips from '../lib/ships.js';
import * as utils from '../lib/utils.js';
// example ctx { good: 'SILVER_ORE', symbol: 'ADYXAX-2' }
// example ctx { asteroidSymbol: XXXXX, good: 'SILVER_ORE', symbol: 'ADYXAX-2' }
// returns the number of units of the good the ship holds
export async function mineUntilFullOf(ctx) {
while(true) {
let cargo = await mineUntilFull({symbol: ctx.symbol});
let good = cargo.inventory.filter(i => i.symbol === ctx.good)[0];
const antimatter = cargo.inventory.filter(i => i.symbol === 'ANTIMATTER')[0];
const junk = cargo.inventory.filter(i => i.symbol !== ctx.good && i.symbol !== 'ANTIMATTER');
if ((good?.units ?? 0) + (antimatter?.units ?? 0) >= cargo.capacity * 0.9) { // > 90% full of the valuable goods
return good.units;
} else { // we are full but need to sell junk
for (let i=0; i<junk.length; ++i) {
await ships.sell({symbol: ctx.symbol, good: junk[i].symbol, units: junk[i].units});
}
}
const ship = dbShips.getShip(ctx.symbol);
const cargo = utils.categorizeCargo(await mineUntilFull({symbol: ctx.symbol}), ctx.good);
const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0);
// > 90% full of the valuable goods ?
if (wantedUnits >= cargo.capacity * 0.9) return;
// we are full but need to sell junk
await selling.sell(ship, ctx.good);
await libShips.navigate({symbol: ship.symbol, waypoint: ctx.asteroidSymbol});
}
}
@ -26,7 +25,7 @@ async function mineUntilFull(ctx) {
while(true) {
const ship = dbShips.getShip(ctx.symbol);
if (ship.cargo.units >= ship.cargo.capacity * 0.9) return ship.cargo;
if (await ships.extract({symbol: ctx.symbol}) === null)
if (await libShips.extract({symbol: ctx.symbol}) === null)
ship = await ship(ctx); // refresh the ships status from the server just in case
}
}