From 330615936955f464a17aa0b7e39fb04d3f074b06 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 23 May 2024 23:49:42 +0200 Subject: [node] factorize sortByPrice --- nodejs/automation/selling.ts | 2 +- nodejs/lib/ships.ts | 13 +++++-------- nodejs/lib/utils.ts | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/nodejs/automation/selling.ts b/nodejs/automation/selling.ts index b17aad0..df9a92b 100644 --- a/nodejs/automation/selling.ts +++ b/nodejs/automation/selling.ts @@ -7,7 +7,7 @@ import { } from '../lib/utils.ts'; import { Ship } from '../lib/ships.ts'; -// example ctx { ship: {XXX}, keep: 'SILVER_ORE' } +// example ctx { ship: {XXX}, good: 'SILVER_ORE' } export async function sell(ship: Ship, good: string): Promise { outer: while(true) { const waypoint = await libSystems.waypoint(ship.nav.waypointSymbol); diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts index 0e3605f..55a36a5 100644 --- a/nodejs/lib/ships.ts +++ b/nodejs/lib/ships.ts @@ -24,6 +24,7 @@ import { import { is_there_a_ship_at_this_waypoint, shortestPath, + sortByPrice, } from './utils.ts'; export class Ship { @@ -105,6 +106,9 @@ export class Ship { // } // this.nav = response.data; //} + isEmpty(): boolean { + return this.cargo.inventory.some(i => i.symbol !== 'ANTIMATTER'); + } isFull(): boolean { return this.cargo.units >= this.cargo.capacity * 0.9; } @@ -273,14 +277,7 @@ export async function purchaseShip(shipType: string): Promise { candidates = backupCandidates; needsNavigate = true; } - candidates.sort(function(a, b) { - if (a.price < b.price) { - return -1; - } else if (a.price > b.price) { - return 1; - } - return 0; - }); + sortByPrice(candidates); if (needsNavigate) { // we did not have a probe in orbit of a shipyard selling ${shipType} // yet, must be early game buying our second probe so let's move the diff --git a/nodejs/lib/utils.ts b/nodejs/lib/utils.ts index 6d051ea..9491583 100644 --- a/nodejs/lib/utils.ts +++ b/nodejs/lib/utils.ts @@ -21,6 +21,10 @@ type Point = { y: number; }; +type Price = { + price: number; +}; + // cargo is a ship.cargo object, want is an optional symbol export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo { const wanted = cargo.inventory.filter(i => i.symbol === want || i.symbol === 'ANTIMATTER'); @@ -60,6 +64,17 @@ export function sortByDistanceFrom(a: Point, points: Array): return result; } +export function sortByPrice(data: Array): void { + data.sort(function(a, b) { + if (a.price < b.price) { + return -1; + } else if (a.price > b.price) { + return 1; + } + return 0; + }); +} + type Step = {waypoint: Waypoint, prev: string, fuel: number, total: number}; type ShortestPath = Array<{symbol: string, fuel: number}>; -- cgit v1.2.3