const uploadElement = document.createElement("input");
const prepareDom = () => {
uploadElement.type = "file";
uploadElement.addEventListener("change", () => {
const reader = new FileReader();
let file = uploadElement.files[0];
reader.addEventListener("load", function () {
importSavegame(reader.result);
}, false);
reader.readAsText(file);
});
document.body.appendChild(uploadElement);
};
const importSavegame = (savegame) => {
try {
character = JSON.parse(savegame).currentCharacterId;
if (character === undefined) {
showInfo("Could not detect character of the provided save game. Exiting...");
return;
}
console.log(character);
insertNewSave(character, savegame);
} catch (error) {
showInfo("Something went wrong. Make sure you selected an unencrpyted version of your save game");
}
};
const insertNewSave = (character, savegame) => {
let highestSlot = Object.keys(localStorage).filter(n => n.startsWith("save:" + character)).map(item => parseInt(item.split(":")[3]));
let freeSlot = Math.max(-1, ...highestSlot);
let currentTime = Math.round((new Date).getTime() / 1000);
localStorage[`save:${character}:${currentTime}:${++freeSlot}`] = savegame;
showInfo("Your savegame should be imported now.");
document.body.removeChild(uploadElement);
};
const showInfo = alert;
prepareDom();