All files / web/src/arcade-games/type-racer-jr types.ts

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

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 1811x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Type Racer Jr. - Type Definitions
 *
 * Zod schemas as single source of truth. TypeScript types inferred via z.infer<>.
 */
 
import { z } from 'zod'
 
// ============================================================================
// Enums & Constants
// ============================================================================
 
export const GameModeSchema = z.enum(['free-play', 'beat-the-clock'])
export type GameMode = z.infer<typeof GameModeSchema>
 
export const DifficultyLevelSchema = z.enum(['level1', 'level2', 'level3'])
export type DifficultyLevel = z.infer<typeof DifficultyLevelSchema>
 
export const GamePhaseSchema = z.enum(['setup', 'playing', 'results'])
export type GamePhase = z.infer<typeof GamePhaseSchema>
 
export const KeyboardLayoutSchema = z.enum(['qwerty', 'dvorak', 'abc'])
export type KeyboardLayout = z.infer<typeof KeyboardLayoutSchema>
 
export const TimeLimitSchema = z.union([z.literal(60), z.literal(90), z.literal(120), z.null()])
export type TimeLimit = z.infer<typeof TimeLimitSchema>
 
// ============================================================================
// Game Entities
// ============================================================================
 
export const WordEntrySchema = z.object({
  word: z.string(),
  emoji: z.string(),
})
export type WordEntry = z.infer<typeof WordEntrySchema>
 
export const CompletedWordSchema = z.object({
  word: z.string(),
  emoji: z.string(),
  stars: z.number(),
  mistakeCount: z.number(),
  durationMs: z.number(),
})
export type CompletedWord = z.infer<typeof CompletedWordSchema>
 
export const PlayerMetadataSchema = z.object({
  id: z.string(),
  name: z.string(),
  emoji: z.string(),
  userId: z.string(),
})
export type PlayerMetadata = z.infer<typeof PlayerMetadataSchema>
 
// ============================================================================
// Game Configuration
// ============================================================================
 
export const TypeRacerJrConfigSchema = z.object({
  gameMode: GameModeSchema,
  timeLimit: TimeLimitSchema,
  startingDifficulty: DifficultyLevelSchema,
  wordCount: z.number().nullable(),
  keyboardLayout: KeyboardLayoutSchema,
  showVirtualKeyboard: z.boolean(),
})
export type TypeRacerJrConfig = z.infer<typeof TypeRacerJrConfigSchema>
 
// ============================================================================
// Game State
// ============================================================================
 
export const TypeRacerJrStateSchema = z.object({
  // Core
  gamePhase: GamePhaseSchema,
  gameMode: GameModeSchema,
  timeLimit: TimeLimitSchema,
  wordCount: z.number().nullable(),
 
  // Keyboard
  keyboardLayout: KeyboardLayoutSchema,
  showVirtualKeyboard: z.boolean(),
 
  // Difficulty
  currentDifficulty: DifficultyLevelSchema,
  consecutiveCleanWords: z.number(),
 
  // Word queue
  wordQueue: z.array(WordEntrySchema),
  currentWordIndex: z.number(),
  completedWords: z.array(CompletedWordSchema),
  usedWords: z.array(z.string()),
 
  // Scoring
  totalStars: z.number(),
  bestStreak: z.number(),
 
  // Timing
  gameStartTime: z.number().nullable(),
  currentWordStartTime: z.number().nullable(),
 
  // Player
  playerId: z.string(),
  playerMetadata: z.record(z.string(), PlayerMetadataSchema),
 
  // End game reason
  endReason: z.enum(['timer-expired', 'all-words-done', 'player-quit']).nullable(),
})
 
export type TypeRacerJrState = z.infer<typeof TypeRacerJrStateSchema>
 
// ============================================================================
// Game Moves
// ============================================================================
 
export type TypeRacerJrMove =
  | {
      type: 'START_GAME'
      playerId: string
      userId: string
      timestamp: number
      data: {
        wordQueue: WordEntry[]
        playerMetadata: Record<string, PlayerMetadata>
      }
    }
  | {
      type: 'COMPLETE_WORD'
      playerId: string
      userId: string
      timestamp: number
      data: {
        word: string
        stars: number
        mistakeCount: number
        durationMs: number
      }
    }
  | {
      type: 'ADVANCE_DIFFICULTY'
      playerId: string
      userId: string
      timestamp: number
      data: {
        newDifficulty: DifficultyLevel
        newWords: WordEntry[]
      }
    }
  | {
      type: 'END_GAME'
      playerId: string
      userId: string
      timestamp: number
      data: {
        reason: 'timer-expired' | 'all-words-done' | 'player-quit'
      }
    }
  | {
      type: 'SET_CONFIG'
      playerId: string
      userId: string
      timestamp: number
      data: {
        field:
          | 'gameMode'
          | 'timeLimit'
          | 'startingDifficulty'
          | 'wordCount'
          | 'keyboardLayout'
          | 'showVirtualKeyboard'
        value: unknown
      }
    }
  | {
      type: 'RESET_GAME'
      playerId: string
      userId: string
      timestamp: number
      data: Record<string, never>
    }