From 6a6cc2f22e806be895f9fddc9ed6cabab395b6cb Mon Sep 17 00:00:00 2001 From: MetroWind Date: Sat, 30 Aug 2025 16:24:35 -0700 Subject: Add plan serialization --- scripts/lib.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'scripts/lib.js') 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; const CELL_SIZE = 32; const GRID_SIZE_X = 21; const GRID_SIZE_Y = 21; +const FLOOR_COUNT = 15; const ASSET_SVG_PROPS = {"width": CELL_SIZE, "height": CELL_SIZE, "version": "1.1"}; const ASSET_WHITE_BG = h("rect", {"x": 0, "y": 0, "width": CELL_SIZE, "height": CELL_SIZE, "fill": "white", "stroke-width": 0 }); + +// Bytes is a Uint8Array +async function compressBytes(bytes) +{ + let blob = new Blob([bytes]); + const ds = new CompressionStream("deflate"); + const compressed = blob.stream().pipeThrough(ds); + let compressed_blob = await new Response(compressed).blob(); + const reader = new FileReader(); + return new Promise((resolve, _) => { + reader.onloadend = (event) => { + const result = event.target.result; + resolve(result.replace(/^data:.+;base64,/, '') + .replaceAll("/", "-").replaceAll("+", "_")); + } + reader.readAsDataURL(compressed_blob); + }); +} + +// Decompress into Uint8Array +function decompressBytes(s) +{ + const decoded = window.atob(s.replaceAll("_", "+").replaceAll("-", "/")); + const decoded_array = Uint8Array.from(decoded, c => c.charCodeAt(0)); + let blob = new Blob([decoded_array]); + const cs = new DecompressionStream("deflate"); + const decompressed = blob.stream().pipeThrough(cs); + return new Response(decompressed).bytes(); +} -- cgit v1.2.3-70-g09d2