breakout71/src/settings.ts

54 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-03-15 21:29:38 +01:00
// Settings
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
}
// 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-15 21:28:00 +02:00
if (cachedSettings[key] !== value) {
needsSaving.add(key);
cachedSettings[key] = value;
}
}
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: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
}
setInterval(commitSettingsChangesToLocalStorage, 500);
2025-03-15 21:29:38 +01:00
export function getTotalScore() {
2025-03-16 17:45:29 +01:00
return getSettingValue("breakout_71_total_score", 0);
}
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 16:11:12 +01:00
export function cycleMaxCoins() {
setSettingValue("max_coins", (getSettingValue("max_coins", 2) + 1) % 7);
}