Files
mayi_coco/assets/scripts/core/StorageService.ts
T

137 lines
3.5 KiB
TypeScript

import { sys } from 'cc';
import { LevelDef, RecordMap } from './Types';
import { recordKey } from './LevelData';
const RECORDS_KEY = 'ant-embroidery-best-v1';
const SOUND_KEY = 'ant-embroidery-sound-v1';
const BGM_KEY = 'ant-embroidery-bgm-v1';
const LOUPE_KEY = 'ant-embroidery-loupe-v1';
const LANG_KEY = 'ant-embroidery-lang-v1';
const TUTORIAL_KEY = 'ant-embroidery-tut-v1';
const ATELIER_KEY = 'ant-embroidery-atelier-v1';
export type AppLang = 'zh' | 'en';
declare const wx: any;
function getRaw(key: string): string | null {
try {
if (typeof wx !== 'undefined' && typeof wx.getStorageSync === 'function') {
const val = wx.getStorageSync(key);
return val === '' || val === undefined || val === null ? null : String(val);
}
return sys.localStorage.getItem(key);
} catch {
return null;
}
}
function setRaw(key: string, value: string): void {
try {
if (typeof wx !== 'undefined' && typeof wx.setStorageSync === 'function') {
wx.setStorageSync(key, value);
return;
}
sys.localStorage.setItem(key, value);
} catch {
// Ignore storage errors on restricted environments.
}
}
export class StorageService {
private records: RecordMap = {};
constructor() {
this.records = this.loadRecords();
}
private loadRecords(): RecordMap {
try {
const raw = getRaw(RECORDS_KEY);
const parsed = raw ? JSON.parse(raw) : {};
const valid: RecordMap = {};
if (parsed && typeof parsed === 'object') {
for (const k of Object.keys(parsed)) {
const rec = parsed[k];
if (
Number.isInteger(rec?.stars) && rec.stars >= 1 && rec.stars <= 3 &&
Number.isFinite(rec?.length) && rec.length > 0
) {
valid[k] = { stars: rec.stars, length: rec.length };
}
}
}
return valid;
} catch {
return {};
}
}
public getRecord(level: LevelDef) {
return this.records[recordKey(level)];
}
public saveRecord(level: LevelDef, stars: number, length: number): boolean {
const key = recordKey(level);
const prev = this.records[key];
if (prev && (stars < prev.stars || (stars === prev.stars && length >= prev.length))) {
return false;
}
this.records[key] = { stars, length };
setRaw(RECORDS_KEY, JSON.stringify(this.records));
return true;
}
public readSoundEnabled(): boolean {
const raw = getRaw(SOUND_KEY);
return raw === null ? true : raw === '1';
}
public saveSoundEnabled(enabled: boolean): void {
setRaw(SOUND_KEY, enabled ? '1' : '0');
}
public readBgmEnabled(): boolean {
const raw = getRaw(BGM_KEY);
return raw === null ? true : raw === '1';
}
public saveBgmEnabled(enabled: boolean): void {
setRaw(BGM_KEY, enabled ? '1' : '0');
}
public readLoupeEnabled(): boolean {
const raw = getRaw(LOUPE_KEY);
return raw === null ? true : raw === '1';
}
public saveLoupeEnabled(enabled: boolean): void {
setRaw(LOUPE_KEY, enabled ? '1' : '0');
}
public readLanguage(): AppLang {
const raw = getRaw(LANG_KEY);
return raw === 'en' ? 'en' : 'zh';
}
public saveLanguage(lang: AppLang): void {
setRaw(LANG_KEY, lang);
}
public hasSeenTutorial(): boolean {
return getRaw(TUTORIAL_KEY) === '1';
}
public markTutorialSeen(): void {
setRaw(TUTORIAL_KEY, '1');
}
public readAtelierRaw(): string | null {
return getRaw(ATELIER_KEY);
}
public saveAtelierRaw(raw: string): void {
setRaw(ATELIER_KEY, raw);
}
}