컴퓨터 생각 시간을 사람처럼: 컴퓨터마다 다른 빠르기, 고를 게 많을수록 길게, 가끔 멈칫

- defaultBotDelay: 할 게 하나면 0.7~1.3초, 고르면 1.6~3.9초(+12% 확률로 1~3초 멈칫)
- botPace: 컴퓨터 id로 고정된 0.7~1.5배 빠르기
- 같은 컴퓨터가 이어서 두는 동작은 절반, 사람과 같은 반응 창은 +1.5초(그대로)
- 차례 시간이 있으면 남은 시간의 절반을 넘기지 않음
- e2e(bots) 컴퓨터 응답 대기 8초 → 15초, docs/06 §12 갱신

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-11 01:23:38 +09:00
parent c97fb4583e
commit 4c967f4de4
6 changed files with 135 additions and 26 deletions

View File

@@ -12,21 +12,53 @@ export interface BotRoom {
readonly game: { state: unknown; players: string[]; finished: boolean } | null;
readonly def: AnyGameDefinition;
now(): number;
/** When the turn timer runs out (room clock), or null without one. */
deadline?(): number | null;
/** 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;
}
/**
* Thinking time: a quick beat for forced moves, a little longer for real choices. When people may act in the same
* window (e.g. 원카드 외치기, 마작 울기, simultaneous choices) the computer waits longer so it never beats a person
* to a reaction just by being fast.
*/
export function defaultBotDelay(choices: number, withPeople = false, random: () => number = Math.random): number {
const base = choices <= 1 ? 400 + Math.floor(random() * 300) : 700 + Math.floor(random() * 800);
return withPeople ? base + 1500 : base;
/** What a planned computer move looks like, for its thinking time. */
export interface BotDelayInput {
/** How many moves the bot can choose from. */
choices: number;
/** People may act in the same window (e.g. 원카드 외치기, 마작 울기, simultaneous choices). */
withPeople: boolean;
/** The bot's user id: each computer has its own pace. */
bot: string;
/** The same bot just acted and is still on (e.g. yacht keep → roll): it already knows what it wants. */
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
* own pace, and a follow-up move of the same turn is quicker. When people may act in the same window the computer
* waits 1.5 s more so it never beats a person to a reaction just by being fast.
*/
export function defaultBotDelay(input: BotDelayInput, random: () => number = Math.random): number {
const { choices, withPeople, bot, followUp } = input;
let ms = choices <= 1 ? 700 + random() * 600 : 1300 + 300 * Math.min(Math.log2(choices), 4) + random() * 1400;
if (choices > 1 && !followUp && random() < 0.12) ms += 1000 + random() * 2000;
ms *= botPace(bot);
if (followUp) ms *= 0.5;
return Math.round(withPeople ? ms + 1500 : ms);
}
/** Never use more than this share of the time left on the turn timer for thinking. */
export const BOT_DEADLINE_SHARE = 0.5;
/** Bot move budget (ms); slower decisions are logged. */
export const BOT_SLOW_MS = 50;
@@ -36,11 +68,13 @@ export class BotDriver {
dueAt: number | null = null;
/** Bot the planned action is for. */
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;
constructor(
private room: BotRoom,
private random: () => number = Math.random,
private delay: (choices: number, withPeople: boolean) => number = (n, w) => defaultBotDelay(n, w, random),
private delay: (input: BotDelayInput) => number = (input) => defaultBotDelay(input, random),
) {}
/** Plans the next bot action if the game is waiting on a bot. Keeps an already planned one (the bot is "thinking"). */
@@ -48,7 +82,11 @@ export class BotDriver {
if (this.dueAt !== null) return;
const next = this.next();
if (!next) return;
this.dueAt = this.room.now() + this.delay(next.choices, next.withPeople);
const now = this.room.now();
let ms = this.delay({ choices: next.choices, withPeople: next.withPeople, bot: next.bot, followUp: next.bot === this.acting });
const deadline = this.room.deadline?.() ?? null;
if (deadline !== null) ms = Math.min(ms, Math.max(0, (deadline - now) * BOT_DEADLINE_SHARE));
this.dueAt = now + ms;
this.planned = next.bot;
this.arm();
}
@@ -106,12 +144,22 @@ export class BotDriver {
}
const ms = performance.now() - t0;
if (ms > BOT_SLOW_MS) this.room.log?.('warn', 'slow bot move', { game: def.id, ms: Math.round(ms) });
let err = action == null ? 'no action' : this.room.run(next.bot, action);
this.acting = next.bot;
try {
this.playPlanned(next.bot, action);
} finally {
this.acting = null;
}
}
private playPlanned(bot: string, action: unknown): void {
const def = this.room.def;
let err = action == null ? 'no action' : this.room.run(bot, action);
if (err) {
this.room.log?.('warn', 'bot action rejected', { game: def.id, action, err });
// Last resort before the turn timer: the game's own timeout action.
try {
if (!this.room.game?.finished) err = this.room.run(next.bot, def.onTimeout(this.room.game!.state, next.bot));
if (!this.room.game?.finished) err = this.room.run(bot, def.onTimeout(this.room.game!.state, bot));
} catch {
// keep err
}