diff options
author | Julien Dessaux | 2024-03-21 17:08:37 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-03-27 15:21:00 +0100 |
commit | d668eac4a63a9aa98c3efff395faa23cfcea1c1b (patch) | |
tree | 691e3bb471bcacadb975f581f73e09b84287b4a5 /nodejs/lib/utils.ts | |
parent | [javascript] fixed mining loop (diff) | |
download | spacetraders-d668eac4a63a9aa98c3efff395faa23cfcea1c1b.tar.gz spacetraders-d668eac4a63a9aa98c3efff395faa23cfcea1c1b.tar.bz2 spacetraders-d668eac4a63a9aa98c3efff395faa23cfcea1c1b.zip |
[node] begin the great typescript rewrite
Diffstat (limited to 'nodejs/lib/utils.ts')
-rw-r--r-- | nodejs/lib/utils.ts | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/nodejs/lib/utils.ts b/nodejs/lib/utils.ts new file mode 100644 index 0000000..c5093a6 --- /dev/null +++ b/nodejs/lib/utils.ts @@ -0,0 +1,25 @@ +import { Cargo, CargoManifest } from '../model/cargo.ts'; + +export type CategorizedCargo = { + wanted: CargoManifest; + goods: CargoManifest; +}; + +// 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'); + const goods = cargo.inventory.filter(i => i.symbol !== want && i.symbol !== 'ANTIMATTER'); + const wobj = wanted.reduce(function(acc: CargoManifest, e) { + acc[e.symbol] = e.units; + return acc; + }, {}); + const gobj = goods.reduce(function(acc: CargoManifest, e) { + acc[e.symbol] = e.units; + return acc; + }, {}); + return {wanted: wobj, goods: gobj}; +} + +export function systemFromWaypoint(waypoint: string): string { + return waypoint.split('-').slice(0,2).join('-'); +} |