diff --git a/src/game/duel/DuelTable.tsx b/src/game/duel/DuelTable.tsx index fb31285..5595336 100644 --- a/src/game/duel/DuelTable.tsx +++ b/src/game/duel/DuelTable.tsx @@ -4,8 +4,8 @@ import { DuelCardFace } from './DuelCardFace.tsx'; import '../play.css'; import './duel.css'; import { - EMPTY_DECL, blockList, isMyTurn, previewCombat, tapAttacker, tapBlocker, toggleAttacker, - type Attack, type BoardCard, type BoardView, type Declaration, type DuelUIAction, type Legal, type SideView, + EMPTY_DECL, blockList, isMyTurn, nextTargets, playerTarget, previewCombat, targetsComplete, tapAttacker, tapBlocker, toggleAttacker, + type Attack, type BoardCard, type Seat, type BoardView, type Declaration, type DuelUIAction, type Legal, type SideView, } from './model.ts'; type Sheet = @@ -14,18 +14,19 @@ type Sheet = | { kind: 'log' } | { kind: 'concede' } | null; const PHASE_LABEL: Record = { - mulligan: 'Opening hand', refresh: 'Refresh', draw: 'Draw', main: 'Main', combat: 'Combat', block: 'Blocks', end: 'End', over: 'Match over', + mulligan: 'Opening hand', main: 'Main', attack: 'Combat', block: 'Blocks', over: 'Match over', }; -function SideBar({ side, who, active }: { side: SideView; who: 'me' | 'opponent'; active: boolean }) { +function SideBar({ side, who, active, target, onTarget }: { side: SideView; who: 'me' | 'opponent'; active: boolean; target?: boolean; onTarget?: () => void }) { const label = `${who === 'me' ? 'You' : side.name}: ${side.notability} Notability, ${side.citations} of ${side.maxCitations} Citations, ${side.library} in library, ${side.hand} in hand`; - return
+ const Tag = target ? 'button' : 'div'; + return {who === 'me' ? 'You' : side.name} -
; + ; } /** @@ -39,11 +40,12 @@ export function DuelTable({ view, legal, onAct, busy, error, onExit, onRecap }: }) { const [sheet, setSheet] = useState(null); const [decl, setDecl] = useState(EMPTY_DECL); - const [targeting, setTargeting] = useState(); + const [targeting, setTargeting] = useState<{ cardId: string; chosen: string[] }>(); const logRef = useRef(null); const mine = isMyTurn(view); - const attackMode = mine && view.phase === 'combat' && !busy; + const oppSeat = (1 - view.seat) as Seat; + const attackMode = mine && view.phase === 'attack' && !busy; const blockMode = !mine && view.phase === 'block' && view.combat.length > 0 && !busy; const stepKey = `${view.turn}:${view.active}:${view.phase}`; useEffect(() => { setDecl(EMPTY_DECL); setTargeting(undefined); }, [stepKey]); @@ -51,24 +53,30 @@ export function DuelTable({ view, legal, onAct, busy, error, onExit, onRecap }: const cards = useMemo(() => new Map([...view.me.battlefield, ...view.opponent.battlefield, ...view.hand].map(c => [c.id, c])), [view]); const attackingIds = new Set(view.combat.map(a => a.attackerId)); - const targets = targeting ? new Set(legal.plays[targeting] ?? []) : null; + const targets = targeting ? nextTargets(legal.plays[targeting.cardId], targeting.chosen) : null; const refs = useMemo(() => { const m = new Map(); for (const c of cards.values()) if (c.url && !m.has(c.url)) m.set(c.url, m.size + 1); return m; }, [cards]); - const play = (cardId: string, targetId?: string) => { setTargeting(undefined); setSheet(null); onAct({ type: 'play', cardId, targetId }); }; + const play = (cardId: string, targetIds: string[]) => { setTargeting(undefined); setSheet(null); onAct({ type: 'play', cardId, targetIds }); }; const startPlay = (card: BoardCard) => { - const t = legal.plays[card.id]; - if (!t) return; - if (t.length) { setTargeting(card.id); setSheet(null); } else play(card.id); + const seqs = legal.plays[card.id]; + if (!seqs) return; + if (targetsComplete(seqs, [])) play(card.id, []); else { setTargeting({ cardId: card.id, chosen: [] }); setSheet(null); } + }; + const chooseTarget = (id: string) => { + if (!targeting || !targets?.has(id)) return; + const chosen = [...targeting.chosen, id]; + if (targetsComplete(legal.plays[targeting.cardId], chosen) && !nextTargets(legal.plays[targeting.cardId], chosen).size) play(targeting.cardId, chosen); + else setTargeting({ ...targeting, chosen }); }; const tapMine = (c: BoardCard) => { - if (targets) { if (targets.has(c.id)) play(targeting!, c.id); return; } + if (targets) { chooseTarget(c.id); return; } if (attackMode && legal.attackers.includes(c.id)) { setDecl(d => toggleAttacker(d, c.id, legal)); return; } if (blockMode && (legal.blockers[c.id]?.length || decl.blocks[c.id])) { setDecl(d => tapBlocker(d, c.id, legal)); return; } setSheet({ kind: 'card', card: c, where: 'mine' }); }; const tapTheirs = (c: BoardCard) => { - if (targets) { if (targets.has(c.id)) play(targeting!, c.id); return; } + if (targets) { chooseTarget(c.id); return; } if (blockMode && attackingIds.has(c.id) && (decl.picked || Object.values(decl.blocks).includes(c.id))) { setDecl(d => tapAttacker(d, c.id, legal)); return; } setSheet({ kind: 'card', card: c, where: 'theirs' }); }; @@ -118,8 +126,8 @@ export function DuelTable({ view, legal, onAct, busy, error, onExit, onRecap }: primary = { label: 'Keep hand', run: () => onAct({ type: 'mulligan', keep: true }) }; secondary = { label: 'Mulligan', run: () => onAct({ type: 'mulligan', keep: false }) }; } else if (targeting) { - const c = cards.get(targeting); - note = <>Choose a target for {c?.title}.; + const c = cards.get(targeting.cardId); + note = <>{targeting.chosen.length ? 'Now choose the next target' : 'Choose a target'} for {c?.title}.; secondary = { label: 'Cancel', run: () => setTargeting(undefined) }; } else if (attackMode) { note = nAtt ? `${nAtt} attacking. Tap a Figure to add or remove it.` : legal.attackers.length ? 'Tap Figures to attack with them.' : 'No Figure can attack.'; @@ -149,14 +157,14 @@ export function DuelTable({ view, legal, onAct, busy, error, onExit, onRecap }:
- + chooseTarget(playerTarget(oppSeat))} /> {row(view.opponent.battlefield, 'theirs')} {row(view.me.battlefield, 'mine')} - + chooseTarget(playerTarget(view.seat))} />