결과는 움직임이 끝날 때까지(시간 상한 없음), 제한 시간은 각자 화면이 연출을 다 보여 준 뒤부터
- lib/motion: useMotionSettled에서 12초 상한을 없앰(명시한 maxMs만) - 서버: 클라이언트 settled(seq)를 받으면 그 사람의 제한 시간을 연출에 걸린 만큼 늦춤(보고 전에는 최대 15초×속도, 보고한 적 없는 화면·컴퓨터·실시간 게임은 예전 그대로), 마감이 바뀌면 deadline 메시지 - 바퀴벌레 포커: 게임을 끝낸 판정은 패배 배너까지 다 끝난 뒤에 끝 화면 - 증강 윷놀이: 선택 창 열림을 후보 묶음마다 따로, 이의있음! 재선택 창은 이의있음! 화면이 사라진 뒤에 - e2e/motion.e2e.ts: 30초 움직임·바퀴벌레 포커 마지막 판정·이의있음! 재선택·다빈치 ×1/8 회귀 확인, 방 타이머 단위 테스트 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -61,6 +61,62 @@ describe('Room timers', () => {
|
||||
expect((room.game!.state as any).moves[0].auto).toBe(true);
|
||||
});
|
||||
|
||||
test('a turn clock starts when the player\'s screen has finished animating (settled), not before', () => {
|
||||
const { room, clock, conn } = harness();
|
||||
const a = conn('A');
|
||||
const b = conn('B');
|
||||
room.join(a, 'player');
|
||||
room.join(b, 'player');
|
||||
room.configure('A', { options: { colorAssignment: 'hostBlack', timeControl: { kind: 'perMove', perMoveSec: 10 } } });
|
||||
expect(room.start('A')).toBeNull();
|
||||
room.clearTimer();
|
||||
const base = clock.now() + 10_000;
|
||||
// A screen that never reported keeps the old clock (pages from before this change, computers).
|
||||
expect(room.effectiveDeadline()).toBe(base);
|
||||
// A's screen finishes the opening 1.5 s later: A's clock is 1.5 s longer, and everyone is told the new deadline.
|
||||
clock.advance(1_500);
|
||||
room.settled('A', room.seq);
|
||||
room.clearTimer();
|
||||
expect(room.effectiveDeadline()).toBe(base + 1_500);
|
||||
expect(b.msgs.at(-1)).toEqual({ t: 'deadline', deadline: base + 1_500 });
|
||||
clock.advance(10_000 - 1);
|
||||
room.onTimer();
|
||||
room.clearTimer();
|
||||
expect((room.game!.state as any).moves).toHaveLength(0);
|
||||
clock.advance(1);
|
||||
room.onTimer();
|
||||
room.clearTimer();
|
||||
expect((room.game!.state as any).moves).toHaveLength(1);
|
||||
// Next state, B to move: a late report for an older state counts nothing, and B (who never reported) keeps the plain clock.
|
||||
const seq = room.seq;
|
||||
const base2 = clock.now() + 10_000;
|
||||
room.settled('A', seq - 1);
|
||||
expect(room.effectiveDeadline()).toBe(base2); // B is to move and B never reported: B's clock is unchanged
|
||||
room.settled('B', seq);
|
||||
room.clearTimer();
|
||||
expect(room.effectiveDeadline()).toBe(base2);
|
||||
});
|
||||
|
||||
test('a player who reported once but not yet for this state: the timer waits for the screen (at most 15 s at ×1)', () => {
|
||||
const { room, clock, conn } = harness();
|
||||
room.join(conn('A'), 'player');
|
||||
room.join(conn('B'), 'player');
|
||||
room.configure('A', { options: { colorAssignment: 'hostBlack', timeControl: { kind: 'perMove', perMoveSec: 10 } } });
|
||||
room.start('A');
|
||||
room.settled('A', room.seq);
|
||||
room.settled('B', room.seq);
|
||||
room.clearTimer();
|
||||
// A plays; B (to move) has not reported this new state yet
|
||||
room.act('A', 1, (room.def.legalActions!(room.game!.state, 'A') as unknown[])[0]);
|
||||
room.clearTimer();
|
||||
const base = clock.now() + 10_000;
|
||||
expect(room.effectiveDeadline()).toBe(base + 15_000);
|
||||
clock.advance(2_000);
|
||||
room.settled('B', room.seq);
|
||||
room.clearTimer();
|
||||
expect(room.effectiveDeadline()).toBe(base + 2_000);
|
||||
});
|
||||
|
||||
test('disconnected player is auto-acted after the grace period, not before the turn deadline rules', () => {
|
||||
const { room, clock, conn } = harness();
|
||||
const a = conn('A');
|
||||
|
||||
@@ -115,6 +115,11 @@ export const LUCK_BUDGET_MS = 1500;
|
||||
const ERROR_LIMIT = 3;
|
||||
/** Streaming actions (drawing) are logged in batches: at most this long between snapshot writes. */
|
||||
const STREAM_FLUSH_MS = 1000;
|
||||
/**
|
||||
* Longest a player's clock waits for their screen to finish animating a change (ms at game speed ×1 before the
|
||||
* shared 1.5× slowdown; scaled like the animations). A screen that never reports (an old tab, a hidden tab) costs at most this.
|
||||
*/
|
||||
export const REVEAL_MAX_MS = 10_000;
|
||||
|
||||
export class Room {
|
||||
status: 'lobby' | 'playing' | 'finished' | 'paused' | 'broken' = 'lobby';
|
||||
@@ -154,6 +159,13 @@ export class Room {
|
||||
/** When each person last said they were typing (to drop repeats within a second). */
|
||||
private typingAt = new Map<string, number>();
|
||||
private consecutiveErrors = 0;
|
||||
/**
|
||||
* The last game state change and when each player's screen finished animating it (client `settled`). A player's
|
||||
* clock starts then, so a choice shown after the animation still gets its full time (`revealExtra`).
|
||||
*/
|
||||
private reveal: { seq: number; at: number; done: Map<string, number> } | null = null;
|
||||
/** Players whose screens report `settled` (pages from before 2026-11-08 don't: their clocks run as before). */
|
||||
private settles = new Set<string>();
|
||||
private readonly now: () => number;
|
||||
/** Plays the computer players' moves (docs/06 §12). */
|
||||
readonly bots: BotDriver;
|
||||
@@ -474,6 +486,7 @@ export class Room {
|
||||
|
||||
private broadcastState(events: unknown[], actor?: string, cs?: number): void {
|
||||
if (!this.game) return;
|
||||
this.reveal = { seq: this.seq, at: this.now(), done: new Map() };
|
||||
let spectatorMsg: ServerMessage | null = null;
|
||||
for (const c of this.conns.values()) {
|
||||
let msg: ServerMessage | null;
|
||||
@@ -1130,13 +1143,40 @@ export class Room {
|
||||
|
||||
/** The deadline clients see (`def.publicDeadline`, default the effective one); timers use `effectiveDeadline`. */
|
||||
private publicDeadline(): number | null {
|
||||
const d = this.effectiveDeadline();
|
||||
const d = this.effectiveDeadline(true);
|
||||
const g = this.game;
|
||||
return g && this.def.publicDeadline ? this.def.publicDeadline(g.state, d) : d;
|
||||
}
|
||||
|
||||
/**
|
||||
* How much later than the game's own deadline `u`'s clock runs out: the time their screen took to finish animating
|
||||
* the last change. Until their screen says so, the timer waits up to `REVEAL_MAX_MS` (scaled like the animations);
|
||||
* computers and screens that never report get no extra time;
|
||||
* `provisional` (the deadline shown to clients) counts only the time that has passed so far.
|
||||
*/
|
||||
private revealExtra(u: string, provisional = false): number {
|
||||
const r = this.reveal;
|
||||
if (!r || !this.game || this.def.realtime || !this.settles.has(u)) return 0;
|
||||
const cap = Math.round((REVEAL_MAX_MS * BOT_SLOWDOWN) / this.gameSpeed());
|
||||
const done = r.done.get(u);
|
||||
if (done !== undefined) return Math.min(cap, done - r.at);
|
||||
return provisional ? Math.min(cap, Math.max(0, this.now() - r.at)) : cap;
|
||||
}
|
||||
|
||||
/** A player's screen finished animating the state `seq` (or a later one): their clock starts now. */
|
||||
settled(userId: string, seq: number): void {
|
||||
const r = this.reveal;
|
||||
if (!r || seq < r.seq || r.done.has(userId) || !this.game || this.game.finished || !this.game.players.includes(userId)) return;
|
||||
const before = this.publicDeadline();
|
||||
this.settles.add(userId);
|
||||
r.done.set(userId, this.now());
|
||||
this.scheduleTimer();
|
||||
const after = this.publicDeadline();
|
||||
if (after !== before) for (const c of this.conns.values()) c.send({ t: 'deadline', deadline: after });
|
||||
}
|
||||
|
||||
/** Earliest time any timed player must be auto-acted for. */
|
||||
effectiveDeadline(): number | null {
|
||||
effectiveDeadline(provisional = false): number | null {
|
||||
const g = this.game;
|
||||
if (!g || g.finished || this.status !== 'playing') return null;
|
||||
const def = this.def;
|
||||
@@ -1146,7 +1186,7 @@ export class Room {
|
||||
const graceMs = def.disconnectGraceMs?.(g.state) ?? this.config.graceSec * 1000;
|
||||
let best: number | null = null;
|
||||
for (const u of timed) {
|
||||
let d = base;
|
||||
let d = base === null ? null : base + this.revealExtra(u, provisional);
|
||||
const p = this.presence.get(u);
|
||||
if (p && !p.connected) {
|
||||
const grace = p.since + graceMs;
|
||||
@@ -1190,7 +1230,7 @@ export class Room {
|
||||
const timed = def.timeoutPlayers ? def.timeoutPlayers(g.state) : def.activePlayers(g.state);
|
||||
const graceMs = def.disconnectGraceMs?.(g.state) ?? this.config.graceSec * 1000;
|
||||
for (const u of timed) {
|
||||
let d = base;
|
||||
let d = base === null ? null : base + this.revealExtra(u);
|
||||
const p = this.presence.get(u);
|
||||
if (p && !p.connected) {
|
||||
const grace = p.since + graceMs;
|
||||
|
||||
@@ -263,6 +263,9 @@ export class Gateway {
|
||||
case 'sync':
|
||||
room.sendSync(this.connFor(ws));
|
||||
return;
|
||||
case 'settled':
|
||||
room.settled(u, m.seq);
|
||||
return;
|
||||
}
|
||||
if (e) err(e);
|
||||
}
|
||||
|
||||
@@ -529,8 +529,9 @@ function Who({ id, players, me }: { id: string; players: Record<string, PublicUs
|
||||
/** The middle of the felt: the card being passed (with what was said), or the last verdict while the next card is chosen. */
|
||||
function Table({ v, me, players, fresh, over }: { v: CockroachView; me: string | null; players: Record<string, PublicUser>; fresh: { slide: boolean; flip: boolean }; over: boolean }) {
|
||||
const r = v.round;
|
||||
// The call that ended the game is played out like any other (card turns, stamp, flight) before the end shows.
|
||||
const lastCall = v.phase === 'finished' && fresh.flip && !over ? v.lastReveal : null;
|
||||
// The call that ended the game is played out like any other (card turns, stamp, flight, the loss banner): the end
|
||||
// shows only once all of it has finished (`over`), not when the card's own flip is done.
|
||||
const lastCall = v.phase === 'finished' && !over ? v.lastReveal : null;
|
||||
if (v.phase === 'finished' && !lastCall) {
|
||||
const f = v.final!;
|
||||
return (
|
||||
|
||||
@@ -333,23 +333,26 @@ export function AugSelect({ v, act, pending, players, blocked = false }: { v: Yu
|
||||
const sel = v.aug!.select!;
|
||||
const [choice, setChoice] = useState<AugId | null>(null);
|
||||
const [target, setTarget] = useState<AugId | ''>('');
|
||||
const [open, setOpen] = useState(false);
|
||||
const mine = sel.offers.length > 0 && sel.picksLeft > 0;
|
||||
const key = sel.offers.join(',');
|
||||
// New offers open the window by themselves, but only after the board has stopped moving (the last move's hop,
|
||||
// the 이의있음! call, `blocked`) so nothing pops up over an animation.
|
||||
const [autoOpen, setAutoOpen] = useState(mine);
|
||||
const ready = useMotionSettled(autoOpen && mine && !blocked);
|
||||
// The window belongs to one set of offers: new offers (혼돈 mode's second pick, the 이의있음! redo) start closed.
|
||||
const [openKey, setOpenKey] = useState<string | null>(null);
|
||||
const open = mine && openKey === key;
|
||||
const setOpen = (on: boolean) => setOpenKey(on ? key : null);
|
||||
// New offers open the window by themselves, but only after the board has stopped moving (the last move's hop)
|
||||
// and the 이의있음! call is gone (`blocked`), so nothing pops up over an animation.
|
||||
const [autoKey, setAutoKey] = useState<string | null>(mine ? key : null);
|
||||
const ready = useMotionSettled(mine && autoKey === key && !blocked);
|
||||
useEffect(() => {
|
||||
setChoice(sel.forced);
|
||||
setTarget('');
|
||||
if (mine) setAutoOpen(true);
|
||||
setAutoKey(mine ? key : null);
|
||||
}, [key]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
useEffect(() => {
|
||||
if (!ready) return;
|
||||
setOpen(true);
|
||||
setAutoOpen(false);
|
||||
}, [ready]);
|
||||
setOpenKey(key);
|
||||
setAutoKey(null);
|
||||
}, [ready]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
const tierText = { start: '범용 + 시작 전용', mid: '범용 + 중간 전용', final: '범용 + 마무리 전용' }[sel.tier];
|
||||
const targets = choice === 'detective' ? sel.detectiveTargets : choice === 'delivery' ? sel.deliveryTargets : [];
|
||||
const needTarget = choice === 'detective' || (choice === 'delivery' && targets.length > 0);
|
||||
|
||||
@@ -122,12 +122,10 @@ export function useMotionHold(on: boolean): void {
|
||||
|
||||
/** The screen must stay still this long (real ms) before results show: bridges the gap between two staged steps. */
|
||||
export const SETTLE_MS = 220;
|
||||
/** Never wait longer than this (real ms, at least): a stuck or very long animation must not hide the result. */
|
||||
const MAX_WAIT_MS = 12_000;
|
||||
|
||||
/**
|
||||
* False while `when` is false; once `when` turns true, becomes true after the game screen has been still for
|
||||
* `SETTLE_MS` (or after the longest wait). Stays true until `when` turns false again.
|
||||
* `SETTLE_MS`, however long the animation takes (no time limit: a result never shows over a running animation).
|
||||
* `maxMs` is only for callers that must show something by a deadline of their own (a round break of a real-time game).
|
||||
*/
|
||||
export function useMotionSettled(when = true, maxMs?: number): boolean {
|
||||
const [settled, setSettled] = useState(false);
|
||||
@@ -141,7 +139,7 @@ export function useMotionSettled(when = true, maxMs?: number): boolean {
|
||||
return;
|
||||
}
|
||||
const start = performance.now();
|
||||
const limit = maxMs ?? Math.max(MAX_WAIT_MS, fxMs(8000));
|
||||
const limit = maxMs ?? Infinity;
|
||||
let stillSince: number | null = null;
|
||||
let raf = 0;
|
||||
const tick = (now: number) => {
|
||||
@@ -161,7 +159,7 @@ export function useMotionSettled(when = true, maxMs?: number): boolean {
|
||||
|
||||
/**
|
||||
* `value` as of the last time the game screen stood still: a new value shows only after the animation that goes
|
||||
* with it has played (a status line saying who won, a score, the result of a throw). `maxMs` caps the wait.
|
||||
* with it has played (a status line saying who won, a score, the result of a throw). `maxMs`: see useMotionSettled.
|
||||
*/
|
||||
export function useAfterMotionValue<T>(value: T, maxMs?: number): T {
|
||||
const [shown, setShown] = useState(value);
|
||||
|
||||
@@ -309,6 +309,10 @@ class GameSocket {
|
||||
useRoom.setState(patch);
|
||||
break;
|
||||
}
|
||||
case 'deadline':
|
||||
// A player's screen finished animating: their clock started, so the deadline moved (same view).
|
||||
if (st.game) useRoom.setState({ game: { ...st.game, deadline: m.deadline } });
|
||||
break;
|
||||
case 'reject':
|
||||
if (m.cs === st.pending) useRoom.setState({ pending: null, slow: false });
|
||||
toast(m.reason, 'error');
|
||||
|
||||
@@ -676,6 +676,12 @@ function GameView({ room, ui, me, players, pending, slow }: { room: RoomView; ui
|
||||
usePlayArea(playArea);
|
||||
const myTurn = ui.isMyTurn(game.view, me.id);
|
||||
const isPlayer = room.seats.some((s) => s?.user.id === me.id) && game.active !== undefined;
|
||||
// Tell the server when this screen has finished animating the latest state: my clock starts then, so a choice that
|
||||
// appears after the animation still gets its whole time.
|
||||
const settledSeq = useAfterMotionValue(game.seq);
|
||||
useEffect(() => {
|
||||
if (isPlayer && room.status === 'playing') socket.send({ t: 'settled', seq: settledSeq });
|
||||
}, [settledSeq]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
const props = {
|
||||
view: game.view,
|
||||
me: me.id,
|
||||
|
||||
Reference in New Issue
Block a user