원카드: 카드 내기와 원카드 외치기 분리(내면서 외치기 없앰), 잡기 전 보호 시간(1~3초, 기본 2초), 컴퓨터도 사람처럼 늦게 외치고 잡기, 한 턴 시간 무제한, 원카드 보너스 옵션
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
/** 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, LIVE_BOT_STRATEGIES } from '@bg/games/bots';
|
||||
import { BOT_STRATEGIES, LIVE_BOT_STRATEGIES, botReactionAt } from '@bg/games/bots';
|
||||
import { classicDeck, type OneCardState } from '@bg/games/one-card';
|
||||
import { BOT_GAMES, ClientMessage, botProfile, isBotId, type ServerMessage } from '@bg/shared';
|
||||
import { openDb } from '../db';
|
||||
import { botPace, defaultBotDelay, type BotDelayInput } from './bots';
|
||||
@@ -338,6 +339,63 @@ describe('playing with bots', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('원카드: computers call and catch on their own reaction clock', () => {
|
||||
test('a computer that played down to one card calls at its reaction time; a person can catch only after 2 s', () => {
|
||||
const h = harness(undefined, undefined, (input) => defaultBotDelay(input, () => 0.5));
|
||||
const room = h.manager.createPractice('A', 'one-card') as Room;
|
||||
rooms.push(room);
|
||||
const a = h.conn('A');
|
||||
room.join(a, 'player');
|
||||
expect(room.start('A')).toBeNull();
|
||||
const bot = bots(room)[0]!;
|
||||
// Rig: the computer's turn with [♥9, ♣3] on ♥5; the person is next.
|
||||
const s = room.game!.state as OneCardState;
|
||||
const deck = classicDeck(1);
|
||||
const take = (ids: string[]) => ids.map((id) => deck.find((c) => c.id === id)!);
|
||||
s.hands = { A: take(['S4', 'S6', 'D8']), [bot]: take(['H9', 'C3']) };
|
||||
s.discard = take(['H5']);
|
||||
s.drawPile = deck.filter((c) => !['S4', 'S6', 'D8', 'H9', 'C3', 'H5'].includes(c.id));
|
||||
s.current = bot;
|
||||
s.phase = { kind: 'turn' };
|
||||
s.pendingAttack = 0;
|
||||
s.declaredSuit = null;
|
||||
s.oneCardWindow = null;
|
||||
room.bots.cancel();
|
||||
room.scheduleTimer();
|
||||
for (let k = 0; k < 5 && !(room.game!.state as OneCardState).oneCardWindow; k++) {
|
||||
h.clock.advance(Math.max(0, (room.bots.dueAt ?? h.clock.now()) - h.clock.now()));
|
||||
room.bots.fire();
|
||||
}
|
||||
let st = room.game!.state as OneCardState;
|
||||
expect(st.oneCardWindow).toMatchObject({ player: bot, declared: false });
|
||||
expect(st.current).toBe('A');
|
||||
const opened = st.oneCardWindow!.openedAt!;
|
||||
// Planned exactly at its reaction time (not squeezed by the turn clock, no thinking chat).
|
||||
const at = botReactionAt(room.def, st, bot, room.profile(bot).level) as number;
|
||||
expect(at).toBeGreaterThanOrEqual(opened + 500);
|
||||
expect(room.bots.dueAt).toBe(at);
|
||||
const rejects = () => a.msgs.filter((m) => m.t === 'reject').length;
|
||||
const before = rejects();
|
||||
room.act('A', 101, { type: 'declareOneCard' });
|
||||
expect(rejects()).toBe(before + 1);
|
||||
if (at > opened + 2000) {
|
||||
// Slow this time: the person catches it first and the planned call is dropped.
|
||||
h.clock.advance(opened + 2000 - h.clock.now());
|
||||
room.act('A', 102, { type: 'declareOneCard' });
|
||||
st = room.game!.state as OneCardState;
|
||||
expect(st.oneCardWindow).toBeNull();
|
||||
expect(st.hands[bot]).toHaveLength(2);
|
||||
expect(room.bots.dueAt).toBeNull();
|
||||
} else {
|
||||
h.clock.advance(at - h.clock.now());
|
||||
room.bots.fire();
|
||||
st = room.game!.state as OneCardState;
|
||||
expect(st.oneCardWindow).toMatchObject({ player: bot, declared: true });
|
||||
expect(st.hands[bot]).toHaveLength(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('practice rooms close once nobody is there', () => {
|
||||
function finishedPractice() {
|
||||
const h = harness();
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
import { secureRandom, type AnyGameDefinition } from '@bg/engine';
|
||||
import { isBotId } from '@bg/shared';
|
||||
import { LIVE_BOT_STRATEGIES, botCandidates, botPace, botPersona, decideBotAction, idLevel, type LiveBotStrategy } from '@bg/games/bots';
|
||||
import { LIVE_BOT_STRATEGIES, botCandidates, botPace, botPersona, botReactionAt, decideBotAction, idLevel, type LiveBotStrategy } from '@bg/games/bots';
|
||||
|
||||
import type { BotBrain } from '../ai/brain';
|
||||
|
||||
@@ -39,6 +39,8 @@ export interface BotDelayInput {
|
||||
bot: string;
|
||||
/** The same bot just acted and is still on (e.g. yacht keep → roll): it already knows what it wants. */
|
||||
followUp: boolean;
|
||||
/** An out-of-turn reaction the game timed itself (원카드 외치기·잡기, `botReactionAt`): wait exactly this long. */
|
||||
reactMs?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,6 +51,7 @@ export interface BotDelayInput {
|
||||
*/
|
||||
export function defaultBotDelay(input: BotDelayInput, random: () => number = secureRandom): number {
|
||||
const { choices, withPeople, bot, followUp } = input;
|
||||
if (input.reactMs !== undefined) return Math.max(0, Math.round(input.reactMs));
|
||||
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);
|
||||
@@ -68,6 +71,8 @@ export class BotDriver {
|
||||
dueAt: number | null = null;
|
||||
/** Bot the planned action is for. */
|
||||
private planned: string | null = null;
|
||||
/** The planned action is that bot's out-of-turn reaction due at this time (not its normal move). */
|
||||
private plannedReact: number | 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. */
|
||||
@@ -87,26 +92,65 @@ 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 || this.busy) return;
|
||||
if (this.busy) return;
|
||||
if (this.dueAt !== null) {
|
||||
// A planned reaction the moment has passed for (someone called first): plan again; anything else stays planned.
|
||||
if (this.plannedReact === null || this.reactionDue()?.bot === this.planned) return;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
this.timer = null;
|
||||
this.dueAt = null;
|
||||
this.planned = null;
|
||||
this.plannedReact = null;
|
||||
}
|
||||
const next = this.next();
|
||||
if (!next) return;
|
||||
// People can see it think: "생각 중…" until it moves.
|
||||
if (this.room.brain?.enabled()) this.room.brain.thinking(next.bot, true);
|
||||
const react = this.reactionDue();
|
||||
if (!next && !react) return;
|
||||
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));
|
||||
let ms = Infinity;
|
||||
if (next) {
|
||||
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));
|
||||
}
|
||||
// An out-of-turn reaction (원카드!) comes first when it is due earlier; it is not squeezed by the turn clock, so a
|
||||
// slow computer can really be late.
|
||||
if (react) {
|
||||
const rms = this.delay({ choices: 1, withPeople: true, bot: react.bot, followUp: false, reactMs: react.at - now });
|
||||
if (rms <= ms) {
|
||||
this.dueAt = now + rms;
|
||||
this.planned = react.bot;
|
||||
this.plannedReact = react.at;
|
||||
return this.arm();
|
||||
}
|
||||
}
|
||||
// People can see it think: "생각 중…" until it moves.
|
||||
if (this.room.brain?.enabled()) this.room.brain.thinking(next!.bot, true);
|
||||
this.dueAt = now + ms;
|
||||
this.planned = next.bot;
|
||||
this.planned = next!.bot;
|
||||
this.plannedReact = null;
|
||||
this.arm();
|
||||
}
|
||||
|
||||
/** The earliest out-of-turn reaction of a computer that may act now (games with `BOT_REACTIONS`). */
|
||||
private reactionDue(): { bot: string; at: number } | null {
|
||||
const g = this.room.game;
|
||||
if (!g || g.finished || this.room.status !== 'playing') return null;
|
||||
let best: { bot: string; at: number } | null = null;
|
||||
for (const p of g.players) {
|
||||
if (!isBotId(p)) continue;
|
||||
const at = botReactionAt(this.room.def, g.state, p, this.room.profile?.(p).level ?? idLevel(p));
|
||||
if (typeof at === 'number' && (!best || at < best.at)) best = { bot: p, at };
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
this.timer = null;
|
||||
if (this.planned) this.room.brain?.thinking(this.planned, false);
|
||||
this.dueAt = null;
|
||||
this.planned = null;
|
||||
this.plannedReact = null;
|
||||
this.liveDue.clear();
|
||||
}
|
||||
|
||||
@@ -219,8 +263,11 @@ export class BotDriver {
|
||||
const active = def.activePlayers(g.state);
|
||||
const waiting = new Set([...active, ...(def.timeoutPlayers?.(g.state) ?? [])]);
|
||||
const withPeople = active.some((p) => !isBotId(p));
|
||||
const timed = new Set(def.timeoutPlayers?.(g.state) ?? active);
|
||||
for (const p of g.players) {
|
||||
if (!isBotId(p) || !waiting.has(p)) continue;
|
||||
// Only there to react out of turn (원카드!): `reactionDue` times that, not the thinking time.
|
||||
if (!timed.has(p) && botReactionAt(def, g.state, p, this.room.profile?.(p).level ?? idLevel(p)) !== null) continue;
|
||||
const choices = botCandidates(def, g.state, p).length;
|
||||
if (choices > 0) return { bot: p, choices, withPeople };
|
||||
}
|
||||
@@ -239,13 +286,16 @@ export class BotDriver {
|
||||
return this.fireLive(live);
|
||||
}
|
||||
const planned = this.planned;
|
||||
const wasReact = this.plannedReact !== null;
|
||||
this.dueAt = null;
|
||||
this.planned = null;
|
||||
const next = this.next();
|
||||
this.plannedReact = null;
|
||||
const react = wasReact ? this.reactionDue() : null;
|
||||
const next = wasReact ? (react && react.bot === planned ? { bot: react.bot } : null) : this.next();
|
||||
const g = this.room.game;
|
||||
if (!next || !g) return;
|
||||
// Another bot's turn came up meanwhile: give it its own thinking time.
|
||||
if (next.bot !== planned) return this.schedule();
|
||||
if (!g) return;
|
||||
// Another bot's turn came up meanwhile (or the reaction is moot): give it its own thinking time.
|
||||
if (!next || next.bot !== planned) return this.schedule();
|
||||
const def = this.room.def;
|
||||
const t0 = performance.now();
|
||||
let action: unknown;
|
||||
@@ -258,7 +308,8 @@ 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) });
|
||||
const brain = this.room.brain;
|
||||
if (brain && action != null && brain.enabled()) {
|
||||
// A reaction (원카드!) is a reflex: no chat before it.
|
||||
if (brain && action != null && brain.enabled() && !wasReact) {
|
||||
// Like a person: maybe type something first (the model writes it; "입력 중…" while typing), then move.
|
||||
const bot = next.bot;
|
||||
this.busy = bot;
|
||||
|
||||
Reference in New Issue
Block a user