From ed8777f56f48d52c4e63ec56db6fbff3485dcd6b Mon Sep 17 00:00:00 2001 From: nif Date: Thu, 18 Apr 2024 17:56:20 +0000 Subject: [PATCH] fix save loading when save doesn't exist will need to figure out how to test this but it should work Signed-off-by: nif --- main.js | 554 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 277 insertions(+), 277 deletions(-) diff --git a/main.js b/main.js index f35e998..e9cdf63 100644 --- a/main.js +++ b/main.js @@ -1,278 +1,278 @@ -// Player data -let player = { - points: 10, - condensed: 0, - gens: [0], - egens: [0], - cupgs: [0, 0, 0, 0], - abs: [], - unlocked: { - condensed: false, - autobuyer: false - }, - abEnabled: true - -} -let local = { - tab: "main-tab", - updates: [], - dontSave: false, -} - -// General event listeners -document.getElementById("main-tab-btn").addEventListener("click", () => { - document.getElementById(local.tab).style = "display: none;" - local.tab = "main-tab" - document.getElementById("main-tab").style = "display: block;" -}) -document.getElementById("condensed-btn").addEventListener("click", () => { - document.getElementById(local.tab).style = "display: none;" - local.tab = "condensed" - document.getElementById("condensed").style = "display: block;" -}) -document.getElementById("settings-btn").addEventListener("click", () => { - document.getElementById(local.tab).style = "display: none;" - local.tab = "settings" - document.getElementById("settings").style = "display: block;" -}) -document.getElementById("changelog-btn").addEventListener("click", () => { - document.getElementById(local.tab).style = "display: none;" - local.tab = "changelog" - document.getElementById("changelog").style = "display: block;" -}) - -// Generator functions -function getPointsPerSecond(gen = -1) { - if (gen > player.gens.length - 2) return 0 - return ((player.egens[gen+1] + player.gens[gen+1]) * (Math.sqrt(player.gens[gen+1] + 1) - ** Math.cbrt((player.cupgs[5] || 0) + 1))) * (2 ** (player.cupgs[1] || 0)) - ** (gen == -1 ? 1.2 ** (player.cupgs[0] || 0) : 1) -} -function pushGenerator() { - const index = player.gens.push(0) - 1 - player.egens.push(0) - newGenerator(index) -} -function newGenerator(index) { - const cost = document.createElement("span") - cost.innerText = ((100 ** index * 10) * (1.5 ** player.gens[index])).toPrecision(3) - const buyBtn = document.createElement("button") - buyBtn.addEventListener("click", () => { - if ((100 ** index * 10) * (1.5 ** player.gens[index]) !== Number.POSITIVE_INFINITY - && player.points >= (100 ** index * 10) * (1.5 ** player.gens[index])) { - player.points -= (100 ** index * 10) * (1.5 ** player.gens[index]) - player.gens[index] ++ - if (player.gens[index] == 10) { pushGenerator() } - } - }) - buyBtn.innerText = "Buy" - const buyMaxBtn = document.createElement("button") - buyMaxBtn.addEventListener("click", () => { - while ((100 ** index * 10) * (1.5 ** player.gens[index]) !== Number.POSITIVE_INFINITY - && player.points >= (100 ** index * 10) * (1.5 ** player.gens[index]) ) { - player.points -= (100 ** index * 10) * (1.5 ** player.gens[index]) - player.gens[index] ++ - if (player.gens[index] == 10) { pushGenerator() } - } - }) - buyMaxBtn.innerText = "Buy Max" - const extra = document.createElement("span") - extra.innerText = 0 - extra.id = "gen-" + index + "-extra" - const gens = document.createElement("span") - gens.innerText = 0 - gens.id = "gen-" + index + "-gens" - const gener = document.createElement("span") - gener.innerText = getPointsPerSecond(index-1).toPrecision(3) - gener.id = "gen-" + index + "-gener" - - const newRow = document.createElement("tr") - newRow.append( - (() => { - const el = document.createElement("td") - el.innerText = "Gen " + (index + 1) - return el - })(), - (() => { - const el = document.createElement("td") - el.append(gens, " + ", extra) - return el - })(), - (() => { - const el = document.createElement("td") - el.append(gener, "/s") - return el - })(), - (() => { - const el = document.createElement("td") - cost.id = "gen-" + index + "-cost" - el.append(cost, " points") - return el - })(), - (() => { - const el = document.createElement("td") - el.append(buyBtn, " ", buyMaxBtn) - return el - })(), - ) - if (index % 2 == 0) newRow.className = "alt" - document.getElementById("gens").appendChild(newRow) - - local.updates.push(function() { - extra.innerText = player.egens[index].toPrecision(3) - gens.innerText = player.gens[index].toPrecision(3) - gener.innerText = getPointsPerSecond(index-1).toPrecision(3) - cost.innerText = ((100 ** index * 10) * (1.5 ** player.gens[index])).toPrecision(3) - }) -} - -// Condensed functions -function condensedGain() { - return Math.sqrt(Math.max(0, player.gens.length - 24)) ** Math.sqrt((player.cupgs[4] || 0) + 1) -} -document.getElementById("cond-btn").addEventListener("click", () => { - if (condensedGain() == 0) return - player.condensed += condensedGain() - player.gens = [0] - player.egens = [0] - local.updates = [] - player.points = 10 -}) -document.getElementById("cupg-0").addEventListener("click", () => { - if (player.condensed >= 1.3 ** (player.cupgs[0] || 0)) { - player.condensed -= 1.3 ** (player.cupgs[0] || 0) - player.cupgs[0] = (player.cupgs[0] || 0) + 1 - } -}) -document.getElementById("cupg-1").addEventListener("click", () => { - if (player.condensed >= 1.3 ** (player.cupgs[1] || 0)) { - player.condensed -= 1.3 ** (player.cupgs[1] || 0) - player.cupgs[1] = (player.cupgs[1] || 0) + 1 - } -}) -document.getElementById("cupg-2").addEventListener("click", () => { - if (player.condensed >= 3 * 1.6 ** (player.cupgs[2] || 0)) { - player.condensed -= 3 * 1.6 ** (player.cupgs[2] || 0) - player.cupgs[2] = (player.cupgs[2] || 0) + 1 - player.abs.push(0) - player.unlocked.autobuyer = true - } -}) -document.getElementById("ab-en-btn").addEventListener("click", () => { - if (player.unlocked.autobuyer) player.abEnabled = !player.abEnabled -}) -document.getElementById("cupg-3").addEventListener("click", () => { - if (player.condensed >= 8 * 2.2 ** (player.cupgs[3] || 0)) { - player.condensed -= 8 * 2.2 ** (player.cupgs[3] || 0) - player.cupgs[3] = (player.cupgs[3] || 0) + 1 - } -}) -document.getElementById("cupg-4").addEventListener("click", () => { - if (player.condensed >= 15 * 3 ** (player.cupgs[4] || 0)) { - player.condensed -= 15 * 3 ** (player.cupgs[4] || 0) - player.cupgs[4] = (player.cupgs[4] || 0) + 1 - } -}) -document.getElementById("cupg-5").addEventListener("click", () => { - if (player.condensed >= 22.5 * 4 ** (player.cupgs[5] || 0)) { - player.condensed -= 22.5 * 4 ** (player.cupgs[5] || 0) - player.cupgs[5] = (player.cupgs[5] || 0) + 1 - } -}) - -// Updates & game state -function saveGame() { - localStorage.setItem("nif's-infinite-generators", btoa(JSON.stringify(player))) - return btoa(JSON.stringify(player)) -} -function loadGame(save = null) { - localStorage.setItem("nif's-infinite-generators", save) - local.dontSave = true - location.reload() -} - -document.getElementById("save-game").addEventListener("click", () => { - document.getElementById("save").innerText = saveGame() -}) -addEventListener("beforeunload", () => { - if (!local.dontSave) { saveGame() } -}) -document.getElementById("load-game").addEventListener("click", () => { - loadGame(document.getElementById("save").innerText) -}) -document.getElementById("wipe-game").addEventListener("click", () => { - if (event.getModifierState("Shift") || - window.confirm("Are you sure you want to wipe your save file? This action is irreversible!")) { - localStorage.setItem("nif's-infinite-generators", btoa("{}")) - local.dontSave = true - location.reload() - } -}) - -{ - let falsePlayer = JSON.parse(atob(localStorage.getItem("nif's-infinite-generators"))) - player = {...player, ...falsePlayer} - player.unlocked = {...player.unlocked, ...falsePlayer.unlocked} - player.cupgs = [...player.cupgs, ...falsePlayer.cupgs] - for (const i in player.gens) newGenerator(Number(i)) -} - -function updateVars() { - const dt = 0.05 - - player.points += getPointsPerSecond() * dt - for (let i = 0; i < player.gens.length - 1; i ++) { - player.egens[i] += getPointsPerSecond(i) * dt - } - - if (player.gens.length >= 25) player.unlocked.condensed = true - - if (player.abEnabled && player.unlocked.autobuyer) { - for (let k in player.abs) { - k = Number(k) - player.abs[k] += dt * Math.sqrt((player.cupgs[3] || 0) + 1) - let buys = Math.floor(player.abs / (2 * 3 ** k)) - player.abs[k] %= 2 * 3 ** k - for (let w = 0; w < buys; w ++) { - if (player.points >= (100 ** k * 10) * (1.5 ** player.gens[k])) { - player.points -= (100 ** k * 10) * (1.5 ** player.gens[k]) - player.gens[k] ++ - if (player.gens[k] == 10) { pushGenerator() } - } - } - } - } -} -function updateDisplay() { - document.getElementById("pts").innerText = player.points.toPrecision(3) - for (const i of local.updates) i(); - document.getElementById("cpts").innerText = player.condensed.toPrecision(3) - document.getElementById("c-gain").innerText = condensedGain().toPrecision(3) - if (player.unlocked.condensed) { - document.getElementById("condensed-btn").style = "display: inline;" - document.getElementById("condensed-points").style = "display: inline;" - } - if (player.unlocked.autobuyer) document.getElementById("ab-en-wrap").style = "display: inline;" - document.getElementById("theme").setAttribute("href", "themes/" + document.getElementById("theme-sel").value + ".css") - document.getElementById("ab-en").style = "color: var(--" + (player.abEnabled ? "good" : "bad") + ")" - document.getElementById("ab-en").innerText = (player.abEnabled ? "Enabled" : "Disabled") - - document.getElementById("cupg-0-eff").innerText = (1.2 ** (player.cupgs[0] || 0)).toPrecision(3) - document.getElementById("cupg-0-cost").innerText = (1.3 ** (player.cupgs[0] || 0)).toPrecision(3) - document.getElementById("cupg-1-eff").innerText = (2 ** (player.cupgs[1] || 0)).toPrecision(3) - document.getElementById("cupg-1-cost").innerText = (1.3 ** (player.cupgs[1] || 0)).toPrecision(3) - document.getElementById("cupg-2-eff").innerText = (player.cupgs[2] || 0).toPrecision(3) - document.getElementById("cupg-2-cost").innerText = (3 * 1.6 ** (player.cupgs[2] || 0)).toPrecision(3) - document.getElementById("cupg-3-eff").innerText = (Math.sqrt((player.cupgs[3] || 0) + 1)).toPrecision(3) - document.getElementById("cupg-3-cost").innerText = (8 * 2.2 ** (player.cupgs[3] || 0)).toPrecision(3) - document.getElementById("cupg-4-eff").innerText = (Math.sqrt((player.cupgs[4] || 0) + 1)).toPrecision(3) - document.getElementById("cupg-4-cost").innerText = (15 * 3 ** (player.cupgs[4] || 0)).toPrecision(3) - document.getElementById("cupg-5-eff").innerText = (Math.cbrt((player.cupgs[5] || 0) + 1)).toPrecision(3) - document.getElementById("cupg-5-cost").innerText = (22.5 * 4 ** (player.cupgs[5] || 0)).toPrecision(3) -} - -setInterval(function() { - updateVars() - updateDisplay() -}, 50) +// Player data +let player = { + points: 10, + condensed: 0, + gens: [0], + egens: [0], + cupgs: [0, 0, 0, 0], + abs: [], + unlocked: { + condensed: false, + autobuyer: false + }, + abEnabled: true + +} +let local = { + tab: "main-tab", + updates: [], + dontSave: false, +} + +// General event listeners +document.getElementById("main-tab-btn").addEventListener("click", () => { + document.getElementById(local.tab).style = "display: none;" + local.tab = "main-tab" + document.getElementById("main-tab").style = "display: block;" +}) +document.getElementById("condensed-btn").addEventListener("click", () => { + document.getElementById(local.tab).style = "display: none;" + local.tab = "condensed" + document.getElementById("condensed").style = "display: block;" +}) +document.getElementById("settings-btn").addEventListener("click", () => { + document.getElementById(local.tab).style = "display: none;" + local.tab = "settings" + document.getElementById("settings").style = "display: block;" +}) +document.getElementById("changelog-btn").addEventListener("click", () => { + document.getElementById(local.tab).style = "display: none;" + local.tab = "changelog" + document.getElementById("changelog").style = "display: block;" +}) + +// Generator functions +function getPointsPerSecond(gen = -1) { + if (gen > player.gens.length - 2) return 0 + return ((player.egens[gen+1] + player.gens[gen+1]) * (Math.sqrt(player.gens[gen+1] + 1) + ** Math.cbrt((player.cupgs[5] || 0) + 1))) * (2 ** (player.cupgs[1] || 0)) + ** (gen == -1 ? 1.2 ** (player.cupgs[0] || 0) : 1) +} +function pushGenerator() { + const index = player.gens.push(0) - 1 + player.egens.push(0) + newGenerator(index) +} +function newGenerator(index) { + const cost = document.createElement("span") + cost.innerText = ((100 ** index * 10) * (1.5 ** player.gens[index])).toPrecision(3) + const buyBtn = document.createElement("button") + buyBtn.addEventListener("click", () => { + if ((100 ** index * 10) * (1.5 ** player.gens[index]) !== Number.POSITIVE_INFINITY + && player.points >= (100 ** index * 10) * (1.5 ** player.gens[index])) { + player.points -= (100 ** index * 10) * (1.5 ** player.gens[index]) + player.gens[index] ++ + if (player.gens[index] == 10) { pushGenerator() } + } + }) + buyBtn.innerText = "Buy" + const buyMaxBtn = document.createElement("button") + buyMaxBtn.addEventListener("click", () => { + while ((100 ** index * 10) * (1.5 ** player.gens[index]) !== Number.POSITIVE_INFINITY + && player.points >= (100 ** index * 10) * (1.5 ** player.gens[index]) ) { + player.points -= (100 ** index * 10) * (1.5 ** player.gens[index]) + player.gens[index] ++ + if (player.gens[index] == 10) { pushGenerator() } + } + }) + buyMaxBtn.innerText = "Buy Max" + const extra = document.createElement("span") + extra.innerText = 0 + extra.id = "gen-" + index + "-extra" + const gens = document.createElement("span") + gens.innerText = 0 + gens.id = "gen-" + index + "-gens" + const gener = document.createElement("span") + gener.innerText = getPointsPerSecond(index-1).toPrecision(3) + gener.id = "gen-" + index + "-gener" + + const newRow = document.createElement("tr") + newRow.append( + (() => { + const el = document.createElement("td") + el.innerText = "Gen " + (index + 1) + return el + })(), + (() => { + const el = document.createElement("td") + el.append(gens, " + ", extra) + return el + })(), + (() => { + const el = document.createElement("td") + el.append(gener, "/s") + return el + })(), + (() => { + const el = document.createElement("td") + cost.id = "gen-" + index + "-cost" + el.append(cost, " points") + return el + })(), + (() => { + const el = document.createElement("td") + el.append(buyBtn, " ", buyMaxBtn) + return el + })(), + ) + if (index % 2 == 0) newRow.className = "alt" + document.getElementById("gens").appendChild(newRow) + + local.updates.push(function() { + extra.innerText = player.egens[index].toPrecision(3) + gens.innerText = player.gens[index].toPrecision(3) + gener.innerText = getPointsPerSecond(index-1).toPrecision(3) + cost.innerText = ((100 ** index * 10) * (1.5 ** player.gens[index])).toPrecision(3) + }) +} + +// Condensed functions +function condensedGain() { + return Math.sqrt(Math.max(0, player.gens.length - 24)) ** Math.sqrt((player.cupgs[4] || 0) + 1) +} +document.getElementById("cond-btn").addEventListener("click", () => { + if (condensedGain() == 0) return + player.condensed += condensedGain() + player.gens = [0] + player.egens = [0] + local.updates = [] + player.points = 10 +}) +document.getElementById("cupg-0").addEventListener("click", () => { + if (player.condensed >= 1.3 ** (player.cupgs[0] || 0)) { + player.condensed -= 1.3 ** (player.cupgs[0] || 0) + player.cupgs[0] = (player.cupgs[0] || 0) + 1 + } +}) +document.getElementById("cupg-1").addEventListener("click", () => { + if (player.condensed >= 1.3 ** (player.cupgs[1] || 0)) { + player.condensed -= 1.3 ** (player.cupgs[1] || 0) + player.cupgs[1] = (player.cupgs[1] || 0) + 1 + } +}) +document.getElementById("cupg-2").addEventListener("click", () => { + if (player.condensed >= 3 * 1.6 ** (player.cupgs[2] || 0)) { + player.condensed -= 3 * 1.6 ** (player.cupgs[2] || 0) + player.cupgs[2] = (player.cupgs[2] || 0) + 1 + player.abs.push(0) + player.unlocked.autobuyer = true + } +}) +document.getElementById("ab-en-btn").addEventListener("click", () => { + if (player.unlocked.autobuyer) player.abEnabled = !player.abEnabled +}) +document.getElementById("cupg-3").addEventListener("click", () => { + if (player.condensed >= 8 * 2.2 ** (player.cupgs[3] || 0)) { + player.condensed -= 8 * 2.2 ** (player.cupgs[3] || 0) + player.cupgs[3] = (player.cupgs[3] || 0) + 1 + } +}) +document.getElementById("cupg-4").addEventListener("click", () => { + if (player.condensed >= 15 * 3 ** (player.cupgs[4] || 0)) { + player.condensed -= 15 * 3 ** (player.cupgs[4] || 0) + player.cupgs[4] = (player.cupgs[4] || 0) + 1 + } +}) +document.getElementById("cupg-5").addEventListener("click", () => { + if (player.condensed >= 22.5 * 4 ** (player.cupgs[5] || 0)) { + player.condensed -= 22.5 * 4 ** (player.cupgs[5] || 0) + player.cupgs[5] = (player.cupgs[5] || 0) + 1 + } +}) + +// Updates & game state +function saveGame() { + localStorage.setItem("nif's-infinite-generators", btoa(JSON.stringify(player))) + return btoa(JSON.stringify(player)) +} +function loadGame(save = null) { + localStorage.setItem("nif's-infinite-generators", save) + local.dontSave = true + location.reload() +} + +document.getElementById("save-game").addEventListener("click", () => { + document.getElementById("save").innerText = saveGame() +}) +addEventListener("beforeunload", () => { + if (!local.dontSave) { saveGame() } +}) +document.getElementById("load-game").addEventListener("click", () => { + loadGame(document.getElementById("save").innerText) +}) +document.getElementById("wipe-game").addEventListener("click", () => { + if (event.getModifierState("Shift") || + window.confirm("Are you sure you want to wipe your save file? This action is irreversible!")) { + localStorage.setItem("nif's-infinite-generators", btoa("{}")) + local.dontSave = true + location.reload() + } +}) + +try { + let falsePlayer = JSON.parse(atob(localStorage.getItem("nif's-infinite-generators"))) + player = {...player, ...falsePlayer} + if (falsePlayer.unlocked) player.unlocked = {...player.unlocked, ...falsePlayer.unlocked} + if (falsePlayer.cupgs) player.cupgs = [...player.cupgs, ...falsePlayer.cupgs] + for (const i in player.gens) newGenerator(Number(i)) +} catch (TypeError) {} + +function updateVars() { + const dt = 0.05 + + player.points += getPointsPerSecond() * dt + for (let i = 0; i < player.gens.length - 1; i ++) { + player.egens[i] += getPointsPerSecond(i) * dt + } + + if (player.gens.length >= 25) player.unlocked.condensed = true + + if (player.abEnabled && player.unlocked.autobuyer) { + for (let k in player.abs) { + k = Number(k) + player.abs[k] += dt * Math.sqrt((player.cupgs[3] || 0) + 1) + let buys = Math.floor(player.abs / (2 * 3 ** k)) + player.abs[k] %= 2 * 3 ** k + for (let w = 0; w < buys; w ++) { + if (player.points >= (100 ** k * 10) * (1.5 ** player.gens[k])) { + player.points -= (100 ** k * 10) * (1.5 ** player.gens[k]) + player.gens[k] ++ + if (player.gens[k] == 10) { pushGenerator() } + } + } + } + } +} +function updateDisplay() { + document.getElementById("pts").innerText = player.points.toPrecision(3) + for (const i of local.updates) i(); + document.getElementById("cpts").innerText = player.condensed.toPrecision(3) + document.getElementById("c-gain").innerText = condensedGain().toPrecision(3) + if (player.unlocked.condensed) { + document.getElementById("condensed-btn").style = "display: inline;" + document.getElementById("condensed-points").style = "display: inline;" + } + if (player.unlocked.autobuyer) document.getElementById("ab-en-wrap").style = "display: inline;" + document.getElementById("theme").setAttribute("href", "themes/" + document.getElementById("theme-sel").value + ".css") + document.getElementById("ab-en").style = "color: var(--" + (player.abEnabled ? "good" : "bad") + ")" + document.getElementById("ab-en").innerText = (player.abEnabled ? "Enabled" : "Disabled") + + document.getElementById("cupg-0-eff").innerText = (1.2 ** (player.cupgs[0] || 0)).toPrecision(3) + document.getElementById("cupg-0-cost").innerText = (1.3 ** (player.cupgs[0] || 0)).toPrecision(3) + document.getElementById("cupg-1-eff").innerText = (2 ** (player.cupgs[1] || 0)).toPrecision(3) + document.getElementById("cupg-1-cost").innerText = (1.3 ** (player.cupgs[1] || 0)).toPrecision(3) + document.getElementById("cupg-2-eff").innerText = (player.cupgs[2] || 0).toPrecision(3) + document.getElementById("cupg-2-cost").innerText = (3 * 1.6 ** (player.cupgs[2] || 0)).toPrecision(3) + document.getElementById("cupg-3-eff").innerText = (Math.sqrt((player.cupgs[3] || 0) + 1)).toPrecision(3) + document.getElementById("cupg-3-cost").innerText = (8 * 2.2 ** (player.cupgs[3] || 0)).toPrecision(3) + document.getElementById("cupg-4-eff").innerText = (Math.sqrt((player.cupgs[4] || 0) + 1)).toPrecision(3) + document.getElementById("cupg-4-cost").innerText = (15 * 3 ** (player.cupgs[4] || 0)).toPrecision(3) + document.getElementById("cupg-5-eff").innerText = (Math.cbrt((player.cupgs[5] || 0) + 1)).toPrecision(3) + document.getElementById("cupg-5-cost").innerText = (22.5 * 4 ** (player.cupgs[5] || 0)).toPrecision(3) +} + +setInterval(function() { + updateVars() + updateDisplay() +}, 50) setInterval(save, 60000) \ No newline at end of file