summaryrefslogtreecommitdiff
path: root/nodejs/lib/utils.ts
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-23 23:49:42 +0200
committerJulien Dessaux2024-05-23 23:49:42 +0200
commit330615936955f464a17aa0b7e39fb04d3f074b06 (patch)
treef742fa990b47a5226c18446407b8de5c619c6363 /nodejs/lib/utils.ts
parent[node] implement ship purchasing (diff)
downloadspacetraders-330615936955f464a17aa0b7e39fb04d3f074b06.tar.gz
spacetraders-330615936955f464a17aa0b7e39fb04d3f074b06.tar.bz2
spacetraders-330615936955f464a17aa0b7e39fb04d3f074b06.zip
[node] factorize sortByPrice
Diffstat (limited to '')
-rw-r--r--nodejs/lib/utils.ts15
1 files changed, 15 insertions, 0 deletions
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<T extends Point>(a: Point, points: Array<T>):
return result;
}
+export function sortByPrice<T extends Price>(data: Array<T>): 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}>;