루미큐브: 루미큐브 앱 규칙을 기본으로(시간 초과는 무조건 되돌리고 1장, 되돌리기는 차례 처음으로, 30초 추가), 타일을 끌어다 칸 판에 놓고 합치기, 움직이는 동안 모두에게 보이게
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
142
apps/server/src/rooms/rummikub.room.test.ts
Normal file
142
apps/server/src/rooms/rummikub.room.test.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* 루미큐브 through a real Room (docs/games/rummikub.md §5.6, §8.2–§8.3): the working table is broadcast to everybody
|
||||
* on every drop, the drag preview goes out as a public `stream` message, and a v1 save is migrated on restore.
|
||||
*/
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import type { ServerMessage } from '@bg/shared';
|
||||
import { GAMES } from '@bg/games';
|
||||
import { cellsOf, freeSpot, type RummikubState, type RummikubView } from '@bg/games/rummikub';
|
||||
import { openDb } from '../db';
|
||||
import { TokenBucket } from '../ws/rate-limit';
|
||||
import { Room, type Conn, type RoomConfig, type RoomSnapshot } from './room';
|
||||
import { RoomStore } from './store';
|
||||
|
||||
type Msgs = ServerMessage[];
|
||||
type StateMsg = Extract<ServerMessage, { t: 'state' }>;
|
||||
|
||||
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 games = { rummikub: GAMES.rummikub! };
|
||||
const deps = { store, games, users: { publicUser: (id: string) => ({ id, nickname: id, avatar: null, kind: 'guest' as const }) }, now: clock.now };
|
||||
const config: RoomConfig = {
|
||||
gameId: 'rummikub',
|
||||
options: { ...games.rummikub.defaultOptions },
|
||||
maxPlayers: 2,
|
||||
visibility: 'private',
|
||||
allowSpectators: true,
|
||||
chatEnabled: true,
|
||||
chatFilter: true,
|
||||
graceSec: 60,
|
||||
};
|
||||
const room = new Room('room1', '482913', config, 'A', deps);
|
||||
store.insertRoom({ id: 'room1', code: '482913', hostId: 'A', gameId: 'rummikub', status: 'lobby', visibility: 'private', config, stateJson: '{}', now });
|
||||
const conn = (p: string) => {
|
||||
const msgs: Msgs = [];
|
||||
const c: Conn & { msgs: Msgs } = { userId: p, msgs, send: (m) => msgs.push(m), close: () => {} };
|
||||
return c;
|
||||
};
|
||||
const conns: Record<string, Conn & { msgs: Msgs }> = { A: conn('A'), B: conn('B') };
|
||||
room.join(conns.A!, 'player');
|
||||
room.join(conns.B!, 'player');
|
||||
expect(room.start('A')).toBeNull();
|
||||
room.clearTimer();
|
||||
conns.S = conn('S');
|
||||
room.join(conns.S, 'spectator');
|
||||
const state = () => room.game!.state as RummikubState;
|
||||
let cs = 100;
|
||||
const act = (p: string, a: unknown) => {
|
||||
room.act(p, ++cs, a);
|
||||
room.clearTimer();
|
||||
return cs;
|
||||
};
|
||||
return { room, store, deps, clock, conns, state, act };
|
||||
}
|
||||
|
||||
const lastView = (m: Msgs) => (m.filter((x): x is StateMsg => x.t === 'state').at(-1)!.view as RummikubView);
|
||||
|
||||
describe('루미큐브 room flow', () => {
|
||||
test('놓을 때마다 바닥이 모두(상대·관전자)에게 바로 보이고, 받침대의 나머지 타일은 보이지 않음', () => {
|
||||
const { conns, state, act } = harness();
|
||||
const cur = state().current;
|
||||
const other = cur === 'A' ? 'B' : 'A';
|
||||
const [t1, t2, ...rest] = state().racks[cur]! as [string, string, ...string[]];
|
||||
act(cur, { type: 'arrange', cells: [{ id: t1, r: 1, c: 2 }], turn: state().turnNo });
|
||||
for (const who of [other, 'S']) {
|
||||
const v = lastView(conns[who]!.msgs);
|
||||
expect(v.draft!.cells).toEqual([{ id: t1, r: 1, c: 2 }]);
|
||||
expect(v.draftEdits).toBe(1);
|
||||
const json = JSON.stringify(v);
|
||||
for (const id of [t2, ...rest]) expect(json.includes(`"${id}"`)).toBe(false);
|
||||
}
|
||||
// Moving it again: everybody sees the new place.
|
||||
act(cur, { type: 'arrange', cells: [{ id: t1, r: 3, c: 7 }, { id: t2, r: 3, c: 8 }], turn: state().turnNo });
|
||||
expect(lastView(conns[other]!.msgs).draft!.cells).toEqual([
|
||||
{ id: t1, r: 3, c: 7 },
|
||||
{ id: t2, r: 3, c: 8 },
|
||||
]);
|
||||
// 되돌리기: back to the turn start for everybody.
|
||||
act(cur, { type: 'resetDraft' });
|
||||
expect(lastView(conns.S!.msgs).draft!.cells).toEqual([]);
|
||||
});
|
||||
|
||||
test('끄는 중인 타일은 stream 메시지로만 중계되고(전체 view 재전송 없음), 받침대 타일은 뒷면(null)', () => {
|
||||
const { conns, state, act } = harness();
|
||||
const cur = state().current;
|
||||
const other = cur === 'A' ? 'B' : 'A';
|
||||
const [t1, t2] = state().racks[cur]! as [string, string];
|
||||
act(cur, { type: 'arrange', cells: [{ id: t1, r: 0, c: 0 }], turn: state().turnNo });
|
||||
const before = { o: conns[other]!.msgs.length, s: conns.S!.msgs.length, me: conns[cur]!.msgs.length };
|
||||
const mine = act(cur, { type: 'drag', ids: [t1, t2], x: 4.5, y: 2, turn: state().turnNo });
|
||||
for (const [who, n] of [
|
||||
[other, before.o],
|
||||
['S', before.s],
|
||||
] as const) {
|
||||
const got = conns[who]!.msgs.slice(n);
|
||||
expect(got).toEqual([{ t: 'stream', seq: expect.any(Number), events: [{ type: 'drag', player: cur, turn: state().turnNo, tiles: [t1, null], x: 4.5, y: 2 }] }]);
|
||||
}
|
||||
// The sender gets the same stream message with its cs (no `pending` lock, no reject).
|
||||
expect(conns[cur]!.msgs.slice(before.me)).toEqual([{ t: 'stream', seq: expect.any(Number), events: expect.any(Array), cs: mine }]);
|
||||
// A late drag from the other player is dropped silently: no error, nothing relayed.
|
||||
const n = conns[other]!.msgs.length;
|
||||
act(other, { type: 'drag', ids: [], x: 0, y: 0 });
|
||||
expect(conns[other]!.msgs.slice(n).some((m) => m.t === 'reject')).toBe(false);
|
||||
});
|
||||
|
||||
test('drag preview pace (5/s) + drops stay well under the socket token bucket (20/s, burst 40)', () => {
|
||||
const bucket = new TokenBucket(20, 40, 0);
|
||||
let dropped = 0;
|
||||
// 30 seconds of non-stop dragging: a preview every 200 ms and a drop every second.
|
||||
for (let t = 0; t < 30_000; t += 200) {
|
||||
if (!bucket.take(t)) dropped++;
|
||||
if (t % 1000 === 0 && !bucket.take(t)) dropped++;
|
||||
}
|
||||
expect(dropped).toBe(0);
|
||||
});
|
||||
|
||||
test('예전 저장(v1, 칸 위치 없음)을 불러오면 칸 판으로 바뀌고 이어서 둘 수 있음', () => {
|
||||
const { room, store, deps, state, act } = harness();
|
||||
act(state().current, { type: 'draw' });
|
||||
room.clearTimer();
|
||||
const { seq, stateJson } = store.openRooms()[0]!;
|
||||
const snap = JSON.parse(stateJson) as RoomSnapshot;
|
||||
const g = snap.game!.state as Record<string, any>;
|
||||
// What a v1 save looked like: sets without places, a draft of sets with an undo history.
|
||||
g.table = [];
|
||||
g.draft = { sets: [], history: [], edits: 0 };
|
||||
snap.game!.stateVersion = 1;
|
||||
const restored = Room.restore('room1', '482913', seq, snap, deps);
|
||||
restored.clearTimer();
|
||||
const s = restored.game!.state as RummikubState;
|
||||
expect(restored.game!.stateVersion).toBe(2);
|
||||
expect(s.draft).toEqual({ cells: [], edits: 0 });
|
||||
const p = s.current;
|
||||
const id = s.racks[p]![0]!;
|
||||
const spot = freeSpot(cellsOf(s.table), 1)!;
|
||||
restored.act(p, 1, { type: 'arrange', cells: [{ id, ...spot }] });
|
||||
restored.clearTimer();
|
||||
expect((restored.game!.state as RummikubState).draft.cells).toEqual([{ id, ...spot }]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user