From 0666b08829ea92c78baab94c006a62ddce09d8ca Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 21:47:59 -0500 Subject: [PATCH] ui-duel: engine-agnostic local match driver; Connected icon; game-over uses the engine's log line (#58) Co-Authored-By: Claude Opus 5.5 --- src/game/duel/DuelCardFace.tsx | 4 ++-- src/game/duel/DuelTable.tsx | 3 ++- src/game/duel/controller.ts | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/game/duel/controller.ts diff --git a/src/game/duel/DuelCardFace.tsx b/src/game/duel/DuelCardFace.tsx index fc25b6e..009c54e 100644 --- a/src/game/duel/DuelCardFace.tsx +++ b/src/game/duel/DuelCardFace.tsx @@ -1,4 +1,4 @@ -import { BookMarked, BookOpen, Building2, CalendarClock, Globe2, Home, Hourglass, Languages, MapPin, Shield, ShieldCheck, Sparkles, Sprout, Star, Swords, TrendingUp, UserRound } from 'lucide-react'; +import { BookMarked, BookOpen, Building2, CalendarClock, Globe2, Home, Hourglass, Languages, Link2, MapPin, Shield, ShieldCheck, Sparkles, Sprout, Star, Swords, TrendingUp, UserRound } from 'lucide-react'; import type { BoardCard, DuelCardType } from './model.ts'; import { cardSummary, typeLine } from './model.ts'; @@ -13,7 +13,7 @@ export const TypeIcon = ({ type, size = 12 }: { type: DuelCardType; size?: numbe /** Keyword icons for the board face, where a word doesn't fit 62 px. The words are in the label and the zoom face. */ const KEYWORD_ICONS: Record = { protected: ShieldCheck, featured: Star, translated: Languages, legacy: Hourglass, stub: Sprout, - 'well-sourced': BookMarked, multilingual: Globe2, local: Home, trending: TrendingUp, + 'well-sourced': BookMarked, multilingual: Globe2, local: Home, trending: TrendingUp, connected: Link2, }; export const KeywordIcon = ({ keyword, size = 11 }: { keyword: string; size?: number }) => { const Icon = KEYWORD_ICONS[keyword.toLowerCase()] ?? Sparkles; diff --git a/src/game/duel/DuelTable.tsx b/src/game/duel/DuelTable.tsx index 736831e..fb31285 100644 --- a/src/game/duel/DuelTable.tsx +++ b/src/game/duel/DuelTable.tsx @@ -107,7 +107,8 @@ export function DuelTable({ view, legal, onAct, busy, error, onExit, onRecap }: let primary: { label: string; run: () => void; disabled?: boolean } | null = null; let secondary: { label: string; run: () => void } | null = null; if (view.phase === 'over') { - note = view.winner === 'draw' ? 'The match was drawn.' : view.winner === view.seat ? `${view.opponent.name} was nominated for deletion. The result was delete.` : 'You were nominated for deletion. The result was delete.'; + // The engine's own game-over sentence (DUEL_DESIGN §8.2); the board never writes story text itself. + note = <>{view.winner === 'draw' ? 'Draw.' : view.winner === view.seat ? 'You won.' : 'You lost.'} {view.log.at(-1)}; primary = onRecap ? { label: 'Battle recap', run: onRecap } : onExit ? { label: 'Done', run: onExit } : null; if (onRecap && onExit) secondary = { label: 'Done', run: onExit }; } else if (busy) { diff --git a/src/game/duel/controller.ts b/src/game/duel/controller.ts new file mode 100644 index 0000000..51bc5dc --- /dev/null +++ b/src/game/duel/controller.ts @@ -0,0 +1,44 @@ +// Local (in-browser) match driver, engine-agnostic. ./adapter.ts binds it to duel-core's shared/duel. +// The bot gets only its own seat's view (viewFor), never the full state (Night 1 rule, #5). +import type { BoardView, DuelUIAction, Legal, Seat } from './model.ts'; + +/** The engine as the board needs it. `A` is the engine's atomic action; `S` its state. */ +export interface DuelEnginePort { + legal(s: S, seat: Seat): A[]; + apply(s: S, seat: Seat, a: A): S; + /** Whose input the engine is waiting for, or null when the match is over. */ + toAct(s: S): Seat | null; + board(s: S, seat: Seat, names: [string, string]): BoardView; + legalForBoard(actions: A[], view: BoardView): Legal; + /** One board-level intent → the engine's atomic actions, in order. Each must be legal when sent. */ + expand(ui: DuelUIAction, actions: A[], view: BoardView): A[]; +} + +export type BotChoose = (s: S, seat: Seat, legal: A[], rng: () => number) => A; + +/** Applies a sequence for one seat, re-checking legality at every step. Throws on the first illegal action. */ +export function applySequence(port: DuelEnginePort, s: S, seat: Seat, seq: A[], same: (a: A, b: A) => boolean): S { + for (const a of seq) { + const legal = port.legal(s, seat); + if (!legal.some(l => same(l, a))) throw new Error('That move is no longer legal.'); + s = port.apply(s, seat, a); + } + return s; +} + +/** Runs bot moves until the human must act or the match ends. Bounded, so a broken bot can't hang the tab. */ +export function runBot(port: DuelEnginePort, s: S, human: Seat, choose: BotChoose, rng: () => number, maxSteps = 500): S { + const bot = (human === 0 ? 1 : 0) as Seat; + for (let i = 0; i < maxSteps; i++) { + if (port.toAct(s) !== bot) return s; + const legal = port.legal(s, bot); + if (!legal.length) return s; + s = port.apply(s, bot, choose(s, bot, legal, rng)); + } + return s; +} + +/** Stable structural equality for plain JSON actions. */ +export const sameAction = (a: unknown, b: unknown): boolean => stable(a) === stable(b); +const stable = (v: unknown): string => JSON.stringify(v, (_k, x) => + x && typeof x === 'object' && !Array.isArray(x) ? Object.fromEntries(Object.entries(x).sort(([p], [q]) => p.localeCompare(q))) : x);