Merge branch 'wip/game-hot-streak' into codex/owner/grp_discord_joke-app

# Conflicts:
#	apps/web/src/games/help.test.ts
#	apps/web/src/games/registry.ts
#	apps/web/src/lib/catalog-ui.ts
#	docs/games/README.md
#	e2e/games.e2e.ts
#	packages/games/package.json
#	packages/games/src/bots/index.ts
#	packages/games/src/index.ts
#	packages/shared/src/bots.ts
#	packages/shared/src/catalog.ts
This commit is contained in:
EJClaw
2026-10-15 23:29:17 +09:00
26 changed files with 5216 additions and 2 deletions

View File

@@ -0,0 +1,150 @@
/** 핫스트릭 through a real Room: races flip on the server clock only, 빨리 보기 needs everyone, practice games finish (docs/games/hot-streak.md §10). */
import { afterEach, describe, expect, test } from 'bun:test';
import { GAMES } from '@bg/games';
import type { HotStreakAction, HotStreakState, HotStreakView } from '@bg/games/hot-streak';
import { botProfile, isBotId, type ServerMessage } from '@bg/shared';
import { openDb } from '../db';
import { RoomManager } from './manager';
import { Room, type Conn } from './room';
import { RoomStore } from './store';
const rooms: Room[] = [];
afterEach(() => {
for (const r of rooms.splice(0)) r.clearTimer();
});
function harness() {
const db = openDb(':memory:');
const store = new RoomStore(db);
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 conn = (userId: string) => {
const msgs: ServerMessage[] = [];
const c: Conn & { msgs: ServerMessage[] } = { userId, msgs, send: (m) => msgs.push(m), close: () => {} };
return c;
};
return { db, clock, manager, conn };
}
type StateMsg = Extract<ServerMessage, { t: 'state' }>;
const views = (c: { msgs: ServerMessage[] }) => c.msgs.filter((m): m is StateMsg => m.t === 'state').map((m) => m.view as HotStreakView);
/** What the person presses, read only from their own screen. */
function personMove(v: HotStreakView, n: number): HotStreakAction | null {
const l = v.legal;
if (l.draft) {
const o = l.draft[n % l.draft.length]!;
return { type: 'DRAFT', stack: o.stack, side: n % 3 === 0 ? 'risky' : 'safe' };
}
if (l.double) return { type: 'DOUBLE', index: 0 };
if (l.submit) return { type: 'SUBMIT', cards: l.submit.cards.slice(0, l.submit.count) };
if (l.pot) return { type: 'POT_BET', mascot: 'D', last: false, side: 'NO', doubled: l.pot.doubleRequired ? 'mascot' : null };
if (l.ready) return { type: 'READY' };
return null;
}
function tick(h: ReturnType<typeof harness>, room: Room, ms = 250) {
h.clock.advance(ms);
if (room.bots.dueAt !== null && room.bots.dueAt <= h.clock.now()) room.bots.fire();
const d = room.effectiveDeadline();
if (d !== null && d <= h.clock.now()) room.onTimer();
}
describe('핫스트릭 room flow', () => {
test.each([1, 3])('컴퓨터 %i명과 연습: 사람은 자기 화면만 보고 끝까지, 남이 넣은 카드는 안 보여요', (nBots) => {
const h = harness();
let room: Room;
if (nBots === 1) room = h.manager.createPractice('A', 'hot-streak') as Room;
else {
const r = h.manager.create('A', 'hot-streak', 'private');
if (typeof r === 'string') throw new Error(r);
room = r;
for (let i = 0; i < nBots; i++) expect(room.addBot('A')).toBeNull();
}
rooms.push(room);
const a = h.conn('A');
room.join(a, 'player');
expect(room.start('A')).toBeNull();
const bots = room.game!.players.filter(isBotId);
expect(bots).toHaveLength(nBots);
let cs = 0;
let mine = 0;
let seen = 0;
for (let i = 0; i < 60_000 && !room.game!.finished; i++) {
tick(h, room);
const vs = views(a);
for (const v of vs.slice(seen)) {
expect(v.me).toBe('A');
const st = room.game!.state as HotStreakState;
for (const b of bots) for (const c of st.hands[b] ?? []) expect(JSON.stringify(v)).not.toContain(c);
}
seen = vs.length;
const v = vs.at(-1);
const m = v && personMove(v, mine);
if (m) {
room.act('A', ++cs, m);
mine++;
}
}
expect(room.game!.finished).toBe(true);
expect(a.msgs.filter((m) => m.t === 'reject')).toEqual([]);
const s = room.game!.state as HotStreakState;
expect(s.results).toHaveLength(3);
expect(s.timeouts.A).toBe(0);
expect(mine).toBeGreaterThan(6);
const row = h.db.query<{ result_json: string }, []>('SELECT result_json FROM games').get()!;
expect(JSON.parse(row.result_json).practice).toBe(true);
});
test('경주는 서버 시계로만 넘어가요: 끊긴 사람이 있어도 2초마다 한 장, 모두 [빨리 보기]면 바로 끝', () => {
const h = harness();
const r = h.manager.create('A', 'hot-streak', 'private');
if (typeof r === 'string') throw new Error(r);
const room = r;
rooms.push(room);
const a = h.conn('A');
const b = h.conn('B');
const c = h.conn('C');
room.join(a, 'player');
room.join(b, 'player');
room.join(c, 'player');
expect(room.start('A')).toBeNull();
let cs = 0;
const st = () => room.game!.state as HotStreakState;
// Everyone plays the betting steps from their own view.
for (let i = 0; i < 400 && st().phase !== 'race'; i++) {
for (const [p, cn] of [['A', a], ['B', b], ['C', c]] as const) {
const v = views(cn).at(-1);
const m = v && personMove(v, i);
if (m) room.act(p, ++cs, m);
}
tick(h, room, 100);
}
expect(st().phase).toBe('race');
// C drops: nothing speeds up (the driver's grace is 0 but cards wait for their time).
room.disconnect(c);
const flipAt = st().race!.nextFlipAt;
expect(room.effectiveDeadline()).toBe(flipAt);
const before = st().race!.processed;
h.clock.advance(flipAt - h.clock.now() - 10);
room.onTimer();
expect(st().race!.processed).toBe(before);
h.clock.advance(10);
room.onTimer();
expect(st().race!.processed).toBe(before + 1);
// Next card two seconds later.
expect(room.effectiveDeadline()).toBe(h.clock.now() + 2000);
// 빨리 보기: A and B agree, C (offline) has not — still running.
room.act('A', ++cs, { type: 'HURRY' });
room.act('B', ++cs, { type: 'HURRY' });
if (st().phase === 'race') {
expect(st().race!.hurry.sort()).toEqual(['A', 'B']);
room.join(c, 'player');
room.act('C', ++cs, { type: 'HURRY' });
}
expect(st().phase).toBe('payout');
expect(st().race!.over).toBe(true);
});
});