From 3d55d03097e22c4cb688295a2c17feebca5cf82a Mon Sep 17 00:00:00 2001 From: biglyderv Date: Mon, 19 May 2025 02:16:05 -0400 Subject: [PATCH] background data stuff --- js/save_load.js | 54 ++++++++++++++++++++++++------------------------- js/tile.js | 11 ++++++++++ 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/js/save_load.js b/js/save_load.js index f1a4815..dc49fc0 100644 --- a/js/save_load.js +++ b/js/save_load.js @@ -6,41 +6,41 @@ function downloadFile(data, filename) { // ONLY works with ascii/latin1 characters. if you'd like to be able to download UTF-16 data (for compression, etc), please find a base64-encoding library for UTF-16, and replace "btoa". - + let a = document.createElement("a"); - a.href = "data:text/plain;base64,"+(btoa(data)); + a.href = "data:text/plain;base64," + (btoa(data)); a.setAttribute("download", filename); a.click(); a.remove(); - + } function uploadFile(options = {}) { base64_output = Boolean(options.useBase64); textEncoding = options.textEncoding ? options.textEncoding : "UTF-8"; - - return new Promise(function(res) { + + return new Promise(function (res) { var input = document.createElement('input'); input.type = 'file'; - - input.onchange = e => { + + input.onchange = e => { var file = e.target.files[0]; var reader = new FileReader(); reader.onload = readerEvent => { - + res(readerEvent.target.result); // this is the content! input.remove(); - + } - - if(base64_output) + + if (base64_output) reader.readAsDataURL(file); else reader.readAsText(file, textEncoding); - - + + } - + input.click(); }); } @@ -71,7 +71,7 @@ function save() { otherArray = new Uint8Array(64); for (let i in otherArray) { - otherArray[i] = (((pal.indexOf(arr[i*2]) * 8) + pal.indexOf(arr[i*2+1])) + 'A'.charCodeAt()) % 128; + otherArray[i] = (((pal.indexOf(arr[i * 2]) * 8) + pal.indexOf(arr[i * 2 + 1])) + 'A'.charCodeAt()) % 128; } } else if (pal.length > 8) { @@ -89,7 +89,7 @@ function save() { 'dat': otherArray ? new TextDecoder('ascii').decode(otherArray) : undefined }; } - + downloadFile(JSON.stringify(jason), "game-data.json"); } @@ -101,8 +101,8 @@ function loadFile(text_data) { canvas.width = jason.width; canvas.height = jason.height; canvas.resize(); - - let mainPal = jason.pal.map(x => mainTiles.resolveID(x[0],x[1])); + + let mainPal = jason.pal.map(x => mainTiles.resolveID(x[0], x[1])); console.log(mainPal); @@ -115,22 +115,22 @@ function loadFile(text_data) { if (pal.length < 2) { for (let i in otherArray) { - otherArray[i] = mainPal[(pal[0])]; + otherArray[i] = mainPal[(pal[0])]; } - + } else if (pal.length < 9) { for (let i in dat) { - otherArray[i*2] = mainPal[pal[((dat[i] - 'A'.charCodeAt()) & 0x38) / 8]]; - otherArray[i*2+1] = mainPal[pal[(dat[i] - 'A'.charCodeAt()) & 0x7]]; + otherArray[i * 2] = mainPal[pal[((dat[i] - 'A'.charCodeAt()) & 0x38) / 8]]; + otherArray[i * 2 + 1] = mainPal[pal[(dat[i] - 'A'.charCodeAt()) & 0x7]]; } - + } else { for (let i in dat) { - otherArray[i] = mainPal[pal[(dat[i] - 'A'.charCodeAt()) & 0x7F]]; + otherArray[i] = mainPal[pal[(dat[i] - 'A'.charCodeAt()) & 0x7F]]; } } - canvas.blocks.set(otherArray,Math.min(i*128,canvas.blocks.length - 128)); + canvas.blocks.set(otherArray, Math.min(i * 128, canvas.blocks.length - 128)); } for (let i in canvas.temp) { @@ -142,7 +142,7 @@ function load() { uploadFile().then((text_data) => { try { loadFile(text_data); - } catch(err) { + } catch (err) { alert("This save file is invalid! Please provide a JSON file, with Altboxels save data.") } }) @@ -156,7 +156,7 @@ if (loc3.get('oops') == 'true') { var loc2; if (loc) { - (async function() { + (async function () { loadFile(await fetch(loc).then(x => x.text())); load(); loc2 = loc3; diff --git a/js/tile.js b/js/tile.js index 51b7556..8ff0125 100644 --- a/js/tile.js +++ b/js/tile.js @@ -62,6 +62,7 @@ function TileManager(row, row2) { this.row = row; this.row2 = row2; this.sel = 0; + this.usedAnon = {}; this.used = {}; @@ -106,6 +107,16 @@ TileManager.prototype.loadSet = function (namespace, tiles) { elem.addEventListener('click', () => { this.sel = tile.number; + if (!this.usedAnon[this.sel]) { + fetch("https://data.dervland.net/", { + "method": "post", + "body": JSON.stringify({'action': `Used ${tile.id} in Altboxels`}), + "headers": { + "content-type": "application/json" + } + }) + } + this.usedAnon[this.sel] = true; }) } }