퀴즈 중 새로고침해도 이어서: 같은 계정이 돌아오면 노래가 나오는 중엔 catchup(서버 시작 시각·위치)으로 소리 끈 채 맞춰 두고 '참가 (소리 켜기)' 누르는 순간 같은 위치에서 재생(다른 사람은 안 멈춤), 준비 중엔 prepare를 다시 보내 그 사람을 기다림(예전엔 문제가 멈출 수 있었음). 지금 문제 정답도 인정. 방장은 끊겨도 15초 유지 후 넘김. 늦게 시작한 play는 지금 위치부터
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -236,6 +236,60 @@ describe('game flow', () => {
|
||||
stop();
|
||||
});
|
||||
|
||||
test('새로고침 while the music runs: the new page catches up on the same timeline, nobody else stops, the answer still counts', async () => {
|
||||
seedLater(() => seed(2));
|
||||
const { users, cls, code } = await room(['방장', '새로고침']);
|
||||
const [a, b] = cls as [Client, Client];
|
||||
const stop = autoReady([a, b]);
|
||||
a.send({ t: 'start' });
|
||||
const play = await nextOf(a, 'play', 0);
|
||||
await sleep(Math.max(0, play.startAt - Date.now()) + 30);
|
||||
const fa = since(a);
|
||||
b.close();
|
||||
await gameWhere(a, (g) => g.scores.some((x) => x.id === users[1]!.id && !x.online));
|
||||
const b2 = await new Client(s.base, users[1]!.cookie).connect();
|
||||
b2.send({ t: 'join', code });
|
||||
const cu = await nextOf(b2, 'catchup', 0);
|
||||
const prep = (await nextOf(a, 'prepare', 0)) as Msg<'prepare'>;
|
||||
expect(cu).toMatchObject({ n: 1, videoId: prep.videoId, clipStart: prep.startSec, startAt: play.startAt, atSec: play.atSec, endSec: play.endSec });
|
||||
expect(b2.messages.some((m) => m.t === 'prepare' || m.t === 'play')).toBe(false);
|
||||
// the others were never paused or re-prepared, and no "들어왔어요" line for someone who was already here
|
||||
await sleep(60);
|
||||
expect(a.messages.slice(fa).some((m) => m.t === 'pause' || m.t === 'prepare')).toBe(false);
|
||||
expect(a.messages.slice(fa).some((m) => m.t === 'chat' && m.line.text.includes('들어왔어요'))).toBe(false);
|
||||
// still a player of this question: the answer counts
|
||||
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(prep.videoId)!.title;
|
||||
b2.send({ t: 'chat', text: title });
|
||||
expect((await gameWhere(a, (g) => g.phase === 'reveal')).history[0]).toMatchObject({ winnerId: users[1]!.id, how: 'correct' });
|
||||
stop();
|
||||
b2.close();
|
||||
});
|
||||
|
||||
test('새로고침 while waiting for ready: the new page gets the prepare again and the room waits for it (no stuck question)', async () => {
|
||||
seedLater(() => seed(2));
|
||||
const { users, cls, code } = await room(['방장', '새로고침']);
|
||||
const [a, b] = cls as [Client, Client];
|
||||
a.send({ t: 'start' });
|
||||
const prep = await nextOf(a, 'prepare', 0);
|
||||
await nextOf(b, 'prepare', 0);
|
||||
b.send({ t: 'audio', n: 1, ev: 'ready' });
|
||||
b.close();
|
||||
await gameWhere(a, (g) => g.scores.some((x) => x.id === users[1]!.id && !x.online));
|
||||
const b2 = await new Client(s.base, users[1]!.cookie).connect();
|
||||
b2.send({ t: 'join', code });
|
||||
expect(await nextOf(b2, 'prepare', 0)).toMatchObject({ n: 1, videoId: prep.videoId, startSec: prep.startSec });
|
||||
// the old page's ready is gone with it: 방장 alone does not start the music
|
||||
const fa = since(a);
|
||||
a.send({ t: 'audio', n: 1, ev: 'ready' });
|
||||
expect((await gameWhere(a, (g) => g.waiting.some((w) => w.id === users[1]!.id))).phase).toBe('prepare');
|
||||
await sleep(60);
|
||||
expect(a.messages.slice(fa).some((m) => m.t === 'play')).toBe(false);
|
||||
b2.send({ t: 'audio', n: 1, ev: 'ready' });
|
||||
const pa = await nextOf(a, 'play', fa);
|
||||
expect((await nextOf(b2, 'play', 0)).startAt).toBe(pa.startAt);
|
||||
b2.close();
|
||||
});
|
||||
|
||||
test('player error 150 swaps the song for everyone under the same number and marks it unplayable', async () => {
|
||||
seedLater(() => seed(3));
|
||||
const { cls } = await room(['방장', '철수']);
|
||||
|
||||
@@ -504,6 +504,24 @@ export class Game {
|
||||
this.broadcast();
|
||||
}
|
||||
|
||||
/**
|
||||
* A player of this question is back (refresh / reconnect, same account within the grace period): bring their player to
|
||||
* where everyone is. Waiting for ready → prepare again (their new page has nothing loaded, so the room waits for them);
|
||||
* music running → catch up on the running timeline without stopping anyone. They can answer as before.
|
||||
*/
|
||||
rejoin(userId: string, send: (m: ServerMessage) => void): void {
|
||||
if (!this.listening || !this.song?.videoId || !this.participants.has(userId) || this.excluded.has(userId)) return;
|
||||
if (this.phase !== 'prepare' && this.phase !== 'play') return;
|
||||
if (this.awaitingReady) {
|
||||
this.ready.delete(userId);
|
||||
if (!this.waitingSince.has(userId)) this.waitingSince.set(userId, this.now());
|
||||
send({ t: 'prepare', code: this.room.code, n: this.n, videoId: this.song.videoId, startSec: this.resumeAt, endSec: this.clip.endSec });
|
||||
this.broadcast();
|
||||
} else if (this.playing && !this.clipOver) {
|
||||
send({ t: 'catchup', code: this.room.code, n: this.n, videoId: this.song.videoId, clipStart: this.clip.startSec, startAt: this.audioStartAt, atSec: this.audioAtSec, endSec: this.clip.endSec });
|
||||
}
|
||||
}
|
||||
|
||||
/** Host: leave out someone who has not been ready for 30s (docs/12 Q1). */
|
||||
exclude(target: string): string | null {
|
||||
const since = this.waitingSince.get(target);
|
||||
|
||||
@@ -150,6 +150,27 @@ describe('rooms over WebSocket', () => {
|
||||
expect((await nextRoom(ca, (x) => x.players.length === 1)).players[0]!.id).toBe(a.id);
|
||||
});
|
||||
|
||||
test('a host who drops (새로고침) stays host for 15s; back in time keeps it, gone longer hands it to someone online', async () => {
|
||||
const { a, b, ca, cb, code } = await setup();
|
||||
ca.close();
|
||||
await nextRoom(cb, (x) => x.players.some((p) => p.id === a.id && !p.online));
|
||||
s.rooms.sweep(Date.now() + 10_000);
|
||||
expect(s.rooms.rooms.get(code)!.hostId).toBe(a.id);
|
||||
// back (a refresh): still host, nothing said
|
||||
const ca2 = await new Client(s.base, ca.cookie).connect();
|
||||
ca2.send({ t: 'join', code });
|
||||
await nextRoom(cb, (x) => x.players.every((p) => p.online));
|
||||
s.rooms.sweep(Date.now() + 20_000);
|
||||
expect(s.rooms.rooms.get(code)!.hostId).toBe(a.id);
|
||||
expect(chatOf(cb).some((l) => l.text.startsWith('방장이 '))).toBe(false);
|
||||
// gone for good: after 15s the next player online takes over
|
||||
ca2.close();
|
||||
await nextRoom(cb, (x) => x.players.some((p) => p.id === a.id && !p.online));
|
||||
s.rooms.sweep(Date.now() + 16_000);
|
||||
expect((await nextRoom(cb, (x) => x.hostId === b.id)).hostId).toBe(b.id);
|
||||
await cb.waitFor((m) => m.t === 'chat' && m.line.text === '방장이 손님님으로 바뀌었어요');
|
||||
});
|
||||
|
||||
test('no player limit: 40 people in one room', async () => {
|
||||
const { ca, code } = await setup();
|
||||
const clients: Client[] = [];
|
||||
|
||||
@@ -12,6 +12,8 @@ import type { WsData } from '../ws/gateway';
|
||||
|
||||
/** Back within this long after a disconnect: keeps the place (and later the score). */
|
||||
export const GRACE_MS = 60_000;
|
||||
/** The host dropped (refresh, short disconnect): stays host this long before someone online takes over. */
|
||||
export const HOST_HOLD_MS = 15_000;
|
||||
const EMPTY_ROOM_MS = 5 * 60_000;
|
||||
/** Chat lines a newcomer gets (the screen keeps 200, docs/06). */
|
||||
export const CHAT_HISTORY = 100;
|
||||
@@ -235,6 +237,7 @@ export class RoomManager {
|
||||
if (prev && prev !== room) this.removePlayer(prev, userId, 'elsewhere');
|
||||
if (ws.data.roomCode && ws.data.roomCode !== code) this.detach(ws);
|
||||
const p = already ?? { joinedAt: Date.now(), ready: false, conns: new Set(), offlineSince: null };
|
||||
const wasHere = !!already && ws.data.roomCode !== code;
|
||||
p.conns.add(ws);
|
||||
p.offlineSince = null;
|
||||
room.players.set(userId, p);
|
||||
@@ -244,8 +247,11 @@ export class RoomManager {
|
||||
if (!room.players.has(room.hostId)) room.hostId = userId;
|
||||
ws.send(JSON.stringify({ t: 'chatlog', code, lines: room.chat } satisfies ServerMessage));
|
||||
room.broadcast();
|
||||
if (room.game) room.game.broadcast();
|
||||
else ws.send(JSON.stringify({ t: 'game', code, game: null } satisfies ServerMessage));
|
||||
if (room.game) {
|
||||
room.game.broadcast();
|
||||
// Refresh / reconnect in the middle of a question: this socket's player joins the music where everyone is.
|
||||
if (wasHere) room.game.rejoin(userId, (m) => ws.send(JSON.stringify(m)));
|
||||
} else ws.send(JSON.stringify({ t: 'game', code, game: null } satisfies ServerMessage));
|
||||
if (!already) room.say(null, room.game && room.game.phase !== 'result' ? `${room.nick(userId)}님이 들어왔어요 · 다음 문제부터 같이 해요` : `${room.nick(userId)}님이 들어왔어요`);
|
||||
return null;
|
||||
}
|
||||
@@ -262,10 +268,8 @@ export class RoomManager {
|
||||
p.offlineSince = Date.now();
|
||||
p.ready = false;
|
||||
room.typing.delete(ws.data.userId);
|
||||
const wasHost = room.hostId === ws.data.userId;
|
||||
if (wasHost) room.pickHost();
|
||||
// A host who dropped keeps the role for HOST_HOLD_MS (a refresh is back in a second or two): see sweep().
|
||||
room.broadcast();
|
||||
if (wasHost && room.hostId !== ws.data.userId) room.say(null, `방장이 ${room.nick(room.hostId)}님으로 바뀌었어요`);
|
||||
room.game?.onGone(ws.data.userId);
|
||||
}
|
||||
}
|
||||
@@ -524,9 +528,18 @@ export class RoomManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Every 10s: drop players gone longer than the grace period, close rooms created but never entered. */
|
||||
/** Every 5s: hand over the host role, drop players gone longer than the grace period, close rooms created but never entered. */
|
||||
sweep(now = Date.now()): void {
|
||||
for (const room of this.rooms.values()) {
|
||||
const host = room.players.get(room.hostId);
|
||||
if (host?.offlineSince && now - host.offlineSince > HOST_HOLD_MS && room.online().length) {
|
||||
const before = room.hostId;
|
||||
room.pickHost();
|
||||
if (room.hostId !== before) {
|
||||
room.broadcast();
|
||||
room.say(null, `방장이 ${room.nick(room.hostId)}님으로 바뀌었어요`);
|
||||
}
|
||||
}
|
||||
for (const [id, p] of room.players) if (p.offlineSince && now - p.offlineSince > GRACE_MS) this.removePlayer(room, id, 'closed');
|
||||
if (room.players.size === 0 && room.emptySince && now - room.emptySince > EMPTY_ROOM_MS) this.close(room);
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ export function startServer(opts: StartOptions) {
|
||||
},
|
||||
});
|
||||
|
||||
const roomSweeper = setInterval(() => rooms.sweep(), 10_000);
|
||||
const roomSweeper = setInterval(() => rooms.sweep(), 5_000);
|
||||
// 입력 중 goes out at most once a second (docs/06); chat itself never waits.
|
||||
const typingFlush = setInterval(() => rooms.flushTyping(), 1000);
|
||||
// The collector runs in its own process, so lobbies re-count now and then to show newly matched songs.
|
||||
|
||||
Reference in New Issue
Block a user