마피아 컴퓨터 플레이어: 각자 타이머로 말하고 투표하는 라이브 봇, 똑똑한 편/허술한 편, 반응 속도 차이

- BotDriver live 모드: 컴퓨터마다 자기 타이머로 화면·채팅을 다시 보고 행동 하나
- mafia/bot.ts: 근거 추리(경찰 결과·경찰 주장·투표 기록) vs 느낌·분위기, 존댓말 vs 반말
- 채팅: 인사, 아침 소식, 지목·동의·반박, 이름 불리면 대답, 반론, 마피아 상의
- 연습 방 6명(PRACTICE_PLAYERS), BOT_GAMES에 mafia
- 테스트: 옵션 조합별 전체 판, 똑똑한 편 승률, 자기 팀 지목 금지, 반응 속도, 대답 E2E

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-11 02:54:14 +09:00
parent 009214c9f3
commit 92e8b2100f
15 changed files with 1202 additions and 25 deletions

View File

@@ -1,7 +1,7 @@
/** Computer players in rooms (docs/06-lobby-rooms.md §12): seating rules, practice rooms, driving, records, restore. */
import { afterEach, describe, expect, test } from 'bun:test';
import { GAMES } from '@bg/games';
import { BOT_STRATEGIES } from '@bg/games/bots';
import { BOT_STRATEGIES, LIVE_BOT_STRATEGIES } from '@bg/games/bots';
import { BOT_GAMES, ClientMessage, botProfile, isBotId, type ServerMessage } from '@bg/shared';
import { openDb } from '../db';
import { botPace, defaultBotDelay, type BotDelayInput } from './bots';
@@ -48,10 +48,11 @@ function drive(room: Room, max = 500): number {
const bots = (room: Room) => room.seatedIds().filter(isBotId);
describe('bot list', () => {
test('BOT_GAMES matches the strategies and leaves out real-time / talk games', () => {
expect([...BOT_GAMES].sort()).toEqual(Object.keys(BOT_STRATEGIES).sort());
test('BOT_GAMES matches the strategies (turn-based + live) and leaves out real-time / drawing games', () => {
expect([...BOT_GAMES].sort()).toEqual([...Object.keys(BOT_STRATEGIES), ...Object.keys(LIVE_BOT_STRATEGIES)].sort());
for (const id of BOT_GAMES) expect(GAMES[id]).toBeDefined();
for (const id of ['dobble', 'halli-galli', 'catchmind', 'mafia']) expect(BOT_GAMES).not.toContain(id);
for (const id of ['dobble', 'halli-galli', 'catchmind']) expect(BOT_GAMES).not.toContain(id);
expect(BOT_GAMES).toContain('mafia');
});
test('each computer has its own fixed pace between quick and careful', () => {
@@ -205,7 +206,7 @@ describe('addBot / removeBot', () => {
describe('createPractice', () => {
test('a private room with just enough computers to start; never offered by quick play', () => {
const { manager, conn } = harness();
const expected: Record<string, number> = { omok: 1, mahjong: 2, bang: 3, dalmuti: 3, quoridor: 1, yacht: 1 };
const expected: Record<string, number> = { omok: 1, mahjong: 2, bang: 3, dalmuti: 3, quoridor: 1, yacht: 1, mafia: 5 };
for (const [gameId, n] of Object.entries(expected)) {
const room = manager.createPractice(`host-${gameId}`, gameId);
if (typeof room === 'string') throw new Error(room);
@@ -216,7 +217,7 @@ describe('createPractice', () => {
expect(room.start(`host-${gameId}`)).toBeNull();
expect(room.game!.players).toContain(`host-${gameId}`);
}
expect(manager.createPractice('X', 'mafia')).toBe('이 게임은 컴퓨터와 할 수 없어요.');
expect(manager.createPractice('X', 'dobble')).toBe('이 게임은 컴퓨터와 할 수 없어요.');
// Quick play never lands in a practice room.
const qp = manager.quickPlay('Q', 'omok');
if (typeof qp === 'string') throw new Error(qp);
@@ -225,6 +226,36 @@ describe('createPractice', () => {
});
});
describe('마피아: talking computers (live bots)', () => {
test('each computer looks at the game on its own clock: they greet, talk, vote at different moments and the game goes on', () => {
const h = harness();
const room = h.manager.createPractice('A', 'mafia') as Room;
rooms.push(room);
room.join(h.conn('A'), 'player');
expect(room.start('A')).toBeNull();
const ids = room.game!.players.filter(isBotId);
expect(room.bots.dueAt).not.toBeNull(); // computers are watching from the start
const voteTimes = new Map<string, number>();
for (let i = 0; i < 10 * 60 * 4 && !room.game!.finished; i++) {
h.clock.advance(250);
if (room.bots.dueAt !== null && room.bots.dueAt <= h.clock.now()) room.bots.fire();
const ph = (room.game!.state as { phase: { kind: string; votes?: Record<string, string> } }).phase;
if (ph.kind === 'vote') for (const v of Object.keys(ph.votes ?? {})) if (!voteTimes.has(v)) voteTimes.set(v, h.clock.now());
const d = room.effectiveDeadline();
if (d !== null && d <= h.clock.now()) room.onTimer();
}
const s = room.game!.state as { chat: { from: string; channel: string; text: string }[]; events: { t: string }[] };
const talkers = new Set(s.chat.filter((m) => m.channel === 'day' && isBotId(m.from)).map((m) => m.from));
expect(talkers.size).toBeGreaterThanOrEqual(3);
expect(s.events.some((e) => e.t === 'vote')).toBe(true);
// Votes came in one by one, not all at the same instant.
expect(new Set(voteTimes.values()).size).toBeGreaterThan(1);
expect(ids.length).toBe(5);
// Names in chat are the players' display names ("컴퓨터 n" / "n번"), never raw ids.
expect(s.chat.some((m) => m.text.includes('bot:'))).toBe(false);
});
});
describe('playing with bots', () => {
function omokVsBot() {
const h = harness();