[node] implemented basic procurement trading loop
This commit is contained in:
parent
8819cf9c67
commit
a34e4fbed4
5 changed files with 132 additions and 46 deletions
|
@ -7,44 +7,49 @@ import * as dbShips from '../database/ships.ts';
|
||||||
import * as api from '../lib/api.ts';
|
import * as api from '../lib/api.ts';
|
||||||
import * as contracts from '../lib/contracts.ts';
|
import * as contracts from '../lib/contracts.ts';
|
||||||
import * as libShips from '../lib/ships.ts';
|
import * as libShips from '../lib/ships.ts';
|
||||||
|
import * as libSystems from '../lib/systems.ts';
|
||||||
import * as systems from '../lib/systems.ts';
|
import * as systems from '../lib/systems.ts';
|
||||||
import * as utils from '../lib/utils.ts';
|
import * as utils from '../lib/utils.ts';
|
||||||
|
|
||||||
export async function init() {
|
export async function init(): Promise<void> {
|
||||||
const cs = dbContracts.getContracts().filter(c => !c.fulfilled);
|
const ship = dbShips.getShips()[0]; // This should always be the command ship
|
||||||
cs.forEach(contract => run(contract));
|
while(true) { // we use the fact that there can only be at most one active contract at a time
|
||||||
|
const contracts = dbContracts.getContracts().filter(c => !c.fulfilled);
|
||||||
|
let contract: Contract;
|
||||||
|
if (contracts.length === 0) {
|
||||||
|
contract = await libShips.negotiate(ship);
|
||||||
|
} else {
|
||||||
|
contract = contracts[0];
|
||||||
|
}
|
||||||
|
await run(contract, ship);
|
||||||
|
await libShips.negotiate(ship);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function run(contract: Contract) {
|
async function run(contract: Contract, ship: Ship): Promise<void> {
|
||||||
await contracts.accept(contract);
|
await contracts.accept(contract);
|
||||||
const contractSystem = utils.systemFromWaypoint(contract.terms.deliver[0].destinationSymbol);
|
|
||||||
let ships = dbShips.getShipsAt(contractSystem);
|
|
||||||
ships = ships.filter(ship => ship.registration.role !== 'SATELLITE'); // filter out probes
|
|
||||||
|
|
||||||
switch(contract.type) {
|
switch(contract.type) {
|
||||||
case 'PROCUREMENT':
|
case 'PROCUREMENT':
|
||||||
await runProcurement(contract, ships);
|
if (contract.terms.deliver[0].tradeSymbol.match(/_ORE$/)) {
|
||||||
|
await runOreProcurement(contract, ship);
|
||||||
|
} else {
|
||||||
|
await runTradeProcurement(contract, ship);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw `Handling of contract type ${contract.type} is not implemented yet`;
|
throw `Handling of contract type ${contract.type} is not implemented yet`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runProcurement(contract: Contract, ships: Array<Ship>) {
|
async function runOreProcurement(contract: Contract, ship: Ship): Promise<void> {
|
||||||
// TODO check if contract is fulfilled!
|
|
||||||
const wantedCargo = contract.terms.deliver[0].tradeSymbol;
|
const wantedCargo = contract.terms.deliver[0].tradeSymbol;
|
||||||
const deliveryPoint = contract.terms.deliver[0].destinationSymbol;
|
const deliveryPoint = contract.terms.deliver[0].destinationSymbol;
|
||||||
const asteroids = await systems.type(ships[0].nav.systemSymbol, 'ENGINEERED_ASTEROID');
|
const asteroids = await systems.type(ship.nav.systemSymbol, 'ENGINEERED_ASTEROID');
|
||||||
const asteroidSymbol = asteroids[0].symbol;
|
const asteroidSymbol = asteroids[0].symbol;
|
||||||
ships.forEach(async function(ship) {
|
|
||||||
while (true) {
|
|
||||||
while (!contract.fulfilled) {
|
while (!contract.fulfilled) {
|
||||||
ship = dbShips.getShip(ship.symbol);
|
ship = dbShips.getShip(ship.symbol);
|
||||||
let goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
|
const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
|
||||||
// If we are in transit, we wait until we arrive
|
// what we do depends on where we are
|
||||||
const delay = new Date(ship.nav.route.arrival).getTime() - new Date().getTime();
|
|
||||||
if (delay > 0) await api.sleep(delay);
|
|
||||||
// Then it depends on where we are
|
|
||||||
switch (ship.nav.waypointSymbol) {
|
switch (ship.nav.waypointSymbol) {
|
||||||
case asteroidSymbol:
|
case asteroidSymbol:
|
||||||
await mining.mineUntilFullFor(contract, ship, asteroidSymbol);
|
await mining.mineUntilFullFor(contract, ship, asteroidSymbol);
|
||||||
|
@ -53,18 +58,81 @@ async function runProcurement(contract: Contract, ships: Array<Ship>) {
|
||||||
case deliveryPoint:
|
case deliveryPoint:
|
||||||
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) break;
|
if (contract.fulfilled) return;
|
||||||
}
|
}
|
||||||
await libShips.navigate(ship, asteroidSymbol);
|
await libShips.navigate(ship, asteroidSymbol);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (await libShips.isFull(ship)) {
|
|
||||||
await selling.sell(ship, wantedCargo);
|
await selling.sell(ship, wantedCargo);
|
||||||
}
|
|
||||||
await libShips.navigate(ship, asteroidSymbol);
|
await libShips.navigate(ship, asteroidSymbol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
contract = await libShips.negotiate(ship); // TODO that's not correct, the procurement type can change
|
}
|
||||||
}
|
|
||||||
});
|
async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void> {
|
||||||
|
const deliver = contract.terms.deliver[0];
|
||||||
|
const deliveryPoint = deliver.destinationSymbol;
|
||||||
|
const wantedCargo = deliver.tradeSymbol;
|
||||||
|
while (!contract.fulfilled) {
|
||||||
|
ship = dbShips.getShip(ship.symbol);
|
||||||
|
const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
|
||||||
|
// make sure we are not carrying useless stuff
|
||||||
|
await selling.sell(ship, wantedCargo);
|
||||||
|
// go buy what we need
|
||||||
|
const rawMarkets = await libSystems.trait(ship.nav.systemSymbol, '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 that exports what we need
|
||||||
|
let buyingPoint: string = null;
|
||||||
|
outer: for (let i = 0; i < markets.length; i++) {
|
||||||
|
const waypointSymbol = markets[i].data.symbol;
|
||||||
|
const market = await libSystems.market(waypointSymbol);
|
||||||
|
for (let j = 0; j < market.exports; j++) {
|
||||||
|
if (market.exports[j].symbol === wantedCargo) {
|
||||||
|
buyingPoint = market.symbol;
|
||||||
|
break outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if we did not find an exporting market we look for an exchange
|
||||||
|
if (buyingPoint === null) {
|
||||||
|
outer: for (let i = 0; i < markets.length; i++) {
|
||||||
|
const waypointSymbol = markets[i].data.symbol;
|
||||||
|
const market = await libSystems.market(waypointSymbol);
|
||||||
|
for (let j = 0; j < market.exchanges; j++) {
|
||||||
|
if (market.exports[j].symbol === wantedCargo) {
|
||||||
|
buyingPoint = market.symbol;
|
||||||
|
break outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (buyingPoint === null) {
|
||||||
|
throw `runTradeProcurement failed, no market exports or exchanges ${wantedCargo}`;
|
||||||
|
}
|
||||||
|
// go buy what we need
|
||||||
|
await libShips.navigate(ship, buyingPoint);
|
||||||
|
const units = Math.min(
|
||||||
|
deliver.unitsRequired - deliver.unitsFulfilled,
|
||||||
|
ship.cargo.capacity - ship.cargo.units,
|
||||||
|
);
|
||||||
|
await libShips.buy(ship, wantedCargo, units);
|
||||||
|
// then make a delivery
|
||||||
|
await libShips.navigate(ship, deliveryPoint);
|
||||||
|
contract = await contracts.deliver(contract, ship);
|
||||||
|
if (contract.fulfilled) return;
|
||||||
|
}
|
||||||
|
console.log("runTradeProcurement not implemented");
|
||||||
|
throw "not implemented";
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import { Response } from '../model/api.ts';
|
||||||
import { Contract } from '../model/contract.ts';
|
import { Contract } from '../model/contract.ts';
|
||||||
import { Ship } from '../model/ship.ts';
|
import { Ship } from '../model/ship.ts';
|
||||||
import * as api from '../lib/api.ts';
|
import * as api from '../lib/api.ts';
|
||||||
|
//import * as agents from '../lib/angent.ts';
|
||||||
import * as libContracts from '../lib/contracts.ts';
|
import * as libContracts from '../lib/contracts.ts';
|
||||||
import * as libShips from '../lib/ships.ts';
|
import * as libShips from '../lib/ships.ts';
|
||||||
|
|
||||||
|
@ -30,10 +31,8 @@ export async function init(): Promise<void> {
|
||||||
switch(json.error?.code) {
|
switch(json.error?.code) {
|
||||||
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened
|
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened
|
||||||
// TODO await agents.agents();
|
// TODO await agents.agents();
|
||||||
const contracts = await libContracts.getContracts();
|
await libContracts.getContracts();
|
||||||
const ongoing = contracts.filter(c => !c.fulfilled);
|
await libShips.getShips();
|
||||||
const ships = await libShips.getShips();
|
|
||||||
if (ongoing.length === 0) libShips.negotiate(ships[0]);
|
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
throw json;
|
throw json;
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { Fuel, Nav, Ship } from '../model/ship.ts';
|
||||||
const addShipStatement = db.prepare(`INSERT INTO ships(data) VALUES (json(?));`);
|
const addShipStatement = db.prepare(`INSERT INTO ships(data) VALUES (json(?));`);
|
||||||
const getShipStatement = db.prepare(`SELECT data FROM ships WHERE data->>'symbol' = ?;`);
|
const getShipStatement = db.prepare(`SELECT data FROM ships WHERE data->>'symbol' = ?;`);
|
||||||
const getShipsAtStatement = db.prepare(`SELECT data FROM ships WHERE data->>'$.nav.systemSymbol' = ?;`);
|
const getShipsAtStatement = db.prepare(`SELECT data FROM ships WHERE data->>'$.nav.systemSymbol' = ?;`);
|
||||||
|
const getShipsStatement = db.prepare(`SELECT data FROM ships;`);
|
||||||
const setShipCargoStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.cargo', json(:cargo)) FROM ships WHERE data->>'symbol' = :symbol) WHERE data->>'symbol' = :symbol;`);
|
const setShipCargoStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.cargo', json(:cargo)) FROM ships WHERE data->>'symbol' = :symbol) WHERE data->>'symbol' = :symbol;`);
|
||||||
const setShipFuelStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.fuel', json(:fuel)) FROM ships WHERE data->>'symbol' = :symbol) WHERE data->>'symbol' = :symbol;`);
|
const setShipFuelStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.fuel', json(:fuel)) FROM ships WHERE data->>'symbol' = :symbol) WHERE data->>'symbol' = :symbol;`);
|
||||||
const setShipNavStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.nav', json(:nav)) FROM ships WHERE data->>'symbol' = :symbol) WHERE data->>'symbol' = :symbol;`);
|
const setShipNavStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.nav', json(:nav)) FROM ships WHERE data->>'symbol' = :symbol) WHERE data->>'symbol' = :symbol;`);
|
||||||
|
@ -16,12 +17,16 @@ export function getShip(symbol: string): Ship {
|
||||||
return JSON.parse(data.data);
|
return JSON.parse(data.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getShips(): Array<Ship> {
|
||||||
|
const data = getShipsStatement.all() as Array<DbData>;
|
||||||
|
return data.map(elt => JSON.parse(elt.data));
|
||||||
|
}
|
||||||
|
|
||||||
export function getShipsAt(symbol: string): Array<Ship> {
|
export function getShipsAt(symbol: string): Array<Ship> {
|
||||||
const data = getShipsAtStatement.all(symbol) as Array<DbData>;
|
const data = getShipsAtStatement.all(symbol) as Array<DbData>;
|
||||||
return data.map(elt => JSON.parse(elt.data));
|
return data.map(elt => JSON.parse(elt.data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export function setShip(data: Ship): void {
|
export function setShip(data: Ship): void {
|
||||||
const changes = updateShipStatement.run({
|
const changes = updateShipStatement.run({
|
||||||
data: JSON.stringify(data),
|
data: JSON.stringify(data),
|
||||||
|
|
|
@ -9,6 +9,20 @@ import * as dbShips from '../database/ships.ts';
|
||||||
//import * as dbSurveys from '../database/surveys.ts';
|
//import * as dbSurveys from '../database/surveys.ts';
|
||||||
import * as systems from '../lib/systems.ts';
|
import * as systems from '../lib/systems.ts';
|
||||||
|
|
||||||
|
export async function buy(ship: Ship, tradeSymbol: string, units: number): Promise<void> {
|
||||||
|
if (units <= 0) return;
|
||||||
|
ship = await getShip(ship);
|
||||||
|
await dock(ship);
|
||||||
|
// TODO take into account the tradevolume, we might need to buy in multiple steps
|
||||||
|
const response = await api.send<{agent: Agent, cargo: Cargo}>({endpoint: `/my/ships/${ship.symbol}/purchase`, method: 'POST', payload: { symbol: tradeSymbol, units: units }}); // TODO transaction field
|
||||||
|
if (response.error) {
|
||||||
|
api.debugLog(response);
|
||||||
|
throw response;
|
||||||
|
}
|
||||||
|
dbShips.setShipCargo(ship.symbol, response.data.cargo);
|
||||||
|
dbAgents.setAgent(response.data.agent);
|
||||||
|
}
|
||||||
|
|
||||||
export async function dock(ship: Ship): Promise<void> {
|
export async function dock(ship: Ship): Promise<void> {
|
||||||
ship = await getShip(ship);
|
ship = await getShip(ship);
|
||||||
if (ship.nav.status === 'DOCKED') return;
|
if (ship.nav.status === 'DOCKED') return;
|
||||||
|
|
|
@ -6,5 +6,5 @@ import * as autoInit from './automation/init.ts';
|
||||||
|
|
||||||
await autoInit.init();
|
await autoInit.init();
|
||||||
|
|
||||||
autoContracting.init();
|
await autoContracting.init();
|
||||||
//autoExploring.init();
|
//autoExploring.init();
|
||||||
|
|
Loading…
Add table
Reference in a new issue