import { z } from 'zod'; import { CLUE_TYPES, DEFUSE_MODULES, SYNC_ROUND_TYPES } from '@two-keys/shared'; const hex = z.string().regex(/^#[0-9a-fA-F]{6}$/, 'expected a #rrggbb colour'); const roleInfo = z.object({ name: z.string().min(1), blurb: z.string() }); const header = { id: z.string().regex(/^[a-z0-9-]+$/), category: z.enum(['murder-mystery', 'escape-room', 'co-op']), title: z.string().min(1), tagline: z.string().min(1), estimatedMinutes: z.number().positive(), difficulty: z.number().int().min(1).max(5), players: z.object({ min: z.literal(2), max: z.literal(2) }), theme: z.object({ primary: hex, accent: hex, background: hex, headingFont: z.string().min(1) }), roles: z.object({ A: roleInfo, B: roleInfo }), intro: z.string().min(1), }; // ---------- story ---------- const clueSchema = z.object({ id: z.string().min(1), title: z.string().min(1), type: z.enum(CLUE_TYPES), body: z.string(), signal: z .string() .regex(/^[.\-/]+$/, 'signal may only contain . - /') .optional(), }); const optionSchema = z.object({ id: z.string().min(1), label: z.string().min(1) }); const lockSchema = z.object({ id: z.string().min(1), prompt: z.string().min(1), inputType: z.enum(['code', 'text', 'choice']), format: z.string().optional(), options: z.array(optionSchema).min(2).optional(), answers: z.array(z.string().min(1)).min(1), hints: z.array(z.string().min(1)).length(3), successText: z.string().optional(), keyName: z.string().optional(), }); const chapterSchema = z.object({ id: z.string().min(1), title: z.string().min(1), narrative: z.string(), clues: z.object({ A: z.array(clueSchema).default([]), B: z.array(clueSchema).default([]), shared: z.array(clueSchema).default([]), }), lock: lockSchema.extend({ successText: z.string().min(1), keyName: z.string().min(1) }), }); const finaleSchema = z.discriminatedUnion('type', [ z.object({ type: z.literal('accusation'), prompt: z.string().min(1), questions: z .array( z.object({ id: z.string().min(1), label: z.string().min(1), options: z.array(optionSchema).min(2), answer: z.string().min(1), }), ) .min(1), maxAttempts: z.number().int().positive(), successEnding: z.string().min(1), failEnding: z.string().min(1), reveal: z.string().min(1), }), z.object({ type: z.literal('escape'), prompt: z.string().min(1), clues: z.object({ A: z.array(clueSchema).default([]), B: z.array(clueSchema).default([]) }), lock: lockSchema, successEnding: z.string().min(1), /** Shown if a time limit runs out. */ failEnding: z.string().min(1).optional(), }), ]); const storySchema = z.object({ ...header, engine: z.literal('story'), rules: z .object({ hintPenaltySeconds: z.number().int().nonnegative().optional(), /** Hard limit on the clock (elapsed + penalties). Running out ends the game. */ timeLimitMinutes: z.number().positive().optional(), timeLimitLabel: z.string().min(1).optional(), }) .default({}), suspects: z .array(z.object({ id: z.string(), name: z.string(), role: z.string(), description: z.string() })) .optional(), chapters: z.array(chapterSchema).min(1), finale: finaleSchema, }); // ---------- defuse ---------- const range = z.object({ min: z.number().int().nonnegative(), max: z.number().int().nonnegative() }); const moduleId = z.enum(DEFUSE_MODULES); const indicators = z.object({ lit: z.array(z.string()), unlit: z.array(z.string()) }).optional(); const defuseSchema = z.object({ ...header, engine: z.literal('defuse'), /** Which manual the solver implements. Each manual's rules live in engines/defuse.ts. */ ruleset: z.enum(['classic', 'overtime']).default('classic'), device: z.object({ serial: z.object({ length: z.number().int().min(2), alphabet: z.string().min(2) }), batteries: range, indicators: z.object({ labels: z.array(z.string().min(1)).min(1), count: range }).optional(), }), levels: z .array( z.object({ id: z.string().min(1), title: z.string().min(1), timeSeconds: z.number().int().positive(), modules: z.array(moduleId).min(1), strikesOverride: z.number().int().positive().optional(), keyName: z.string().min(1), }), ) .min(1), strikes: z.object({ max: z.number().int().positive(), speedUpPerStrike: z.number().nonnegative() }), modules: z.object({ wires: z.object({ count: range, colors: z.array(z.string()).min(2) }), button: z.object({ labels: z.array(z.string()).min(1), colors: z.array(z.string()).min(1), stripColors: z.array(z.string()).min(1), }), keypad: z.object({ symbols: z.record(z.string()), columns: z.array(z.array(z.string()).min(4)).min(1), }), passwords: z .object({ columnsCount: z.number().int().positive(), lettersPerColumn: z.number().int().min(2).max(26), words: z.array(z.string().regex(/^[A-Z]+$/)).min(2), }) .optional(), morse: z.object({ words: z.record(z.string().regex(/^\d\.\d{3}$/)) }).optional(), simon: z.object({ colors: z.array(z.string()).min(2), stages: range }).optional(), }), manual: z.string().min(1), fixtures: z.object({ wires: z.array( z.object({ serial: z.string(), batteries: z.number().int(), indicators, wires: z.array(z.string()), expect: z.object({ type: z.literal('cut'), index: z.number().int() }), }), ), button: z.array( z.object({ batteries: z.number().int(), indicators, label: z.string(), color: z.string(), strip: z.string(), expect: z.union([ z.object({ type: z.literal('tap') }), z.object({ type: z.literal('hold'), releaseDigit: z.number().int() }), ]), }), ), keypad: z.array(z.object({ symbols: z.array(z.string()), expect: z.array(z.string()) })), passwords: z.array(z.object({ columns: z.array(z.string()), expect: z.string() })).default([]), morse: z .array(z.object({ signal: z.string(), expect: z.object({ word: z.string(), frequency: z.string() }) })) .default([]), simon: z .array( z.object({ serial: z.string(), strikes: z.number().int(), flashes: z.array(z.string()), expect: z.array(z.string()), }), ) .default([]), }), }); // ---------- sync ---------- const questionSchema = z.object({ id: z.string().min(1), q: z.string().min(1), options: z.array(z.coerce.string()).min(2), }); const telepathySchema = z.object({ id: z.string().min(1), q: z.string().min(1) }); const rankSchema = z.object({ id: z.string().min(1), q: z.string().min(1), items: z.array(z.coerce.string()).min(3), }); const syncSchema = z.object({ ...header, engine: z.literal('sync'), rules: z.object({ questionsPerRound: z.number().int().positive(), /** Default for rounds that don't set their own. */ pointsForKey: z.number().int().positive().default(3), replaysPerRound: z.number().int().nonnegative(), }), rounds: z .array( z.object({ id: z.string().min(1), title: z.string().min(1), type: z.enum(SYNC_ROUND_TYPES), subject: z.enum(['A', 'B']).optional(), /** rank rounds: who does the ranking on each question, cycled. */ subjectPattern: z.array(z.enum(['A', 'B'])).min(1).optional(), pointsForKey: z.number().int().positive().optional(), blurb: z.string(), keyName: z.string().min(1), }), ) .min(1), match: z.array(questionSchema).default([]), predict: z.array(questionSchema).default([]), telepathy: z.array(telepathySchema).default([]), rank: z.array(rankSchema).default([]), }); // ---------- labyrinth ---------- const dir = z.enum(['n', 'e', 's', 'w']); const labyrinthSchema = z.object({ ...header, engine: z.literal('labyrinth'), grid: z.array(z.string().regex(/^[#.SEKT]+$/, 'grid may only contain # . S E K T')).min(3), glyphs: z.array( z.object({ x: z.number().int(), y: z.number().int(), glyph: z.string(), label: z.string().min(1) }), ), lanterns: z.array(z.object({ x: z.number().int(), y: z.number().int(), name: z.string().min(1) })), lanternsRequired: z.number().int().positive().default(3), /** One-way gates: you may step from (x, y) in `dir`, never back the other way. */ oneWay: z.array(z.object({ x: z.number().int(), y: z.number().int(), dir })).default([]), explorer: z .object({ /** `relative` swaps N/E/S/W for forward/left/right/back from the way the Explorer faces. */ compass: z.enum(['absolute', 'relative']).default('absolute'), startFacing: dir.default('n'), }) .default({}), explorerView: z.object({ flavour: z.object({ blank: z.string(), trap: z.string(), lantern: z.string(), exitLocked: z.string(), exitOpen: z.string(), gate: z.string().optional(), }), }), navigatorView: z.object({ showMoveCount: z.boolean().default(true), /** `mirror-x` serves the map flipped left to right. */ mapTransform: z.enum(['none', 'mirror-x']).default('none'), banner: z.string().optional(), }), scoring: z.object({ trapPenaltySeconds: z.number().nonnegative(), parMoves: z.number().int().positive() }), successEnding: z.string().min(1), }); export const gameSchema = z.discriminatedUnion('engine', [ storySchema, defuseSchema, syncSchema, labyrinthSchema, ]); export type StoryGame = z.infer; export type DefuseGame = z.infer; export type SyncGame = z.infer; export type LabyrinthGame = z.infer; export type Game = z.infer; export type Clue = z.infer; export type Lock = z.infer; export type SyncQuestion = { id: string; q: string; options?: string[]; items?: string[] };