Merge branch 'wip/game-castles-of-burgundy' 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-16 00:13:37 +09:00
25 changed files with 5216 additions and 8 deletions

View File

@@ -0,0 +1,91 @@
/** 버건디의 성 through a real Room: a practice game against a computer, played from the person's own screen, with turn clocks (docs/games/castles-of-burgundy.md §8.5). */
import { afterEach, describe, expect, test } from 'bun:test';
import { GAMES } from '@bg/games';
import type { CobAction, CobState, CobView } from '@bg/games/castles-of-burgundy';
import { botProfile, isBotId, type ServerMessage } from '@bg/shared';
import { openDb } from '../db';
import { RoomManager } from './manager';
import type { Room, 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 CobView | null;
/** What the person presses, read only from their own screen: place > take > anything but ending the turn early. */
function personMove(v: CobView): CobAction | null {
const my = v.my;
if (!my || !my.moves.length) return null;
const rank = (a: CobAction) =>
a.type === 'useDie' ? (a.act.kind === 'place' ? 0 : a.act.kind === 'take' ? 1 : 3) : a.type === 'endTurn' ? 9 : a.type === 'buy' ? 8 : 2;
let best = my.moves.map((a, i) => ({ a, i })).sort((x, y) => rank(x.a) - rank(y.a) || (my.costs[x.i] ?? 0) - (my.costs[y.i] ?? 0) || x.i - y.i)[0]!.a;
if (my.needDiscard) {
if ((best.type === 'useDie' || best.type === 'bonus') && best.act.kind === 'take') best = { ...best, act: { ...best.act, discard: 0 } };
else if ((best.type === 'buy' || best.type === 'depotTake' || best.type === 'soloTrade') && 'tile' in best) best = { ...best, discard: 0 } as CobAction;
}
return best;
}
describe('버건디의 성 room flow', () => {
test('컴퓨터와 연습: 사람은 자기 화면만 보고 두고, 가끔 시간을 넘겨도 끝까지 가요', () => {
const h = harness();
const room = h.manager.createPractice('A', 'castles-of-burgundy') as Room;
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.length).toBeGreaterThan(0);
let cs = 0;
let mine = 0;
let lastTurn = -1;
let skipTurn = false;
for (let i = 0; i < 60_000 && !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);
if (!v || !v.my) continue;
if (v.turnSeq !== lastTurn) {
lastTurn = v.turnSeq;
skipTurn = v.turnSeq % 7 === 3; // now and then the person lets the clock run out
}
if (skipTurn) continue;
const m = personMove(v);
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 CobState;
expect(s.stage).toBe('finished');
expect(s.log.some((e) => e.t === 'end')).toBe(true);
expect(mine).toBeGreaterThan(40);
const last = lastView(a)!;
expect(last.final!.map((r) => r.player).sort()).toEqual(['A', ...bots].sort());
const row = h.db.query<{ result_json: string }, []>('SELECT result_json FROM games').get()!;
expect(JSON.parse(row.result_json).practice).toBe(true);
}, 60_000);
});