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,4 +1,5 @@
import * as mining from './mining.js';
import * as selling from './selling.js';
import * as dbContracts from '../database/contracts.js';
import * as dbShips from '../database/ships.js';
import * as api from '../lib/api.js';
@ -43,7 +44,8 @@ async function runProcurement(contract, ships) {
// Then it depends on where we are
switch (ship.nav.waypointSymbol) {
case asteroidSymbol:
let response = await mining.mineUntilFullOf({
await mining.mineUntilFullOf({
asteroidSymbol: asteroidSymbol,
good: wantedCargo,
symbol: ship.symbol
});
@ -59,6 +61,8 @@ async function runProcurement(contract, ships) {
await libShips.navigate({symbol: ship.symbol, waypoint: asteroidSymbol});
break;
default:
// we were either selling or started contracting
await selling.sell(ship, wantedCargo);
await libShips.navigate({symbol: ship.symbol, waypoint: asteroidSymbol});
}
}

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

View file

@ -0,0 +1,76 @@
import * as dbMarkets from '../database/markets.js';
import * as dbShips from '../database/ships.js';
import * as api from '../lib/api.js';
import * as libShips from '../lib/ships.js';
import * as libSystems from '../lib/systems.js';
import * as utils from '../lib/utils.js';
// example ctx { ship: {XXX}, keep: 'SILVER_ORE' }
export async function sell(ship, keep) {
outer: while(true) {
// first lets see what we want to sell
let cargo = utils.categorizeCargo(ship.cargo, keep);
// get the marketdata from our location
const market = await libSystems.market(ship.nav.waypointSymbol);
// can we sell anything here?
const goods = whatCanBeTradedAt(cargo.goods, market.imports.concat(market.exchange));
for (let i = 0; i < goods.length; i++) {
const symbol = goods[i].symbol;
await libShips.sell({
good: symbol,
symbol: ship.symbol,
units: cargo.goods[symbol],
});
delete cargo.goods[symbol];
};
// are we done selling everything we can?
ship = dbShips.getShip(ship.symbol);
cargo = utils.categorizeCargo(ship.cargo, keep);
if (Object.keys(cargo.goods).length === 0) {
return;
}
// we need to move somewhere else to sell our remaining goods
// first we look into markets in our system
const rawMarkets = await libSystems.trait({symbol: ship.nav.systemSymbol, trait: '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
for (let i = 0; i < markets.length; i++) {
const waypointSymbol = markets[i].data.symbol;
const market = await libSystems.market(waypointSymbol);
// 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 libShips.navigate({symbol: ship.symbol, waypoint: waypointSymbol});
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);
// if we can sell there we need to go
if (whatCanBeTradedAt(cargo.goods, market.exchange).length > 0) {
await libShips.navigate({symbol: ship.symbol, waypoint: waypointSymbol});
continue outer;
}
}
throw new Error(`Ship {ship.symbol} has found no importing or exchanging market for its cargo in the system`);
}
}
function whatCanBeTradedAt(cargo, goods) {
if (goods === undefined) return [];
return goods.filter(g => cargo[g.symbol] !== undefined );
}

View file

@ -139,7 +139,7 @@ export async function refuel(ctx) {
}
export async function sell(ctx) {
// TODO check if our current waypoint has a marketplace (and sells fuel)?
// TODO check if our current waypoint has a marketplace?
await dock(ctx);
const ship = dbShips.getShip(ctx.symbol);
const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/sell`, method: 'POST', payload: { symbol: ctx.good, units: ctx.units }});

View file

@ -1,3 +1,18 @@
// cargo is a ship.cargo object, want is an optional symbol
export function categorizeCargo(cargo, want) {
const wanted = cargo.inventory.filter(i => i.symbol === want || i.symbol === 'ANTIMATTER');
const goods = cargo.inventory.filter(i => i.symbol !== want && i.symbol !== 'ANTIMATTER');
const wobj = wanted.reduce(function(acc, e) {
acc[e.symbol] = e.units;
return acc;
}, {});
const gobj = goods.reduce(function(acc, e) {
acc[e.symbol] = e.units;
return acc;
}, {});
return {wanted: wobj, goods: gobj};
}
export function systemFromWaypoint(waypoint) {
return waypoint.split('-').slice(0,2).join('-');
}