From 4c967f4de453139b1a011b8b3e90b99e5fabd66d Mon Sep 17 00:00:00 2001 From: EJClaw Date: Sun, 11 Oct 2026 01:23:38 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BB=B4=ED=93=A8=ED=84=B0=20=EC=83=9D?= =?UTF-8?q?=EA=B0=81=20=EC=8B=9C=EA=B0=84=EC=9D=84=20=EC=82=AC=EB=9E=8C?= =?UTF-8?q?=EC=B2=98=EB=9F=BC:=20=EC=BB=B4=ED=93=A8=ED=84=B0=EB=A7=88?= =?UTF-8?q?=EB=8B=A4=20=EB=8B=A4=EB=A5=B8=20=EB=B9=A0=EB=A5=B4=EA=B8=B0,?= =?UTF-8?q?=20=EA=B3=A0=EB=A5=BC=20=EA=B2=8C=20=EB=A7=8E=EC=9D=84=EC=88=98?= =?UTF-8?q?=EB=A1=9D=20=EA=B8=B8=EA=B2=8C,=20=EA=B0=80=EB=81=94=20?= =?UTF-8?q?=EB=A9=88=EC=B9=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- apps/server/src/rooms/bots.room.test.ts | 75 ++++++++++++++++++++++--- apps/server/src/rooms/bots.ts | 72 ++++++++++++++++++++---- apps/server/src/rooms/room.ts | 5 +- apps/server/src/server.ts | 3 +- docs/06-lobby-rooms.md | 2 +- e2e/bots.e2e.ts | 4 +- 6 files changed, 135 insertions(+), 26 deletions(-) diff --git a/apps/server/src/rooms/bots.room.test.ts b/apps/server/src/rooms/bots.room.test.ts index e13945b..fee9c36 100644 --- a/apps/server/src/rooms/bots.room.test.ts +++ b/apps/server/src/rooms/bots.room.test.ts @@ -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', () => { diff --git a/apps/server/src/rooms/bots.ts b/apps/server/src/rooms/bots.ts index 9003a8e..3ab8ad5 100644 --- a/apps/server/src/rooms/bots.ts +++ b/apps/server/src/rooms/bots.ts @@ -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) => 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 } diff --git a/apps/server/src/rooms/room.ts b/apps/server/src/rooms/room.ts index 5c236c0..8d95e71 100644 --- a/apps/server/src/rooms/room.ts +++ b/apps/server/src/rooms/room.ts @@ -6,7 +6,7 @@ import { ulid } from 'ulid'; import { SeededRng, createSeed, seedToHex, type AnyGameDefinition, type GameResult, type RngState } from '@bg/engine'; import { botsAllowed, isBotId, makeBotId, botNumber, maskProfanity, type PublicUser, type RoomView, type ServerMessage, type SeatView } from '@bg/shared'; import { sha256Hex } from '../auth/sessions'; -import { BotDriver } from './bots'; +import { BotDriver, type BotDelayInput } from './bots'; import type { LogEntry, RoomStore } from './store'; import { applyLocks, type EffectiveGame, type SiteSettings } from '../admin/settings'; @@ -82,7 +82,7 @@ export interface RoomDeps { /** Admin settings (docs/14-admin.md). Optional so unit tests can run without them. */ settings?: { game(id: string): EffectiveGame | null; getSite(): SiteSettings }; /** Computer players' thinking time (ms) for a decision with `choices` options; tests may pin it. */ - botDelay?: (choices: number, withPeople: boolean) => number; + botDelay?: (input: BotDelayInput) => number; } const CHAT_HISTORY = 30; @@ -149,6 +149,7 @@ export class Room { return room.def; }, now: this.now, + deadline: () => this.effectiveDeadline(), run: (bot, action) => { const parsed = this.def.actionSchema.safeParse(action); return parsed.success ? this.runAction(bot, parsed.data, undefined, false) : '잘못된 요청이에요.'; diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 74fb18e..a1a55d8 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -10,6 +10,7 @@ import { SESSION_COOKIE, Sessions } from './auth/sessions'; import { Users, toPublicUser } from './auth/users'; import { createHttpApp, type HttpDeps } from './http/app'; import { RoomManager } from './rooms/manager'; +import type { BotDelayInput } from './rooms/bots'; import { RoomStore } from './rooms/store'; import { Replays } from './rooms/replay'; import { Gateway, type WsData } from './ws/gateway'; @@ -33,7 +34,7 @@ export interface StartOptions { staticDir?: string | null; discordExchange?: HttpDeps['discordExchange']; /** Computer players' thinking time (tests shorten it). */ - botDelay?: (choices: number, withPeople: boolean) => number; + botDelay?: (input: BotDelayInput) => number; /** Upper bound for waiting on Bun's server.stop() (tests shorten it). */ stopWaitMs?: number; } diff --git a/docs/06-lobby-rooms.md b/docs/06-lobby-rooms.md index 29f096e..362abce 100644 --- a/docs/06-lobby-rooms.md +++ b/docs/06-lobby-rooms.md @@ -108,6 +108,6 @@ interface Room { - 대기실: 방장은 첫 빈자리의 [🤖 컴퓨터 넣기]로 넣고, 컴퓨터 자리의 [✕]로 뺀다(대기실에서만). 컴퓨터 이름은 "컴퓨터 1", "컴퓨터 2"…, 아바타는 🤖. 늘 준비 완료이고, 방장은 컴퓨터에게 넘어가지 않는다. 사람이 한 명은 앉아야 시작된다. - 컴퓨터가 앉은 방은 연습 방이다: 공개로 바꿀 수 없고, 공개 방 목록·빠른 시작에 나오지 않는다. 결과는 남지만 전적(`user_stats`)에는 넣지 않는다. 화면에 "연습 게임이에요. 기록에는 남지 않아요." 표시. - 컴퓨터가 할 수 있는 게임: 실시간·대화 게임(도블, 할리갈리, 그림 맞히기, 마피아)을 뺀 전부(`packages/shared/src/bots.ts`의 `BOT_GAMES`). -- 움직임: 서버의 `BotDriver`(`apps/server/src/rooms/bots.ts`)가 게임이 컴퓨터를 기다릴 때(차례, 반응 창, 동시 선택, 다음 판 준비) 0.4~1.5초 쉬었다가 한 수를 둔다. 사람과 같은 행동 경로(검사·기록)를 거치고, 서버가 다시 켜져도 이어서 둔다. +- 움직임: 서버의 `BotDriver`(`apps/server/src/rooms/bots.ts`)가 게임이 컴퓨터를 기다릴 때(차례, 반응 창, 동시 선택, 다음 판 준비) 사람처럼 생각할 시간을 두고 한 수를 둔다(2026-10-11). 할 게 하나뿐이면 0.7~1.3초, 고를 게 있으면 1.6~3.9초(고를 수가 많을수록 길게), 가끔(12%) 1~3초 더 멈칫한다. 컴퓨터마다 빠르기가 달라서(`botPace`: 0.7배 빠른 편~1.5배 신중한 편, 컴퓨터 id로 고정) 같은 판에서도 반응 속도가 다르다. 같은 컴퓨터가 이어서 두는 동작(요트 굴리기 → 고르기 등)은 절반 시간, 사람도 함께 반응할 수 있는 창(원카드 외치기 등)은 1.5초 더. 차례 시간이 있으면 남은 시간의 절반을 넘기지 않는다. 사람과 같은 행동 경로(검사·기록)를 거치고, 서버가 다시 켜져도 이어서 둔다. - 수 고르기: `packages/games/src/<게임>/bot.ts`. 자기 화면(`view(state, 나)`)과 둘 수 있는 수만 보고 고르므로 남의 패를 볼 수 없다. 강하지 않지만 엉뚱하지 않게(오목은 4를 막고, 오셀로는 귀를 잡고, 카드 게임은 패 세기에 맞게). - 사람이 모두 나가면 게임은 무효로 끝난다. diff --git a/e2e/bots.e2e.ts b/e2e/bots.e2e.ts index 8a259ea..b0ed69f 100644 --- a/e2e/bots.e2e.ts +++ b/e2e/bots.e2e.ts @@ -92,13 +92,13 @@ try { // Three answers at most: even moving first the computer has only four stones, so the game is still running for // the steps below. for (let turn = 0; turn < 8 && answers < 3; turn++) { - await a.waitForFunction(() => /내 차례예요|게임이 끝|이겼|졌/.test(document.querySelector('.turn-status')?.textContent ?? ''), null, { timeout: 8000 }); + await a.waitForFunction(() => /내 차례예요|게임이 끝|이겼|졌/.test(document.querySelector('.turn-status')?.textContent ?? ''), null, { timeout: 15000 }); if (await resultShown(a)) break; const before = await stones(a); const cell = order.find((i) => !before.has(i) && (i % 15) % 2 === turn % 2)!; await clickCell(a, cell % 15, Math.floor(cell / 15)); // Our stone, then the computer's answer (it thinks ~1 s). - await a.waitForFunction((n) => document.querySelectorAll('svg[role="grid"] g:not([opacity="0.55"]) > circle[r="0.46"]').length >= n + 2 || !!document.querySelector('[role="dialog"]'), before.size, { timeout: 8000 }); + await a.waitForFunction((n) => document.querySelectorAll('svg[role="grid"] g:not([opacity="0.55"]) > circle[r="0.46"]').length >= n + 2 || !!document.querySelector('[role="dialog"]'), before.size, { timeout: 15000 }); if (await resultShown(a)) break; answers++; if (answers === 2) await a.screenshot({ path: join(SHOTS, 'bots-2-omok-pc.png') });