Files
joke-app/apps/server/src/rooms/uno.room.test.ts
EJClaw ac37a6a6d8 카드 게임 시간 무제한(우노·뱅!·달무티·고스톱·도둑잡기·포커·바퀴벌레 포커·99), 공통 카드 움직임(card-fx), 우노·달무티·도둑잡기·포커 소리·애니메이션
- 무제한(null): 차례 시계·타임아웃 없음, 자리 비움만 짧은 시계, 연결 끊김은 방 유예 뒤 자동 행동 그대로
- 포커는 리바이 고르는 시간도 무제한 가능, 고스톱은 고·스톱 선택 시간도 무제한 가능, 뱅!은 반응 시간·캐릭터 고르기도
- card-fx: 날아가는 카드·칩(ghost), 제자리 날아오기, 배너, view 변화 감지 훅(처음 렌더·다시 보기는 건너뜀)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 13:04:11 +09:00

91 lines
3.9 KiB
TypeScript

/** 우노 with computers in a real room (docs/games/uno.md §9.1): a person and three computers play to the end. */
import { afterEach, expect, test } from 'bun:test';
import { GAMES } from '@bg/games';
import { botProfile, isBotId, type ServerMessage } from '@bg/shared';
import type { UnoState } from '@bg/games/uno';
import { openDb } from '../db';
import { RoomManager } from './manager';
import type { Conn, Room } from './room';
import { RoomStore } from './store';
const rooms: Room[] = [];
afterEach(() => {
for (const r of rooms.splice(0)) r.clearTimer();
});
function practice(options: Record<string, unknown> = {}) {
const store = new RoomStore(openDb(':memory:'));
let now = 1_000_000;
const clock = { now: () => now, advance: (ms: number) => (now += ms) };
const users = { publicUser: (id: string) => botProfile(id) ?? { id, nickname: id, avatar: null, kind: 'guest' as const } };
const manager = new RoomManager({ store, games: GAMES, users, now: clock.now, botDelay: () => 0 });
const room = manager.createPractice('A', 'uno') as Room;
rooms.push(room);
const msgs: ServerMessage[] = [];
const a: Conn = { userId: 'A', send: (m) => msgs.push(m), close: () => {} };
room.join(a, 'player');
room.addBot('A');
room.addBot('A');
room.configure('A', { options: { ...(room.config.options as object), ...options } });
expect(room.start('A')).toBeNull();
return { room, clock, msgs, a };
}
/** Bots move at once; when the game waits on the person, they play their first legal action (or the clock runs out). */
function playToEnd(room: Room, clock: { advance: (ms: number) => void }, humanActs: boolean): number {
const def = GAMES.uno!;
let cs = 0;
let steps = 0;
for (; steps < 20_000 && !room.game!.finished; steps++) {
if (room.bots.dueAt !== null) {
room.bots.fire();
continue;
}
const s = room.game!.state as UnoState;
const legal = def.legalActions!(s, 'A');
if (humanActs && legal.length && def.activePlayers(s).includes('A')) {
room.act('A', ++cs, legal.find((x) => (x as { type: string }).type !== 'catchUno') ?? legal[0]);
continue;
}
clock.advance(61_000);
room.onTimer();
}
return steps;
}
test('a person and three computers finish a game of 우노 (person plays)', () => {
const { room, clock } = practice();
expect(room.game!.players.filter(isBotId)).toHaveLength(3);
playToEnd(room, clock, true);
expect(room.game!.finished).toBe(true);
expect(room.status).toBe('finished');
});
test('500 points with house rules: the person only times out, the computers still finish', () => {
const { room, clock, msgs } = practice({ scoring: 'to500', stacking: true, drawUntilPlayable: true });
playToEnd(room, clock, false);
expect(room.game!.finished).toBe(true);
const s = room.game!.state as UnoState;
expect(s.rounds.length).toBeGreaterThanOrEqual(1);
expect(msgs.some((m) => m.t === 'state')).toBe(true);
// The person's screen shows card counts only for the computers.
const view = JSON.stringify(GAMES.uno!.view(s, 'A'));
for (const bot of s.seats.filter(isBotId)) for (const c of s.hands[bot]!) expect(view).not.toContain(`"${c.id}"`);
});
test('무제한 time: no clock while the person is here; a dropped connection is still auto-played after the grace time', () => {
const { room, clock, a } = practice({ turnSeconds: null });
// Let the computers move until the game waits on the person.
for (let i = 0; i < 200 && room.bots.dueAt !== null; i++) room.bots.fire();
const s = () => room.game!.state as UnoState;
expect(GAMES.uno!.activePlayers(s())).toContain('A');
expect(room.effectiveDeadline()).toBeNull();
const turn = s().turnNo;
room.disconnect(a);
const d = room.effectiveDeadline();
expect(d).not.toBeNull();
clock.advance(d! - clock.advance(0) + 1);
room.onTimer();
expect(s().turnNo !== turn || s().log.some((e) => e.t === 'timeout' && e.p === 'A')).toBe(true);
});