5~7단계 게임: 숨긴 유튜브 동기화 재생(전원 준비·버퍼링이면 모두 멈춤·30초 늦은 사람 제외·재생 오류 곡 교체), 정답 판정·점수·정답 블록, 입력 중·단축키, 힌트·스킵·다시듣기 투표, 3모드, 결과·이어풀기·다시 하기, 퀴즈 저장·불러오기 + 마지막 사람이 나가면 방 바로 닫기

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-18 22:11:14 +09:00
parent af2dce5b50
commit 7266fc8d25
27 changed files with 3461 additions and 46 deletions

View File

@@ -23,6 +23,21 @@ export const ClientMessage = z.discriminatedUnion('t', [
z.object({ t: z.literal('host'), userId: z.string().max(40) }),
/** Room chat (docs/06; stage 4 = waiting room only). `n` echoes back so the sender can confirm its optimistic line. */
z.object({ t: z.literal('chat'), text: z.string().min(1).max(CHAT_MAX), n: z.string().max(24).optional() }),
// ---- game (docs/05, stage 5~7)
/** Host: start a game from the waiting room. */
z.object({ t: z.literal('start') }),
/** Playback report for question `n` (docs/12 Q1): ready = buffered at the position, blocked = no sound, stall = buffering, clipEnd, error = player error code. */
z.object({ t: z.literal('audio'), n: z.number().int().min(1), ev: z.enum(['ready', 'blocked', 'stall', 'clipEnd', 'error']), code: z.number().int().optional() }),
/** Vote toggles (docs/05 스킵·힌트, docs/12 Q3·Q4). */
z.object({ t: z.literal('vote'), kind: z.enum(['skip', 'hint', 'replay']) }),
z.object({ t: z.literal('typing'), on: z.boolean() }),
/** Host game controls: pause/resume, end now, 이어풀기, 다시 하기 (back to the waiting room). */
z.object({ t: z.literal('game'), action: z.enum(['pause', 'resume', 'end', 'continue', 'again']) }),
/** Host: leave this player out of this question only (not ready for 30s, docs/12 Q1). */
z.object({ t: z.literal('exclude'), userId: z.string().max(40) }),
/** Host (Discord account): save / load the quiz (docs/12 Q8). */
z.object({ t: z.literal('save'), name: z.string().trim().min(1).max(40) }),
z.object({ t: z.literal('load'), id: z.number().int().positive() }),
]);
export type ClientMessage = z.infer<typeof ClientMessage>;
@@ -56,6 +71,72 @@ export interface ChatLine {
text: string;
/** Sender's nonce for its optimistic line. */
n?: string;
/**
* 정답 줄 + 5줄 블록 as one item (docs/06): the correct chat (or a system line when nobody got it) and `노래제목 - 가수`.
* none = skipped / time over (grey block).
*/
answer?: { text: string; none?: boolean };
}
export type GamePhase = 'prepare' | 'play' | 'reveal' | 'result';
export interface GameScore {
id: string;
nickname: string;
score: number;
correct: number;
/** Average answer time of the questions this player got (tie-break, docs/05 점수). */
avgMs: number | null;
online: boolean;
}
export interface GameHistoryItem {
n: number;
title: string;
artist: string;
year: number | null;
level: number | null;
genres: string[];
videoId: string | null;
winnerId: string | null;
winner: string | null;
ms: number | null;
how: 'correct' | 'skip' | 'timeout' | 'end';
}
/** Game state for everyone (docs/05). Never has the video id or the answer of the running question. */
export interface GameView {
phase: GamePhase;
n: number;
total: number;
mode: import('./room-config').Mode;
/** This question's players (heard it from the start); others join at the next one. */
participants: string[];
excluded: string[];
/** Not ready yet (prepare, or re-prepare after a pause), with the server time they started waiting. */
waiting: { id: string; since: number }[];
/** Everyone stopped (docs/12 Q1): buffering / no sound / the host paused. Timers do not run meanwhile. */
paused: null | { reason: 'stall' | 'blocked' | 'host'; who: string[] };
/** Server time the music (re)starts: before it the screen counts 3·2·1. */
startAt: number | null;
/** Question clock: elapsedMs + (runningSince ? serverNow - runningSince : 0); limitMs null = no limit. */
clock: { elapsedMs: number; runningSince: number | null; limitMs: number | null };
/** Seconds of the clip (null = 끝까지 with unknown length) for the progress bar. */
clipSec: number | null;
clipOver: boolean;
hint: string | null;
/** 자동 공개 at this many seconds (null = none / 직접선택). */
hintAtSec: number | null;
lyrics: string[] | null;
votes: { skip: string[]; hint: string[]; replay: string[] };
need: { skip: number | null; hint: number | null; replay: number | null };
scores: GameScore[];
/** Questions of this game so far, newest last; during reveal the last one is the current question. */
history: GameHistoryItem[];
/** Server time the reveal ends. */
revealUntil: number | null;
/** 이어풀기 possible: songs left that this quiz has not played. */
left: number;
}
export type ServerMessage =
@@ -63,6 +144,17 @@ export type ServerMessage =
| { t: 'room'; room: RoomView }
| { t: 'left'; code: string; reason: 'leave' | 'kicked' | 'closed' | 'elsewhere' }
| { t: 'chat'; code: string; line: ChatLine }
/** 정답 줄 + 5줄 블록 in one message (docs/06). */
| { t: 'answer'; code: string; line: ChatLine }
| { t: 'game'; code: string; game: GameView | null }
/** Load the video muted at `startSec`, buffer, report audio ready (docs/12 Q1). */
| { t: 'prepare'; code: string; n: number; videoId: string; startSec: number; endSec: number | null }
/** Everyone unmutes and plays from `atSec` at server time `startAt`; stop at `endSec` by playback position. */
| { t: 'play'; code: string; n: number; startAt: number; atSec: number; endSec: number | null }
/** Stop now (someone is buffering / the host paused / the clip is over). */
| { t: 'pause'; code: string; n: number }
| { t: 'typing'; code: string; ids: string[] }
| { t: 'saved'; id: number; name: string }
/** Recent lines for someone who just joined (or came back). */
| { t: 'chatlog'; code: string; lines: ChatLine[] }
| { t: 'notice'; text: string }