background data stuff

This commit is contained in:
biglyderv 2025-05-19 02:16:05 -04:00
parent 66d0642006
commit 3d55d03097
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
2 changed files with 38 additions and 27 deletions

View file

@ -6,41 +6,41 @@
function downloadFile(data, filename) { 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". // 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"); 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.setAttribute("download", filename);
a.click(); a.click();
a.remove(); a.remove();
} }
function uploadFile(options = {}) { function uploadFile(options = {}) {
base64_output = Boolean(options.useBase64); base64_output = Boolean(options.useBase64);
textEncoding = options.textEncoding ? options.textEncoding : "UTF-8"; textEncoding = options.textEncoding ? options.textEncoding : "UTF-8";
return new Promise(function(res) { return new Promise(function (res) {
var input = document.createElement('input'); var input = document.createElement('input');
input.type = 'file'; input.type = 'file';
input.onchange = e => { input.onchange = e => {
var file = e.target.files[0]; var file = e.target.files[0];
var reader = new FileReader(); var reader = new FileReader();
reader.onload = readerEvent => { reader.onload = readerEvent => {
res(readerEvent.target.result); // this is the content! res(readerEvent.target.result); // this is the content!
input.remove(); input.remove();
} }
if(base64_output) if (base64_output)
reader.readAsDataURL(file); reader.readAsDataURL(file);
else else
reader.readAsText(file, textEncoding); reader.readAsText(file, textEncoding);
} }
input.click(); input.click();
}); });
} }
@ -71,7 +71,7 @@ function save() {
otherArray = new Uint8Array(64); otherArray = new Uint8Array(64);
for (let i in otherArray) { 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) { } else if (pal.length > 8) {
@ -89,7 +89,7 @@ function save() {
'dat': otherArray ? new TextDecoder('ascii').decode(otherArray) : undefined 'dat': otherArray ? new TextDecoder('ascii').decode(otherArray) : undefined
}; };
} }
downloadFile(JSON.stringify(jason), "game-data.json"); downloadFile(JSON.stringify(jason), "game-data.json");
} }
@ -101,8 +101,8 @@ function loadFile(text_data) {
canvas.width = jason.width; canvas.width = jason.width;
canvas.height = jason.height; canvas.height = jason.height;
canvas.resize(); 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); console.log(mainPal);
@ -115,22 +115,22 @@ function loadFile(text_data) {
if (pal.length < 2) { if (pal.length < 2) {
for (let i in otherArray) { for (let i in otherArray) {
otherArray[i] = mainPal[(pal[0])]; otherArray[i] = mainPal[(pal[0])];
} }
} else if (pal.length < 9) { } else if (pal.length < 9) {
for (let i in dat) { for (let i in dat) {
otherArray[i*2] = mainPal[pal[((dat[i] - 'A'.charCodeAt()) & 0x38) / 8]]; 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 + 1] = mainPal[pal[(dat[i] - 'A'.charCodeAt()) & 0x7]];
} }
} else { } else {
for (let i in dat) { 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) { for (let i in canvas.temp) {
@ -142,7 +142,7 @@ function load() {
uploadFile().then((text_data) => { uploadFile().then((text_data) => {
try { try {
loadFile(text_data); loadFile(text_data);
} catch(err) { } catch (err) {
alert("This save file is invalid! Please provide a JSON file, with Altboxels save data.") 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; var loc2;
if (loc) { if (loc) {
(async function() { (async function () {
loadFile(await fetch(loc).then(x => x.text())); loadFile(await fetch(loc).then(x => x.text()));
load(); load();
loc2 = loc3; loc2 = loc3;

View file

@ -62,6 +62,7 @@ function TileManager(row, row2) {
this.row = row; this.row = row;
this.row2 = row2; this.row2 = row2;
this.sel = 0; this.sel = 0;
this.usedAnon = {};
this.used = {}; this.used = {};
@ -106,6 +107,16 @@ TileManager.prototype.loadSet = function (namespace, tiles) {
elem.addEventListener('click', () => { elem.addEventListener('click', () => {
this.sel = tile.number; 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;
}) })
} }
} }