Files
sudoku/src/models/StorageManager.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-05-13 20:31:44 +08:00
import { config } from "../constants/config"
2025-05-14 20:05:17 +08:00
import { DataStage } from "../types/global"
2025-05-14 19:22:21 +08:00
import { DOStage } from "./DOStage"
2025-05-13 20:31:44 +08:00
export class StorageManager {
private static instance: StorageManager
public static getInstance(): StorageManager {
if (!StorageManager.instance) {
StorageManager.instance = new StorageManager()
}
return StorageManager.instance
}
2025-05-14 10:14:55 +08:00
private changed = false
2025-05-13 20:31:44 +08:00
private stageID: string = "";
2025-05-14 20:05:17 +08:00
private stageMap: Map<string, DataStage> = new Map();
2025-05-13 20:31:44 +08:00
public init(): void {
2025-05-14 10:14:55 +08:00
this.changed = false
2025-05-13 20:31:44 +08:00
}
public loadStage(stageID: string): DOStage {
this.stageID = stageID
2025-05-14 20:05:17 +08:00
var dataStage: DataStage
2025-05-13 20:31:44 +08:00
if (this.stageMap.has(stageID)) {
dataStage = this.stageMap.get(stageID)
}
else {
var jsonStr = Laya.LocalStorage.getItem(stageID)
if (jsonStr) {
dataStage = JSON.parse(jsonStr)
this.stageMap.set(stageID, dataStage)
}
else {
2025-05-14 20:05:17 +08:00
dataStage = {mistake: 0, difficulty: config.DIFFICULTY_TYPE.Easy, duration: 0, note_open: false, blocks: new Array(), candys: new Array()}
2025-05-13 20:31:44 +08:00
this.stageMap.set(stageID, dataStage)
}
}
2025-05-14 10:14:55 +08:00
var levelStr = "aBCeIGFhdEDFcAHBGIhigBdfecAgHDiFBCaeBEAGHCIdffcIdEAHbgcABfGEdiHIFHaCDGEBDGEHbiaFC"
return new DOStage(dataStage, levelStr)
2025-05-13 20:31:44 +08:00
}
2025-05-14 12:17:47 +08:00
public cleanStage(): void {
if (this.stageID && this.stageID.length > 0) {
if (this.stageMap.has(this.stageID)) {
this.stageMap.delete(this.stageID)
}
Laya.LocalStorage.removeItem(this.stageID)
this.stageID = ""
}
}
2025-05-13 20:31:44 +08:00
public saveStage(): void {
2025-05-14 10:14:55 +08:00
if (this.changed) {
this.changed = false
if (this.stageMap.has(this.stageID)) {
var dataStage = this.stageMap.get(this.stageID)
var jsonStr = JSON.stringify(dataStage)
Laya.LocalStorage.setItem(this.stageID, jsonStr)
}
2025-05-13 20:31:44 +08:00
}
}
2025-05-14 10:14:55 +08:00
public onChanged(): void {
this.changed = true
}
2025-05-13 20:31:44 +08:00
}