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 <nif@incremental.social>
This commit is contained in:
Nif 2024-04-18 17:56:20 +00:00
parent 9ca5fb3399
commit ed8777f56f

554
main.js
View file

@ -1,278 +1,278 @@
// Player data // Player data
let player = { let player = {
points: 10, points: 10,
condensed: 0, condensed: 0,
gens: [0], gens: [0],
egens: [0], egens: [0],
cupgs: [0, 0, 0, 0], cupgs: [0, 0, 0, 0],
abs: [], abs: [],
unlocked: { unlocked: {
condensed: false, condensed: false,
autobuyer: false autobuyer: false
}, },
abEnabled: true abEnabled: true
} }
let local = { let local = {
tab: "main-tab", tab: "main-tab",
updates: [], updates: [],
dontSave: false, dontSave: false,
} }
// General event listeners // General event listeners
document.getElementById("main-tab-btn").addEventListener("click", () => { document.getElementById("main-tab-btn").addEventListener("click", () => {
document.getElementById(local.tab).style = "display: none;" document.getElementById(local.tab).style = "display: none;"
local.tab = "main-tab" local.tab = "main-tab"
document.getElementById("main-tab").style = "display: block;" document.getElementById("main-tab").style = "display: block;"
}) })
document.getElementById("condensed-btn").addEventListener("click", () => { document.getElementById("condensed-btn").addEventListener("click", () => {
document.getElementById(local.tab).style = "display: none;" document.getElementById(local.tab).style = "display: none;"
local.tab = "condensed" local.tab = "condensed"
document.getElementById("condensed").style = "display: block;" document.getElementById("condensed").style = "display: block;"
}) })
document.getElementById("settings-btn").addEventListener("click", () => { document.getElementById("settings-btn").addEventListener("click", () => {
document.getElementById(local.tab).style = "display: none;" document.getElementById(local.tab).style = "display: none;"
local.tab = "settings" local.tab = "settings"
document.getElementById("settings").style = "display: block;" document.getElementById("settings").style = "display: block;"
}) })
document.getElementById("changelog-btn").addEventListener("click", () => { document.getElementById("changelog-btn").addEventListener("click", () => {
document.getElementById(local.tab).style = "display: none;" document.getElementById(local.tab).style = "display: none;"
local.tab = "changelog" local.tab = "changelog"
document.getElementById("changelog").style = "display: block;" document.getElementById("changelog").style = "display: block;"
}) })
// Generator functions // Generator functions
function getPointsPerSecond(gen = -1) { function getPointsPerSecond(gen = -1) {
if (gen > player.gens.length - 2) return 0 if (gen > player.gens.length - 2) return 0
return ((player.egens[gen+1] + player.gens[gen+1]) * (Math.sqrt(player.gens[gen+1] + 1) 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)) ** Math.cbrt((player.cupgs[5] || 0) + 1))) * (2 ** (player.cupgs[1] || 0))
** (gen == -1 ? 1.2 ** (player.cupgs[0] || 0) : 1) ** (gen == -1 ? 1.2 ** (player.cupgs[0] || 0) : 1)
} }
function pushGenerator() { function pushGenerator() {
const index = player.gens.push(0) - 1 const index = player.gens.push(0) - 1
player.egens.push(0) player.egens.push(0)
newGenerator(index) newGenerator(index)
} }
function newGenerator(index) { function newGenerator(index) {
const cost = document.createElement("span") const cost = document.createElement("span")
cost.innerText = ((100 ** index * 10) * (1.5 ** player.gens[index])).toPrecision(3) cost.innerText = ((100 ** index * 10) * (1.5 ** player.gens[index])).toPrecision(3)
const buyBtn = document.createElement("button") const buyBtn = document.createElement("button")
buyBtn.addEventListener("click", () => { buyBtn.addEventListener("click", () => {
if ((100 ** index * 10) * (1.5 ** player.gens[index]) !== Number.POSITIVE_INFINITY 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.points -= (100 ** index * 10) * (1.5 ** player.gens[index]) player.points -= (100 ** index * 10) * (1.5 ** player.gens[index])
player.gens[index] ++ player.gens[index] ++
if (player.gens[index] == 10) { pushGenerator() } if (player.gens[index] == 10) { pushGenerator() }
} }
}) })
buyBtn.innerText = "Buy" buyBtn.innerText = "Buy"
const buyMaxBtn = document.createElement("button") const buyMaxBtn = document.createElement("button")
buyMaxBtn.addEventListener("click", () => { buyMaxBtn.addEventListener("click", () => {
while ((100 ** index * 10) * (1.5 ** player.gens[index]) !== Number.POSITIVE_INFINITY 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.points -= (100 ** index * 10) * (1.5 ** player.gens[index]) player.points -= (100 ** index * 10) * (1.5 ** player.gens[index])
player.gens[index] ++ player.gens[index] ++
if (player.gens[index] == 10) { pushGenerator() } if (player.gens[index] == 10) { pushGenerator() }
} }
}) })
buyMaxBtn.innerText = "Buy Max" buyMaxBtn.innerText = "Buy Max"
const extra = document.createElement("span") const extra = document.createElement("span")
extra.innerText = 0 extra.innerText = 0
extra.id = "gen-" + index + "-extra" extra.id = "gen-" + index + "-extra"
const gens = document.createElement("span") const gens = document.createElement("span")
gens.innerText = 0 gens.innerText = 0
gens.id = "gen-" + index + "-gens" gens.id = "gen-" + index + "-gens"
const gener = document.createElement("span") const gener = document.createElement("span")
gener.innerText = getPointsPerSecond(index-1).toPrecision(3) gener.innerText = getPointsPerSecond(index-1).toPrecision(3)
gener.id = "gen-" + index + "-gener" gener.id = "gen-" + index + "-gener"
const newRow = document.createElement("tr") const newRow = document.createElement("tr")
newRow.append( newRow.append(
(() => { (() => {
const el = document.createElement("td") const el = document.createElement("td")
el.innerText = "Gen " + (index + 1) el.innerText = "Gen " + (index + 1)
return el return el
})(), })(),
(() => { (() => {
const el = document.createElement("td") const el = document.createElement("td")
el.append(gens, " + ", extra) el.append(gens, " + ", extra)
return el return el
})(), })(),
(() => { (() => {
const el = document.createElement("td") const el = document.createElement("td")
el.append(gener, "/s") el.append(gener, "/s")
return el return el
})(), })(),
(() => { (() => {
const el = document.createElement("td") const el = document.createElement("td")
cost.id = "gen-" + index + "-cost" cost.id = "gen-" + index + "-cost"
el.append(cost, " points") el.append(cost, " points")
return el return el
})(), })(),
(() => { (() => {
const el = document.createElement("td") const el = document.createElement("td")
el.append(buyBtn, " ", buyMaxBtn) el.append(buyBtn, " ", buyMaxBtn)
return el return el
})(), })(),
) )
if (index % 2 == 0) newRow.className = "alt" if (index % 2 == 0) newRow.className = "alt"
document.getElementById("gens").appendChild(newRow) document.getElementById("gens").appendChild(newRow)
local.updates.push(function() { local.updates.push(function() {
extra.innerText = player.egens[index].toPrecision(3) extra.innerText = player.egens[index].toPrecision(3)
gens.innerText = player.gens[index].toPrecision(3) gens.innerText = player.gens[index].toPrecision(3)
gener.innerText = getPointsPerSecond(index-1).toPrecision(3) gener.innerText = getPointsPerSecond(index-1).toPrecision(3)
cost.innerText = ((100 ** index * 10) * (1.5 ** player.gens[index])).toPrecision(3) cost.innerText = ((100 ** index * 10) * (1.5 ** player.gens[index])).toPrecision(3)
}) })
} }
// Condensed functions // Condensed functions
function condensedGain() { function condensedGain() {
return Math.sqrt(Math.max(0, player.gens.length - 24)) ** Math.sqrt((player.cupgs[4] || 0) + 1) return Math.sqrt(Math.max(0, player.gens.length - 24)) ** Math.sqrt((player.cupgs[4] || 0) + 1)
} }
document.getElementById("cond-btn").addEventListener("click", () => { document.getElementById("cond-btn").addEventListener("click", () => {
if (condensedGain() == 0) return if (condensedGain() == 0) return
player.condensed += condensedGain() player.condensed += condensedGain()
player.gens = [0] player.gens = [0]
player.egens = [0] player.egens = [0]
local.updates = [] local.updates = []
player.points = 10 player.points = 10
}) })
document.getElementById("cupg-0").addEventListener("click", () => { document.getElementById("cupg-0").addEventListener("click", () => {
if (player.condensed >= 1.3 ** (player.cupgs[0] || 0)) { if (player.condensed >= 1.3 ** (player.cupgs[0] || 0)) {
player.condensed -= 1.3 ** (player.cupgs[0] || 0) player.condensed -= 1.3 ** (player.cupgs[0] || 0)
player.cupgs[0] = (player.cupgs[0] || 0) + 1 player.cupgs[0] = (player.cupgs[0] || 0) + 1
} }
}) })
document.getElementById("cupg-1").addEventListener("click", () => { document.getElementById("cupg-1").addEventListener("click", () => {
if (player.condensed >= 1.3 ** (player.cupgs[1] || 0)) { if (player.condensed >= 1.3 ** (player.cupgs[1] || 0)) {
player.condensed -= 1.3 ** (player.cupgs[1] || 0) player.condensed -= 1.3 ** (player.cupgs[1] || 0)
player.cupgs[1] = (player.cupgs[1] || 0) + 1 player.cupgs[1] = (player.cupgs[1] || 0) + 1
} }
}) })
document.getElementById("cupg-2").addEventListener("click", () => { document.getElementById("cupg-2").addEventListener("click", () => {
if (player.condensed >= 3 * 1.6 ** (player.cupgs[2] || 0)) { if (player.condensed >= 3 * 1.6 ** (player.cupgs[2] || 0)) {
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.cupgs[2] = (player.cupgs[2] || 0) + 1
player.abs.push(0) player.abs.push(0)
player.unlocked.autobuyer = true player.unlocked.autobuyer = true
} }
}) })
document.getElementById("ab-en-btn").addEventListener("click", () => { document.getElementById("ab-en-btn").addEventListener("click", () => {
if (player.unlocked.autobuyer) player.abEnabled = !player.abEnabled if (player.unlocked.autobuyer) player.abEnabled = !player.abEnabled
}) })
document.getElementById("cupg-3").addEventListener("click", () => { document.getElementById("cupg-3").addEventListener("click", () => {
if (player.condensed >= 8 * 2.2 ** (player.cupgs[3] || 0)) { if (player.condensed >= 8 * 2.2 ** (player.cupgs[3] || 0)) {
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 player.cupgs[3] = (player.cupgs[3] || 0) + 1
} }
}) })
document.getElementById("cupg-4").addEventListener("click", () => { document.getElementById("cupg-4").addEventListener("click", () => {
if (player.condensed >= 15 * 3 ** (player.cupgs[4] || 0)) { if (player.condensed >= 15 * 3 ** (player.cupgs[4] || 0)) {
player.condensed -= 15 * 3 ** (player.cupgs[4] || 0) player.condensed -= 15 * 3 ** (player.cupgs[4] || 0)
player.cupgs[4] = (player.cupgs[4] || 0) + 1 player.cupgs[4] = (player.cupgs[4] || 0) + 1
} }
}) })
document.getElementById("cupg-5").addEventListener("click", () => { document.getElementById("cupg-5").addEventListener("click", () => {
if (player.condensed >= 22.5 * 4 ** (player.cupgs[5] || 0)) { if (player.condensed >= 22.5 * 4 ** (player.cupgs[5] || 0)) {
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 player.cupgs[5] = (player.cupgs[5] || 0) + 1
} }
}) })
// Updates & game state // Updates & game state
function saveGame() { function saveGame() {
localStorage.setItem("nif's-infinite-generators", btoa(JSON.stringify(player))) localStorage.setItem("nif's-infinite-generators", btoa(JSON.stringify(player)))
return btoa(JSON.stringify(player)) return btoa(JSON.stringify(player))
} }
function loadGame(save = null) { function loadGame(save = null) {
localStorage.setItem("nif's-infinite-generators", save) localStorage.setItem("nif's-infinite-generators", save)
local.dontSave = true local.dontSave = true
location.reload() location.reload()
} }
document.getElementById("save-game").addEventListener("click", () => { document.getElementById("save-game").addEventListener("click", () => {
document.getElementById("save").innerText = saveGame() document.getElementById("save").innerText = saveGame()
}) })
addEventListener("beforeunload", () => { addEventListener("beforeunload", () => {
if (!local.dontSave) { saveGame() } if (!local.dontSave) { saveGame() }
}) })
document.getElementById("load-game").addEventListener("click", () => { document.getElementById("load-game").addEventListener("click", () => {
loadGame(document.getElementById("save").innerText) loadGame(document.getElementById("save").innerText)
}) })
document.getElementById("wipe-game").addEventListener("click", () => { document.getElementById("wipe-game").addEventListener("click", () => {
if (event.getModifierState("Shift") || if (event.getModifierState("Shift") ||
window.confirm("Are you sure you want to wipe your save file? This action is irreversible!")) { window.confirm("Are you sure you want to wipe your save file? This action is irreversible!")) {
localStorage.setItem("nif's-infinite-generators", btoa("{}")) localStorage.setItem("nif's-infinite-generators", btoa("{}"))
local.dontSave = true local.dontSave = true
location.reload() location.reload()
} }
}) })
{ try {
let falsePlayer = JSON.parse(atob(localStorage.getItem("nif's-infinite-generators"))) let falsePlayer = JSON.parse(atob(localStorage.getItem("nif's-infinite-generators")))
player = {...player, ...falsePlayer} player = {...player, ...falsePlayer}
player.unlocked = {...player.unlocked, ...falsePlayer.unlocked} if (falsePlayer.unlocked) player.unlocked = {...player.unlocked, ...falsePlayer.unlocked}
player.cupgs = [...player.cupgs, ...falsePlayer.cupgs] if (falsePlayer.cupgs) player.cupgs = [...player.cupgs, ...falsePlayer.cupgs]
for (const i in player.gens) newGenerator(Number(i)) for (const i in player.gens) newGenerator(Number(i))
} } catch (TypeError) {}
function updateVars() { function updateVars() {
const dt = 0.05 const dt = 0.05
player.points += getPointsPerSecond() * dt player.points += getPointsPerSecond() * dt
for (let i = 0; i < player.gens.length - 1; i ++) { for (let i = 0; i < player.gens.length - 1; i ++) {
player.egens[i] += getPointsPerSecond(i) * dt player.egens[i] += getPointsPerSecond(i) * dt
} }
if (player.gens.length >= 25) player.unlocked.condensed = true if (player.gens.length >= 25) player.unlocked.condensed = true
if (player.abEnabled && player.unlocked.autobuyer) { if (player.abEnabled && player.unlocked.autobuyer) {
for (let k in player.abs) { for (let k in player.abs) {
k = Number(k) k = Number(k)
player.abs[k] += dt * Math.sqrt((player.cupgs[3] || 0) + 1) player.abs[k] += dt * Math.sqrt((player.cupgs[3] || 0) + 1)
let buys = Math.floor(player.abs / (2 * 3 ** k)) let buys = Math.floor(player.abs / (2 * 3 ** k))
player.abs[k] %= 2 * 3 ** k player.abs[k] %= 2 * 3 ** k
for (let w = 0; w < buys; w ++) { for (let w = 0; w < buys; w ++) {
if (player.points >= (100 ** k * 10) * (1.5 ** player.gens[k])) { if (player.points >= (100 ** k * 10) * (1.5 ** player.gens[k])) {
player.points -= (100 ** k * 10) * (1.5 ** player.gens[k]) player.points -= (100 ** k * 10) * (1.5 ** player.gens[k])
player.gens[k] ++ player.gens[k] ++
if (player.gens[k] == 10) { pushGenerator() } if (player.gens[k] == 10) { pushGenerator() }
} }
} }
} }
} }
} }
function updateDisplay() { function updateDisplay() {
document.getElementById("pts").innerText = player.points.toPrecision(3) document.getElementById("pts").innerText = player.points.toPrecision(3)
for (const i of local.updates) i(); for (const i of local.updates) i();
document.getElementById("cpts").innerText = player.condensed.toPrecision(3) document.getElementById("cpts").innerText = player.condensed.toPrecision(3)
document.getElementById("c-gain").innerText = condensedGain().toPrecision(3) document.getElementById("c-gain").innerText = condensedGain().toPrecision(3)
if (player.unlocked.condensed) { if (player.unlocked.condensed) {
document.getElementById("condensed-btn").style = "display: inline;" document.getElementById("condensed-btn").style = "display: inline;"
document.getElementById("condensed-points").style = "display: inline;" document.getElementById("condensed-points").style = "display: inline;"
} }
if (player.unlocked.autobuyer) document.getElementById("ab-en-wrap").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("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").style = "color: var(--" + (player.abEnabled ? "good" : "bad") + ")"
document.getElementById("ab-en").innerText = (player.abEnabled ? "Enabled" : "Disabled") 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-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-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-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-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-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-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-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-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-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-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-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) document.getElementById("cupg-5-cost").innerText = (22.5 * 4 ** (player.cupgs[5] || 0)).toPrecision(3)
} }
setInterval(function() { setInterval(function() {
updateVars() updateVars()
updateDisplay() updateDisplay()
}, 50) }, 50)
setInterval(save, 60000) setInterval(save, 60000)