diff --git a/package.json b/package.json index 89a92b604..6d2daf917 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "eventemitter3": "^4.0.7", "fast-mersenne-twister": "^1.0.3", "i18next": "^22.4.9", - "localforage": "^1.10.0", "lodash.clonedeepwith": "^4.5.0", "lz-string": "^1.4.4", "worker-timers": "^7.0.53", @@ -40,7 +39,7 @@ "csslint:fix": "npx stylelint Synergism.css --fix", "htmllint": "npx htmlhint index.html", "build:esbuild": "npx esbuild src/Synergism.ts --bundle --minify --target=\"chrome58,firefox57,safari11,edge29\" --outfile=\"./dist/out.js\"", - "watch:esbuild": "npx esbuild src/Synergism.ts --bundle --minify --target=\"chrome58,firefox57,safari11,edge29\" --outfile=\"./dist/out.js\" --watch", + "watch:esbuild": "npx esbuild src/Synergism.ts --bundle --minify --target=\"chrome58,firefox57,safari11,edge29\" --outfile=\"./dist/out.js\" --watch --sourcemap", "check:tsc": "npx -p typescript tsc", "watch:tsc": "npx -p typescript tsc --watch", "prepare": "husky install", diff --git a/src/Calculate.ts b/src/Calculate.ts index ae8f454d1..1c3abe714 100644 --- a/src/Calculate.ts +++ b/src/Calculate.ts @@ -1360,7 +1360,7 @@ export const timeWarp = async () => { DOMCacheGetOrSet('offlineContainer').style.display = 'flex' DOMCacheGetOrSet('offlineBlur').style.display = '' - await calculateOffline(timeUse) + calculateOffline(timeUse) } export const calculateOffline = async (forceTime = 0) => { @@ -1565,7 +1565,7 @@ export const calculateOffline = async (forceTime = 0) => { player.loadedNov13Vers = true } - await saveSynergy() + saveSynergy() updateTalismanInventory() calculateObtainium() diff --git a/src/Corruptions.ts b/src/Corruptions.ts index 8c524a201..0411e5414 100644 --- a/src/Corruptions.ts +++ b/src/Corruptions.ts @@ -1,5 +1,5 @@ import i18next from 'i18next' -import { DOMCacheGetOrSet } from './Cache/DOM' +import { DOMCacheGetOrSet, DOMCacheHas } from './Cache/DOM' import { PCoinUpgradeEffects } from './PseudoCoinUpgrades' import { format, player } from './Synergism' import { IconSets } from './Themes' @@ -141,6 +141,8 @@ export const corruptionDisplay = (index: number) => { export const corruptionStatsUpdate = () => { for (let i = 2; i <= 9; i++) { // https://discord.com/channels/677271830838640680/706329553639047241/841749032841379901 + if (!DOMCacheHas(`corrCurrent${i}`)) continue + const a = DOMCacheGetOrSet(`corrCurrent${i}`) const b = DOMCacheGetOrSet(`corrNext${i}`) a.textContent = format(player.usedCorruptions[i]) @@ -219,7 +221,6 @@ export const corruptionLoadoutTableCreate = () => { const row = table.insertRow() for (let j = 0; j <= corrCount; j++) { const cell = row.insertCell() - cell.className = `test${j}` if (j === 0) { // First column if (i === 0) { // First row cell.textContent = i18next.t('corruptions.loadoutTable.next') @@ -272,6 +273,9 @@ export const corruptionLoadoutTableCreate = () => { cell.appendChild(btn) } } + + updateCorruptionLoadoutNames() + corruptionStatsUpdate() } export const corruptionLoadoutTableUpdate = (updateRow = 0) => { diff --git a/src/EventListeners.ts b/src/EventListeners.ts index 0c7634fd0..be56349ae 100644 --- a/src/EventListeners.ts +++ b/src/EventListeners.ts @@ -1084,7 +1084,7 @@ TODO: Fix this entire tab it's utter shit DOMCacheGetOrSet('getBlueberries').addEventListener('click', () => exportBlueberryTree()) DOMCacheGetOrSet('refundBlueberries').addEventListener('click', () => resetBlueberryTree()) // Import blueberries - DOMCacheGetOrSet('importBlueberries').addEventListener('change', async (e) => importData(e, importBlueberryTree)) + DOMCacheGetOrSet('importBlueberries').addEventListener('change', (e) => importData(e, importBlueberryTree)) // Toggle subtabs of Singularity tab for (let index = 0; index < 4; index++) { @@ -1098,7 +1098,7 @@ TODO: Fix this entire tab it's utter shit DOMCacheGetOrSet('unsmith').addEventListener('click', () => clickSmith()) // Import button - DOMCacheGetOrSet('importfile').addEventListener('change', async (e) => importData(e, importSynergism)) + DOMCacheGetOrSet('importfile').addEventListener('change', (e) => importData(e, importSynergism)) for (let i = 1; i <= 5; i++) { DOMCacheGetOrSet(`switchTheme${i}`).addEventListener('click', () => toggleTheme(false, i, true)) diff --git a/src/ImportExport.ts b/src/ImportExport.ts index 1a0ff336b..6ae29aea5 100644 --- a/src/ImportExport.ts +++ b/src/ImportExport.ts @@ -1,6 +1,5 @@ import ClipboardJS from 'clipboard' import i18next from 'i18next' -import localforage from 'localforage' import LZString from 'lz-string' import { achievementaward } from './Achievements' import { DOMCacheGetOrSet } from './Cache/DOM' @@ -15,7 +14,7 @@ import { playerJsonSchema } from './saves/PlayerJsonSchema' import { shopData } from './Shop' import { singularityData } from './singularity' import { synergismStage } from './Statistics' -import { blankSave, format, player, reloadShit, saveCheck, saveSynergy } from './Synergism' +import { blankSave, format, player, reloadShit, saveSynergy } from './Synergism' import { changeSubTab, changeTab, Tabs } from './Tabs' import type { Player } from './types/Synergism' import { Alert, Confirm, Prompt } from './UpdateHTML' @@ -248,8 +247,7 @@ export const exportSynergism = async ( : 1 if (+player.singularityUpgrades.goldenQuarks3.getEffect().bonus > 0) { player.goldenQuarks += Math.floor( - player.goldenQuarksTimer - / (3600 / +player.singularityUpgrades.goldenQuarks3.getEffect().bonus) + player.goldenQuarksTimer / (3600 / +player.singularityUpgrades.goldenQuarks3.getEffect().bonus) ) * bonusGQMultiplier player.goldenQuarksTimer = player.goldenQuarksTimer % (3600 / +player.singularityUpgrades.goldenQuarks3.getEffect().bonus) @@ -260,17 +258,13 @@ export const exportSynergism = async ( } } - const saved = await saveSynergy() - - if (!saved) { + if (!saveSynergy()) { return } - const save = (await localforage.getItem('Synergysave2')) - ?? localStorage.getItem('Synergysave2') - const saveString = typeof save === 'string' ? save : await save?.text() + const saveString = localStorage.getItem('Synergysave2') - if (saveString === undefined) { + if (!saveString) { return Alert('How?') } @@ -308,12 +302,12 @@ export const resetGame = async (force = true) => { changeSubTab(Tabs.Singularity, { page: 0 }) // set 'singularity main' changeSubTab(Tabs.Settings, { page: 0 }) // set 'statistics main' // Import Game - await importSynergism(btoa(JSON.stringify(hold.data)), true) + importSynergism(btoa(JSON.stringify(hold.data)), true) } export const importData = async ( e: Event, - importFunc: (save: string | null) => Promise | Promise + importFunc: (save: string | null) => void ) => { const element = e.target as HTMLInputElement const file = element.files![0] @@ -338,7 +332,7 @@ export const importData = async ( return importFunc(save) } -export const importSynergism = async (input: string | null, reset = false) => { +export const importSynergism = (input: string | null, reset = false) => { if (typeof input !== 'string') { return Alert(i18next.t('importexport.unableImport')) } @@ -358,13 +352,9 @@ export const importSynergism = async (input: string | null, reset = false) => { return Alert(i18next.t('importexport.unableImport')) } - saveCheck.canSave = false - const item = new Blob([saveString], { type: 'text/plain' }) localStorage.setItem('Synergysave2', saveString) - await localforage.setItem('Synergysave2', item) - await reloadShit(reset) - saveCheck.canSave = true + reloadShit(reset) return } else { return Alert(i18next.t('importexport.loadTestInLive')) @@ -975,9 +965,7 @@ export const promocodes = async (input: string | null, amount?: number) => { el.textContent = i18next.t('importexport.promocodes.invalid') } - const saved = await saveSynergy() // should fix refresh bug where you can continuously enter promocodes - - if (!saved) { + if (!saveSynergy()) { return } diff --git a/src/Login.ts b/src/Login.ts index 2e798e80c..d15f8914e 100644 --- a/src/Login.ts +++ b/src/Login.ts @@ -1,7 +1,6 @@ /// import i18next from 'i18next' -import localforage from 'localforage' import { DOMCacheGetOrSet } from './Cache/DOM' import { importSynergism } from './ImportExport' import { QuarkHandler, setQuarkBonus } from './Quark' @@ -259,9 +258,7 @@ async function logout () { } async function saveToCloud () { - const save = (await localforage.getItem('Synergysave2') - .then((b) => b?.text()) - .catch(() => null)) ?? localStorage.getItem('Synergysave2') + const save = localStorage.getItem('Synergysave2') if (typeof save !== 'string') { console.log('Yeah, no save here.') @@ -286,7 +283,7 @@ async function getCloudSave () { const response = await fetch('https://synergism.cc/api/v1/saves/get') const save = await response.json() as CloudSave - await importSynergism(save?.save ?? null) + importSynergism(save?.save ?? null) } const hasCaptcha = new WeakSet() diff --git a/src/Reset.ts b/src/Reset.ts index 228cf0d6c..14160988d 100644 --- a/src/Reset.ts +++ b/src/Reset.ts @@ -1119,6 +1119,7 @@ export const updateSingularityGlobalPerks = () => { } } +// TODO(@KhafraDev): find way to make this sync export const singularity = async (setSingNumber = -1): Promise => { if (player.runelevels[6] === 0 && setSingNumber === -1) { return Alert( @@ -1356,7 +1357,7 @@ export const singularity = async (setSingNumber = -1): Promise => { const saveCode47 = player.codes.get(47) ?? false const saveCode48 = player.codes.get(48) ?? false - await importSynergism(btoa(JSON.stringify(hold)), true) + importSynergism(btoa(JSON.stringify(hold)), true) // Techically possible to import game during reset. But that will only "hurt" that imported save // TODO: Do not enable data that has never used an event code @@ -1376,7 +1377,7 @@ export const singularity = async (setSingNumber = -1): Promise => { player.promoCodeTiming.time = Date.now() // Save again at the end of singularity reset - void saveSynergy() + saveSynergy() } const resetUpgrades = (i: number) => { diff --git a/src/Synergism.ts b/src/Synergism.ts index 7405ecece..7d29685c3 100644 --- a/src/Synergism.ts +++ b/src/Synergism.ts @@ -137,7 +137,6 @@ import { // import { LegacyShopUpgrades } from './types/LegacySynergism'; import i18next from 'i18next' -import localforage from 'localforage' import { BlueberryUpgrade, blueberryUpgradeData, @@ -182,7 +181,6 @@ import { import { changeSubTab, changeTab, Tabs } from './Tabs' import { settingAnnotation, toggleIconSet, toggleTheme } from './Themes' import { clearTimeout, clearTimers, setInterval, setTimeout } from './Timers' -import type { PlayerSave } from './types/LegacySynergism' export const player: Player = { firstPlayed: new Date().toISOString(), @@ -1561,13 +1559,7 @@ export const blankSave = Object.assign({}, player, { codes: new Map(Array.from({ length: 48 }, (_, i) => [i + 1, false])) }) -// The main cause of the double singularity bug was caused by a race condition -// when the game was saving just as the user was entering a Singularity. To fix -// this, hopefully, we disable saving the game when in the prompt or currently -// entering a Singularity. -export const saveCheck = { canSave: true } - -export const saveSynergy = async (button?: boolean): Promise => { +export const saveSynergy = (button?: boolean) => { player.offlinetick = Date.now() player.loaded1009 = true player.loaded1009hotfix1 = true @@ -1576,17 +1568,9 @@ export const saveSynergy = async (button?: boolean): Promise => { const save = btoa(JSON.stringify(p)) if (save !== null) { - const saveBlob = new Blob([save], { type: 'text/plain' }) - - // Should prevent overwritting of localforage that is currently used - if (!saveCheck.canSave) { - return false - } - localStorage.setItem('Synergysave2', save) - await localforage.setItem('Synergysave2', saveBlob) } else { - await Alert(i18next.t('testing.errorSaving')) + void Alert(i18next.t('testing.errorSaving')) return false } @@ -1599,14 +1583,9 @@ export const saveSynergy = async (button?: boolean): Promise => { return true } -const loadSynergy = async () => { - const save = (await localforage.getItem('Synergysave2')) - ?? localStorage.getItem('Synergysave2') - - const saveString = typeof save === 'string' ? save : await save?.text() - const data = saveString - ? (JSON.parse(atob(saveString)) as PlayerSave & Record) - : null +const loadSynergy = () => { + const saveString = localStorage.getItem('Synergysave2') + const data = saveString ? JSON.parse(atob(saveString)) : null if (testing || !prod) { Object.defineProperties(window, { @@ -5138,7 +5117,7 @@ export const resetCheck = async ( return Alert(i18next.t('main.singularityCancelled')) } else { await singularity() - await saveSynergy() + saveSynergy() return Alert( i18next.t('main.welcomeToSingularity', { x: format(player.singularityCount) @@ -6245,7 +6224,7 @@ export const showExitOffline = () => { * Reloads shit. * @param reset if this param is passed, offline progression will not be calculated. */ -export const reloadShit = async (reset = false) => { +export const reloadShit = (reset = false) => { clearTimers() // Shows a reset button when page loading seems to stop or cause an error @@ -6256,16 +6235,7 @@ export const reloadShit = async (reset = false) => { disableHotkeys() - // Wait a tick to continue. This is a (likely futile) attempt to see if this solves save corrupting. - // This ensures all queued tasks are executed before continuing on. - await new Promise((res) => { - setTimeout(res, 0) - }) - - const save = (await localforage.getItem('Synergysave2')) - ?? localStorage.getItem('Synergysave2') - - const saveObject = typeof save === 'string' ? save : await save?.text() + const saveObject = localStorage.getItem('Synergysave2') if (saveObject) { const dec = LZString.decompressFromBase64(saveObject) @@ -6283,28 +6253,22 @@ export const reloadShit = async (reset = false) => { } localStorage.clear() - const blob = new Blob([saveString], { type: 'text/plain' }) localStorage.setItem('Synergysave2', saveString) - await localforage.setItem('Synergysave2', blob) - await Alert(i18next.t('main.transferredFromLZ')) + Alert(i18next.t('main.transferredFromLZ')) } - await loadSynergy() + loadSynergy() } if (!reset) { - await calculateOffline() + calculateOffline() } else { if (!player.singularityChallenges.limitedTime.rewards.preserveQuarks) { player.worlds.reset() } - // saving is disabled during a singularity event to prevent bug - // early return here if the save fails can keep game state from properly resetting after a singularity - if (saveCheck.canSave) { - const saved = await saveSynergy() - if (!saved) { - return - } + + if (!saveSynergy()) { + return } } @@ -6359,17 +6323,13 @@ export const reloadShit = async (reset = false) => { typeof navigator.storage?.persist === 'function' && typeof navigator.storage?.persisted === 'function' ) { - const persistent = await navigator.storage.persisted() - - if (!persistent) { - const isPersistentNow = await navigator.storage.persist() - - if (isPersistentNow) { - void Alert(i18next.t('main.dataPersistent')) - } - } else { - console.log(`Storage is persistent! (persistent = ${persistent})`) - } + navigator.storage.persisted() + .then((persistent) => persistent ? Promise.resolve(false) : navigator.storage.persist()) + .then((isPersistentNow) => { + if (isPersistentNow) { + void Alert(i18next.t('main.dataPersistent')) + } + }) } const saveType = DOMCacheGetOrSet('saveType') as HTMLInputElement @@ -6414,12 +6374,11 @@ window.addEventListener('load', async () => { document.title = `Synergism v${version}` generateEventHandlers() - - void reloadShit() + reloadShit() corruptionButtonsAdd() corruptionLoadoutTableCreate() -}) +}, { once: true }) window.addEventListener('unload', () => { // This fixes a bug in Chrome (who would have guessed?) that diff --git a/translations/de.json b/translations/de.json index 29caff695..7027b0091 100644 --- a/translations/de.json +++ b/translations/de.json @@ -3589,4 +3589,4 @@ "ADD_CODE_CAP_BUFF": "Code 'add' Capacity is multiplied by <>" } } -} +} diff --git a/translations/es.json b/translations/es.json index 197285960..29c9155d3 100644 --- a/translations/es.json +++ b/translations/es.json @@ -3589,4 +3589,4 @@ "ADD_CODE_CAP_BUFF": "Code 'add' Capacity is multiplied by <>" } } -} +} diff --git a/translations/fr.json b/translations/fr.json index 36f300f35..8960f5dc6 100644 --- a/translations/fr.json +++ b/translations/fr.json @@ -3589,4 +3589,4 @@ "ADD_CODE_CAP_BUFF": "Code 'add' Capacity is multiplied by <>" } } -} +} diff --git a/translations/nl.json b/translations/nl.json index 8d76380af..04059fc72 100644 --- a/translations/nl.json +++ b/translations/nl.json @@ -3589,4 +3589,4 @@ "ADD_CODE_CAP_BUFF": "Code 'add' Capacity is multiplied by <>" } } -} +} diff --git a/translations/pl.json b/translations/pl.json index 81a9c8bf8..0558b3e9a 100644 --- a/translations/pl.json +++ b/translations/pl.json @@ -3589,4 +3589,4 @@ "ADD_CODE_CAP_BUFF": "Code 'add' Capacity is multiplied by <>" } } -} +} diff --git a/translations/ru.json b/translations/ru.json index 9e78d02b2..20b8ba371 100644 --- a/translations/ru.json +++ b/translations/ru.json @@ -3589,4 +3589,4 @@ "ADD_CODE_CAP_BUFF": "Ёмкость кода 'add' увеличена в <> раз" } } -} +} diff --git a/translations/zh.json b/translations/zh.json index a874b5df6..51dbc914b 100644 --- a/translations/zh.json +++ b/translations/zh.json @@ -3589,4 +3589,4 @@ "ADD_CODE_CAP_BUFF": "使“增加”代码的次数上限变为原来的<>倍" } } -} +}