diff options
Diffstat (limited to 'nms-freighter-planner/scripts/lib.js')
-rw-r--r-- | nms-freighter-planner/scripts/lib.js | 40 |
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 @@ | |||
1 | const h = preact.h; | ||
2 | const CELL_SIZE = 32; | ||
3 | const GRID_SIZE_X = 21; | ||
4 | const GRID_SIZE_Y = 21; | ||
5 | const FLOOR_COUNT = 15; | ||
6 | |||
7 | const ASSET_SVG_PROPS = {"width": CELL_SIZE, "height": CELL_SIZE, | ||
8 | "version": "1.1"}; | ||
9 | const 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 | ||
14 | async 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 | ||
32 | function 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 | } | ||