aboutsummaryrefslogtreecommitdiff
path: root/nms-freighter-planner/scripts/lib.js
diff options
context:
space:
mode:
authorMetroWind <chris.corsair@gmail.com>2025-09-25 16:16:58 -0700
committerMetroWind <chris.corsair@gmail.com>2025-09-25 16:16:58 -0700
commit5f168246cca42ba54b67bd84fbd69fd6a5b9f820 (patch)
treeace07dfcb83ea1baac5062a02fe6d0a81ea4bf22 /nms-freighter-planner/scripts/lib.js
parenta1fa9840be9ca1b092f722be7b61c142cf80d8e8 (diff)
This repo will be used for all small web apps.
The NMS Freighter Planner is moved into its own dir.
Diffstat (limited to 'nms-freighter-planner/scripts/lib.js')
-rw-r--r--nms-freighter-planner/scripts/lib.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/nms-freighter-planner/scripts/lib.js b/nms-freighter-planner/scripts/lib.js
new file mode 100644
index 0000000..9a5e957
--- /dev/null
+++ b/nms-freighter-planner/scripts/lib.js
@@ -0,0 +1,40 @@
1const h = preact.h;
2const CELL_SIZE = 32;
3const GRID_SIZE_X = 21;
4const GRID_SIZE_Y = 21;
5const FLOOR_COUNT = 15;
6
7const ASSET_SVG_PROPS = {"width": CELL_SIZE, "height": CELL_SIZE,
8 "version": "1.1"};
9const ASSET_WHITE_BG = h("rect", {"x": 0, "y": 0, "width": CELL_SIZE,
10 "height": CELL_SIZE, "fill": "white",
11 "stroke-width": 0 });
12
13// Bytes is a Uint8Array
14async function compressBytes(bytes)
15{
16 let blob = new Blob([bytes]);
17 const ds = new CompressionStream("deflate");
18 const compressed = blob.stream().pipeThrough(ds);
19 let compressed_blob = await new Response(compressed).blob();
20 const reader = new FileReader();
21 return new Promise((resolve, _) => {
22 reader.onloadend = (event) => {
23 const result = event.target.result;
24 resolve(result.replace(/^data:.+;base64,/, '')
25 .replaceAll("/", "-").replaceAll("+", "_"));
26 }
27 reader.readAsDataURL(compressed_blob);
28 });
29}
30
31// Decompress into Uint8Array
32function decompressBytes(s)
33{
34 const decoded = window.atob(s.replaceAll("_", "+").replaceAll("-", "/"));
35 const decoded_array = Uint8Array.from(decoded, c => c.charCodeAt(0));
36 let blob = new Blob([decoded_array]);
37 const cs = new DecompressionStream("deflate");
38 const decompressed = blob.stream().pipeThrough(cs);
39 return new Response(decompressed).bytes();
40}