summaryrefslogtreecommitdiff
path: root/nodejs/automation
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-17 22:01:29 +0200
committerJulien Dessaux2024-05-17 22:01:29 +0200
commitccbfd9deb947c776782b80229be4513485321a88 (patch)
tree7f7f5e7816cabe4a88ed9c66ca9b29762fccb1e5 /nodejs/automation
parent[node] Added agent class, and fixed contract updates (diff)
downloadspacetraders-ccbfd9deb947c776782b80229be4513485321a88.tar.gz
spacetraders-ccbfd9deb947c776782b80229be4513485321a88.tar.bz2
spacetraders-ccbfd9deb947c776782b80229be4513485321a88.zip
[node] fixed and optimized contracting
Diffstat (limited to 'nodejs/automation')
-rw-r--r--nodejs/automation/contracting.ts58
-rw-r--r--nodejs/automation/init.ts4
2 files changed, 33 insertions, 29 deletions
diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts
index 4648b0d..2215b2c 100644
--- a/nodejs/automation/contracting.ts
+++ b/nodejs/automation/contracting.ts
@@ -76,43 +76,45 @@ async function runTradeProcurement(contract: Contract, ship: Ship): Promise<void
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 markets = sortByDistanceFrom(ship.nav.route.destination, await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE'));
- // check from the closest one that exports what we need
- let buyingPoint: string = "";
- 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.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 === "") {
+ if (ship.cargo.units < ship.cargo.capacity) {
+ // go buy what we need
+ const markets = sortByDistanceFrom(ship.nav.route.destination, await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE'));
+ // check from the closest one that exports what we need
+ let buyingPoint: string = "";
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) {
+ 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++) {
+ 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
await ship.navigate(deliveryPoint);
await contract.deliver(ship);
diff --git a/nodejs/automation/init.ts b/nodejs/automation/init.ts
index 7ec1252..1fa9949 100644
--- a/nodejs/automation/init.ts
+++ b/nodejs/automation/init.ts
@@ -5,7 +5,7 @@ import {
} from '../lib/api.ts';
import { Agent, initAgent, setAgent } from '../lib/agent.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';
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
// TODO await agents.agents();
await initAgent();
+ await initShips();
return;
default:
throw json;
@@ -36,4 +37,5 @@ export async function init(): Promise<void> {
db.reset();
dbTokens.addToken(json.data.token);
setAgent(json.data.agent);
+ await initShips();
}