1
0
Fork 0

[node] stop trying to optimize useless things like local database calls

This commit is contained in:
Julien Dessaux 2024-03-28 12:11:36 +01:00
parent a1d6b03ec9
commit 3cb4f4df51
Signed by: adyxax
GPG key ID: F92E51B86E07177E
12 changed files with 79 additions and 57 deletions

View file

@ -46,8 +46,8 @@ async function runProcurement(contract: Contract, ships: Array<Ship>) {
// Then it depends on where we are
switch (ship.nav.waypointSymbol) {
case asteroidSymbol:
ship = await mining.mineUntilFullOf(wantedCargo, ship, asteroidSymbol);
ship = await libShips.navigate(ship, deliveryPoint);
await mining.mineUntilFullOf(wantedCargo, ship, asteroidSymbol);
await libShips.navigate(ship, deliveryPoint);
break;
case deliveryPoint:
if (goodCargo !== undefined) { // we could be here if a client restart happens right after selling before we navigate away
@ -55,12 +55,12 @@ async function runProcurement(contract: Contract, ships: Array<Ship>) {
contract = await contracts.deliver(contract, ship);
if (contract.fulfilled) break;
}
ship = await libShips.navigate(ship, asteroidSymbol);
await libShips.navigate(ship, asteroidSymbol);
break;
default:
// we were either selling or started contracting
ship = await selling.sell(ship, wantedCargo);
ship = await libShips.navigate(ship, asteroidSymbol);
await selling.sell(ship, wantedCargo);
await libShips.navigate(ship, asteroidSymbol);
}
}
// TODO repurpose the ship

View file

@ -8,7 +8,8 @@ import { Response } from '../model/api.ts';
import { Contract } from '../model/contract.ts';
import { Ship } from '../model/ship.ts';
import * as api from '../lib/api.ts';
import * as ships from '../lib/ships.ts';
import * as libContracts from '../lib/contracts.ts';
import * as libShips from '../lib/ships.ts';
const symbol = process.env.NODE_ENV === 'test' ? 'ADYXAX-0' : 'ADYXAX-JS';
@ -27,17 +28,19 @@ export async function init(): Promise<void> {
const json = await response.json() as Response<{agent: Agent, contract: Contract, ship: Ship, token: string}>;
if (json.error !== undefined) {
switch(json.error?.code) {
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened
return;
default:
throw json;
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened
await libContracts.contracts();
await libShips.ships();
return;
default:
throw json;
}
}
db.reset();
dbTokens.addToken(json.data.token);
dbAgents.addAgent(json.data.agent);
dbContracts.setContract(json.data.contract);
dbShips.setShip(json.data.ship);
dbTokens.addToken(json.data.token);
// Temporary fix to fetch the data on the startup probe
ships.ships();
await libShips.ships();
}

View file

@ -5,10 +5,11 @@ import * as libShips from '../lib/ships.js';
import * as utils from '../lib/utils.js';
import { Ship } from '../model/ship.ts';
export async function mineUntilFullOf(good: string, ship: Ship, asteroidSymbol: string): Promise<Ship> {
export async function mineUntilFullOf(good: string, ship: Ship, asteroidSymbol: string): Promise<void> {
// TODO find a good asteroid
while(true) {
ship = await mineUntilFull(ship);
await mineUntilFull(ship);
ship = dbShips.getShip(ship.symbol) as Ship;
const cargo = utils.categorizeCargo(ship.cargo, good);
const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0);
// > 90% full of the valuable goods ?
@ -21,9 +22,11 @@ export async function mineUntilFullOf(good: string, ship: Ship, asteroidSymbol:
// example ctx { symbol: 'ADYXAX-2' }
// extract the ship's cargo contents when more than 80% full then returns the ships cargo object
async function mineUntilFull(ship: Ship): Promise<Ship> {
for (;ship.cargo.units <= ship.cargo.capacity * 0.9; ship = await libShips.extract(ship)) {}
return ship;
async function mineUntilFull(ship: Ship): Promise<void> {
ship = dbShips.getShip(ship.symbol) as Ship;
while (ship.cargo.units <= ship.cargo.capacity * 0.9) {
ship.cargo = await libShips.extract(ship);
}
}
// TODO surveying the asteroid field

View file

@ -11,6 +11,7 @@ import { Ship } from '../model/ship.ts';
// example ctx { ship: {XXX}, keep: 'SILVER_ORE' }
export async function sell(ship: Ship, good: string): Promise<Ship> {
outer: while(true) {
ship = dbShips.getShip(ship.symbol);
// first lets see what we want to sell
let cargo = utils.categorizeCargo(ship.cargo, good);
// get the marketdata from our location
@ -19,7 +20,7 @@ export async function sell(ship: Ship, good: string): Promise<Ship> {
const goods = whatCanBeTradedAt(cargo.goods, market.imports.concat(market.exchange));
for (let i = 0; i < goods.length; i++) {
const symbol = goods[i].symbol;
ship = await libShips.sell(ship, good);
await libShips.sell(ship, good);
};
// are we done selling everything we can?
cargo = utils.categorizeCargo(ship.cargo, good);
@ -49,7 +50,7 @@ export async function sell(ship: Ship, good: string): Promise<Ship> {
// 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) {
ship = await libShips.navigate(ship, waypointSymbol);
await libShips.navigate(ship, waypointSymbol);
continue outer;
}
}
@ -59,7 +60,7 @@ export async function sell(ship: Ship, good: string): Promise<Ship> {
const market = await libSystems.market(waypointSymbol);
// if we can sell there we need to go
if (whatCanBeTradedAt(cargo.goods, market.exchange).length > 0) {
ship = await libShips.navigate(ship, waypointSymbol);
await libShips.navigate(ship, waypointSymbol);
continue outer;
}
}