2025-03-15 21:29:38 +01:00
|
|
|
// Settings
|
|
|
|
|
2025-04-20 12:56:50 +02:00
|
|
|
import {toast} from "./toast";
|
|
|
|
import {t} from "./i18n/i18n";
|
|
|
|
|
2025-03-15 21:29:38 +01:00
|
|
|
let cachedSettings: { [key: string]: unknown } = {};
|
|
|
|
|
2025-04-15 21:28:00 +02:00
|
|
|
try {
|
|
|
|
for (let key in localStorage) {
|
|
|
|
try {
|
|
|
|
cachedSettings[key] = JSON.parse(localStorage.getItem(key) || "null");
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
}
|
2025-04-15 17:31:57 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
|
2025-03-15 21:29:38 +01:00
|
|
|
export function getSettingValue<T>(key: string, defaultValue: T) {
|
2025-03-16 17:45:29 +01:00
|
|
|
return (cachedSettings[key] as T) ?? defaultValue;
|
2025-03-15 21:29:38 +01:00
|
|
|
}
|
|
|
|
|
2025-04-15 21:25:27 +02:00
|
|
|
// We avoid using localstorage synchronously for perf reasons
|
2025-04-15 21:28:00 +02:00
|
|
|
let needsSaving: Set<string> = new Set();
|
2025-03-15 21:29:38 +01:00
|
|
|
export function setSettingValue<T>(key: string, value: T) {
|
2025-04-16 09:38:43 +02:00
|
|
|
needsSaving.add(key);
|
|
|
|
cachedSettings[key] = value;
|
2025-04-15 21:25:27 +02:00
|
|
|
}
|
2025-04-20 12:56:50 +02:00
|
|
|
|
2025-04-16 09:26:10 +02:00
|
|
|
export function commitSettingsChangesToLocalStorage() {
|
2025-03-16 17:45:29 +01:00
|
|
|
try {
|
2025-04-15 21:28:00 +02:00
|
|
|
for (let key of needsSaving) {
|
|
|
|
localStorage.setItem(key, JSON.stringify(cachedSettings[key]));
|
2025-04-15 21:25:27 +02:00
|
|
|
}
|
2025-04-15 21:28:00 +02:00
|
|
|
needsSaving.clear();
|
2025-03-16 17:45:29 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
2025-04-16 09:26:10 +02:00
|
|
|
}
|
2025-04-16 09:38:43 +02:00
|
|
|
setInterval(() => commitSettingsChangesToLocalStorage(), 500);
|
2025-03-15 21:29:38 +01:00
|
|
|
|
2025-03-16 14:29:14 +01:00
|
|
|
export function getTotalScore() {
|
2025-03-16 17:45:29 +01:00
|
|
|
return getSettingValue("breakout_71_total_score", 0);
|
2025-03-16 14:29:14 +01:00
|
|
|
}
|
|
|
|
|
2025-03-23 16:11:12 +01:00
|
|
|
export function getCurrentMaxCoins() {
|
2025-04-15 17:31:57 +02:00
|
|
|
return Math.pow(2, getSettingValue("max_coins", 2)) * 200;
|
2025-03-23 16:11:12 +01:00
|
|
|
}
|
|
|
|
export function getCurrentMaxParticles() {
|
2025-04-15 21:28:00 +02:00
|
|
|
return getCurrentMaxCoins();
|
2025-03-23 15:48:21 +01:00
|
|
|
}
|
2025-03-23 16:11:12 +01:00
|
|
|
export function cycleMaxCoins() {
|
2025-04-15 21:25:27 +02:00
|
|
|
setSettingValue("max_coins", (getSettingValue("max_coins", 2) + 1) % 7);
|
2025-03-23 15:48:21 +01:00
|
|
|
}
|
2025-04-20 12:56:50 +02:00
|
|
|
|
|
|
|
let asked=false
|
|
|
|
export function askForPersistentStorage(){
|
|
|
|
if(asked) return
|
|
|
|
asked=true
|
|
|
|
if (navigator.storage && navigator.storage.persist) {
|
|
|
|
navigator.storage.persist().then((persistent) => {
|
|
|
|
if (persistent) {
|
|
|
|
toast(t('settings.storage_granted'))
|
|
|
|
} else {
|
|
|
|
toast(t('settings.storage_refused'))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|