M9: 컴퓨터 상대 — 게임 22종 컴퓨터 전략, 서버 구동(BotDriver), 연습 방

- packages/shared/src/bots.ts: 컴퓨터 id(bot:<n>:<ulid>)·프로필(컴퓨터 n)·BOT_GAMES
  (도블·할리갈리·그림 맞히기·마피아 제외)
- packages/games/src/bots: 전략 계약(자기 view + 합법 수만), decideBotAction(validate 재검사),
  게임별 <게임>/bot.ts 22개 + 행동 테스트, 전 게임 판 끝까지 돌리는 공통 테스트
- 서버: Room.addBot/removeBot(방장·대기실만, 방은 친구만), RoomManager.createPractice
  (POST /api/rooms {practice:true}), BotDriver(사람 같은 지연, 사람과 같은 창이면 더 천천히,
  재시작 후 다시 예약), 연습 게임은 전적 제외(result_json.practice), 사람이 모두 나가면 무효
- 프로토콜 addBot/removeBot, 문서 03·06 §12·08·09 §8

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-06 13:32:28 +09:00
parent c27cb0a11a
commit a3f81ded37
67 changed files with 5113 additions and 18 deletions

View File

@@ -0,0 +1,50 @@
/** Every bot-enabled game: bots play only accepted actions, react in every window and never stall a game. */
import { describe, expect, test } from 'bun:test';
import { seedN, type AnyGameDefinition } from '@bg/engine';
import { GAMES } from '../index';
import { BOT_STRATEGIES, decideBotAction } from './index';
import { botPlayout } from './playout';
const isBot = (p: string) => p.startsWith('bot:');
const seats = (n: number, humans: number) => Array.from({ length: n }, (_, i) => (i < humans ? `human${i}` : `bot:${i}:x`));
function counts(def: AnyGameDefinition): number[] {
const range = def.playersFor?.(def.defaultOptions) ?? { min: def.minPlayers, max: def.maxPlayers };
const out = new Set([Math.max(2, range.min), Math.min(range.max, 4)]);
return [...out].filter((n) => !def.playerCountError?.(def.defaultOptions, n));
}
describe.each(Object.keys(BOT_STRATEGIES))('%s', (id) => {
const def = GAMES[id]!;
test('has a game definition with legalActions', () => {
expect(def).toBeDefined();
expect(typeof def.legalActions).toBe('function');
});
for (const n of counts(def)) {
test(`${n} players: one human who only times out, the rest bots — the game finishes`, () => {
for (let s = 1; s <= 3; s++) {
const r = botPlayout(def, { players: seats(n, 1), seed: seedN(s), isBot, maxSteps: 20_000 });
expect(r.finished).toBe(true);
expect(r.botTimeouts).toBe(0);
expect(r.maxBotMs).toBeLessThan(250);
}
});
}
test('bots only: a whole game finishes without timeouts', () => {
const n = counts(def).at(-1)!;
for (let s = 1; s <= 2; s++) {
const r = botPlayout(def, { players: seats(n, 0), seed: seedN(100 + s), isBot, maxSteps: 30_000 });
expect(r.finished).toBe(true);
expect(r.botTimeouts).toBe(0);
}
});
});
test('decideBotAction returns null when the bot has nothing to do', () => {
const def = GAMES.omok!;
const s = def.setup({ players: ['a', 'bot:1:x'], options: def.defaultOptions, rng: { next: () => 0.5, int: () => 0 } as never, now: 0 });
const waiting = def.activePlayers(s)[0]!;
const other = waiting === 'a' ? 'bot:1:x' : 'a';
expect(decideBotAction(def, s, other, { now: 0 })).toBeNull();
expect(decideBotAction(def, s, waiting, { now: 0 })).not.toBeNull();
});