wynnbuilder-idk/js/builder.js

155 lines
5.2 KiB
JavaScript

function getItemNameFromID(id) {
if (redirectMap.has(id)) {
return getItemNameFromID(redirectMap.get(id));
}
return idMap.get(id);
}
function getTomeNameFromID(id) {
if (tomeRedirectMap.has(id)) {
return getTomeNameFromID(tomeRedirectMap.get(id));
}
return tomeIDMap.get(id);
}
function parsePowdering(powder_info) {
// TODO: Make this run in linear instead of quadratic time... ew
let powdering = [];
for (let i = 0; i < 5; ++i) {
let powders = "";
let n_blocks = Base64.toInt(powder_info.charAt(0));
// console.log(n_blocks + " blocks");
powder_info = powder_info.slice(1);
for (let j = 0; j < n_blocks; ++j) {
let block = powder_info.slice(0,5);
console.log(block);
let six_powders = Base64.toInt(block);
for (let k = 0; k < 6 && six_powders != 0; ++k) {
powders += powderNames.get((six_powders & 0x1f) - 1);
six_powders >>>= 5;
}
powder_info = powder_info.slice(5);
}
powdering[i] = powders;
}
return [powdering, powder_info];
}
function populateBuildList() {
const buildList = document.getElementById("build-choice");
const savedBuilds = window.localStorage.getItem("builds") === null ? {} : JSON.parse(window.localStorage.getItem("builds"));
for (const buildName of Object.keys(savedBuilds).sort()) {
const buildOption = document.createElement("option");
buildOption.setAttribute("value", buildName);
buildList.appendChild(buildOption);
}
}
function saveBuild() {
if (player_build) {
const savedBuilds = window.localStorage.getItem("builds") === null ? {} : JSON.parse(window.localStorage.getItem("builds"));
const saveName = document.getElementById("build-name").value;
const encodedBuild = encodeBuild(player_build);
if ((!Object.keys(savedBuilds).includes(saveName)
|| document.getElementById("saved-error").textContent !== "") && encodedBuild !== "") {
savedBuilds[saveName] = encodedBuild.replace("#", "");
window.localStorage.setItem("builds", JSON.stringify(savedBuilds));
document.getElementById("saved-error").textContent = "";
document.getElementById("saved-build").textContent = "Build saved locally";
const buildList = document.getElementById("build-choice");
const buildOption = document.createElement("option");
buildOption.setAttribute("value", saveName);
buildList.appendChild(buildOption);
} else {
document.getElementById("saved-build").textContent = "";
if (encodedBuild === "") {
document.getElementById("saved-error").textContent = "Empty build";
}
else {
document.getElementById("saved-error").textContent = "Exists. Overwrite?";
}
}
}
}
function loadBuild() {
let savedBuilds = window.localStorage.getItem("builds") === null ? {} : JSON.parse(window.localStorage.getItem("builds"));
let saveName = document.getElementById("build-name").value;
if (Object.keys(savedBuilds).includes(saveName)) {
decodeBuild(savedBuilds[saveName])
document.getElementById("loaded-error").textContent = "";
document.getElementById("loaded-build").textContent = "Build loaded";
} else {
document.getElementById("loaded-build").textContent = "";
document.getElementById("loaded-error").textContent = "Build doesn't exist";
}
}
function resetFields(){
for (let i in powderInputs) {
setValue(powderInputs[i], "");
}
for (let i in equipmentInputs) {
setValue(equipmentInputs[i], "");
}
setValue("str-skp", "0");
setValue("dex-skp", "0");
setValue("int-skp", "0");
setValue("def-skp", "0");
setValue("agi-skp", "0");
setValue("level-choice", "106");
location.hash = "";
calculateBuild();
}
function toggleID() {
let button = document.getElementById("show-id-button");
let targetDiv = document.getElementById("id-edit");
if (button.classList.contains("toggleOn")) { //toggle the pressed button off
targetDiv.style.display = "none";
button.classList.remove("toggleOn");
}
else {
targetDiv.style.display = "block";
button.classList.add("toggleOn");
}
}
function toggleButton(button_id) {
let button = document.getElementById(button_id);
if (button) {
if (button.classList.contains("toggleOn")) {
button.classList.remove("toggleOn");
} else {
button.classList.add("toggleOn");
}
}
}
// toggle tab
function toggle_tab(tab) {
if (document.querySelector("#"+tab).style.display == "none") {
document.querySelector("#"+tab).style.display = "";
} else {
document.querySelector("#"+tab).style.display = "none";
}
}
// TODO: Learn and use await
function init() {
console.log("builder.js init");
init_autocomplete();
decodeBuild(url_tag);
}
//load_init(init3);
(async function() {
let load_promises = [ load_init(), load_ing_init(), load_tome_init() ];
await Promise.all(load_promises);
init();
})();