aboutsummaryrefslogtreecommitdiff
path: root/scripts/lib.js
diff options
context:
space:
mode:
authorMetroWind <chris.corsair@gmail.com>2025-08-30 16:24:35 -0700
committerMetroWind <chris.corsair@gmail.com>2025-08-30 16:24:35 -0700
commit6a6cc2f22e806be895f9fddc9ed6cabab395b6cb (patch)
treedcc41b9bb566d900c7cb743de4c6aea276e4748e /scripts/lib.js
parent71745d48cf310fb853d134b4fae5039ea15e9631 (diff)
Add plan serialization
Diffstat (limited to 'scripts/lib.js')
-rw-r--r--scripts/lib.js30
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;
2const CELL_SIZE = 32; 2const CELL_SIZE = 32;
3const GRID_SIZE_X = 21; 3const GRID_SIZE_X = 21;
4const GRID_SIZE_Y = 21; 4const GRID_SIZE_Y = 21;
5const FLOOR_COUNT = 15;
5 6
6const ASSET_SVG_PROPS = {"width": CELL_SIZE, "height": CELL_SIZE, 7const ASSET_SVG_PROPS = {"width": CELL_SIZE, "height": CELL_SIZE,
7 "version": "1.1"}; 8 "version": "1.1"};
8const ASSET_WHITE_BG = h("rect", {"x": 0, "y": 0, "width": CELL_SIZE, 9const 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
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}