all repos — clicker-ts @ a5aa159cf9bf808c5aab354e5760dd1f821f8a01

Unnamed repository; edit this file 'description' to name the repository.

src/utils/storage.ts (view raw)

 1import { GameState } from '../types';
 2
 3const GAME_SAVE_KEY = 'clicker_game_save';
 4
 5export const saveGame = (gameState: GameState): void => {
 6  try {
 7    const serializedState = JSON.stringify(gameState);
 8    localStorage.setItem(GAME_SAVE_KEY, serializedState);
 9  } catch (error) {
10    console.error('Failed to save game:', error);
11  }
12};
13
14export const loadGame = (): GameState | null => {
15  try {
16    const serializedState = localStorage.getItem(GAME_SAVE_KEY);
17    if (!serializedState) return null;
18    return JSON.parse(serializedState) as GameState;
19  } catch (error) {
20    console.error('Failed to load game:', error);
21    return null;
22  }
23};
24
25export const resetGame = (): void => {
26  try {
27    localStorage.removeItem(GAME_SAVE_KEY);
28  } catch (error) {
29    console.error('Failed to reset game:', error);
30  }
31};