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

View file

@ -6,9 +6,9 @@ const getContractStatement = db.prepare(`SELECT data FROM contracts WHERE data->
const getContractsStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'fulfilled' = false;`);
const updateContractStatement = db.prepare(`UPDATE contracts SET data = json(:data) WHERE data->>'id' = :id;`);
export function getContract(id: string): Contract|null {
export function getContract(id: string): Contract {
const data = getContractStatement.get(id) as DbData|undefined;
if (!data) return null;
if (!data) throw `invalid id ${id} in getContract database call`;
return JSON.parse(data.data);
}

View file

@ -10,9 +10,9 @@ const setShipFuelStatement = db.prepare(`UPDATE ships SET data = (SELECT json_se
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 updateShipStatement = db.prepare(`UPDATE ships SET data = json(:data) WHERE data->>'symbol' = :symbol;`);
export function getShip(symbol: string): Ship|null {
export function getShip(symbol: string): Ship {
const data = getShipStatement.get(symbol) as DbData|undefined;
if (!data) return null;
if (!data) throw `invalid symbol ${symbol} in getShip database call`;
return JSON.parse(data.data);
}

View file

@ -10,6 +10,7 @@ import * as dbShips from '../database/ships.ts';
import * as libShips from '../lib/ships.ts';
export async function accept(contract: Contract): Promise<Contract> {
contract = dbContracts.getContract(contract.id);
if (contract.accepted) return contract;
const response = await api.send<{agent: Agent, contract: Contract, type: ''}>({endpoint: `/my/contracts/${contract.id}/accept`, method: 'POST'});
if (response.error) {
@ -28,13 +29,15 @@ export async function contracts(): Promise<Array<Contract>> {
}
export async function deliver(contract: Contract, ship: Ship): Promise<Contract> {
contract = dbContracts.getContract(contract.id);
ship = dbShips.getShip(ship.symbol);
if (contract.terms.deliver[0].unitsRequired >= contract.terms.deliver[0].unitsFulfilled) {
return await fulfill(contract);
}
const tradeSymbol = contract.terms.deliver[0].tradeSymbol;
let units = 0;
ship.cargo.inventory.forEach(i => {if (i.symbol === tradeSymbol) units = i.units; });
ship = await libShips.dock(ship); // we need to be docked to deliver
await libShips.dock(ship); // we need to be docked to deliver
const response = await api.send<{contract: Contract, cargo: Cargo}>({ endpoint: `/my/contracts/${contract.id}/deliver`, method: 'POST', payload: {
shipSymbol: ship.symbol,
tradeSymbol: tradeSymbol,
@ -58,6 +61,7 @@ export async function deliver(contract: Contract, ship: Ship): Promise<Contract>
}
export async function fulfill(contract: Contract): Promise<Contract> {
contract = dbContracts.getContract(contract.id);
if (contract.fulfilled) return contract;
const response = await api.send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${contract.id}/fulfill`, method: 'POST'});
if (response.error) {

View file

@ -8,8 +8,9 @@ import * as dbShips from '../database/ships.ts';
//import * as dbSurveys from '../database/surveys.ts';
import * as systems from '../lib/systems.ts';
export async function dock(ship: Ship): Promise<Ship> {
if (ship.nav.status === 'DOCKED') return ship;
export async function dock(ship: Ship): Promise<void> {
ship = dbShips.getShip(ship.symbol);
if (ship.nav.status === 'DOCKED') return;
const response = await api.send<{nav: Nav}>({endpoint: `/my/ships/${ship.symbol}/dock`, method: 'POST'});
if (response.error) {
switch(response.error.code) {
@ -23,16 +24,16 @@ export async function dock(ship: Ship): Promise<Ship> {
}
}
dbShips.setShipNav(ship.symbol, response.data.nav);
ship.nav = response.data.nav;
return ship;
}
export async function extract(ship: Ship): Promise<Ship> {
export async function extract(ship: Ship): Promise<Cargo> {
ship = dbShips.getShip(ship.symbol);
if (ship.cargo.units >= ship.cargo.capacity * 0.9) return ship.cargo;
// TODO move to a suitable asteroid?
// const asteroidFields = await systems.type({symbol: ship.nav.systemSymbol, type: 'ENGINEERED_ASTEROID'});
// TODO if there are multiple fields, find the closest one?
//await navigate({symbol: ctx.symbol, waypoint: asteroidFields[0].symbol});
ship = await orbit(ship);
await orbit(ship);
// TODO handle surveying?
const response = await api.send<{cooldown: Cooldown, cargo: Cargo}>({endpoint: `/my/ships/${ship.symbol}/extract`, method: 'POST'}); // TODO extraction and events api response fields cf https://spacetraders.stoplight.io/docs/spacetraders/b3931d097608d-extract-resources
if (response.error) {
@ -42,17 +43,16 @@ export async function extract(ship: Ship): Promise<Ship> {
await api.sleep(errorData.cooldown.remainingSeconds * 1000);
return await extract(ship);
case 4228: // ship is full
return ship;
return ship.cargo;
default: // yet unhandled error
api.debugLog(response);
throw response;
}
} else {
dbShips.setShipCargo(ship.symbol, response.data.cargo);
ship.cargo = response.data.cargo;
await api.sleep(response.data.cooldown.remainingSeconds*1000);
}
return ship;
return response.data.cargo
}
//function hasMount(shipSymbol, mountSymbol) {
@ -67,9 +67,10 @@ export async function extract(ship: Ship): Promise<Ship> {
// return response;
//}
export async function navigate(ship: Ship, waypoint: string): Promise<Ship> {
if (ship.nav.waypointSymbol === waypoint) return ship;
ship = await orbit(ship);
export async function navigate(ship: Ship, waypoint: string): Promise<void> {
ship = dbShips.getShip(ship.symbol);
if (ship.nav.waypointSymbol === waypoint) return;
await orbit(ship);
// TODO if we do not have enough fuel, make a stop to refuel along the way or drift to the destination
const response = await api.send<{fuel: Fuel, nav: Nav}>({endpoint: `/my/ships/${ship.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: waypoint }}); // TODO events field
if (response.error) {
@ -85,15 +86,12 @@ export async function navigate(ship: Ship, waypoint: string): Promise<Ship> {
}
dbShips.setShipFuel(ship.symbol, response.data.fuel);
dbShips.setShipNav(ship.symbol, response.data.nav);
ship.fuel = response.data.fuel;
ship.nav = response.data.nav
const delay = new Date(response.data.nav.route.arrival).getTime() - new Date().getTime() ;
await api.sleep(delay);
response.data.nav.status = 'IN_ORBIT'; // we arrive in orbit
dbShips.setShipNav(ship.symbol, response.data.nav);
ship.nav = response.data.nav
// TODO only refuel at the start of a journey, if we do not have enough OR if the destination does not sell fuel?
return await refuel(ship);
await refuel(ship);
}
//export async function negotiate(ctx) {
@ -101,8 +99,9 @@ export async function navigate(ship: Ship, waypoint: string): Promise<Ship> {
// return await api.send({endpoint: `/my/ships/${ctx.ship}/negotiate/contract`, method: 'POST'});
//}
export async function orbit(ship: Ship): Promise<Ship> {
if (ship.nav.status === 'IN_ORBIT') return ship;
export async function orbit(ship: Ship): Promise<void> {
ship = dbShips.getShip(ship.symbol);
if (ship.nav.status === 'IN_ORBIT') return;
const response = await api.send<{nav: Nav}>({endpoint: `/my/ships/${ship.symbol}/orbit`, method: 'POST'});
if (response.error) {
switch(response.error.code) {
@ -115,8 +114,6 @@ export async function orbit(ship: Ship): Promise<Ship> {
}
}
dbShips.setShipNav(ship.symbol, response.data.nav);
ship.nav = response.data.nav;
return ship;
}
//export async function purchase(ctx) {
@ -131,10 +128,11 @@ export async function orbit(ship: Ship): Promise<Ship> {
// return response.data;
//}
export async function refuel(ship: Ship): Promise<Ship> {
if (ship.fuel.current >= ship.fuel.capacity * 0.9) return ship;
export async function refuel(ship: Ship): Promise<void> {
ship = dbShips.getShip(ship.symbol);
if (ship.fuel.current >= ship.fuel.capacity * 0.9) return;
// TODO check if our current waypoint has a marketplace (and sells fuel)?
ship = await dock(ship);
await dock(ship);
const response = await api.send<{agent: Agent, fuel: Fuel}>({endpoint: `/my/ships/${ship.symbol}/refuel`, method: 'POST'}); // TODO transaction field
if (response.error) {
api.debugLog(response);
@ -142,13 +140,12 @@ export async function refuel(ship: Ship): Promise<Ship> {
}
dbShips.setShipFuel(ship.symbol, response.data.fuel);
dbAgents.setAgent(response.data.agent);
ship.fuel = response.data.fuel;
return ship;
}
export async function sell(ship: Ship, tradeSymbol: string): Promise<Ship> {
export async function sell(ship: Ship, tradeSymbol: string): Promise<Cargo> {
ship = dbShips.getShip(ship.symbol);
// TODO check if our current waypoint has a marketplace and buys tradeSymbol?
ship = await dock(ship);
await dock(ship);
let units = 0;
ship.cargo.inventory.forEach(i => {if (i.symbol === tradeSymbol) units = i.units; });
const response = await api.send<{agent: Agent, cargo: Cargo}>({endpoint: `/my/ships/${ship.symbol}/sell`, method: 'POST', payload: { symbol: tradeSymbol, units: units }}); // TODO transaction field
@ -158,8 +155,7 @@ export async function sell(ship: Ship, tradeSymbol: string): Promise<Ship> {
}
dbShips.setShipCargo(ship.symbol, response.data.cargo);
dbAgents.setAgent(response.data.agent);
ship.cargo = response.data.cargo;
return ship;
return response.data.cargo;
}
export async function ships(): Promise<Array<Ship>> {

View file

@ -5,5 +5,6 @@ import * as autoInit from './automation/init.ts';
//import * as contracts from './lib/contracts.ts';
await autoInit.init();
autoContracting.init();
//autoExploring.init();

View file

@ -10,7 +10,8 @@
},
"devDependencies": {
"esrun": "^3.2.26",
"typescript": "^5.4.3"
"typescript": "^5.4.3",
"typescript-language-server": "^4.3.3"
},
"engines": {
"node": ">=21.6.2"
@ -1057,6 +1058,18 @@
"node": ">=14.17"
}
},
"node_modules/typescript-language-server": {
"version": "4.3.3",
"resolved": "https://registry.npmjs.org/typescript-language-server/-/typescript-language-server-4.3.3.tgz",
"integrity": "sha512-3QLj57Ru9S6zv10sa4z1pA3TIR1Rdkd04Ke0EszbO4fx5PLdlYhlC/PMxwlyxls9wrZs7wPCME1Ru0s1Gabz4Q==",
"dev": true,
"bin": {
"typescript-language-server": "lib/cli.mjs"
},
"engines": {
"node": ">=18"
}
},
"node_modules/undici-types": {
"version": "5.26.5",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",

View file

@ -6,7 +6,8 @@
},
"devDependencies": {
"esrun": "^3.2.26",
"typescript": "^5.4.3"
"typescript": "^5.4.3",
"typescript-language-server": "^4.3.3"
},
"dependencies": {
"@types/better-sqlite3": "^7.6.9",

View file

@ -44,7 +44,7 @@
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
// "allowJs": false, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */