-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocal_storage_manager.ts
More file actions
71 lines (63 loc) · 1.42 KB
/
local_storage_manager.ts
File metadata and controls
71 lines (63 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { GameState } from './types.js'
/**
* Local Storage Manager
*
* Singleton class that manages the local storage.
*/
export class LocalStorageManager {
/** Storage */
static storage: Storage = window.localStorage
constructor() {}
/**
* Gets the best score.
*
* @returns Best Score
*/
static getBestScore(): number {
return parseInt(LocalStorageManager.storage.getItem('bestScore') || '0', 10)
}
/**
* Sets the best score.
*
* @param score Best Score
*/
static setBestScore(score: number): void {
// Lab 11: Update Best Score
}
/**
* Gets the game state.
*
* @returns Game State
*/
static getGameState(): GameState | null {
const gameState = LocalStorageManager.storage.getItem('gameState')
return gameState !== null
? JSON.parse(LocalStorageManager.storage.getItem('gameState') as string)
: null
}
/**
* Sets the game state.
*
* @param gameState Game State
*/
static setGameState(gameState: GameState): void {
LocalStorageManager.storage.setItem(
'gameState',
JSON.stringify({
...gameState,
grid: {
size: gameState.grid.size,
cells: gameState.grid.cells
}
})
)
}
/**
* Clears the game state.
*
* All values are set to their original defaults.
*/
static clearGameState(): void {
LocalStorageManager.storage.removeItem('gameState')
}
}