diff options
author | Julien Dessaux | 2024-04-06 21:36:42 +0200 |
---|---|---|
committer | Julien Dessaux | 2024-04-07 23:01:52 +0200 |
commit | 3e80bc8a4d3127d17dbc3f52301d33a79e53a980 (patch) | |
tree | 41caed2075ed39f8eec5f3465aded203ae377c58 /nodejs/automation/selling.ts | |
parent | [node] waypoints usage refactoring (diff) | |
download | spacetraders-3e80bc8a4d3127d17dbc3f52301d33a79e53a980.tar.gz spacetraders-3e80bc8a4d3127d17dbc3f52301d33a79e53a980.tar.bz2 spacetraders-3e80bc8a4d3127d17dbc3f52301d33a79e53a980.zip |
[node] multiple contracting fixes and some more refactoring
Diffstat (limited to '')
-rw-r--r-- | nodejs/automation/selling.ts | 21 |
1 files changed, 5 insertions, 16 deletions
diff --git a/nodejs/automation/selling.ts b/nodejs/automation/selling.ts index 6cb7d32..5dee8f7 100644 --- a/nodejs/automation/selling.ts +++ b/nodejs/automation/selling.ts @@ -1,6 +1,9 @@ import * as dbMarkets from '../database/markets.ts'; import * as libSystems from '../lib/systems.ts'; -import { categorizeCargo } from '../lib/utils.ts'; +import { + categorizeCargo, + sortByDistanceFrom, +} from '../lib/utils.ts'; import { Ship } from '../lib/ships.ts'; import { CargoManifest, @@ -26,21 +29,7 @@ export async function sell(ship: Ship, good: string): Promise<Ship> { 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; - }); + const markets = sortByDistanceFrom(ship.nav.route.destination, await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE')); // check from the closest one if they import what we need to sell for (let i = 0; i < markets.length; i++) { const waypoint = await libSystems.waypoint(markets[i].data.symbol); |