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>
138 lines
7.7 KiB
TypeScript
138 lines
7.7 KiB
TypeScript
// The single place the Play UI touches the engine (import only from shared/game/index.ts).
|
|
import {
|
|
DEFAULT_RULESET, applyAction, actionProblem, createMatch, isOver, laneCapacity, legalActions, result, viewFor,
|
|
type Action, type CardInPlay, type LogEvent, type MatchResult, type MatchState, type PlayerId, type PlayerView, type Ruleset,
|
|
} from '../../shared/game/index.ts';
|
|
import { toUICard } from './cards.ts';
|
|
import { portalInfo, STAT_LABEL } from './rulesText.ts';
|
|
import type { GameMode, LaneView, MatchView, ResultView, UIAction, UICard } from './types.ts';
|
|
|
|
export const STANDARD_RULESET: Ruleset = DEFAULT_RULESET;
|
|
export const LOCAL_MODES: GameMode[] = [{
|
|
id: 'standard', name: 'Standard', host: { kind: 'local' }, ruleset: DEFAULT_RULESET,
|
|
description: 'Three Topics, six turns. Win two Topics.',
|
|
}];
|
|
|
|
function toUI(a: Action): UIAction {
|
|
switch (a.type) {
|
|
case 'play': return { type: 'play', cardId: a.cardId, lane: a.lane, engine: a };
|
|
case 'undo': return { type: 'unstage', cardId: a.cardId, engine: a };
|
|
case 'concede': return { type: 'concede', engine: a };
|
|
default: return { type: 'endTurn', engine: a };
|
|
}
|
|
}
|
|
function toEngine(a: UIAction, player: PlayerId): Action {
|
|
if (a.engine) return { ...a.engine, player } as Action;
|
|
switch (a.type) {
|
|
case 'play': return { type: 'play', player, cardId: a.cardId, lane: a.lane };
|
|
case 'unstage': return { type: 'undo', player, cardId: a.cardId };
|
|
case 'concede': return { type: 'concede', player };
|
|
default: return { type: 'commit', player };
|
|
}
|
|
}
|
|
|
|
function logLine(e: LogEvent, me: PlayerId, title: (id: string) => string): string | null {
|
|
const who = (p: PlayerId) => (p === me ? 'You' : 'Opponent');
|
|
switch (e.t) {
|
|
case 'turnStart': return `Turn ${e.turn}. ${e.initiative === me ? 'You reveal first.' : 'Opponent reveals first.'}`;
|
|
case 'portalRevealed': return `Portal revealed: ${e.topic.name}.`;
|
|
case 'underdog': return `${who(e.player)} got the underdog bonus.`;
|
|
case 'reveal': return `${who(e.player)} played ${title(e.cardId)}.`;
|
|
case 'fizzle': return e.player === me ? `Your ${title(e.cardId)} fizzled (no room) and went back to hand.` : 'An opponent play fizzled.'; // never name a card in their hand
|
|
case 'effect': return `${e.source}: ${e.text}`;
|
|
case 'concede': return `${who(e.player)} ${e.afk ? 'timed out' : 'conceded'}.`;
|
|
case 'matchOver': return 'Match over.';
|
|
default: return null; // draws are private
|
|
}
|
|
}
|
|
|
|
export const engine = {
|
|
name: 'shared/game v1', mock: false,
|
|
createMatch(ruleset: Ruleset, decks: [UICard[], UICard[]], seed: string): MatchState {
|
|
return createMatch(ruleset, [decks[0].map(c => c.game), decks[1].map(c => c.game)], seed);
|
|
},
|
|
legal(state: MatchState, player: PlayerId): UIAction[] {
|
|
if (isOver(state)) return [];
|
|
const acts = legalActions(state, player).map(toUI);
|
|
return acts.length ? [...acts, { type: 'concede', engine: { type: 'concede', player } }] : acts;
|
|
},
|
|
apply(state: MatchState, player: PlayerId, action: UIAction): MatchState {
|
|
return applyAction(state, toEngine(action, player));
|
|
},
|
|
problem(state: MatchState, player: PlayerId, action: UIAction): string | null {
|
|
return actionProblem(state, toEngine(action, player));
|
|
},
|
|
view(state: MatchState, me: PlayerId): MatchView {
|
|
const titles = new Map<string, string>();
|
|
for (const p of state.players) for (const c of [...p.hand, ...p.deck]) titles.set(c.cardId, c.title);
|
|
for (const l of state.lanes) for (const side of l.cards) for (const c of side) titles.set(c.card.cardId, c.card.title);
|
|
const log = state.log.filter(e => e.t !== 'draw')
|
|
.map(e => logLine(e, me, id => titles.get(id) ?? 'a card')).filter((x): x is string => !!x).slice(-30);
|
|
const pv = viewFor(state, me);
|
|
return { ...viewFromPlayerView(pv, { log, capacity: i => laneCapacity(state, i) }), engine: { view: pv, seat: me } };
|
|
},
|
|
isOver: (s: MatchState) => isOver(s),
|
|
result(state: MatchState, me: PlayerId): ResultView {
|
|
return resultView(result(state)!, me, state.lanes.map(l => l.topic.name), viewFor(state, me).lanes.map(l => l.leader));
|
|
},
|
|
};
|
|
|
|
/** Stub-style Portals cap a lane; online views don't carry state, so read the cap from the ruleset. */
|
|
function capacityFromView(v: PlayerView, i: number): number {
|
|
const t = v.lanes[i].topic, r = v.ruleset;
|
|
if (v.lanes[i].portalRevealed && t.portal === 'stub') return (r.portalParams[t.id] ?? r.portalParams.stub)?.capacity ?? Math.min(2, r.laneCapacity);
|
|
return r.laneCapacity;
|
|
}
|
|
|
|
/** Build the screen model from what one seat may see (local engine or a hosting node). */
|
|
export function viewFromPlayerView(v: PlayerView, opts: { log?: string[]; capacity?: (lane: number) => number } = {}): MatchView {
|
|
const rules = v.ruleset;
|
|
const me = v.you;
|
|
const onBoard = (c: CardInPlay, power: number, stat: string, allOnTopic: boolean) =>
|
|
toUICard(c.card, rules, { id: `${c.owner}:${c.card.cardId}`, power, onTopic: allOnTopic || c.card.home === stat }); // both seats may share loaner cardIds
|
|
const lanes: LaneView[] = v.lanes.map((lane, i) => {
|
|
const portal = lane.portalRevealed ? portalInfo(lane.topic.portal, lane.topic.id, rules) : undefined;
|
|
const allOn = lane.portalRevealed && lane.topic.portal === 'disambiguation';
|
|
return {
|
|
index: i, stat: lane.topic.stat, revealed: lane.portalRevealed,
|
|
name: lane.portalRevealed ? lane.topic.name : STAT_LABEL[lane.topic.stat],
|
|
modifier: portal, portalNote: lane.portalRevealed ? undefined : `Portal reveals on turn ${rules.portalReveal[i]}`,
|
|
mine: lane.cards[0].map((c, k) => onBoard(c, lane.cardPower[0][k], lane.topic.stat, allOn)),
|
|
theirs: lane.cards[1].map((c, k) => onBoard(c, lane.cardPower[1][k], lane.topic.stat, allOn)),
|
|
staged: v.queue.filter(q => q.lane === i).flatMap(q => {
|
|
const c = v.hand.find(h => h.cardId === q.cardId);
|
|
return c ? [toUICard(c, rules)] : [];
|
|
}),
|
|
myPower: lane.power[0], theirPower: lane.power[1], leader: lane.leader,
|
|
capacity: opts.capacity ? opts.capacity(i) : capacityFromView(v, i),
|
|
};
|
|
});
|
|
const queued = new Set(v.queue.map(q => q.cardId));
|
|
return {
|
|
me, turn: v.turn, maxTurns: rules.turns, energy: v.energyLeft, maxEnergy: v.energy,
|
|
saved: Math.max(0, v.energy - (rules.energyByTurn[v.turn - 1] ?? 0) - rules.bonusEnergy),
|
|
hand: v.hand.filter(c => !queued.has(c.cardId)).map(c => toUICard(c, rules)), deckCount: v.deckCount,
|
|
oppHandCount: v.opponent.handCount, oppDeckCount: v.opponent.deckCount, oppQueued: v.opponent.queuedCount,
|
|
lanes, log: opts.log ?? [], waitingForOpponent: v.committed && v.phase !== 'over', over: v.phase === 'over',
|
|
};
|
|
}
|
|
|
|
/** Results from `me`'s point of view: in a ResultView, 0 always means "you" and 1 "your opponent". */
|
|
export function resultView(r: MatchResult, me: PlayerId, laneNames: string[], leaders?: ('you' | 'opponent' | null)[]): ResultView {
|
|
const opp: PlayerId = me === 0 ? 1 : 0;
|
|
return {
|
|
winner: r.winner === null ? null : r.winner === me ? 0 : 1, turns: r.turns, reason: r.reason,
|
|
totalPower: [r.totalPower[me], r.totalPower[opp]],
|
|
lanes: r.lanePower.map((pw, i) => {
|
|
const a = pw[me], b = pw[opp];
|
|
const lead = leaders?.[i]; // the consensus tie-break (on-topic count) can decide an equal lane
|
|
const winner = lead !== undefined ? (lead === 'you' ? 0 : lead === 'opponent' ? 1 : null) : a === b ? null : a > b ? 0 : 1;
|
|
return { name: laneNames[i] ?? `Topic ${i + 1}`, myPower: a, theirPower: b, winner };
|
|
}),
|
|
};
|
|
}
|
|
|
|
/** Engine actions from a host's legalActions, mapped for the UI (the host checks the seat). */
|
|
export const uiActions = (acts: Action[]): UIAction[] => acts.map(toUI);
|
|
export const engineAction = toEngine;
|