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

- 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

@@ -4,7 +4,7 @@ import { GAMES } from '@bg/games';
import { BOT_STRATEGIES } from '@bg/games/bots';
import { BOT_GAMES, ClientMessage, botProfile, isBotId, type ServerMessage } from '@bg/shared';
import { openDb } from '../db';
import { defaultBotDelay } from './bots';
import { botPace, defaultBotDelay, type BotDelayInput } from './bots';
import { RoomManager } from './manager';
import { Room, type Conn, type RoomSnapshot } from './room';
import { RoomStore } from './store';
@@ -14,13 +14,13 @@ afterEach(() => {
for (const r of rooms.splice(0)) r.clearTimer();
});
function harness(log?: (level: 'info' | 'warn' | 'error', msg: string) => void, offlineSince?: (userId: string) => number | null) {
function harness(log?: (level: 'info' | 'warn' | 'error', msg: string) => void, offlineSince?: (userId: string) => number | null, botDelay: (input: BotDelayInput) => number = () => 0) {
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, log, offlineSince });
const manager = new RoomManager({ store, games: GAMES, users, now: clock.now, botDelay, log, offlineSince });
const conn = (userId: string) => {
const msgs: ServerMessage[] = [];
const c: Conn & { msgs: ServerMessage[] } = { userId, msgs, send: (m) => msgs.push(m), close: () => {} };
@@ -54,11 +54,70 @@ describe('bot list', () => {
for (const id of ['dobble', 'halli-galli', 'catchmind', 'mafia']) expect(BOT_GAMES).not.toContain(id);
});
test('thinking time: short for forced moves, longer for choices, longest when people act in the same window', () => {
const r = () => 0.5;
expect(defaultBotDelay(1, false, r)).toBe(550);
expect(defaultBotDelay(5, false, r)).toBe(1100);
expect(defaultBotDelay(5, true, r)).toBe(2600);
test('each computer has its own fixed pace between quick and careful', () => {
const ids = Array.from({ length: 200 }, (_, i) => `bot:${(i % 9) + 1}:01J${String(i).padStart(23, '0')}`);
const paces = ids.map(botPace);
for (const p of paces) expect(p >= 0.7 && p < 1.5).toBe(true);
expect(botPace(ids[0]!)).toBe(paces[0]!);
expect(Math.min(...paces)).toBeLessThan(0.85);
expect(Math.max(...paces)).toBeGreaterThan(1.35);
});
test('thinking time: a beat for forced moves, longer for choices, quicker follow-ups, longest when people act too', () => {
const bot = 'bot:1:01J0000000000000000000000';
const pace = botPace(bot);
const r = () => 0.5; // middle of every range, no hesitation (0.5 >= 0.12)
const d = (choices: number, withPeople = false, followUp = false, random = r) => defaultBotDelay({ choices, withPeople, bot, followUp }, random);
expect(d(1)).toBe(Math.round(1000 * pace));
expect(d(4)).toBe(Math.round(2600 * pace));
expect(d(225)).toBe(Math.round(3200 * pace)); // many choices: capped (log2 → 4)
expect(d(4, false, true)).toBe(Math.round(1300 * pace));
expect(d(4, true)).toBe(Math.round(2600 * pace + 1500));
// Sometimes it hesitates on a real choice.
expect(d(4, false, false, () => 0.05)).toBe(Math.round((1300 + 600 + 70 + 1000 + 100) * pace));
// Never instant, never absurdly slow.
for (let i = 0; i < 500; i++) {
const id = `bot:${i}:x`;
const forced = defaultBotDelay({ choices: 1, withPeople: false, bot: id, followUp: false });
const choice = defaultBotDelay({ choices: 30, withPeople: false, bot: id, followUp: false });
expect(forced).toBeGreaterThanOrEqual(480);
expect(forced).toBeLessThanOrEqual(1960);
expect(choice).toBeGreaterThanOrEqual(1000);
expect(choice).toBeLessThanOrEqual(10_400);
}
});
test('thinking never takes more than half of the time left on the turn timer', () => {
const h = harness(undefined, undefined, () => 60 * 60_000);
const room = h.manager.createPractice('A', 'omok') as Room;
rooms.push(room);
room.join(h.conn('A'), 'player');
room.configure('A', { options: { ...(room.config.options as object), colorAssignment: 'hostBlack' } });
expect(room.start('A')).toBeNull();
room.act('A', 1, { type: 'place', x: 7, y: 7 });
const deadline = room.effectiveDeadline();
expect(deadline).not.toBeNull();
const now = h.clock.now();
expect(room.bots.dueAt).toBe(now + (deadline! - now) / 2);
});
test('a bot that is still on after its own move gets a quicker follow-up', () => {
const inputs: BotDelayInput[] = [];
const h = harness(undefined, undefined, (i) => (inputs.push(i), 0));
const room = h.manager.createPractice('A', 'yacht') as Room;
rooms.push(room);
room.join(h.conn('A'), 'player');
expect(room.start('A')).toBeNull();
// Let the person time out until the bots have played a few turns.
for (let i = 0; i < 40 && inputs.length < 12; i++) {
drive(room);
const at = room.effectiveDeadline();
if (at === null) break;
h.clock.advance(at - h.clock.now());
room.onTimer();
}
expect(inputs.some((i) => i.followUp)).toBe(true);
expect(inputs.some((i) => !i.followUp)).toBe(true);
});
test('protocol accepts addBot / removeBot', () => {