All files / web/src/lib/arcade/matching-pairs-framework define-matching-pairs-game.ts

100% Statements 71/71
100% Branches 1/1
100% Functions 1/1
100% Lines 71/71

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 721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
/**
 * Matching Pairs Framework - Top-Level Factory
 *
 * `defineMatchingPairsGame()` takes a variant definition and produces
 * a complete GameDefinition, context hook, and validator.
 */
 
import { defineGame } from '../game-sdk/define-game'
import type { GameManifest } from '../manifest-schema'
import type {
  BaseMatchingCard,
  BaseMatchingConfig,
  MatchingPairsGameBundle,
  MatchingPairsVariant,
} from './types'
import { createMatchingPairsValidator } from './create-validator'
import { createMatchingPairsProvider } from './create-provider'
import { createMatchingPairsGameComponent } from './components/GenericGameComponent'
 
export interface DefineMatchingPairsGameOptions<
  TCard extends BaseMatchingCard,
  TConfig extends BaseMatchingConfig,
> {
  /** Game manifest (for the game registry) */
  manifest: GameManifest
 
  /** The variant definition — the main extension point */
  variant: MatchingPairsVariant<TCard, TConfig>
 
  /** Optional: Runtime config validation function */
  validateConfig?: (config: unknown) => config is TConfig
}
 
/**
 * Define a complete matching-pairs game from a variant definition.
 *
 * Returns:
 * - `game`: A GameDefinition for the game registry
 * - `useMatchingPairs`: Context hook for the game's provider
 * - `validator`: Server-side validator instance
 */
export function defineMatchingPairsGame<
  TCard extends BaseMatchingCard,
  TConfig extends BaseMatchingConfig,
>(
  options: DefineMatchingPairsGameOptions<TCard, TConfig>
): MatchingPairsGameBundle<TCard, TConfig> {
  const { manifest, variant, validateConfig } = options
 
  // Create the three main pieces
  const validator = createMatchingPairsValidator(variant)
  const { Provider, useMatchingPairs } = createMatchingPairsProvider(variant)
  const GameComponent = createMatchingPairsGameComponent(variant, useMatchingPairs)
 
  // Wire into the standard game SDK
  // Cast defaultConfig and validateConfig to satisfy GameConfig's index signature
  const game = defineGame({
    manifest,
    Provider,
    GameComponent,
    validator: validator as any, // Safe: the validator satisfies GameValidator
    defaultConfig: variant.defaultConfig as any,
    validateConfig: validateConfig as any,
  })
 
  return {
    game,
    useMatchingPairs,
    validator,
  }
}