All files / web/src/arcade-games/memory-quiz types.ts

100% Statements 289/289
100% Branches 0/0
100% Functions 0/0
100% Lines 289/289

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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 2901x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Memory Quiz - Type Definitions
 *
 * This module uses Zod schemas as the single source of truth for types.
 * TypeScript types are inferred from Zod schemas using z.infer<>.
 */
 
import { z } from 'zod'
 
// ============================================================================
// Difficulty Levels (const + derived schema)
// ============================================================================
 
export const DIFFICULTY_LEVELS = {
  beginner: {
    name: 'Beginner',
    range: { min: 1, max: 9 },
    description: 'Single digits (1-9)',
  },
  easy: {
    name: 'Easy',
    range: { min: 10, max: 99 },
    description: 'Two digits (10-99)',
  },
  medium: {
    name: 'Medium',
    range: { min: 100, max: 499 },
    description: 'Three digits (100-499)',
  },
  hard: {
    name: 'Hard',
    range: { min: 500, max: 999 },
    description: 'Large numbers (500-999)',
  },
  expert: {
    name: 'Expert',
    range: { min: 1, max: 999 },
    description: 'Mixed range (1-999)',
  },
} as const
 
export const DifficultyLevelSchema = z.enum(['beginner', 'easy', 'medium', 'hard', 'expert'])
export type DifficultyLevel = z.infer<typeof DifficultyLevelSchema>
 
// ============================================================================
// Core Types (Zod Schemas)
// ============================================================================
 
export const SelectedCountSchema = z.union([
  z.literal(2),
  z.literal(5),
  z.literal(8),
  z.literal(12),
  z.literal(15),
])
export type SelectedCount = z.infer<typeof SelectedCountSchema>
 
export const PlayModeSchema = z.enum(['cooperative', 'competitive'])
export type PlayMode = z.infer<typeof PlayModeSchema>
 
export const GamePhaseSchema = z.enum(['setup', 'display', 'input', 'results'])
export type GamePhase = z.infer<typeof GamePhaseSchema>
 
// ============================================================================
// Game Entities (Zod Schemas)
// ============================================================================
 
export const QuizCardSchema = z
  .object({
    number: z.number(),
    // svgComponent and element are runtime-only, not serialized but allowed via passthrough
  })
  .passthrough()
 
export type QuizCard = {
  number: number
  svgComponent: JSX.Element | null // Runtime only
  element: HTMLElement | null // Runtime only
}
 
export const PlayerScoreSchema = z.object({
  correct: z.number(),
  incorrect: z.number(),
})
export type PlayerScore = z.infer<typeof PlayerScoreSchema>
 
export const PlayerMetadataSchema = z.object({
  id: z.string(),
  name: z.string(),
  emoji: z.string(),
  userId: z.string(),
  color: z.string().optional(),
})
export type PlayerMetadata = z.infer<typeof PlayerMetadataSchema>
 
export const WrongGuessAnimationSchema = z.object({
  number: z.number(),
  id: z.string(),
  timestamp: z.number(),
})
export type WrongGuessAnimation = z.infer<typeof WrongGuessAnimationSchema>
 
// ============================================================================
// Game Configuration (Zod Schema)
// ============================================================================
 
export const MemoryQuizConfigSchema = z.object({
  selectedCount: SelectedCountSchema,
  displayTime: z.number(),
  selectedDifficulty: DifficultyLevelSchema,
  playMode: PlayModeSchema,
})
export type MemoryQuizConfig = z.infer<typeof MemoryQuizConfigSchema>
 
// ============================================================================
// Game State (Zod Schema)
// ============================================================================
 
export const MemoryQuizStateSchema = z.object({
  // Core game data
  cards: z.array(QuizCardSchema),
  quizCards: z.array(QuizCardSchema),
  correctAnswers: z.array(z.number()),
 
  // Game progression
  currentCardIndex: z.number(),
  displayTime: z.number(),
  selectedCount: z.number(),
  selectedDifficulty: DifficultyLevelSchema,
 
  // Input system state
  foundNumbers: z.array(z.number()),
  guessesRemaining: z.number(),
  currentInput: z.string(),
  incorrectGuesses: z.number(),
 
  // Multiplayer state
  activePlayers: z.array(z.string()),
  playerMetadata: z.record(z.string(), PlayerMetadataSchema),
  playerScores: z.record(z.string(), PlayerScoreSchema),
  playMode: PlayModeSchema,
  numberFoundBy: z.record(z.string(), z.string()), // Maps number (as string key) to userId who found it
 
  // UI state
  gamePhase: GamePhaseSchema,
  finishButtonsBound: z.boolean(),
  wrongGuessAnimations: z.array(WrongGuessAnimationSchema),
 
  // Timing
  gameStartTime: z.number().nullable(),
 
  // Keyboard state
  hasPhysicalKeyboard: z.boolean().nullable(),
  testingMode: z.boolean(),
  showOnScreenKeyboard: z.boolean(),
 
  // Runtime-only (will be null in serialized state, managed by components)
  prefixAcceptanceTimeout: z.any().optional(),
})
 
/**
 * Core game state type
 * Includes both serializable fields and runtime-only fields for component use
 */
export type MemoryQuizState = Omit<
  z.infer<typeof MemoryQuizStateSchema>,
  'cards' | 'quizCards' | 'prefixAcceptanceTimeout'
> & {
  cards: QuizCard[]
  quizCards: QuizCard[]
  prefixAcceptanceTimeout: NodeJS.Timeout | null
}
 
// ============================================================================
// Legacy Reducer Actions (deprecated - will be removed)
// ============================================================================
 
export type QuizAction =
  | { type: 'SET_CARDS'; cards: QuizCard[] }
  | { type: 'SET_DISPLAY_TIME'; time: number }
  | { type: 'SET_SELECTED_COUNT'; count: number }
  | { type: 'SET_DIFFICULTY'; difficulty: DifficultyLevel }
  | { type: 'SET_PLAY_MODE'; playMode: 'cooperative' | 'competitive' }
  | { type: 'START_QUIZ'; quizCards: QuizCard[] }
  | { type: 'NEXT_CARD' }
  | { type: 'SHOW_INPUT_PHASE' }
  | { type: 'ACCEPT_NUMBER'; number: number; playerId?: string }
  | { type: 'REJECT_NUMBER'; playerId?: string }
  | { type: 'ADD_WRONG_GUESS_ANIMATION'; number: number }
  | { type: 'CLEAR_WRONG_GUESS_ANIMATIONS' }
  | { type: 'SET_INPUT'; input: string }
  | { type: 'SET_PREFIX_TIMEOUT'; timeout: NodeJS.Timeout | null }
  | { type: 'SHOW_RESULTS' }
  | { type: 'RESET_QUIZ' }
  | { type: 'SET_PHYSICAL_KEYBOARD'; hasKeyboard: boolean | null }
  | { type: 'SET_TESTING_MODE'; enabled: boolean }
  | { type: 'TOGGLE_ONSCREEN_KEYBOARD' }
 
// ============================================================================
// Game Moves (TypeScript union - could be Zod later if needed)
// ============================================================================
 
export type MemoryQuizMove =
  | {
      type: 'START_QUIZ'
      playerId: string
      userId: string
      timestamp: number
      data: {
        numbers: number[]
        quizCards?: QuizCard[]
        activePlayers: string[]
        playerMetadata: Record<string, PlayerMetadata>
      }
    }
  | {
      type: 'NEXT_CARD'
      playerId: string
      userId: string
      timestamp: number
      data: Record<string, never>
    }
  | {
      type: 'SHOW_INPUT_PHASE'
      playerId: string
      userId: string
      timestamp: number
      data: Record<string, never>
    }
  | {
      type: 'ACCEPT_NUMBER'
      playerId: string
      userId: string
      timestamp: number
      data: { number: number }
    }
  | {
      type: 'REJECT_NUMBER'
      playerId: string
      userId: string
      timestamp: number
      data: Record<string, never>
    }
  | {
      type: 'SET_INPUT'
      playerId: string
      userId: string
      timestamp: number
      data: { input: string }
    }
  | {
      type: 'SHOW_RESULTS'
      playerId: string
      userId: string
      timestamp: number
      data: Record<string, never>
    }
  | {
      type: 'RESET_QUIZ'
      playerId: string
      userId: string
      timestamp: number
      data: Record<string, never>
    }
  | {
      type: 'SET_CONFIG'
      playerId: string
      userId: string
      timestamp: number
      data: {
        field: 'selectedCount' | 'displayTime' | 'selectedDifficulty' | 'playMode'
        value: any
      }
    }
  | {
      type: 'JOIN_GAME'
      playerId: string
      userId: string
      timestamp: number
      data: {
        playerId: string
        playerName: string
        emoji: string
        color: string
        userId: string
      }
    }
 
export type MemoryQuizSetConfigMove = Extract<MemoryQuizMove, { type: 'SET_CONFIG' }>