aboutsummaryrefslogtreecommitdiff
path: root/nms-freighter-planner/scripts/lib.js
blob: 9a5e957c0c0a8da916ff8effe9c6d27d9cb33cbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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();
}