From a1d6b03ec98abbc073b5b73b631da6ea3eae4eb9 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 27 Mar 2024 15:20:14 +0100 Subject: [node] finished the great typescript rewrite --- nodejs/automation/contracting.ts | 16 ++++----- nodejs/automation/exploration.js | 11 ------ nodejs/automation/exploration.ts | 9 +++++ nodejs/automation/init.js | 39 --------------------- nodejs/automation/init.ts | 43 +++++++++++++++++++++++ nodejs/automation/mining.js | 33 ----------------- nodejs/automation/mining.ts | 29 +++++++++++++++ nodejs/automation/selling.js | 76 ---------------------------------------- nodejs/automation/selling.ts | 72 +++++++++++++++++++++++++++++++++++++ 9 files changed, 159 insertions(+), 169 deletions(-) delete mode 100644 nodejs/automation/exploration.js create mode 100644 nodejs/automation/exploration.ts delete mode 100644 nodejs/automation/init.js create mode 100644 nodejs/automation/init.ts delete mode 100644 nodejs/automation/mining.js create mode 100644 nodejs/automation/mining.ts delete mode 100644 nodejs/automation/selling.js create mode 100644 nodejs/automation/selling.ts (limited to 'nodejs/automation') diff --git a/nodejs/automation/contracting.ts b/nodejs/automation/contracting.ts index 2857778..aa4cd95 100644 --- a/nodejs/automation/contracting.ts +++ b/nodejs/automation/contracting.ts @@ -34,7 +34,7 @@ async function runProcurement(contract: Contract, ships: Array) { // TODO check if contract is fulfilled! const wantedCargo = contract.terms.deliver[0].tradeSymbol; const deliveryPoint = contract.terms.deliver[0].destinationSymbol; - const asteroids = await systems.type({symbol: ships[0].nav.systemSymbol, type: 'ENGINEERED_ASTEROID'}); + const asteroids = await systems.type(ships[0].nav.systemSymbol, 'ENGINEERED_ASTEROID'); const asteroidSymbol = asteroids[0].symbol; ships.forEach(async function(ship) { while (!contract.fulfilled) { @@ -46,12 +46,8 @@ async function runProcurement(contract: Contract, ships: Array) { // Then it depends on where we are switch (ship.nav.waypointSymbol) { case asteroidSymbol: - await mining.mineUntilFullOf({ - asteroidSymbol: asteroidSymbol, - good: wantedCargo, - symbol: ship.symbol - }); - await libShips.navigate(ship, deliveryPoint); + ship = await mining.mineUntilFullOf(wantedCargo, ship, asteroidSymbol); + ship = 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 @@ -59,12 +55,12 @@ async function runProcurement(contract: Contract, ships: Array) { contract = await contracts.deliver(contract, ship); if (contract.fulfilled) break; } - await libShips.navigate(ship, asteroidSymbol); + ship = await libShips.navigate(ship, asteroidSymbol); break; default: // we were either selling or started contracting - await selling.sell(ship, wantedCargo); - await libShips.navigate(ship, asteroidSymbol); + ship = await selling.sell(ship, wantedCargo); + ship = await libShips.navigate(ship, asteroidSymbol); } } // TODO repurpose the ship diff --git a/nodejs/automation/exploration.js b/nodejs/automation/exploration.js deleted file mode 100644 index e4f24f1..0000000 --- a/nodejs/automation/exploration.js +++ /dev/null @@ -1,11 +0,0 @@ -import db from '../database/db.js'; -import * as dbSystems from '../database/systems.js'; -import * as api from '../lib/api.js'; - -export async function init() { - const response = await api.send({endpoint: `/systems`, page: Math.max(1, Math.floor(dbSystems.getSystemsCount()/20)), priority: 100}); - if (response.error !== undefined) { - throw response; - } - db.transaction(() => response.forEach(function(system) { try { dbSystems.addSystem(system); } catch {} }))(); -} diff --git a/nodejs/automation/exploration.ts b/nodejs/automation/exploration.ts new file mode 100644 index 0000000..67fbfe4 --- /dev/null +++ b/nodejs/automation/exploration.ts @@ -0,0 +1,9 @@ +import db from '../database/db.ts'; +import * as dbSystems from '../database/systems.ts'; +import { System } from '../model/system.ts'; +import * as api from '../lib/api.ts'; + +export async function init(): Promise { + const response = await api.sendPaginated({endpoint: `/systems`, page: Math.max(1, Math.floor(dbSystems.getSystemsCount()/20)), priority: 100}); + db.transaction(() => response.forEach(function(system) { try { dbSystems.addSystem(system); } catch {} }))(); +} diff --git a/nodejs/automation/init.js b/nodejs/automation/init.js deleted file mode 100644 index 93e5a70..0000000 --- a/nodejs/automation/init.js +++ /dev/null @@ -1,39 +0,0 @@ -import * as dbAgents from '../database/agents.js'; -import * as db from '../database/db.js'; -import * as dbContracts from '../database/contracts.js'; -import * as dbShips from '../database/ships.js'; -import * as dbTokens from '../database/tokens.js'; -import * as api from '../lib/api.js'; -import * as ships from '../lib/ships.js'; - -const symbol = process.env.NODE_ENV === 'test' ? 'ADYXAX-0' : 'ADYXAX-TS'; - -// This function registers then inits the database -export async function init() { - const response = await fetch('https://api.spacetraders.io/v2/register', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - symbol: symbol, - faction: "COSMIC", - }), - }); - const json = await response.json(); - 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; - } - } - db.reset(); - 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(); -} diff --git a/nodejs/automation/init.ts b/nodejs/automation/init.ts new file mode 100644 index 0000000..b3c7a40 --- /dev/null +++ b/nodejs/automation/init.ts @@ -0,0 +1,43 @@ +import * as dbAgents from '../database/agents.ts'; +import * as db from '../database/db.ts'; +import * as dbContracts from '../database/contracts.ts'; +import * as dbShips from '../database/ships.ts'; +import * as dbTokens from '../database/tokens.ts'; +import { Agent } from '../model/agent.ts'; +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'; + +const symbol = process.env.NODE_ENV === 'test' ? 'ADYXAX-0' : 'ADYXAX-JS'; + +// This function registers then inits the database +export async function init(): Promise { + const response = await fetch('https://api.spacetraders.io/v2/register', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + symbol: symbol, + faction: "COSMIC", + }), + }); + 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; + } + } + db.reset(); + 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(); +} diff --git a/nodejs/automation/mining.js b/nodejs/automation/mining.js deleted file mode 100644 index 79a784a..0000000 --- a/nodejs/automation/mining.js +++ /dev/null @@ -1,33 +0,0 @@ -import * as selling from './selling.js'; -import * as dbShips from '../database/ships.js'; -import * as api from '../lib/api.js'; -import * as libShips from '../lib/ships.js'; -import * as utils from '../lib/utils.js'; - -// example ctx { asteroidSymbol: XXXXX, good: 'SILVER_ORE', symbol: 'ADYXAX-2' } -// returns the number of units of the good the ship holds -export async function mineUntilFullOf(ctx) { - while(true) { - const ship = dbShips.getShip(ctx.symbol); - const cargo = utils.categorizeCargo(await mineUntilFull({symbol: ctx.symbol}), ctx.good); - const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0); - // > 90% full of the valuable goods ? - if (wantedUnits >= ship.cargo.capacity * 0.9) return; - // we are full but need to sell junk - await selling.sell(ship, ctx.good); - await libShips.navigate({symbol: ship.symbol, waypoint: ctx.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(ctx) { - while(true) { - const ship = dbShips.getShip(ctx.symbol); - if (ship.cargo.units >= ship.cargo.capacity * 0.9) return ship.cargo; - if (await libShips.extract({symbol: ctx.symbol}) === null) - await ship(ctx); // refresh the ships status from the server just in case - } -} - -// TODO surveying the asteroid field diff --git a/nodejs/automation/mining.ts b/nodejs/automation/mining.ts new file mode 100644 index 0000000..5e36de5 --- /dev/null +++ b/nodejs/automation/mining.ts @@ -0,0 +1,29 @@ +import * as selling from './selling.js'; +import * as dbShips from '../database/ships.js'; +import * as api from '../lib/api.js'; +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 { + // TODO find a good asteroid + while(true) { + ship = await mineUntilFull(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 ? + if (wantedUnits >= ship.cargo.capacity * 0.9) return ship; + // we are full but need to sell junk + await selling.sell(ship, good); + await libShips.navigate(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; +} + +// TODO surveying the asteroid field diff --git a/nodejs/automation/selling.js b/nodejs/automation/selling.js deleted file mode 100644 index 3d444af..0000000 --- a/nodejs/automation/selling.js +++ /dev/null @@ -1,76 +0,0 @@ -import * as dbMarkets from '../database/markets.js'; -import * as dbShips from '../database/ships.js'; -import * as api from '../lib/api.js'; -import * as libShips from '../lib/ships.js'; -import * as libSystems from '../lib/systems.js'; -import * as utils from '../lib/utils.js'; - -// example ctx { ship: {XXX}, keep: 'SILVER_ORE' } -export async function sell(ship, keep) { - outer: while(true) { - // first lets see what we want to sell - let cargo = utils.categorizeCargo(ship.cargo, keep); - // get the marketdata from our location - const market = await libSystems.market(ship.nav.waypointSymbol); - // can we sell anything here? - const goods = whatCanBeTradedAt(cargo.goods, market.imports.concat(market.exchange)); - for (let i = 0; i < goods.length; i++) { - const symbol = goods[i].symbol; - await libShips.sell({ - good: symbol, - symbol: ship.symbol, - units: cargo.goods[symbol], - }); - delete cargo.goods[symbol]; - }; - // are we done selling everything we can? - ship = dbShips.getShip(ship.symbol); - cargo = utils.categorizeCargo(ship.cargo, keep); - if (Object.keys(cargo.goods).length === 0) { - return; - } - // we need to move somewhere else to sell our remaining goods - // first we look into markets in our system - const rawMarkets = await libSystems.trait({symbol: ship.nav.systemSymbol, trait: 'MARKETPLACE'}); - // sorted by distance from where we are - const markets = rawMarkets.map(function (m) { return { - data: m, - distance: (m.x - ship.nav.route.destination.x) ** 2 + (m.y - ship.nav.route.destination.y) ** 2, - }}); - markets.sort(function(a, b) { - if (a.distance < b.distance) { - return -1; - } else if (a.distance > b.distance) { - return 1; - } - return 0; - }); - // check from the closest one if they import what we need to sell - for (let i = 0; i < markets.length; i++) { - const waypointSymbol = markets[i].data.symbol; - const market = await libSystems.market(waypointSymbol); - // 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) { - await libShips.navigate({symbol: ship.symbol, waypoint: waypointSymbol}); - continue outer; - } - } - // check from the closest one if they exchange what we need to sell - for (let i = 0; i < markets.length; i++) { - const waypointSymbol = markets[i].data.symbol; - const market = await libSystems.market(waypointSymbol); - // if we can sell there we need to go - if (whatCanBeTradedAt(cargo.goods, market.exchange).length > 0) { - await libShips.navigate({symbol: ship.symbol, waypoint: waypointSymbol}); - continue outer; - } - } - throw new Error(`Ship {ship.symbol} has found no importing or exchanging market for its cargo in the system`); - } -} - -function whatCanBeTradedAt(cargo, goods) { - if (goods === undefined) return []; - return goods.filter(g => cargo[g.symbol] !== undefined ); -} diff --git a/nodejs/automation/selling.ts b/nodejs/automation/selling.ts new file mode 100644 index 0000000..dbdacfe --- /dev/null +++ b/nodejs/automation/selling.ts @@ -0,0 +1,72 @@ +import * as dbMarkets from '../database/markets.ts'; +import * as dbShips from '../database/ships.ts'; +import * as api from '../lib/api.ts'; +import * as libShips from '../lib/ships.ts'; +import * as libSystems from '../lib/systems.ts'; +import * as utils from '../lib/utils.ts'; +import { CargoManifest } from '../model/cargo.ts'; +import { CommonThing } from '../model/common.ts'; +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) { + // first lets see what we want to sell + let cargo = utils.categorizeCargo(ship.cargo, good); + // get the marketdata from our location + const market = await libSystems.market(ship.nav.waypointSymbol); + // can we sell anything here? + 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); + }; + // are we done selling everything we can? + cargo = utils.categorizeCargo(ship.cargo, good); + if (Object.keys(cargo.goods).length === 0) { + return ship; + } + // we need to move somewhere else to sell our remaining goods + // first we look into markets in our system + const rawMarkets = await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE'); + // sorted by distance from where we are + const markets = rawMarkets.map(function (m) { return { + data: m, + distance: (m.x - ship.nav.route.destination.x) ** 2 + (m.y - ship.nav.route.destination.y) ** 2, + }}); + markets.sort(function(a, b) { + if (a.distance < b.distance) { + return -1; + } else if (a.distance > b.distance) { + return 1; + } + return 0; + }); + // check from the closest one if they import what we need to sell + for (let i = 0; i < markets.length; i++) { + const waypointSymbol = markets[i].data.symbol; + const market = await libSystems.market(waypointSymbol); + // 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); + continue outer; + } + } + // check from the closest one if they exchange what we need to sell + for (let i = 0; i < markets.length; i++) { + const waypointSymbol = markets[i].data.symbol; + 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); + continue outer; + } + } + throw new Error(`Ship {ship.symbol} has found no importing or exchanging market for its cargo in the system`); + } +} + +function whatCanBeTradedAt(cargo: CargoManifest, goods: Array): Array { + return goods.filter(g => cargo[g.symbol] !== undefined ); +} -- cgit v1.2.3