Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Sep 27, 2024
1 parent 01664a9 commit 1f8230e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 26 deletions.
7 changes: 4 additions & 3 deletions src/EventListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
promocodesPrompt,
reloadDeleteGame,
resetGame,
safeLocalStorage,
updateSaveString
} from './ImportExport'
import { buyPlatonicUpgrades, createPlatonicDescription } from './Platonic'
Expand Down Expand Up @@ -1108,7 +1109,7 @@ TODO: Fix this entire tab it's utter shit
const element = event.target as HTMLInputElement

if (!element.checked) {
localStorage.removeItem('copyToClipboard')
safeLocalStorage.removeItem('copyToClipboard')
event.stopPropagation()
return
}
Expand All @@ -1119,9 +1120,9 @@ TODO: Fix this entire tab it's utter shit

if (confirmed) {
element.checked = !element.checked
localStorage.setItem('copyToClipboard', '')
safeLocalStorage.setItem('copyToClipboard', '')
} else {
localStorage.removeItem('copyToClipboard')
safeLocalStorage.removeItem('copyToClipboard')
}
})

Expand Down
54 changes: 47 additions & 7 deletions src/ImportExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export const importSynergism = async (input: string | null, reset = false) => {

saveCheck.canSave = false
await setSave(saveString)
localStorage.setItem('saveScumIsCheating', Date.now().toString())
safeLocalStorage.setItem('saveScumIsCheating', Date.now().toString())

await reloadShit(reset)
saveCheck.canSave = true
Expand Down Expand Up @@ -846,11 +846,11 @@ export const promocodes = async (input: string | null, amount?: number) => {
} else if (input === 'gamble') {
if (
typeof player.skillCode === 'number'
|| typeof localStorage.getItem('saveScumIsCheating') === 'string'
|| typeof safeLocalStorage.getItem('saveScumIsCheating') === 'string'
) {
if (
(Date.now() - player.skillCode!) / 1000 < 3600
|| (Date.now() - Number(localStorage.getItem('saveScumIsCheating')))
|| (Date.now() - Number(safeLocalStorage.getItem('saveScumIsCheating')))
/ 1000
< 3600
) {
Expand Down Expand Up @@ -884,7 +884,7 @@ export const promocodes = async (input: string | null, amount?: number) => {
))
}

localStorage.setItem('saveScumIsCheating', Date.now().toString())
safeLocalStorage.setItem('saveScumIsCheating', Date.now().toString())
const dice = (window.crypto.getRandomValues(new Uint8Array(1))[0] % 6) + 1 // [1, 6]

if (dice === 1) {
Expand Down Expand Up @@ -1230,11 +1230,11 @@ const dailyCodeReward = () => {
}

export const handleLastModified = (lastModified: number) => {
const localStorageFirstPlayed = localStorage.getItem('firstPlayed')
const localStorageFirstPlayed = safeLocalStorage.getItem('firstPlayed')
const lastModifiedDate = new Date(lastModified)

if (localStorageFirstPlayed === null) {
localStorage.setItem('firstPlayed', lastModifiedDate.toISOString())
safeLocalStorage.setItem('firstPlayed', lastModifiedDate.toISOString())
return
}

Expand All @@ -1245,7 +1245,7 @@ export const handleLastModified = (lastModified: number) => {
// for the new file, set the oldest date to the last modified.
if (localFirstPlayedDate.getTime() > lastModifiedDate.getTime()) {
player.firstPlayed = lastModifiedDate.toISOString()
localStorage.setItem('firstPlayed', player.firstPlayed)
safeLocalStorage.setItem('firstPlayed', player.firstPlayed)
}
}

Expand All @@ -1270,3 +1270,43 @@ export const setSave = async (saveString: string) => {
Notification(`Error saving: ${e}`, 2000)
}
}

export const safeLocalStorage = {
/**
* Same as localStorage.setItem, but errors are swallowed
*/
setItem (key: string, value: string) {
try {
localStorage.setItem(key, value)
} catch {}
},

/**
* Same as localStorage.removeItem, but errors are swallowed
*/
removeItem (key: string) {
try {
localStorage.removeItem(key)
} catch {}
},

/**
* Same as localStorage.getItem, but errors are swallowed
*/
getItem (key: string) {
try {
return localStorage.getItem(key)
} catch {
return null
}
},

/**
* Same as localStorage.clear, but errors are swallowed
*/
clear () {
try {
localStorage.clear()
} catch {}
}
}
12 changes: 6 additions & 6 deletions src/Synergism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ 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'
import { getSaveString, setSave } from './ImportExport'
import { getSaveString, safeLocalStorage, setSave } from './ImportExport'

export const player: Player = {
firstPlayed: new Date().toISOString(),
Expand Down Expand Up @@ -6206,7 +6206,7 @@ export const reloadShit = async (reset = false) => {
return Alert(i18next.t('save.loadFailed'))
}

localStorage.clear()
safeLocalStorage.clear()
await setSave(saveString)
await Alert(i18next.t('main.transferredFromLZ'))
}
Expand Down Expand Up @@ -6274,9 +6274,9 @@ export const reloadShit = async (reset = false) => {

setInterval(cacheReinitialize, 15000)

if (localStorage.getItem('pleaseStar') === null) {
if (safeLocalStorage.getItem('pleaseStar') === null) {
void Alert(i18next.t('main.starRepo'))
localStorage.setItem('pleaseStar', '')
safeLocalStorage.setItem('pleaseStar', '')
}

// All versions of Chrome and Firefox supported by the game have this API,
Expand All @@ -6299,7 +6299,7 @@ export const reloadShit = async (reset = false) => {
}

const saveType = DOMCacheGetOrSet('saveType') as HTMLInputElement
saveType.checked = localStorage.getItem('copyToClipboard') !== null
saveType.checked = safeLocalStorage.getItem('copyToClipboard') !== null
}

function playerNeedsReminderToExport () {
Expand Down Expand Up @@ -6346,7 +6346,7 @@ window.addEventListener('load', async () => {
corruptionButtonsAdd()
corruptionLoadoutTableCreate()

handleLogin().catch(console.error)
handleLogin().catch((e) => console.error('caught error', e))
})

window.addEventListener('unload', () => {
Expand Down
7 changes: 4 additions & 3 deletions src/Themes.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import i18next from 'i18next'
import { DOMCacheGetOrSet } from './Cache/DOM'
import { safeLocalStorage } from './ImportExport'
import { player } from './Synergism'

export const toggleTheme = (initial = false, themeNumber = 1, change = false) => {
const themeButton = DOMCacheGetOrSet('theme')
const body = document.body

if (change) {
localStorage.setItem('theme', `${themeNumber}`)
safeLocalStorage.setItem('theme', `${themeNumber}`)
body.style.setProperty('--transition', '750ms')
body.style.setProperty('--transition-extra', '600ms')
} else {
themeNumber = Number(localStorage.getItem('theme') ?? 1)
themeNumber = Number(safeLocalStorage.getItem('theme') ?? 1)
}

/* Full reset for easy out of order change */
Expand Down Expand Up @@ -107,7 +108,7 @@ export const toggleTheme = (initial = false, themeNumber = 1, change = false) =>
}

if (themeNumber === 1) {
localStorage.removeItem('theme')
safeLocalStorage.removeItem('theme')
themeButton.textContent = 'Dark Mode'
} else if (themeNumber === 2) { // 'Darker Mode'
body.style.setProperty('--header-color', 'black')
Expand Down
10 changes: 3 additions & 7 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import i18next, { type Resource } from 'i18next'
import { DOMCacheGetOrSet } from './Cache/DOM'
import { prod } from './Config'
import { safeLocalStorage } from './ImportExport'
import ColorTextPlugin from './Plugins/ColorText'
import { Confirm } from './UpdateHTML'

Expand All @@ -21,12 +22,7 @@ const languageCache = new Map<string, { translation: Resource }>()

export const init = async (): Promise<void> => {
const resources: Record<string, Resource> = {}
let language: string
try {
language = localStorage.getItem('language') ?? 'en'
} catch {
language = 'en'
}
const language = safeLocalStorage.getItem('language') ?? 'en'

const response = await fetch(`./translations/${language}.json`)
const file = await response.json() as Resource
Expand Down Expand Up @@ -76,7 +72,7 @@ function buildLanguageButton (langID: string, name: string, flag: string) {

// i18next.addResourceBundle
await i18next.changeLanguage(langID)
localStorage.setItem('language', langID)
safeLocalStorage.setItem('language', langID)

const shouldReload = await Confirm(i18next.t('general.languageChange'))

Expand Down

0 comments on commit 1f8230e

Please sign in to comment.