- 엔진 packages/games/src/gemblo/(board·options·types·index·bot): 463칸 판(3인 241·4인 295·듀오 157칸), 블록 18개 72칸(방향 74/뒤집기 포함 92개 미리 계산), 같은 색 변 맞댐 금지, 꼭짓점 이음선(한 블록이 사이 두 칸을 덮으면 막힘, 두 블록 틈은 통과), 놓을 곳 없는 색 자동 끝남, 남은 칸 점수·공동 순위 - 방 설정 §2 전부: 자리, 2명 2색/3색/듀오, 3명 1색/2색, 5명 장애물 자동/직접 고르기/없음, 이음선 막힘, 뒤집기(기본 허용), 점수(남은 칸/블로커스 보너스), 팀전(2명씩·3명씩), 금색 방해 블록 0/3/6, 이음점 표시, 시간(한 번당·피셔), 시간 초과 처리(자동 배치/넘김/끝), 연속 자동 한도, 무르기 - 합법 배치: 이음점 × 블록 × 방향 × 맞출 칸, (블록·방향·기준 칸) 도장으로 중복 제거, apply·legalActions 수 ms - 컴퓨터: 큰 블록 + 내 이음점 늘리기 + 남의 이음점 막기 + 초반 가운데로, 상위 80개만 자세히, humanBest 1~60단계, 한 수 10ms 안팎 - 화면 apps/web/src/games/gemblo/: 납작한 육각 SVG 판(안 쓰는 칸 흐리게, 보석 색 + 색약용 무늬), 마우스 미리보기·휠/R 돌리기·F 뒤집기, 휴대폰 칸 누르기·끌어 옮기기·[놓기], 이음점을 누르면 그 점을 덮는 합법 배치를 차례로, 두 손가락 확대·이동과 [확대]/[전체], 키보드 조작, 5인 장애물 고르기·놓기, 무르기·기권, 끝나면 색별 남은 블록, 소리·애니메이션, 도움말·30초 요약 - 등록: GAMES·BOT_STRATEGIES·BOT_GAMES·CATALOG·GAME_UI·GAME_ICON·판·주사위 탭, 운 제외(NO_LUCK), docs/games/README.md, docs/games/gemblo.md(구현 결정 13장) - 테스트: 규칙 30개(문서 §10 전부, 5인 장애물 시드 100개, 클라이언트 판정 = validate 1만 개, 무작위 대국 11가지 모드), 컴퓨터 4개, 도움말 2개, 방 통합 2개(연습 한 판, 5명 직접 고르기 + 시간 초과만 하는 사람), e2e games.e2e.ts gemblo Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
5.3 KiB
TypeScript
114 lines
5.3 KiB
TypeScript
/** 젬블로 through a real Room: practice against computers from the person's own screen, and the turn clock (docs/games/gemblo.md §8.5, §10). */
|
|
import { afterEach, describe, expect, test } from 'bun:test';
|
|
import { GAMES } from '@bg/games';
|
|
import type { GembloAction, GembloState, GembloView } from '@bg/games/gemblo';
|
|
import { legalPlacements, obstaclePlacements } from '@bg/games/gemblo/board';
|
|
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 lastView = (c: { msgs: ServerMessage[] }) => (c.msgs.filter((m): m is StateMsg => m.t === 'state').at(-1)?.view ?? null) as GembloView | null;
|
|
|
|
/** What the person does, read only from their own screen: tap the first link point, take the first way it offers. */
|
|
function personMove(v: GembloView, me: string): GembloAction | null {
|
|
if (v.turnPlayer !== me || v.phase === 'finished') return null;
|
|
if (v.phase === 'obstacleDraft') return { type: 'pickObstacle', pieceId: v.obstacle!.pool[0]! };
|
|
if (v.phase === 'obstaclePlace') {
|
|
const p = obstaclePlacements(v, v.obstacle!.hands[me]!, v.obstacle!.sectorCount, undefined, 1)[0]!;
|
|
return { type: 'placeObstacle', pieceId: p.pieceId, orient: p.orient, q: p.q, r: p.r };
|
|
}
|
|
if (!v.legal) return null;
|
|
const c = v.colors[v.turn]!;
|
|
// A link point may be one no block left can cover (only the 1-cell block would fit): try the next point.
|
|
for (const point of v.legal.bridgePoints) {
|
|
const p = legalPlacements(v, c, v.allowFlip, { points: [point] })[0];
|
|
if (p) return { type: 'place', pieceId: p.pieceId, orient: p.orient, q: p.q, r: p.r };
|
|
}
|
|
throw new Error('my turn without a placement');
|
|
}
|
|
|
|
function drive(h: ReturnType<typeof harness>, room: Room, a: ReturnType<ReturnType<typeof harness>['conn']>, play: boolean) {
|
|
let cs = 0;
|
|
let mine = 0;
|
|
for (let i = 0; i < 20_000 && !room.game!.finished; i++) {
|
|
h.clock.advance(500);
|
|
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 m = play && v ? personMove(v, 'A') : null;
|
|
if (m) {
|
|
room.act('A', ++cs, m);
|
|
mine++;
|
|
}
|
|
}
|
|
return mine;
|
|
}
|
|
|
|
describe('젬블로 room flow', () => {
|
|
test('컴퓨터 1명과 연습: 사람은 자기 화면(이음점 → 놓는 방법)만 보고 끝까지', () => {
|
|
const h = harness();
|
|
const room = h.manager.createPractice('A', 'gemblo') as Room;
|
|
rooms.push(room);
|
|
const a = h.conn('A');
|
|
room.join(a, 'player');
|
|
expect(room.start('A')).toBeNull();
|
|
const mine = drive(h, room, a, true);
|
|
expect(room.game!.finished).toBe(true);
|
|
expect(a.msgs.filter((m) => m.t === 'reject')).toEqual([]);
|
|
const s = room.game!.state as GembloState;
|
|
expect(s.colors).toHaveLength(4); // 2명 = 2색씩
|
|
expect(s.placements.filter((p) => p.auto)).toEqual([]);
|
|
expect(mine).toBeGreaterThan(10);
|
|
const v = lastView(a)!;
|
|
expect(v.result!.ranking.flat().sort()).toEqual(['A', ...s.players.filter(isBotId)].sort());
|
|
const row = h.db.query<{ result_json: string }, []>('SELECT result_json FROM games').get()!;
|
|
expect(JSON.parse(row.result_json).practice).toBe(true);
|
|
});
|
|
|
|
test('5명(장애물 직접 고르기) + 시간 초과만 하는 사람: 자동 배치 두 번 뒤 그 사람 색은 끝나고 게임은 끝까지', () => {
|
|
const h = harness();
|
|
const r = h.manager.create('A', 'gemblo', 'private');
|
|
if (typeof r === 'string') throw new Error(r);
|
|
const room = r;
|
|
rooms.push(room);
|
|
const a = h.conn('A');
|
|
room.join(a, 'player');
|
|
for (let i = 0; i < 4; i++) expect(room.addBot('A')).toBeNull();
|
|
expect(room.configure('A', { options: { ...GAMES.gemblo!.defaultOptions, fivePlayerObstacles: 'draft' } })).toBeNull();
|
|
expect(room.start('A')).toBeNull();
|
|
expect((room.game!.state as GembloState).phase).toBe('obstacleDraft');
|
|
drive(h, room, a, false);
|
|
expect(room.game!.finished).toBe(true);
|
|
const s = room.game!.state as GembloState;
|
|
expect(s.placements.filter((p) => p.kind === 'obstacle').length).toBeGreaterThanOrEqual(13);
|
|
const auto = s.placements.filter((p) => p.auto && p.kind === 'play' && s.colors.find((c) => c.color === p.color)?.owner === 'A');
|
|
expect(auto).toHaveLength(2);
|
|
expect(s.out).toEqual([expect.objectContaining({ playerId: 'A', reason: 'timeLoss' })]);
|
|
expect(lastView(a)!.result!.ranking.at(-1)).toEqual(['A']);
|
|
});
|
|
});
|