From 75ce8f5ba54bd66811bae44b9134fcaa56e316c0 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 21:56:22 -0500 Subject: [PATCH] ui-duel: DuelBotMatch (eligible players only, bot sees viewFor only) (#58) Co-Authored-By: Claude Opus 5.5 --- src/game/duel/DuelBotMatch.tsx | 32 +++----------------------------- src/game/duel/decks.ts | 32 ++++++++++++++++++++++++++++++++ tests/ui-duel-adapter.test.ts | 2 +- 3 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 src/game/duel/decks.ts diff --git a/src/game/duel/DuelBotMatch.tsx b/src/game/duel/DuelBotMatch.tsx index 72970f5..4cc8e62 100644 --- a/src/game/duel/DuelBotMatch.tsx +++ b/src/game/duel/DuelBotMatch.tsx @@ -1,43 +1,17 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import type { DuelEngineApi } from '../../../shared/duel/index.ts'; import { DEFAULT_DUEL_RULESET } from '../../../shared/duel/ruleset.ts'; -import type { DuelAction, DuelCard, DuelRuleset, DuelState, PlayerView, Seat } from '../../../shared/duel/types.ts'; +import type { DuelAction, DuelCard, DuelRuleset, DuelState, Seat } from '../../../shared/duel/types.ts'; import type { Articles } from './adapter.ts'; import { DuelBoard } from './DuelBoard.tsx'; import { DuelGate } from './DuelGate.tsx'; +import { botDeck, hash, mulberry, playerDeck, type BotMatchCard, type DuelBot } from './decks.ts'; -/** A collection card as ui-pvp passes it (ownership's ChainCard satisfies this). `duel` is the server-derived frozen profile. */ -export type BotMatchCard = { tokenId: string; title: string; url?: string; imageUrl?: string; extract?: string; duel?: DuelCard }; -/** The bot sees only its own seat's view (Night 1 rule, #5). */ -export type DuelBot = { name: string; choose(view: PlayerView, legal: DuelAction[], rng: () => number): DuelAction }; +export type { BotMatchCard, DuelBot }; const HUMAN: Seat = 0, BOT: Seat = 1; const BOT_STEP_MS = 300; -function mulberry(seed: number) { - return () => { seed = (seed + 0x6d2b79f5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; -} -const hash = (s: string) => { let h = 2166136261; for (const c of s) h = Math.imul(h ^ c.charCodeAt(0), 16777619); return h >>> 0; }; - -/** The bot's own deck: `size` distinct cards from a pool (the 236 real sample cards), seeded, within the copy limit. */ -export function botDeck(pool: readonly DuelCard[], size: number, seed: string): DuelCard[] { - const rng = mulberry(hash(`bot-deck:${seed}`)); - const shuffled = [...pool].sort((a, b) => a.cardId.localeCompare(b.cardId)); - for (let i = shuffled.length - 1; i > 0; i--) { const j = Math.floor(rng() * (i + 1)); [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; } - return shuffled.slice(0, size).map(c => ({ ...c, cardId: `bot:${c.cardId}` })); -} - -/** Resolves the player's deck: exactly `deckSize` owned cards, each with a frozen profile. Returns problems instead of a partial deck. */ -export function playerDeck(deck: string[], cards: BotMatchCard[], deckSize: number): { cards: DuelCard[]; pending: string[]; missing: string[] } { - const byId = new Map(cards.map(c => [c.tokenId, c])); - const out: DuelCard[] = [], pending: string[] = [], missing: string[] = []; - for (const id of deck) { - const c = byId.get(id); - if (!c) missing.push(id); else if (!c.duel) pending.push(c.title); else out.push({ ...c.duel, cardId: id }); - } - return { cards: out.length === deckSize && deck.length === deckSize ? out : [], pending, missing }; -} - /** * A bot match in the browser, for eligible players only (BRIEF decisions 2–3): the deck must be * exactly `deckSize` cards the player owns. Off-chain; nothing moves or burns. diff --git a/src/game/duel/decks.ts b/src/game/duel/decks.ts new file mode 100644 index 0000000..877cf82 --- /dev/null +++ b/src/game/duel/decks.ts @@ -0,0 +1,32 @@ +// Pure deck helpers for the bot match (no React, no CSS), so they are unit-tested under Node. +import type { DuelAction, DuelCard, PlayerView } from '../../../shared/duel/types.ts'; + +/** A collection card as ui-pvp passes it (ownership's ChainCard satisfies this). `duel` is the server-derived frozen profile. */ +export type BotMatchCard = { tokenId: string; title: string; url?: string; imageUrl?: string; extract?: string; duel?: DuelCard }; +/** The bot sees only its own seat's view (Night 1 rule, #5). */ +export type DuelBot = { name: string; choose(view: PlayerView, legal: DuelAction[], rng: () => number): DuelAction }; + +export function mulberry(seed: number) { + return () => { seed = (seed + 0x6d2b79f5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; +} +export const hash = (s: string) => { let h = 2166136261; for (const c of s) h = Math.imul(h ^ c.charCodeAt(0), 16777619); return h >>> 0; }; + +/** The bot's own deck: `size` distinct cards from a pool (the 236 real sample cards), seeded, within the copy limit. */ +export function botDeck(pool: readonly DuelCard[], size: number, seed: string): DuelCard[] { + const rng = mulberry(hash(`bot-deck:${seed}`)); + const shuffled = [...pool].sort((a, b) => a.cardId.localeCompare(b.cardId)); + for (let i = shuffled.length - 1; i > 0; i--) { const j = Math.floor(rng() * (i + 1)); [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; } + return shuffled.slice(0, size).map(c => ({ ...c, cardId: `bot:${c.cardId}` })); +} + +/** Resolves the player's deck: exactly `deckSize` owned cards, each with a frozen profile. Returns problems instead of a partial deck. */ +export function playerDeck(deck: string[], cards: BotMatchCard[], deckSize: number): { cards: DuelCard[]; pending: string[]; missing: string[] } { + const byId = new Map(cards.map(c => [c.tokenId, c])); + const out: DuelCard[] = [], pending: string[] = [], missing: string[] = []; + for (const id of deck) { + const c = byId.get(id); + if (!c) missing.push(id); else if (!c.duel) pending.push(c.title); else out.push({ ...c.duel, cardId: id }); + } + return { cards: out.length === deckSize && deck.length === deckSize ? out : [], pending, missing }; +} + diff --git a/tests/ui-duel-adapter.test.ts b/tests/ui-duel-adapter.test.ts index db6837b..9db5a59 100644 --- a/tests/ui-duel-adapter.test.ts +++ b/tests/ui-duel-adapter.test.ts @@ -97,7 +97,7 @@ test('fallback log: real names and titles only, no adjectives', () => { }); test('bot match decks: exactly deckSize owned, profiled cards; the bot deck is its own and deterministic', async () => { - const { botDeck, playerDeck } = await import('../src/game/duel/DuelBotMatch.tsx'); + const { botDeck, playerDeck } = await import('../src/game/duel/decks.ts'); const cards = Array.from({ length: 41 }, (_, i) => ({ tokenId: `t${i}`, title: `Card ${i}`, duel: card(`d${i}`, { pageId: i }) })); const ids = cards.slice(0, 40).map(c => c.tokenId); const ok = playerDeck(ids, cards, 40);