diff options
author | Julien Dessaux | 2024-03-28 12:11:36 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-03-28 12:11:36 +0100 |
commit | 3cb4f4df51059919b292fefb5f7a3e1ad99c9a91 (patch) | |
tree | bf97a8bdea32985f4c1a95e3759d5044b398789d /nodejs/automation | |
parent | [node] finished the great typescript rewrite (diff) | |
download | spacetraders-3cb4f4df51059919b292fefb5f7a3e1ad99c9a91.tar.gz spacetraders-3cb4f4df51059919b292fefb5f7a3e1ad99c9a91.tar.bz2 spacetraders-3cb4f4df51059919b292fefb5f7a3e1ad99c9a91.zip |
[node] stop trying to optimize useless things like local database calls
Diffstat (limited to 'nodejs/automation')
-rw-r--r-- | nodejs/automation/contracting.ts | 10 | ||||
-rw-r--r-- | nodejs/automation/init.ts | 17 | ||||
-rw-r--r-- | nodejs/automation/mining.ts | 13 | ||||
-rw-r--r-- | nodejs/automation/selling.ts | 7 |
4 files changed, 27 insertions, 20 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<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 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<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(); } 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<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 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<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; } } |