퀴즈 중 새로고침해도 이어서: 같은 계정이 돌아오면 노래가 나오는 중엔 catchup(서버 시작 시각·위치)으로 소리 끈 채 맞춰 두고 '참가 (소리 켜기)' 누르는 순간 같은 위치에서 재생(다른 사람은 안 멈춤), 준비 중엔 prepare를 다시 보내 그 사람을 기다림(예전엔 문제가 멈출 수 있었음). 지금 문제 정답도 인정. 방장은 끊겨도 15초 유지 후 넘김. 늦게 시작한 play는 지금 위치부터
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
* Hidden YouTube IFrame player with server-timed playback (docs/12 Q1, prototype tools/prototypes/iframe-sync).
|
||||
* - prepare: load muted at the start position, wait until really PLAYING (an ad would come first), pause, seek back → ready
|
||||
* (reloads by itself at 8s and 16s)
|
||||
* - play: at server time `startAt` unmute and play from `atSec`; stop by playback POSITION at `endSec`
|
||||
* - play: at server time `startAt` unmute and play from `atSec` (started late: from where everyone is now); stop by playback POSITION at `endSec`
|
||||
* (checked every frame + 10ms); 0.3s drift correction every second; buffering > 0.5s → stall; muted / not playing
|
||||
* 0.3s after the start → blocked
|
||||
* - catchUp: back in a running question (refresh / reconnect): load muted where everyone is, then play along; a fresh
|
||||
* page first waits for "참가 (소리 켜기)" (the browser blocks sound before a click), the others never stop for it
|
||||
* A fake player (localStorage mq:fakePlayer = "1") lets e2e runs play 30 questions without YouTube.
|
||||
*/
|
||||
import { create } from 'zustand';
|
||||
@@ -217,6 +219,8 @@ let bufferingSince: number | null = null;
|
||||
let stallSent = false;
|
||||
let pendingReady: { n: number; tk: number; at: number } | null = null;
|
||||
let timeline: { startAt: number; atSec: number; endSec: number | null } | null = null;
|
||||
/** catchUp loaded but this page has no click yet: play along right after "참가 (소리 켜기)". */
|
||||
let pendingCatchUp: { n: number; tk: number; startAt: number; atSec: number; endSec: number | null } | null = null;
|
||||
|
||||
const useFake = () => {
|
||||
try {
|
||||
@@ -296,13 +300,17 @@ export const player = {
|
||||
e.unlock();
|
||||
const held = pendingReady;
|
||||
pendingReady = null;
|
||||
const along = pendingCatchUp?.tk === token ? pendingCatchUp : null;
|
||||
pendingCatchUp = null;
|
||||
usePlayer.setState({ unlocked: true, needsGesture: false });
|
||||
const tk = token;
|
||||
const release = () => held && held.tk === token && report(held.n, 'ready');
|
||||
if (useFake()) return void release();
|
||||
if (useFake()) return void (along ? player.play(along.n, along.startAt, along.atSec, along.endSec) : release());
|
||||
void waitFor(() => e.state() === 1, 5000, tk).then(async () => {
|
||||
e.volume(usePlayer.getState().volume);
|
||||
if (tk !== token || timeline) return;
|
||||
// Came back mid-question: join the music where everyone is now.
|
||||
if (along) return player.play(along.n, along.startAt, along.atSec, along.endSec);
|
||||
e.pause();
|
||||
if (!held) return;
|
||||
// The question was waiting for this click: back to the position everyone resumes from, then ready.
|
||||
@@ -319,6 +327,7 @@ export const player = {
|
||||
stopLoop();
|
||||
timeline = null;
|
||||
pendingReady = null;
|
||||
pendingCatchUp = null;
|
||||
usePlayer.setState({ loading: true, playing: false, pos: startSec, clip: { start: startSec, end: endSec } });
|
||||
const e = eng();
|
||||
await e.init();
|
||||
@@ -350,6 +359,7 @@ export const player = {
|
||||
curN = n;
|
||||
stopLoop();
|
||||
pendingReady = null;
|
||||
pendingCatchUp = null;
|
||||
const e = eng();
|
||||
timeline = { startAt, atSec, endSec };
|
||||
stallSent = false;
|
||||
@@ -359,7 +369,9 @@ export const player = {
|
||||
if (tk !== token) return;
|
||||
e.volume(usePlayer.getState().volume);
|
||||
e.mute(false);
|
||||
if (Math.abs(e.time() - atSec) > 0.15) e.seek(atSec);
|
||||
// On time this is atSec; joining a running timeline (catchUp, a late start) it is where everyone is now.
|
||||
const at = atSec + Math.max(0, now() - startAt) / 1000;
|
||||
if (Math.abs(e.time() - at) > 0.15) e.seek(at);
|
||||
e.play();
|
||||
usePlayer.setState({ playing: true });
|
||||
// 소리 확인 (docs/12 Q1): 0.3s later it must be unmuted and playing.
|
||||
@@ -394,11 +406,45 @@ export const player = {
|
||||
else go();
|
||||
},
|
||||
|
||||
/** Back in a question whose music is running (refresh / reconnect): load muted where everyone is, then play along. */
|
||||
async catchUp(n: number, videoId: string, clipStart: number, startAt: number, atSec: number, endSec: number | null): Promise<void> {
|
||||
const at = () => atSec + Math.max(0, now() - startAt) / 1000;
|
||||
if (endSec !== null && at() >= endSec) return; // the clip is over: nothing to hear until the next question
|
||||
const tk = ++token;
|
||||
curN = n;
|
||||
stopLoop();
|
||||
timeline = null;
|
||||
pendingReady = null;
|
||||
pendingCatchUp = null;
|
||||
usePlayer.setState({ loading: true, playing: false, pos: at(), clip: { start: clipStart, end: endSec } });
|
||||
const e = eng();
|
||||
await e.init();
|
||||
if (tk !== token) return;
|
||||
e.mute(true);
|
||||
if (e.videoId() === videoId && e.state() !== -1 && e.state() !== 5) {
|
||||
e.seek(at());
|
||||
e.play();
|
||||
} else e.load(videoId, at());
|
||||
let ok = await waitFor(() => e.state() === 1, 8000, tk);
|
||||
if (!ok && tk === token) (e.load(videoId, at()), (ok = await waitFor(() => e.state() === 1, 8000, tk)));
|
||||
if (!ok || tk !== token) return;
|
||||
usePlayer.setState({ loading: false });
|
||||
const s = usePlayer.getState();
|
||||
if (!s.unlocked || s.needsGesture) {
|
||||
// A fresh page: the browser allows sound only after a click. Wait muted; "참가 (소리 켜기)" starts it in sync.
|
||||
e.pause();
|
||||
pendingCatchUp = { n, tk, startAt, atSec, endSec };
|
||||
return;
|
||||
}
|
||||
player.play(n, startAt, atSec, endSec);
|
||||
},
|
||||
|
||||
pause(): void {
|
||||
token++;
|
||||
stopLoop();
|
||||
timeline = null;
|
||||
pendingReady = null;
|
||||
pendingCatchUp = null;
|
||||
const e = engine;
|
||||
if (e && e.state() === 1) e.pause();
|
||||
usePlayer.setState({ playing: false, loading: false, pos: e?.time() ?? 0 });
|
||||
|
||||
@@ -136,6 +136,9 @@ function onMessage(m: ServerMessage): void {
|
||||
case 'pause':
|
||||
if (m.code === useRoom.getState().code) player.pause();
|
||||
return;
|
||||
case 'catchup':
|
||||
if (m.code === useRoom.getState().code) void player.catchUp(m.n, m.videoId, m.clipStart, m.startAt, m.atSec, m.endSec);
|
||||
return;
|
||||
case 'typing':
|
||||
if (m.code === useRoom.getState().code) useRoom.setState({ typing: m.ids });
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user