diff options
author | MetroWind <chris.corsair@gmail.com> | 2025-08-30 16:24:35 -0700 |
---|---|---|
committer | MetroWind <chris.corsair@gmail.com> | 2025-08-30 16:24:35 -0700 |
commit | 6a6cc2f22e806be895f9fddc9ed6cabab395b6cb (patch) | |
tree | dcc41b9bb566d900c7cb743de4c6aea276e4748e /scripts/lib.js | |
parent | 71745d48cf310fb853d134b4fae5039ea15e9631 (diff) |
Add plan serialization
Diffstat (limited to 'scripts/lib.js')
-rw-r--r-- | scripts/lib.js | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/scripts/lib.js b/scripts/lib.js index 4e4850e..9a5e957 100644 --- a/scripts/lib.js +++ b/scripts/lib.js | |||
@@ -2,9 +2,39 @@ const h = preact.h; | |||
2 | const CELL_SIZE = 32; | 2 | const CELL_SIZE = 32; |
3 | const GRID_SIZE_X = 21; | 3 | const GRID_SIZE_X = 21; |
4 | const GRID_SIZE_Y = 21; | 4 | const GRID_SIZE_Y = 21; |
5 | const FLOOR_COUNT = 15; | ||
5 | 6 | ||
6 | const ASSET_SVG_PROPS = {"width": CELL_SIZE, "height": CELL_SIZE, | 7 | const ASSET_SVG_PROPS = {"width": CELL_SIZE, "height": CELL_SIZE, |
7 | "version": "1.1"}; | 8 | "version": "1.1"}; |
8 | const ASSET_WHITE_BG = h("rect", {"x": 0, "y": 0, "width": CELL_SIZE, | 9 | const ASSET_WHITE_BG = h("rect", {"x": 0, "y": 0, "width": CELL_SIZE, |
9 | "height": CELL_SIZE, "fill": "white", | 10 | "height": CELL_SIZE, "fill": "white", |
10 | "stroke-width": 0 }); | 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 | } | ||