diff options
author | Julien Dessaux | 2024-03-27 15:20:14 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-03-27 15:21:37 +0100 |
commit | a1d6b03ec98abbc073b5b73b631da6ea3eae4eb9 (patch) | |
tree | 1566c60bf1155e62821d9561ba1cc998b04b8ea5 /nodejs/automation/selling.ts | |
parent | [all] fixed erroneous contracts index (diff) | |
download | spacetraders-a1d6b03ec98abbc073b5b73b631da6ea3eae4eb9.tar.gz spacetraders-a1d6b03ec98abbc073b5b73b631da6ea3eae4eb9.tar.bz2 spacetraders-a1d6b03ec98abbc073b5b73b631da6ea3eae4eb9.zip |
[node] finished the great typescript rewrite
Diffstat (limited to '')
-rw-r--r-- | nodejs/automation/selling.ts (renamed from nodejs/automation/selling.js) | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/nodejs/automation/selling.js b/nodejs/automation/selling.ts index 3d444af..dbdacfe 100644 --- a/nodejs/automation/selling.js +++ b/nodejs/automation/selling.ts @@ -1,37 +1,34 @@ -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'; +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, keep) { +export async function sell(ship: Ship, good: string): Promise<Ship> { outer: while(true) { // first lets see what we want to sell - let cargo = utils.categorizeCargo(ship.cargo, keep); + 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; - await libShips.sell({ - good: symbol, - symbol: ship.symbol, - units: cargo.goods[symbol], - }); - delete cargo.goods[symbol]; + ship = await libShips.sell(ship, good); }; // are we done selling everything we can? - ship = dbShips.getShip(ship.symbol); - cargo = utils.categorizeCargo(ship.cargo, keep); + cargo = utils.categorizeCargo(ship.cargo, good); if (Object.keys(cargo.goods).length === 0) { - return; + 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({symbol: ship.nav.systemSymbol, trait: 'MARKETPLACE'}); + 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, @@ -52,7 +49,7 @@ export async function sell(ship, keep) { // 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}); + ship = await libShips.navigate(ship, waypointSymbol); continue outer; } } @@ -62,7 +59,7 @@ export async function sell(ship, keep) { 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}); + ship = await libShips.navigate(ship, waypointSymbol); continue outer; } } @@ -70,7 +67,6 @@ export async function sell(ship, keep) { } } -function whatCanBeTradedAt(cargo, goods) { - if (goods === undefined) return []; +function whatCanBeTradedAt(cargo: CargoManifest, goods: Array<CommonThing>): Array<CommonThing> { return goods.filter(g => cargo[g.symbol] !== undefined ); } |