1
0
Fork 0

[node] fixed and optimized contracting

This commit is contained in:
Julien Dessaux 2024-05-17 22:01:29 +02:00
parent 6504e44ffa
commit ccbfd9deb9
Signed by: adyxax
GPG key ID: F92E51B86E07177E
5 changed files with 61 additions and 44 deletions

View file

@ -76,43 +76,45 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void
const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0] const goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
// make sure we are not carrying useless stuff // make sure we are not carrying useless stuff
await selling.sell(ship, wantedCargo); await selling.sell(ship, wantedCargo);
// go buy what we need if (ship.cargo.units < ship.cargo.capacity) {
const markets = sortByDistanceFrom(ship.nav.route.destination, await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE')); // go buy what we need
// check from the closest one that exports what we need const markets = sortByDistanceFrom(ship.nav.route.destination, await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE'));
let buyingPoint: string = ""; // check from the closest one that exports what we need
outer: for (let i = 0; i < markets.length; i++) { let buyingPoint: string = "";
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;
break outer;
}
}
}
// if we did not find an exporting market we look for an exchange
if (buyingPoint === "") {
outer: for (let i = 0; i < markets.length; i++) { outer: for (let i = 0; i < markets.length; i++) {
const waypoint = await libSystems.waypoint(markets[i].data.symbol); const waypoint = await libSystems.waypoint(markets[i].data.symbol);
const market = await libSystems.market(waypoint); const market = await libSystems.market(waypoint);
for (let j = 0; j < market.exchange.length; j++) { for (let j = 0; j < market.exports.length; j++) {
if (market.exchange[j].symbol === wantedCargo) { if (market.exports[j].symbol === wantedCargo) {
buyingPoint = market.symbol; buyingPoint = market.symbol;
break outer; break outer;
} }
} }
} }
// 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 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.exchange[j].symbol === wantedCargo) {
buyingPoint = market.symbol;
break outer;
}
}
}
}
if (buyingPoint === "") {
throw `runTradeProcurement failed, no market exports or exchanges ${wantedCargo}`;
}
// go buy what we need
await ship.navigate(await libSystems.waypoint(buyingPoint));
const units = Math.min(
deliver.unitsRequired - deliver.unitsFulfilled,
ship.cargo.capacity - ship.cargo.units,
);
await ship.purchase(wantedCargo, units);
} }
if (buyingPoint === "") {
throw `runTradeProcurement failed, no market exports or exchanges ${wantedCargo}`;
}
// go buy what we need
await ship.navigate(await libSystems.waypoint(buyingPoint));
const units = Math.min(
deliver.unitsRequired - deliver.unitsFulfilled,
ship.cargo.capacity - ship.cargo.units,
);
await ship.purchase(wantedCargo, units);
// then make a delivery // then make a delivery
await ship.navigate(deliveryPoint); await ship.navigate(deliveryPoint);
await contract.deliver(ship); await contract.deliver(ship);

View file

@ -5,7 +5,7 @@ import {
} from '../lib/api.ts'; } from '../lib/api.ts';
import { Agent, initAgent, setAgent } from '../lib/agent.ts'; import { Agent, initAgent, setAgent } from '../lib/agent.ts';
import { Contract } from '../lib/contracts.ts'; import { Contract } from '../lib/contracts.ts';
import { Ship } from '../lib/ships.ts'; import { initShips, Ship } from '../lib/ships.ts';
import * as libContracts from '../lib/contracts.ts'; import * as libContracts from '../lib/contracts.ts';
const symbol = process.env.NODE_ENV === 'test' ? 'ADYXAX-0' : 'ADYXAX-JS'; const symbol = process.env.NODE_ENV === 'test' ? 'ADYXAX-0' : 'ADYXAX-JS';
@ -28,6 +28,7 @@ export async function init(): Promise<void> {
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();
await initAgent(); await initAgent();
await initShips();
return; return;
default: default:
throw json; throw json;
@ -36,4 +37,5 @@ export async function init(): Promise<void> {
db.reset(); db.reset();
dbTokens.addToken(json.data.token); dbTokens.addToken(json.data.token);
setAgent(json.data.agent); setAgent(json.data.agent);
await initShips();
} }

View file

@ -53,8 +53,8 @@ export class Contract {
debugLog(response); debugLog(response);
throw response; throw response;
} }
this.accepted = contract.accepted; this.accepted = response.data.contract.accepted;
this.terms = contract.terms; this.terms = response.data.contract.terms;
setAgent(response.data.agent); setAgent(response.data.agent);
} }
async deliver(ship: Ship): Promise<void> { async deliver(ship: Ship): Promise<void> {
@ -84,7 +84,7 @@ export class Contract {
throw response; throw response;
} }
} }
this.terms = contract.terms; this.terms = response.data.contract.terms;
ship.cargo = response.data.cargo; ship.cargo = response.data.cargo;
if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) { if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) {
return await this.fulfill(); return await this.fulfill();
@ -100,6 +100,6 @@ export class Contract {
} }
setAgent(response.data.agent); setAgent(response.data.agent);
this.fulfilled = true; this.fulfilled = true;
this.terms = contract.terms; this.terms = response.data.contract.terms;
} }
}; };

View file

@ -25,15 +25,6 @@ import {
shortestPath, shortestPath,
} from './utils.ts'; } from './utils.ts';
export async function getShips(): Promise<Array<Ship>> {
const response = await send<Array<Ship>>({endpoint: `/my/ships`, page: 1});
if (response.error) {
debugLog(response);
throw response;
}
return response.data.map(ship => new Ship(ship));
}
export class Ship { export class Ship {
cargo: Cargo; cargo: Cargo;
cooldown: Cooldown; cooldown: Cooldown;
@ -228,3 +219,18 @@ export class Ship {
return this.cargo; return this.cargo;
} }
} }
let myShips: Array<Ship> = [];
export function getShips(): Array<Ship> {
return myShips;
}
export async function initShips(): Promise<void> {
const response = await send<Array<Ship>>({endpoint: `/my/ships`, page: 1});
if (response.error) {
debugLog(response);
throw response;
}
myShips = response.data.map(ship => new Ship(ship));
}

View file

@ -1,10 +1,17 @@
import * as autoContracting from './automation/contracting.ts'; import * as autoContracting from './automation/contracting.ts';
//import * as autoExploring from './automation/exploration.ts'; //import * as autoExploring from './automation/exploration.ts';
import * as autoInit from './automation/init.ts'; import * as autoInit from './automation/init.ts';
import { getAgent } from './lib/agent.ts';
import { getShips } from './lib/ships.ts'; import { getShips } from './lib/ships.ts';
import { debugLog, send } from './lib/api.ts';
//debugLog(await send({endpoint: '/'}));
await autoInit.init(); await autoInit.init();
const ships = await getShips(); debugLog(getAgent());
await autoContracting.run(ships[0]); // dedicate the command ship to running contracts debugLog(await getAgent().purchaseShip());
//const ships = getShips();
//await autoContracting.run(ships[0]); // dedicate the command ship to running contracts
//autoExploring.init(); //autoExploring.init();