contract.ts copies the unions and fields verbatim; a compile check confirmed duel-core's PlayerView, DuelAction (both ways), LogEvent, DuelEngineApi and DuelRuleset are assignable to it. It becomes re-exports once #72 merges. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useMemo, useState } from 'react';
|
||||
import type { DuelAction, LogEvent, PlayerView } from '../../../shared/duel/types.ts';
|
||||
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';
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
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, Seat } from '../../../shared/duel/types.ts';
|
||||
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';
|
||||
@@ -16,16 +14,16 @@ 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 = DEFAULT_DUEL_RULESET, playerName = 'You', onMine }: {
|
||||
export function DuelBotMatch<S, R extends DuelRulesetLike>({ deck, cards, onExit, onFinish, engine, bot, botPool, rules, playerName = 'You', onMine }: {
|
||||
deck: string[]; cards: BotMatchCard[]; onExit: () => void; onFinish: (matchId?: string) => void;
|
||||
engine: DuelEngineApi; bot: DuelBot; botPool: readonly DuelCard[]; rules?: DuelRuleset; playerName?: string; onMine?: () => void;
|
||||
engine: DuelEngineLike<S, R>; 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<DuelState | null>(null);
|
||||
const [state, setState] = useState<S | null>(null);
|
||||
const [error, setError] = useState<string>();
|
||||
const rng = useRef(mulberry(hash(seed)));
|
||||
const current = useRef<DuelState | null>(null);
|
||||
const current = useRef<S | null>(null);
|
||||
current.current = state;
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// duel-core (shared/duel) ↔ the board's view model. The only file that knows engine shapes.
|
||||
import type { DuelAction, LogEvent, PermanentView, PlayerView, Seat, TargetRef } from '../../../shared/duel/types.ts';
|
||||
import type { DuelAction, LogEvent, PermanentView, PlayerView, Seat, TargetRef } from './contract.ts';
|
||||
import { boardCard, type ArticleBits } from './cards.ts';
|
||||
import type { Attack, BoardCard, BoardView, DuelUIAction, Legal, SideView } from './model.ts';
|
||||
import { NO_LEGAL } from './model.ts';
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* The part of duel-core's contract (shared/duel/types.ts and index.ts, PR #72 at 7904199) that the
|
||||
* board uses, mirrored so this PR doesn't stack on #72 (HANDBOOK: stub the interface). Field names
|
||||
* and unions are copied verbatim; engine-only details are widened to `unknown`, so real engine values
|
||||
* are assignable here. When #72 is on night/2026-11-17 this file becomes re-exports of shared/duel.
|
||||
*/
|
||||
export type Seat = 0 | 1;
|
||||
export type CardType = 'Figure' | 'Location' | 'Event' | 'Institution' | 'Doctrine';
|
||||
export type Portal = 'Arts' | 'Earth' | 'History' | 'Science' | 'Society' | 'Neutral';
|
||||
export type Keyword = 'Protected' | 'Featured' | 'Legacy' | 'Multilingual' | 'Translated' | 'Well-sourced' | 'Stub' | 'Local' | 'Trending';
|
||||
export type ConnectedTier = 'Linked' | 'Compatriot' | 'Contemporary' | 'Kin';
|
||||
export type Phase = 'mulligan' | 'main' | 'attack' | 'block' | 'over';
|
||||
export type EndReason = 'life' | 'library' | 'concede' | 'timeout' | 'turnCap';
|
||||
|
||||
export interface DuelCard {
|
||||
cardId: string;
|
||||
pageId: number | null;
|
||||
revisionId?: number;
|
||||
title: string;
|
||||
description?: string;
|
||||
type: CardType;
|
||||
subtype?: string | null;
|
||||
portal: Portal;
|
||||
cost: number;
|
||||
power: number | null;
|
||||
toughness: number | null;
|
||||
keywords: Keyword[];
|
||||
territory?: number;
|
||||
effects: unknown[];
|
||||
rulesText: string;
|
||||
flavor: string;
|
||||
connect?: unknown;
|
||||
rarity?: 'Common' | 'Rare' | 'Epic' | 'Legendary';
|
||||
deriveVersion?: string;
|
||||
token?: boolean;
|
||||
}
|
||||
|
||||
/** Only deckSize is read here; the engine validates the rest. */
|
||||
export interface DuelRulesetLike { deckSize: number }
|
||||
|
||||
export interface CardInstance { uid: string; owner: Seat; card: DuelCard }
|
||||
export interface CombatState { attackers: string[]; blocks: Record<string, string> }
|
||||
export interface DuelResult { winner: Seat | null; reason: EndReason }
|
||||
|
||||
export type TargetRef = { kind: 'card'; uid: string } | { kind: 'player'; seat: Seat };
|
||||
|
||||
export type DuelAction =
|
||||
| { type: 'keep' }
|
||||
| { type: 'mulligan' }
|
||||
| { type: 'play'; uid: string; targets?: TargetRef[] }
|
||||
| { type: 'endPhase' }
|
||||
| { type: 'toggleAttacker'; uid: string }
|
||||
| { type: 'confirmAttack' }
|
||||
| { type: 'block'; blocker: string; attacker: string }
|
||||
| { type: 'confirmBlocks' }
|
||||
| { type: 'concede' }
|
||||
| { type: 'timeout' };
|
||||
|
||||
export interface CardRef { uid: string; cardId: string; title: string; pageId: number | null; type: CardType; power?: number; toughness?: number }
|
||||
|
||||
export type LogEvent = { seq: number; turn: number } & (
|
||||
| { kind: 'start'; firstSeat: Seat }
|
||||
| { kind: 'mulligan'; seat: Seat }
|
||||
| { kind: 'keep'; seat: Seat; handSize: number }
|
||||
| { kind: 'turnStart'; seat: Seat; citations: number; life: [number, number] }
|
||||
| { kind: 'draw'; seat: Seat; card?: CardRef }
|
||||
| { kind: 'deploy'; seat: Seat; card: CardRef }
|
||||
| { kind: 'cast'; seat: Seat; card: CardRef; targets: TargetRef[] }
|
||||
| { kind: 'adopt'; seat: Seat; card: CardRef; to: CardRef }
|
||||
| { kind: 'tokenCreated'; seat: Seat; card: CardRef; source: CardRef }
|
||||
| { kind: 'connected'; seat: Seat; card: CardRef; with: CardRef; tier: ConnectedTier; via: string; bonus: number }
|
||||
| { kind: 'rivalry'; card: CardRef; with: CardRef; tier: ConnectedTier; via: string }
|
||||
| { kind: 'effect'; seat: Seat; source: CardRef; op: string; template?: string; targets: TargetRef[] }
|
||||
| { kind: 'attack'; seat: Seat; attackers: CardRef[] }
|
||||
| { kind: 'block'; seat: Seat; blocks: { blocker: CardRef; attacker: CardRef }[] }
|
||||
| { kind: 'effectDamage'; source: CardRef; target: CardRef; amount: number }
|
||||
| { kind: 'combatDamage'; source: CardRef; target: CardRef; amount: number }
|
||||
| { kind: 'playerDamage'; source: CardRef | null; seat: Seat; amount: number; life: number; combat: boolean }
|
||||
| { kind: 'lifeGain'; source: CardRef; seat: Seat; amount: number; life: number }
|
||||
| { kind: 'destroyed'; card: CardRef; owner: Seat }
|
||||
| { kind: 'bounced'; card: CardRef; owner: Seat }
|
||||
| { kind: 'discard'; seat: Seat; card: CardRef }
|
||||
| { kind: 'fatigue'; seat: Seat; amount: number }
|
||||
| { kind: 'concede'; seat: Seat }
|
||||
| { kind: 'timeout'; seat: Seat }
|
||||
| { kind: 'gameOver'; winner: Seat | null; reason: EndReason; life: [number, number] }
|
||||
);
|
||||
|
||||
export interface PermanentView {
|
||||
uid: string;
|
||||
owner: Seat;
|
||||
controller: Seat;
|
||||
card: DuelCard;
|
||||
tapped: boolean;
|
||||
sick: boolean;
|
||||
damage: number;
|
||||
counters: number;
|
||||
attachedTo?: string;
|
||||
power: number;
|
||||
toughness: number;
|
||||
keywords: Keyword[];
|
||||
connected?: { with: string; tier: ConnectedTier; via: string; bonus: number };
|
||||
}
|
||||
|
||||
export interface SideView {
|
||||
seat: Seat;
|
||||
life: number;
|
||||
citations: number;
|
||||
maxCitations: number;
|
||||
handSize: number;
|
||||
librarySize: number;
|
||||
graveyard: CardInstance[];
|
||||
bankSeconds: number;
|
||||
ready: boolean;
|
||||
mulligansTaken: number;
|
||||
}
|
||||
|
||||
export interface PlayerView {
|
||||
engine: 'duel';
|
||||
seat: Seat;
|
||||
turn: number;
|
||||
active: Seat;
|
||||
phase: Phase;
|
||||
toAct: Seat | null;
|
||||
you: SideView & { hand: CardInstance[] };
|
||||
opponent: SideView;
|
||||
battlefield: PermanentView[];
|
||||
combat: CombatState;
|
||||
result: DuelResult | null;
|
||||
version: number;
|
||||
rules: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* The slice of `DuelEngineApi` (shared/duel/index.ts) the bot match calls. Method syntax on purpose:
|
||||
* the real engine (typed on the full DuelState/DuelRuleset/DuelCard) is assignable to it.
|
||||
*/
|
||||
export interface DuelEngineLike<S = unknown, R = unknown> {
|
||||
createDuel(rules: R, decks: [DuelCard[], DuelCard[]], seed: string): S;
|
||||
legalActions(state: S, seat: Seat): DuelAction[];
|
||||
applyAction(state: S, seat: Seat, action: DuelAction): S;
|
||||
toAct(state: S): Seat | null;
|
||||
isOver(state: S): boolean;
|
||||
viewFor(state: S, seat: Seat): PlayerView;
|
||||
logFor(state: S, seat: Seat): LogEvent[];
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// 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';
|
||||
import type { DuelAction, DuelCard, PlayerView } from './contract.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?: unknown };
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// ui-duel (#58): the duel board's own view model. Board components read only these types, so the
|
||||
// engine contract (shared/duel/types.ts, duel-core) is mapped in exactly one place: ./adapter.ts.
|
||||
// engine contract (duel-core, mirrored in ./contract.ts) is mapped in exactly one place: ./adapter.ts.
|
||||
// Nothing here moves cards on-chain; matches are off-chain and never burn or transfer.
|
||||
|
||||
export type DuelCardType = 'Figure' | 'Location' | 'Event' | 'Institution' | 'Doctrine';
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { DEFAULT_DUEL_RULESET } from '../shared/duel/ruleset.ts';
|
||||
import type { DuelAction, DuelCard, LogEvent, PermanentView, PlayerView, SideView } from '../shared/duel/types.ts';
|
||||
import type { DuelAction, DuelCard, LogEvent, PermanentView, PlayerView, SideView } from '../src/game/duel/contract.ts';
|
||||
import { boardFromView, expand, legalFromActions, plainLog } from '../src/game/duel/adapter.ts';
|
||||
|
||||
const card = (id: string, extra: Partial<DuelCard> = {}): DuelCard => ({
|
||||
@@ -29,7 +28,7 @@ function view(): PlayerView {
|
||||
perm('1:4', 1, card('gar', { type: 'Location', power: 0, toughness: 3 })),
|
||||
perm('1:5', 1, card('doc', { type: 'Doctrine', power: null, toughness: null }), { attachedTo: '1:3' }),
|
||||
],
|
||||
combat: { attackers: ['0:1'], blocks: {} }, result: null, version: 9, rules: DEFAULT_DUEL_RULESET,
|
||||
combat: { attackers: ['0:1'], blocks: {} }, result: null, version: 9, rules: {},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user