From 3cb4f4df51059919b292fefb5f7a3e1ad99c9a91 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 28 Mar 2024 12:11:36 +0100 Subject: [node] stop trying to optimize useless things like local database calls --- nodejs/automation/contracting.ts | 10 ++++---- nodejs/automation/init.ts | 17 +++++++------ nodejs/automation/mining.ts | 13 ++++++---- nodejs/automation/selling.ts | 7 +++--- nodejs/database/contracts.ts | 4 +-- nodejs/database/ships.ts | 4 +-- nodejs/lib/contracts.ts | 6 ++++- nodejs/lib/ships.ts | 54 +++++++++++++++++++--------------------- nodejs/main.ts | 1 + nodejs/package-lock.json | 15 ++++++++++- nodejs/package.json | 3 ++- nodejs/tsconfig.json | 2 +- 12 files changed, 79 insertions(+), 57 deletions(-) diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts index aa4cd95..344672a 100644 --- a/nodejs/automation/contracting.ts +++ b/nodejs/automation/contracting.ts @@ -46,8 +46,8 @@ async function runProcurement(contract: Contract, ships: Array) { // 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) { 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 diff --git a/nodejs/automation/init.ts b/nodejs/automation/init.ts index b3c7a40..2850fac 100644 --- a/nodejs/automation/init.ts +++ b/nodejs/automation/init.ts @@ -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 { 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(); } diff --git a/nodejs/automation/mining.ts b/nodejs/automation/mining.ts index 5e36de5..e1230fd 100644 --- a/nodejs/automation/mining.ts +++ b/nodejs/automation/mining.ts @@ -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 { +export async function mineUntilFullOf(good: string, ship: Ship, asteroidSymbol: string): Promise { // 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 { - for (;ship.cargo.units <= ship.cargo.capacity * 0.9; ship = await libShips.extract(ship)) {} - return ship; +async function mineUntilFull(ship: Ship): Promise { + 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 diff --git a/nodejs/automation/selling.ts b/nodejs/automation/selling.ts index dbdacfe..987b8ae 100644 --- a/nodejs/automation/selling.ts +++ b/nodejs/automation/selling.ts @@ -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 { 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 { 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 { // 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 { 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; } } diff --git a/nodejs/database/contracts.ts b/nodejs/database/contracts.ts index ff24524..8442622 100644 --- a/nodejs/database/contracts.ts +++ b/nodejs/database/contracts.ts @@ -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); } diff --git a/nodejs/database/ships.ts b/nodejs/database/ships.ts index 414a059..f918099 100644 --- a/nodejs/database/ships.ts +++ b/nodejs/database/ships.ts @@ -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); } diff --git a/nodejs/lib/contracts.ts b/nodejs/lib/contracts.ts index 0adb5f6..54347cb 100644 --- a/nodejs/lib/contracts.ts +++ b/nodejs/lib/contracts.ts @@ -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 = 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> { } export async function deliver(contract: Contract, ship: Ship): Promise { + 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 } export async function fulfill(contract: Contract): Promise { + 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) { diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts index 955824e..79b20a6 100644 --- a/nodejs/lib/ships.ts +++ b/nodejs/lib/ships.ts @@ -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 { - if (ship.nav.status === 'DOCKED') return ship; +export async function dock(ship: Ship): Promise { + 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 { } } dbShips.setShipNav(ship.symbol, response.data.nav); - ship.nav = response.data.nav; - return ship; } -export async function extract(ship: Ship): Promise { +export async function extract(ship: Ship): Promise { + 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 { 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 { // return response; //} -export async function navigate(ship: Ship, waypoint: string): Promise { - if (ship.nav.waypointSymbol === waypoint) return ship; - ship = await orbit(ship); +export async function navigate(ship: Ship, waypoint: string): Promise { + 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 { } 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 { // return await api.send({endpoint: `/my/ships/${ctx.ship}/negotiate/contract`, method: 'POST'}); //} -export async function orbit(ship: Ship): Promise { - if (ship.nav.status === 'IN_ORBIT') return ship; +export async function orbit(ship: Ship): Promise { + 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 { } } 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 { // return response.data; //} -export async function refuel(ship: Ship): Promise { - if (ship.fuel.current >= ship.fuel.capacity * 0.9) return ship; +export async function refuel(ship: Ship): Promise { + 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 { } 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 { +export async function sell(ship: Ship, tradeSymbol: string): Promise { + 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 { } 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> { diff --git a/nodejs/main.ts b/nodejs/main.ts index ae10ffc..e07f331 100755 --- a/nodejs/main.ts +++ b/nodejs/main.ts @@ -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(); diff --git a/nodejs/package-lock.json b/nodejs/package-lock.json index cc5da1d..7f69b64 100644 --- a/nodejs/package-lock.json +++ b/nodejs/package-lock.json @@ -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", diff --git a/nodejs/package.json b/nodejs/package.json index ac17019..21050b8 100644 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -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", diff --git a/nodejs/tsconfig.json b/nodejs/tsconfig.json index b6ddffc..8b75445 100644 --- a/nodejs/tsconfig.json +++ b/nodejs/tsconfig.json @@ -44,7 +44,7 @@ // "noResolve": true, /* Disallow 'import's, 'require's or ''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'. */ -- cgit v1.2.3