새 게임 핫스트릭: 마스코트 넷의 카드 경주에 티켓으로 거는 파티 게임(2~10명, 놀이용 가상 돈)
- 엔진 packages/games/src/hot-streak/(cards·rules·race·index·sim·bot): 뱀 순서 티켓 뽑기(안전/위험 면) → 비밀 카드 넣기 → 경주 → 정산, 경주 3번 - 경주는 서버 타이머가 flipSeconds마다 카드 한 장씩(timeoutPlayers 한 자리 + timeoutNotBefore, 끊겨도 빨라지지 않음), 모두 [빨리 보기]면 끝까지 바로 - 카드 53장·충돌·기어가기·꺾기·초록 카드·허들·경주로 접기(13→10→7→3→0)·시상대 공동 실격·사이드 질문 12개 판정 - 섞을 때마다 카드 id 접미사(#gen)를 새로 붙여 유출 검사가 헷갈리지 않음 - §2 옵션 13개 모두: bettingMode(9~10명 상금 나누기 포함), races, finalBet, draftStart, reverseBets, cardsPerRace, burn, spicyDeck, hurdles, potScale, lifeStory, turnSeconds, flipSeconds - 컴퓨터: 공개 정보로 덱을 추정해 몬테카를로 경주(레벨 1: 30번 ~ 60: 400번), 티켓·두 배·카드 넣기·상금 나누기 기대값, humanBest - 화면 apps/web/src/games/hot-streak/: 직접 그린 SVG 마스코트(곰·핫도그·아귀·여왕)와 카드, PC 가로·휴대폰 세로 경주로, 경주 기록을 한 박자 늦게 다시 그리기(폴짝·쾅·실격·등수·접기), 티켓 더미·두 배·비밀 카드·상금 나누기·정산·최종 결과(인생 결과), 도움말·30초 요약 - 등록: GAMES·BOT_STRATEGIES·BOT_GAMES·CATALOG·GAME_UI·GAME_ICON·여럿이 파티 탭, docs/games/README.md, 설계 문서 §13 구현 메모(정한 세부 규칙) - 테스트: 엔진 64개(§10 체크리스트·§5.8 예시·무작위 대국·시간 초과만으로 끝·유출 없음), 컴퓨터 15개, 도움말 2개, 방 통합 3개, e2e/games.e2e.ts hot-streak(PC + 휴대폰, 320px 넘침 확인) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
150
apps/server/src/rooms/hot-streak.room.test.ts
Normal file
150
apps/server/src/rooms/hot-streak.room.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user