- 공통 틀 race: 같은 시드 판을 모두 각자 동시에, 3초 카운트다운, 제한 시간/무제한, 포기, 다 푼 시간 → 푼 만큼 순위, live 컴퓨터 - 지뢰찾기: 초급·중급·고급, 모두에게 같은 곳이 열린 채 시작, 깃발·한꺼번에 열기, 휴대폰 길게 누르기·깃발 모드 - 사천성: 마작패 72·112·136장, 두 번 이하로 꺾이는 선, 항상 풀리는 배치, 막히면 자동 섞기, 힌트 - 솔리테어: 클론다이크(1장·3장)·프리셀·스파이더(1·2·4무늬), 되돌리기, 자동 정리 - 스도쿠: 답이 하나인 문제 생성기(쉬움·보통·어려움), 메모, 겹침 표시, 힌트 - 네모네모 로직: 그림 37개(5×5·10×10·15×15, 모두 줄 논리만으로 풀림), 드래그 칠하기·X 표시, 풀면 그림과 이름 - 게임 46개, 컴퓨터 상대 42개, 게임 고르기에 혼자·퍼즐 탭 (아발론·모노레일은 아직 공개 전이라 이 배포에서 뺌; 작업 갈래 codex/owner/grp_discord_joke-app 2773f5c..에서 옮김) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
122 lines
5.2 KiB
TypeScript
122 lines
5.2 KiB
TypeScript
/** 솔리테어 through a real Room: a practice race against a computer, hidden cards never reach my screen, away players (docs/games/solitaire.md §8). */
|
|
import { afterEach, describe, expect, test } from 'bun:test';
|
|
import { GAMES } from '@bg/games';
|
|
import { legalOn, type SolitaireState, type SolitaireView } from '@bg/games/solitaire';
|
|
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 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 conn = (userId: string) => {
|
|
const msgs: ServerMessage[] = [];
|
|
const c: Conn & { msgs: ServerMessage[] } = { userId, msgs, send: (m) => msgs.push(m), close: () => {} };
|
|
return c;
|
|
};
|
|
return { clock, manager, conn };
|
|
}
|
|
|
|
const lastView = (c: { msgs: ServerMessage[] }) => (c.msgs.filter((m) => m.t === 'state').at(-1) as Extract<ServerMessage, { t: 'state' }>).view as SolitaireView;
|
|
|
|
function twoPlayers(h: ReturnType<typeof harness>, options: Record<string, unknown>) {
|
|
const r = h.manager.create('A', 'solitaire', 'private');
|
|
if (typeof r === 'string') throw new Error(r);
|
|
const room = r as Room;
|
|
rooms.push(room);
|
|
const a = h.conn('A');
|
|
const b = h.conn('B');
|
|
room.join(a, 'player');
|
|
room.join(b, 'player');
|
|
room.ready('B', true);
|
|
expect(room.configure('A', { options })).toBeNull();
|
|
expect(room.start('A')).toBeNull();
|
|
h.clock.advance(3000);
|
|
room.onTimer();
|
|
return { room, a, b, s: () => room.game!.state as SolitaireState };
|
|
}
|
|
|
|
describe('solitaire · 솔리테어 room flow', () => {
|
|
test('컴퓨터와 연습: 사람은 2초마다 한 수, 1분 뒤 포기하고 컴퓨터는 끝까지 가요. 내 화면에는 뒤집힌 카드와 더미가 없어요', () => {
|
|
const h = harness();
|
|
const room = h.manager.createPractice('A', 'solitaire') as Room;
|
|
rooms.push(room);
|
|
const a = h.conn('A');
|
|
room.join(a, 'player');
|
|
expect(room.start('A')).toBeNull();
|
|
const bot = room.game!.players.find(isBotId)!;
|
|
expect(bot).toBeDefined();
|
|
let cs = 0;
|
|
let moves = 0;
|
|
const s = () => room.game!.state as SolitaireState;
|
|
for (let i = 0; i < 4 * 1000 && !room.game!.finished; i++) {
|
|
h.clock.advance(250);
|
|
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();
|
|
const v = lastView(a);
|
|
const playing = v.board && v.phase === 'playing' && v.seats.find((x) => x.id === 'A')!.status === 'playing';
|
|
if (playing) {
|
|
const b = s().boards.A!;
|
|
const json = JSON.stringify(v);
|
|
for (const c of [...b.stock, ...b.t.flatMap((col, k) => col.slice(0, b.down[k]))]) expect(json).not.toContain(`"${c}"`);
|
|
}
|
|
if (i % 8 === 0 && playing) {
|
|
if (h.clock.now() - v.countdownEndsAt > 60_000) room.act('A', ++cs, { type: 'stop' });
|
|
else {
|
|
const legal = legalOn(v.board!).filter((m) => m.type !== 'undo' && m.type !== 'auto');
|
|
const m = legal.find((x) => x.type === 'move' && x.to === 'f') ?? legal.find((x) => x.type === 'draw') ?? legal[0];
|
|
if (m) {
|
|
room.act('A', ++cs, m);
|
|
moves++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
expect(room.game!.finished).toBe(true);
|
|
expect(moves).toBeGreaterThan(10);
|
|
expect(a.msgs.filter((m) => m.t === 'reject')).toHaveLength(0);
|
|
expect(s().boards[bot]!.hist.length).toBeGreaterThan(0);
|
|
expect(lastView(a).result!.ranking.map((r) => r.id).sort()).toEqual(['A', bot].sort());
|
|
});
|
|
|
|
test('제한 시간: 연결이 끊긴 사람은 일찍 시간 초과되지 않고, 마감에 서버 시계로 끝나요', () => {
|
|
const h = harness();
|
|
const { room, b, s } = twoPlayers(h, { mode: 'spider', draw: 1, suits: 2, timeLimitSec: 600 });
|
|
expect(s().phase).toBe('playing');
|
|
room.disconnect(b);
|
|
expect(room.effectiveDeadline()).toBe(s().endsAt);
|
|
h.clock.advance(70_000);
|
|
room.onTimer();
|
|
expect(s().status.B).toBe('playing');
|
|
h.clock.advance(s().endsAt! - h.clock.now());
|
|
room.onTimer();
|
|
expect(room.game!.finished).toBe(true);
|
|
expect(s().final!.endReason).toBe('time');
|
|
});
|
|
|
|
test('무제한: 끊긴 사람은 유예 뒤 한 번만 포기 처리, 남은 사람이 포기하면 끝', () => {
|
|
const h = harness();
|
|
const { room, b, s } = twoPlayers(h, { mode: 'freecell', draw: 1, suits: 1, timeLimitSec: null });
|
|
expect(room.effectiveDeadline()).toBeNull();
|
|
room.disconnect(b);
|
|
const d = room.effectiveDeadline()!;
|
|
h.clock.advance(d - h.clock.now());
|
|
room.onTimer();
|
|
expect(s().status.B).toBe('done');
|
|
expect(room.effectiveDeadline()).toBeNull();
|
|
room.act('A', 1, { type: 'stop' });
|
|
expect(room.game!.finished).toBe(true);
|
|
});
|
|
});
|