/** WebSocket protocol client→server schema (zod; server-side validation), docs/03-realtime-protocol.md. */ import { z } from 'zod'; import { CHAT_MAX } from './types'; export * from './types'; const code = z.string().regex(/^\d{6}$/); const userId = z.string().min(1).max(64); export const ClientMessage = z.discriminatedUnion('t', [ z.object({ t: z.literal('join'), code, as: z.enum(['player', 'spectator']).default('player') }), z.object({ t: z.literal('leave') }), /** Left the room page without leaving the room: seat kept, no more room messages until the next join. */ z.object({ t: z.literal('detach') }), z.object({ t: z.literal('seat'), seat: z.number().int().min(0).max(31).optional() }), z.object({ t: z.literal('unseat') }), z.object({ t: z.literal('ready'), ready: z.boolean() }), z.object({ t: z.literal('config'), gameId: z.string().max(40).optional(), options: z.unknown().optional(), maxPlayers: z.number().int().min(1).max(32).optional(), visibility: z.enum(['private', 'public']).optional(), turnSeconds: z.number().int().min(5).max(600).nullable().optional(), allowSpectators: z.boolean().optional(), chatEnabled: z.boolean().optional(), chatFilter: z.boolean().optional(), botChat: z.boolean().optional(), }), z.object({ t: z.literal('start') }), z.object({ t: z.literal('kick'), userId }), z.object({ t: z.literal('host'), userId }), z.object({ t: z.literal('addBot'), seat: z.number().int().min(0).max(31).optional() }), z.object({ t: z.literal('removeBot'), userId }), z.object({ t: z.literal('botLevel'), userId, pick: z.enum(['random', 'easy', 'normal', 'hard']) }), z.object({ t: z.literal('act'), cs: z.number().int().min(1), a: z.unknown() }), z.object({ t: z.literal('chat'), text: z.string().min(1).max(CHAT_MAX) }), z.object({ t: z.literal('emote'), id: z.string().max(20) }), z.object({ t: z.literal('rematch') }), z.object({ t: z.literal('void') }), z.object({ t: z.literal('sync') }), z.object({ t: z.literal('ping'), ts: z.number() }), ]); export type ClientMessage = z.infer;