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 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 21:47:59 -05:00
co-authored by Claude Opus 5.5
parent 2fe16a66c8
commit 0666b08829
3 changed files with 48 additions and 3 deletions
+2 -2
View File
@@ -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<string, typeof Star> = {
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;
+2 -1
View File
@@ -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 = <><b>{view.winner === 'draw' ? 'Draw.' : view.winner === view.seat ? 'You won.' : 'You lost.'}</b> {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) {
+44
View File
@@ -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<S, A> {
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, A> = (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<S, A>(port: DuelEnginePort<S, A>, 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<S, A>(port: DuelEnginePort<S, A>, s: S, human: Seat, choose: BotChoose<S, A>, 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);