forked from WikiDeck/wikideck
Lane plate shows Opp total on top and You below with the Portal rule and tie-break reason; results use the engine's lane leaders (on-topic tie); opponent fizzles never name a card in hand; guest names no longer use secret characters; pick one of the four starter decks in the lobby; '+N saved' energy in the turn banner; keyword words fit; hand fade hint; phones hide node chrome on the Play route and keep the Play button clear of the floating account button. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
127 lines
9.6 KiB
TypeScript
127 lines
9.6 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
||
import { Flag, Hourglass, Layers3, LoaderCircle, X, Zap } from 'lucide-react';
|
||
import { GameCard, StatIcon } from './GameCard.tsx';
|
||
import { STAT_HINT, STAT_LABEL } from './rulesText.ts';
|
||
import type { LaneView, MatchView, UIAction, UICard } from './types.ts';
|
||
|
||
type Sheet = { kind: 'card'; card: UICard; side: 'mine' | 'theirs' } | { kind: 'lane'; lane: LaneView } | { kind: 'concede' } | null;
|
||
|
||
/** The match screen itself: board, thumb dock and bottom sheets. Controllers (local bot / hosted lobby) feed it. */
|
||
export function MatchBoard({ title, opponent, view, legal, onAct, problem, error, onTopicBonus }: {
|
||
title: string; opponent: string; view: MatchView; legal: UIAction[]; onAct: (a: UIAction) => void;
|
||
problem?: (a: UIAction) => string | null; error?: string; onTopicBonus: number[];
|
||
}) {
|
||
const [selected, setSelected] = useState<string>();
|
||
const [sheet, setSheet] = useState<Sheet>(null);
|
||
const [banner, setBanner] = useState('');
|
||
const prevBoard = useRef(new Set<string>());
|
||
const [fresh, setFresh] = useState(new Set<string>());
|
||
const boardKey = view.lanes.map(l => [...l.mine, ...l.theirs].map(c => c.id).join(',')).join('|');
|
||
|
||
// Turn feedback: banner on turn change, and a short highlight on newly revealed cards.
|
||
const lastTurn = useRef(view.turn);
|
||
useEffect(() => {
|
||
const ids = new Set(boardKey.split(/[|,]/).filter(Boolean));
|
||
const added = new Set([...ids].filter(id => !prevBoard.current.has(id)));
|
||
prevBoard.current = ids;
|
||
if (added.size) { setFresh(added); const t = setTimeout(() => setFresh(new Set()), 900); return () => clearTimeout(t); }
|
||
}, [boardKey]);
|
||
useEffect(() => {
|
||
if (view.turn !== lastTurn.current && !view.over) {
|
||
lastTurn.current = view.turn;
|
||
const saved = view.saved ? ` (+${view.saved} saved)` : '';
|
||
setBanner(view.turn === view.maxTurns ? `Final turn · ${view.maxEnergy} energy${saved}` : `Turn ${view.turn} · ${view.maxEnergy} energy${saved}`);
|
||
const t = setTimeout(() => setBanner(''), 1400);
|
||
return () => clearTimeout(t);
|
||
}
|
||
}, [view.turn, view.over, view.maxTurns, view.maxEnergy, view.saved]);
|
||
|
||
const act = onAct;
|
||
const canPlay = (cardId: string, lane: number) => legal.some(a => a.type === 'play' && a.cardId === cardId && a.lane === lane);
|
||
const playable = (cardId: string) => legal.some(a => a.type === 'play' && a.cardId === cardId);
|
||
const sel = view.hand.find(c => c.id === selected);
|
||
const tapLane = (lane: LaneView) => {
|
||
if (sel && canPlay(sel.id, lane.index)) { act({ type: 'play', cardId: sel.id, lane: lane.index }); setSelected(undefined); }
|
||
else setSheet({ kind: 'lane', lane });
|
||
};
|
||
const ending = view.waitingForOpponent;
|
||
const lastEvent = view.log.at(-1);
|
||
|
||
return <section className="match" aria-label={title}>
|
||
<header className="match-bar">
|
||
<span className="match-turn"><Hourglass size={14} /> Turn <b>{view.turn}</b>/{view.maxTurns}</span>
|
||
<span className="match-opp" aria-label={`Opponent: ${view.oppHandCount} cards in hand, ${view.oppDeckCount} in deck`}>{opponent} · <Layers3 size={13} /> {view.oppHandCount}{view.oppQueued ? ` · ${view.oppQueued} queued` : ''}</span>
|
||
<button className="icon-button" onClick={() => setSheet({ kind: 'concede' })} aria-label="Concede match"><Flag size={16} /></button>
|
||
</header>
|
||
{banner && <div className="turn-banner" role="status">{banner}</div>}
|
||
|
||
<div className="lanes" style={{ gridTemplateColumns: `repeat(${view.lanes.length}, minmax(0, 1fr))` }}>
|
||
{view.lanes.map(lane => {
|
||
const target = !!sel && canPlay(sel.id, lane.index);
|
||
const lead = lane.leader === 'you' ? 'ahead' : lane.leader === 'opponent' ? 'behind' : 'even';
|
||
const onTopic = !!sel && (sel.home === lane.stat || (lane.revealed && lane.modifier?.name === 'Disambiguation'));
|
||
const topicBonus = sel ? onTopicBonus[sel.cost] ?? 0 : 0;
|
||
return <div key={lane.index} className={`lane ${target ? 'is-target' : ''} ${sel && !target ? 'is-blocked' : ''}`}>
|
||
<div className="lane-side theirs" aria-label={`Opponent cards in ${lane.name}`}>
|
||
{lane.theirs.map(c => <GameCard key={c.id} card={c} size="board" fresh={fresh.has(c.id)} onClick={() => setSheet({ kind: 'card', card: c, side: 'theirs' })} />)}
|
||
</div>
|
||
<button className={`lane-plate ${lead}`} onClick={() => tapLane(lane)}
|
||
aria-label={target ? `Play ${sel!.title} to ${lane.name}${onTopic ? `, on-topic +${topicBonus}` : ''}` : `${lane.name} Topic (${STAT_LABEL[lane.stat]}): you ${lane.myPower}, opponent ${lane.theirPower}. ${lane.modifier?.text ?? lane.portalNote ?? ''}`}>
|
||
<span className="lane-score theirs" aria-hidden="true"><small>Opp</small>{lane.theirPower}</span>
|
||
<span className="lane-name"><StatIcon stat={lane.stat} size={13} /><span>{lane.name}</span>{!lane.revealed && <small>?</small>}
|
||
{lane.modifier && <small className="lane-rule">{lane.modifier.text}</small>}
|
||
{lane.myPower === lane.theirPower && lane.leader && <small className="lane-tie">Tie: {lane.leader === 'you' ? 'you lead' : 'they lead'} on-topic</small>}</span>
|
||
<span className="lane-score mine" aria-hidden="true"><small>You</small>{lane.myPower}</span>
|
||
</button>
|
||
<div className="lane-side mine" aria-label={`Your cards in ${lane.name}`}>
|
||
{lane.mine.map(c => <GameCard key={c.id} card={c} size="board" fresh={fresh.has(c.id)} onClick={() => setSheet({ kind: 'card', card: c, side: 'mine' })} />)}
|
||
{lane.staged.map(c => <GameCard key={c.id} card={c} size="board" label={`Take back ${c.title}`} onClick={() => act({ type: 'unstage', cardId: c.id })} selected />)}
|
||
{target && <span className={`lane-drop ${onTopic ? 'on-topic' : ''}`}>{onTopic ? `On-topic +${topicBonus}` : 'Play here'}</span>}
|
||
</div>
|
||
</div>;
|
||
})}
|
||
</div>
|
||
|
||
<div className="play-dock match-dock">
|
||
<p className="dock-note" role="status" aria-live="polite">
|
||
{error ? <span className="dock-error">{error}</span>
|
||
: ending ? <><LoaderCircle className="spin" size={14} /> Opponent is choosing…</>
|
||
: sel ? <><Zap size={12} /> <b>{sel.title}</b>{sel.keyword ? ` · ${sel.keyword.name}: ${sel.keyword.text}` : ''} Tap a lane.</>
|
||
: lastEvent}
|
||
</p>
|
||
<div className="hand" aria-label="Your hand">
|
||
{view.hand.length ? view.hand.map(c => <GameCard key={c.id} card={c} selected={selected === c.id} dim={!playable(c.id)}
|
||
onClick={ending ? undefined : () => setSelected(selected === c.id ? undefined : c.id)} />)
|
||
: <span className="muted">No cards in hand.</span>}
|
||
</div>
|
||
<div className="dock-row">
|
||
<span className="energy" aria-label={`${view.energy} of ${view.maxEnergy} energy left`}>
|
||
{Array.from({ length: view.maxEnergy }, (_, i) => <i key={i} className={i < view.energy ? 'on' : ''} />)}
|
||
<b>{view.energy}</b>
|
||
</span>
|
||
<span className="deck-count"><Layers3 size={13} /> {view.deckCount}</span>
|
||
<button className="button dark end-turn" disabled={ending || view.over} onClick={() => { setSelected(undefined); act({ type: 'endTurn' }); }}>
|
||
{ending ? 'Waiting…' : view.turn === view.maxTurns ? 'End match' : 'End turn'}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
{sheet && <div className="sheet-backdrop" onClick={e => { if (e.target === e.currentTarget) setSheet(null); }}>
|
||
<div className="sheet" role="dialog" aria-modal="true" aria-label="Details">
|
||
<button className="modal-close" onClick={() => setSheet(null)} aria-label="Close"><X size={18} /></button>
|
||
{sheet.kind === 'card' && <><div className="sheet-card"><GameCard card={sheet.card} /></div>
|
||
<h3>{sheet.card.title}</h3><p className="muted">{sheet.side === 'mine' ? 'Your card' : 'Opponent card'} · {sheet.card.category} · cost {sheet.card.cost} · power {sheet.card.power} · home {STAT_LABEL[sheet.card.home]}{sheet.card.trend ? (sheet.card.trend > 0 ? ' · trending ▲' : ' · trending ▼') : ''}{sheet.card.onTopic ? ' · on-topic' : ''}{sheet.card.power !== sheet.card.basePower ? ` (base ${sheet.card.basePower})` : ''}</p>
|
||
{sheet.card.keyword && <p><b>{sheet.card.keyword.name}.</b> {sheet.card.keyword.text}</p>}</>}
|
||
{sheet.kind === 'lane' && <><h3><StatIcon stat={sheet.lane.stat} size={18} /> {sheet.lane.name}</h3>
|
||
<p>Judged on <b>{STAT_LABEL[sheet.lane.stat]}</b> ({STAT_HINT[sheet.lane.stat]}). Cards whose home stat is {STAT_LABEL[sheet.lane.stat]} get an on-topic bonus here.</p>
|
||
{sheet.lane.modifier ? <p><b>Portal: {sheet.lane.modifier.name}.</b> {sheet.lane.modifier.text}</p> : sheet.lane.portalNote && <p className="muted">{sheet.lane.portalNote}.</p>}
|
||
<p className="muted">You {sheet.lane.myPower} · Opponent {sheet.lane.theirPower} · up to {sheet.lane.capacity} cards each.</p>
|
||
{sel && !canPlay(sel.id, sheet.lane.index) && <p className="dock-error">{sel.title} can’t go here: {problem?.({ type: 'play', cardId: sel.id, lane: sheet.lane.index }) ?? (sel.cost > view.energy ? 'not enough energy' : 'no room')}.</p>}</>}
|
||
{sheet.kind === 'concede' && <><h3>Concede this match?</h3><p className="muted">It counts as a loss. Nothing on-chain changes.</p>
|
||
<div className="play-actions"><button className="button outline" onClick={() => setSheet(null)}>Keep playing</button>
|
||
<button className="button dark" onClick={() => { setSheet(null); act({ type: 'concede' }); }}><Flag size={15} /> Concede</button></div></>}
|
||
</div>
|
||
</div>}
|
||
</section>;
|
||
}
|