diff --git a/src/game/duel/DuelBoard.tsx b/src/game/duel/DuelBoard.tsx new file mode 100644 index 0000000..7d64c82 --- /dev/null +++ b/src/game/duel/DuelBoard.tsx @@ -0,0 +1,39 @@ +import { useMemo, useState } from 'react'; +import type { DuelAction, LogEvent, PlayerView } from './contract.ts'; +import { boardFromView, expand, legalFromActions, plainLog, type Articles } from './adapter.ts'; +import { DuelTable } from './DuelTable.tsx'; +import type { DuelUIAction } from './model.ts'; + +/** + * The duel board on duel-core's types, for any controller (ui-pvp's OnlinePlay, the local bot match). + * One board intent can be several atomic actions (toggleAttacker × N, confirmAttack): they go out + * through `onAct` in order, each awaited, each an element of `legal` or legal once the previous ones apply. + */ +export function DuelBoard({ view, legal, onAct, opponentName, playerName = 'You', error, busy, log, logEvents, articles, clock, onExit, onRecap }: { + view: PlayerView; legal: DuelAction[]; onAct: (a: DuelAction) => void | Promise; + opponentName: string; playerName?: string; error?: string; busy?: boolean; + /** Rendered log lines (duel-design's story renderer). If absent, `logEvents` are shown with plain templates. */ + log?: string[]; logEvents?: LogEvent[]; + /** cardId → article image/url, from the chain. */ + articles?: Articles; + clock?: { turn: number; bank: number }; + onExit?: () => void; onRecap?: () => void; +}) { + const [sending, setSending] = useState(false); + const [sendError, setSendError] = useState(); + const names = useMemo<[string, string]>(() => (view.seat === 0 ? [playerName, opponentName] : [opponentName, playerName]), [view.seat, playerName, opponentName]); + const lines = useMemo(() => log ?? plainLog(logEvents ?? [], names), [log, logEvents, names]); + const board = useMemo(() => boardFromView(view, names, lines, articles, clock), [view, names, lines, articles, clock]); + const boardLegal = useMemo(() => legalFromActions(legal), [legal]); + + const act = async (ui: DuelUIAction) => { + const seq = expand(ui, legal); + setSending(true); setSendError(undefined); + try { for (const a of seq) await onAct(a); } + catch (e) { setSendError(e instanceof Error ? e.message : 'That move was not accepted.'); } + finally { setSending(false); } + }; + + return { void act(ui); }} busy={busy || sending} + error={error ?? sendError} onExit={onExit} onRecap={onRecap} />; +} diff --git a/src/game/duel/DuelBotMatch.tsx b/src/game/duel/DuelBotMatch.tsx new file mode 100644 index 0000000..b98d1ba --- /dev/null +++ b/src/game/duel/DuelBotMatch.tsx @@ -0,0 +1,78 @@ +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import type { DuelAction, DuelCard, DuelEngineLike, DuelRulesetLike, Seat } from './contract.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'; + +export type { BotMatchCard, DuelBot }; + +const HUMAN: Seat = 0, BOT: Seat = 1; +const BOT_STEP_MS = 300; + +/** + * 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. + */ +export function DuelBotMatch({ deck, cards, onExit, onFinish, engine, bot, botPool, rules, playerName = 'You', onMine }: { + deck: string[]; cards: BotMatchCard[]; onExit: () => void; onFinish: (matchId?: string) => void; + engine: DuelEngineLike; bot: DuelBot; botPool: readonly DuelCard[]; rules: R; playerName?: string; onMine?: () => void; +}) { + const mine = useMemo(() => playerDeck(deck, cards, rules.deckSize), [deck, cards, rules.deckSize]); + const [seed] = useState(() => `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`); + const [state, setState] = useState(null); + const [error, setError] = useState(); + const rng = useRef(mulberry(hash(seed))); + const current = useRef(null); + current.current = state; + + useEffect(() => { + if (!mine.cards.length) return; + try { setState(engine.createDuel(rules, [mine.cards, botDeck(botPool, rules.deckSize, seed)], seed)); } + catch (e) { setError(e instanceof Error ? e.message : 'This deck can’t be played.'); } + }, [engine, rules, mine.cards, botPool, seed]); + + // The bot moves one atomic action at a time, so the board shows its plays and declarations. + useEffect(() => { + if (!state || engine.toAct(state) !== BOT) return; + const t = setTimeout(() => { + const legal = engine.legalActions(state, BOT); + if (!legal.length) return; + let a: DuelAction; + try { a = bot.choose(engine.viewFor(state, BOT), legal, rng.current); } catch { a = legal[0]; } + if (!legal.some(l => JSON.stringify(l) === JSON.stringify(a))) a = legal[0]; + if (current.current !== state) return; + const next = engine.applyAction(state, BOT, a); + current.current = next; + setState(next); + }, BOT_STEP_MS); + return () => clearTimeout(t); + }, [state, engine, bot]); + + // Applied synchronously through a ref, so a confirmed declaration (toggleAttacker × N, confirmAttack) + // sees each previous action, and an illegal action throws back to DuelBoard, which shows the error. + const onAct = useCallback((a: DuelAction) => { + const s = current.current; + if (!s) return; + const next = engine.applyAction(s, HUMAN, a); + current.current = next; + setError(undefined); + setState(next); + }, [engine]); + + const articles = useMemo(() => Object.fromEntries(cards.map(c => [c.tokenId, { image: c.imageUrl, url: c.url, extract: c.extract }])), [cards]); + + if (!mine.cards.length) { + if (deck.length < rules.deckSize || mine.missing.length) return ; + return

Card profiles pending

+

These cards don’t have a duel profile yet, so they can’t be played: {mine.pending.join(', ')}. Pick other cards for your deck, or try again shortly.

+
; + } + if (!state) return error ?

Can’t start this match

{error}

+
: null; + + const view = engine.viewFor(state, HUMAN); + return onFinish() : onExit} />; +} diff --git a/src/game/duel/DuelCardFace.tsx b/src/game/duel/DuelCardFace.tsx new file mode 100644 index 0000000..dcf5cd8 --- /dev/null +++ b/src/game/duel/DuelCardFace.tsx @@ -0,0 +1,90 @@ +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'; + +const TYPE_ICONS: Record = { + Figure: UserRound, Location: MapPin, Event: CalendarClock, Institution: Building2, Doctrine: BookOpen, +}; +export const TypeIcon = ({ type, size = 12 }: { type: DuelCardType; size?: number }) => { + const Icon = TYPE_ICONS[type] ?? BookOpen; + return