All files / repo/src/hooks useGamePersistence.ts

100% Statements 28/28
50% Branches 4/8
100% Functions 4/4
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  1x   1x 3x   3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   3x 1x 1x   3x 1x 1x   3x 3x 3x 3x 3x 3x  
import type { GameState } from '@/components/GameCanvas/GameCanvas.types';
import { useGameStateStorage } from './useGameStateStorage';
 
export function useGamePersistence(seed: string, difficulty: number) {
  const { save, load, clear } = useGameStateStorage();
 
  function saveState(state: GameState) {
    save({
      dateStart: state.dateStart ?? new Date().toISOString(),
      tiles: state.tiles,
      elapsedSeconds: state.elapsedSeconds,
      moves: state.moves,
      seed: state.seed,
      difficulty: state.difficulty,
      matchedPairs: state?.matchedPairs ?? [],
      matchCount: state?.matchCount ?? 0,
      mismatchCount: state?.mismatchCount ?? 0,
    });
  }
 
  function loadState() {
    return load();
  }
 
  function clearState() {
    clear();
  }
 
  return {
    saveState,
    loadState,
    clearState,
  };
}