게임 속도 배율: 1배도 애니메이션·연출·컴퓨터 생각 시간을 예전의 1.5배로 늦추고, 관리자 사이트 설정 "게임 속도 배율"(1/16~16배, 기본 1)로 나눠 조절. 저장하면 열린 방에도 바로 적용(RoomView.gameSpeed 재전송)

- 서버: SiteSettings.gameSpeed, /api/config에 노출, 컴퓨터 생각 시간(defaultBotDelay)과 마피아·라이어 컴퓨터가 살펴보는 간격에 BOT_SLOWDOWN(1.5)/배율. 게임이 정한 반응 시각(원카드 외치기)은 그대로
- 웹: lib/motion.ts — .game-layout 안 CSS 애니메이션·트랜지션은 useMotionScope가 playbackRate로, Web Animations는 fxAnim, 연출 타이머(배너 유지·다음 단계·복사본 정리·숫자 세기·다시 보기 한 칸)는 fxMs. 채팅·role="timer"·규칙 시간·꾹 눌러 보기는 제외
- 관리자 사이트 탭에 배율 버튼(1/16~16)과 직접 입력
- 테스트: 배율 검증(API 400/200, 열린 방 즉시 반영), 컴퓨터 생각 시간 배율, motion 단위 테스트, e2e/speed.e2e.ts(돌 애니메이션 재생 배율·컴퓨터 응답 시간), 관리자 e2e에 배율 저장
- 문서: 14-admin §5, 07-ui-ux, 06-lobby-rooms, 13-decisions, README

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-14 01:24:25 +09:00
parent 5e8f5b821f
commit 18ef402a6a
54 changed files with 536 additions and 134 deletions

View File

@@ -0,0 +1,33 @@
import { afterEach, expect, test } from 'bun:test';
import { BASE_SLOWDOWN, fxAnim, fxMs, gameSpeed, motionScale, setGameSpeed } from './motion';
afterEach(() => setGameSpeed(1));
test('speed ×1 is a little slower than the written lengths; the admin speed divides them', () => {
expect(BASE_SLOWDOWN).toBe(1.5);
expect(fxMs(1000)).toBe(1500);
setGameSpeed(2);
expect(fxMs(1000)).toBe(750);
setGameSpeed(1 / 16);
expect(fxMs(1000)).toBe(24_000);
setGameSpeed(16);
expect(fxMs(1600)).toBe(150);
});
test('out-of-range or broken speeds are clamped / ignored', () => {
setGameSpeed(100);
expect(gameSpeed()).toBe(16);
setGameSpeed(0.001);
expect(gameSpeed()).toBe(1 / 16);
setGameSpeed('fast');
expect(gameSpeed()).toBe(1);
setGameSpeed(undefined);
expect(motionScale()).toBe(1.5);
});
test('fxAnim plays a Web Animation at the game speed (delay and duration together)', () => {
const anim = { playbackRate: 1 } as Animation;
setGameSpeed(3);
expect(fxAnim(anim).playbackRate).toBe(2);
expect(fxAnim(null)).toBeNull();
});

View File

@@ -0,0 +1,79 @@
/**
* Game speed on screen (admin setting `gameSpeed`, docs/14-admin.md §5; docs/07-ui-ux.md).
* Everything that moves in a game takes `motionScale()` times the length written in the code:
* - CSS animations and transitions inside the game screen: `useMotionScope` sets their `playbackRate`
* (delays and staggers included), so game CSS keeps its plain durations.
* - Web Animations (`el.animate(...)`): wrap them in `fxAnim` (flying copies live on <body>, outside the scope).
* - Timers that stage effects (how long a banner stays, the next step of a sequence, removing a flying copy): `fxMs`.
* Real-time things (turn timers, `[role="timer"]` bars, the drawing stream) and the chat are not scaled.
*/
import { useEffect, type RefObject } from 'react';
import { clampGameSpeed, GAME_SPEED_DEFAULT } from '@bg/shared/lite';
/** At speed ×1 everything takes this much longer than the durations written in the code (2026-10-14: a bit slower). */
export const BASE_SLOWDOWN = 1.5;
/** Animations inside these are left alone (real time / not part of the game). */
const SKIP = '.chat-panel, [role="timer"]';
let speed = GAME_SPEED_DEFAULT;
const listeners = new Set<() => void>();
/** Sets the speed from /api/config or the room view (out-of-range values are clamped). */
export function setGameSpeed(x: unknown): void {
const v = clampGameSpeed(x);
if (v === speed) return;
speed = v;
for (const f of listeners) f();
}
export const gameSpeed = (): number => speed;
/** How many times longer than written: `BASE_SLOWDOWN / speed`. */
export const motionScale = (): number => BASE_SLOWDOWN / speed;
/** A staging delay / duration in ms at the current game speed. */
export const fxMs = (ms: number): number => Math.round(ms * motionScale());
/** Plays a Web Animation at the current game speed (its duration, delay and staggers all scale). */
export function fxAnim<T extends Animation | null | undefined>(anim: T): T {
if (anim) anim.playbackRate = 1 / motionScale();
return anim;
}
function targetOf(a: Animation): Element | null {
const t = a.effect instanceof KeyframeEffect ? a.effect.target : null;
return t instanceof Element ? t : null;
}
/** Sets the playback rate of every animation under `root` (except chat / timers). Exported for tests. */
export function applyMotion(root: Element): void {
const rate = 1 / motionScale();
for (const a of root.getAnimations({ subtree: true })) {
if (a.playbackRate === rate) continue;
const t = targetOf(a);
if (t && t.closest(SKIP)) continue;
a.playbackRate = rate;
}
}
/**
* Keeps the CSS animations and transitions under `ref` (the game screen) at the game speed. Game animations start
* from DOM changes (a new element, a class, a style); the mutation observer runs before the next frame, so a new
* animation never plays a frame at the wrong speed.
*/
export function useMotionScope(ref: RefObject<Element | null>): void {
useEffect(() => {
const root = ref.current;
if (!root || typeof MutationObserver === 'undefined' || typeof root.getAnimations !== 'function') return;
const run = () => applyMotion(root);
const obs = new MutationObserver(run);
obs.observe(root, { subtree: true, childList: true, attributes: true });
listeners.add(run);
run();
return () => {
obs.disconnect();
listeners.delete(run);
};
}, [ref]);
}