All files / repo/src/stores useGameStore.ts

87.5% Statements 119/136
50% Branches 10/20
66.66% Functions 8/12
87.5% Lines 119/136

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 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155  1x 1x 1x 1x 1x   1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x 3x 3x   3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   7x 1x 1x 1x 1x 1x 1x 1x 1x 1x   7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   7x 2x 2x 2x 2x 2x 2x   7x 2x 2x 2x 2x 2x 2x   7x 1x 1x 1x 1x   7x       7x       7x       7x       7x 1x 1x   7x 2x 1x 2x   7x                   7x   7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x  
import type { GameState, MatchRecord, Tile } from '@/components/GameCanvas/GameCanvas.types';
import { generateShuffledTiles } from '@/shared/utils/generateTiles';
import { isDifficultyType } from '@/shared/utils/isDifficultyType';
import { getTimeStamp } from '@/shared/utils/timeStamp';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
 
export const useGameStore = defineStore('game', () => {
  const dateStart = ref<string | null>(null);
  const seed = ref('');
  const difficulty = ref(1);
  const tiles = ref<Tile[]>([]);
  const moves = ref(0);
  const elapsed = ref(0);
  const matchCount = ref(0);
  const mismatchCount = ref(0);
  const matchedPairs = ref<MatchRecord[]>([]);
  const gameStarted = ref(false);
  const gameOver = ref(false);
  const sidebarVisible = ref(true);
  const devPanelVisible = ref(false);
 
  function startGame(payload: { seed: string; difficulty: number }) {
    const newSeed = payload.seed?.trim() || Date.now().toString();
    const newDifficulty = isDifficultyType(payload.difficulty) ? payload.difficulty : 1;
 
    seed.value = newSeed;
    difficulty.value = newDifficulty;
 
    const generatedTiles = generateShuffledTiles(newSeed, newDifficulty);
    tiles.value = generatedTiles;
    moves.value = 0;
    elapsed.value = 0;
    gameOver.value = false;
    gameStarted.value = true;
    matchedPairs.value = [];
    matchCount.value = 0;
    mismatchCount.value = 0;
    dateStart.value = new Date().toISOString();
  }
 
  function restartGame() {
    const generatedTiles = generateShuffledTiles(seed.value, difficulty.value);
    tiles.value = generatedTiles;
    moves.value = 0;
    elapsed.value = 0;
    gameOver.value = false;
    matchedPairs.value = [];
    matchCount.value = 0;
    mismatchCount.value = 0;
  }
 
  function restoreGame(state: GameState) {
    dateStart.value = state.dateStart ?? null;
    seed.value = state.seed;
    difficulty.value = state.difficulty;
    tiles.value = state.tiles;
    elapsed.value = state.elapsedSeconds;
    moves.value = state.moves;
    gameStarted.value = true;
    gameOver.value = false;
    matchedPairs.value = state?.matchedPairs ?? [];
    matchCount.value = state?.matchCount ?? 0;
    mismatchCount.value = state?.mismatchCount ?? 0;
  }
 
  function incrementMatch(a: Tile, b: Tile) {
    const { haloAngle: haloAngleA, ...restA } = a ?? {};
    const { haloAngle: haloAngleB, ...restB } = b ?? {};
    matchCount.value++;
    const timestamp = getTimeStamp(dateStart.value as string);
    matchedPairs.value.push({ a: restA, b: restB, result: 'match', timestamp });
  }
 
  function incrementMismatch(a: Tile, b: Tile) {
    const { haloAngle: haloAngleA, ...restA } = a ?? {};
    const { haloAngle: haloAngleB, ...restB } = b ?? {};
    mismatchCount.value++;
    const timestamp = getTimeStamp(dateStart.value as string);
    matchedPairs.value.push({ a: restA, b: restB, result: 'mismatch', timestamp });
  }
 
  function resetStats() {
    matchCount.value = 0;
    mismatchCount.value = 0;
    matchedPairs.value = [];
  }
 
  function incrementMoves() {
    moves.value++;
  }
 
  function incrementTime() {
    elapsed.value++;
  }
 
  function setGameOver(val: boolean) {
    gameOver.value = val;
  }
 
  function setGameStarted(val: boolean) {
    gameStarted.value = val;
  }
 
  function toggleDevPanel() {
    devPanelVisible.value = !devPanelVisible.value;
  }
 
  function toggleSidebar(val?: boolean) {
    if (typeof val === 'boolean') sidebarVisible.value = val;
    else sidebarVisible.value = !sidebarVisible.value;
  }
 
  const gameState = computed(() => ({
    dateStart: dateStart.value,
    tiles: tiles.value,
    elapsedSeconds: elapsed.value,
    moves: moves.value,
    seed: seed.value,
    difficulty: difficulty.value,
    matchCount: matchCount.value ?? 0,
    mismatchCount: mismatchCount.value ?? 0,
    matchedPairs: matchedPairs.value ?? [],
  }));
 
  return {
    dateStart,
    seed,
    difficulty,
    tiles,
    moves,
    elapsed,
    gameStarted,
    gameOver,
    startGame,
    restartGame,
    restoreGame,
    incrementMoves,
    incrementTime,
    matchCount,
    mismatchCount,
    matchedPairs,
    incrementMatch,
    incrementMismatch,
    resetStats,
    setGameOver,
    setGameStarted,
    gameState,
    devPanelVisible,
    toggleDevPanel,
    sidebarVisible,
    toggleSidebar,
  };
});