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

- 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

@@ -5,7 +5,9 @@
*/
import type { AnyGameDefinition } from '@bg/engine';
import { isBotId } from '@bg/shared';
import { botCandidates, decideBotAction } from '@bg/games/bots';
import { LIVE_BOT_STRATEGIES, botCandidates, botPace, botPersona, decideBotAction, type LiveBotStrategy } from '@bg/games/bots';
export { botPace };
export interface BotRoom {
readonly status: string;
@@ -17,6 +19,8 @@ export interface BotRoom {
/** Runs the action as `actor` through the room's normal path; returns an error message on rejection. */
run(actor: string, action: unknown): string | null;
log?: (level: 'info' | 'warn' | 'error', msg: string, extra?: Record<string, unknown>) => void;
/** Display name of a player (live bots talk about each other by name). */
nameOf?(id: string): string;
}
/** What a planned computer move looks like, for its thinking time. */
@@ -31,16 +35,6 @@ export interface BotDelayInput {
followUp: boolean;
}
/**
* Each computer's own pace (0.7 quick … 1.5 careful), fixed for the bot id so "컴퓨터 2" keeps its character for the
* whole game, while a new room's 컴퓨터 2 (new id) may be different.
*/
export function botPace(bot: string): number {
let h = 2166136261;
for (let i = 0; i < bot.length; i++) h = Math.imul(h ^ bot.charCodeAt(i), 16777619);
return 0.7 + ((h >>> 0) / 2 ** 32) * 0.8;
}
/**
* Human-like thinking time (ms). A forced move still takes a moment to notice (0.7~1.3 s); a real choice takes longer
* the more moves there are (1.6~3.9 s), and now and then the computer hesitates (+1~3 s). Each bot scales it by its
@@ -70,6 +64,10 @@ export class BotDriver {
private planned: string | null = null;
/** Bot whose action is being played right now: if it is still on afterwards, its next move is a follow-up. */
private acting: string | null = null;
/** Live games (마피아): when each computer looks at the game again, and its notes for the current game. */
private liveDue = new Map<string, number>();
private liveMemory = new Map<string, Record<string, unknown>>();
private liveGame: unknown = null;
constructor(
private room: BotRoom,
@@ -79,6 +77,7 @@ export class BotDriver {
/** Plans the next bot action if the game is waiting on a bot. Keeps an already planned one (the bot is "thinking"). */
schedule(): void {
if (this.liveStrategy()) return this.scheduleLive();
if (this.dueAt !== null) return;
const next = this.next();
if (!next) return;
@@ -96,6 +95,67 @@ export class BotDriver {
this.timer = null;
this.dueAt = null;
this.planned = null;
this.liveDue.clear();
}
private liveStrategy(): LiveBotStrategy | null {
return LIVE_BOT_STRATEGIES[this.room.def.id] ?? null;
}
/**
* Live games: every computer has its own timer and is asked again and again (it may also do nothing), so several
* computers talk and vote at the same time, each at its own speed. A planned look stays planned.
*/
private scheduleLive(): void {
const g = this.room.game;
if (!g || g.finished || this.room.status !== 'playing') {
if (this.timer) clearTimeout(this.timer);
this.timer = null;
this.dueAt = null;
return;
}
if (this.liveGame !== g) {
this.liveGame = g;
this.liveMemory.clear();
this.liveDue.clear();
}
const now = this.room.now();
for (const p of g.players) {
if (isBotId(p) && !this.liveDue.has(p)) this.liveDue.set(p, now + Math.round((300 + this.random() * 1200) * botPace(p)));
}
const next = Math.min(...this.liveDue.values());
this.dueAt = Number.isFinite(next) ? next : null;
if (this.dueAt !== null) this.arm();
}
/** Lets every computer whose time has come look at the game once. */
private fireLive(strategy: LiveBotStrategy): void {
const now = this.room.now();
const due = [...this.liveDue.entries()].filter(([, t]) => t <= now).sort((a, b) => a[1] - b[1]);
for (const [bot] of due) {
const g = this.room.game;
if (!g || g.finished || this.room.status !== 'playing') return;
const def = this.room.def;
const memory = this.liveMemory.get(bot) ?? {};
this.liveMemory.set(bot, memory);
const names: Record<string, string> = {};
for (const p of g.players) names[p] = this.room.nameOf?.(p) ?? p;
let decision: { action: unknown; next: number } = { action: null, next: 3000 };
const t0 = performance.now();
try {
decision = strategy({ me: bot, view: structuredClone(def.view(g.state, bot)), now, random: this.random, persona: botPersona(bot), names, memory });
} catch (err) {
this.room.log?.('error', 'bot decision failed', { game: def.id, err: String(err) });
}
const ms = performance.now() - t0;
if (ms > BOT_SLOW_MS) this.room.log?.('warn', 'slow bot move', { game: def.id, ms: Math.round(ms) });
this.liveDue.set(bot, now + Math.max(100, decision.next));
if (decision.action == null) continue;
if (!def.validate(g.state, bot, decision.action, { now }).ok) continue; // the moment passed (phase changed); look again later
const err = this.room.run(bot, decision.action);
if (err) this.room.log?.('warn', 'bot action rejected', { game: def.id, action: decision.action, err });
}
this.scheduleLive();
}
private arm(): void {
@@ -125,6 +185,11 @@ export class BotDriver {
this.timer = null;
if (this.dueAt === null) return;
if (this.room.now() < this.dueAt) return this.arm();
const live = this.liveStrategy();
if (live) {
this.dueAt = null;
return this.fireLive(live);
}
const planned = this.planned;
this.dueAt = null;
this.planned = null;