From 5f168246cca42ba54b67bd84fbd69fd6a5b9f820 Mon Sep 17 00:00:00 2001 From: MetroWind Date: Thu, 25 Sep 2025 16:16:58 -0700 Subject: This repo will be used for all small web apps. The NMS Freighter Planner is moved into its own dir. --- nms-freighter-planner/scripts/lib.js | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 nms-freighter-planner/scripts/lib.js (limited to 'nms-freighter-planner/scripts/lib.js') 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 @@ +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