Files
wikideck/shared/duel/text.ts
T
claudeanderic dbb89ce9c5 Night 2: 40-card Magic-style duel, PvP with owned decks, polite Wikimedia client, Android mining (#23)
Night 2 (2026-11-17), from the `claude/wikideck` fork's `night/2026-11-17` branch. It follows Night 1 (#22).

## What's in it
- **Duel engine** (fork #72, #80): 40-card decks of cards you own, Notability/Citations, keywords, effects T1–T12, Connected, a hidden-information-safe `viewFor`/`logFor`, and replay. Balance v1 tuning (#84) gives the first player about 51%.
- **Card profiles** (#70, #82): Figure, Location, Event, Institution or Doctrine, plus stats, keywords and rules text, all derived from real article data. Templates only.
- **Design and story** (#66, #69, #83): DUEL_DESIGN.md, plus the match log and the encyclopedic "Battle of X (2026)" recap.
- **PvP** (#74, #77, #81, #76, #85):
  - The server checks ownership, and 40-card eligibility is enforced.
  - Modes: Classic (ranked), Draft and Trending.
  - Invites, quick match, Elo, a ladder and recap pages. Online matches render on the duel board.
- **Duel board UI** (#79): MTG-style card faces and combat declaration, built for 360 px first.
- **Polite Wikimedia client** (#73, #75): throttled, cached and identified requests, the mint-path fix, and a prefetch pool.
- **Android mining** (#78): native RandomX, as a sideload build. The emulator mined a testnet block.
- **Testnet ops** (#67, #68).

PR numbers refer to `claude/wikideck`.

## Status
- **Tests:** 423, of which 422 pass, 0 fail and 1 is skipped. The build is OK (`node:24-alpine`, `--network none`).
- **Testnet:** running this commit (036489c) on r350.
- **Untouched:** no changes to `server/testnet/consensus.ts`, `randomx.ts`, `native/` or the mainnet compose.
- **Review:** night-review reviewed fork #66–#79. #80–#85 came from the agents' unfinished branches and were not reviewed.
- **Known gaps:**
  - The PvP end-to-end test hasn't been run (it needs two eom.dev accounts).
  - Bot matches aren't reachable from Play.
  - Card identity pre-bake hasn't been done.
  - The RandomX verifier latch (`server/testnet/randomx.ts:140`) is still open.

This PR is for Eric to merge.

🤖 Generated with [Claude Code](https://claude.com/claude-code)Reviewed-on: #23

Co-authored-by: Claude <claude@eom.dev>
2026-11-18 18:12:24 +00:00

150 lines
7.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Rules text in MTG's terse register (designer review §3.4–3.5, critic S7, BRIEF decision 5).
* Deterministic templates filled only with the card's own derived numbers and keyword names.
* No prose about the subject, no invented facts: flavour text is the article's own first sentence.
* Owner: duel-cards.
*/
import type { DuelCard, Keyword } from './derive.ts';
import type { EffectSpec } from './effects.ts';
/** Reminder text per keyword (DUEL_DESIGN §4). */
export const KEYWORD_REMINDER: Readonly<Record<Keyword, string>> = {
Protected: 'Can\'t be the target of Events or Doctrines an opponent controls.',
Featured: 'Can\'t be blocked except by Featured, Translated or Multilingual Figures.',
Legacy: 'When this is destroyed, draw a card.',
Multilingual: 'Can block Featured Figures. Can\'t be blocked by Local Figures.',
Translated: 'Can block Featured Figures.',
'Well-sourced': 'Prevent the first 1 damage that would be dealt to this each turn.',
Stub: 'At the start of your turn, put a +1/+1 counter on this, to a maximum of 2.',
Local: 'Gets +1/+1 while you control a Compatriot Location.',
Trending: 'Can attack the turn it enters. Gets +1/+0.',
};
const NUMBER_WORDS = ['zero', 'one', 'two', 'three', 'four', 'five', 'six'];
const word = (n: number) => NUMBER_WORDS[n] ?? String(n);
const signed = (n: number) => (n >= 0 ? `+${n}` : `−${-n}`);
const pt = (p = 0, t = 0) => `${signed(p)}/${signed(t)}`;
const TARGET_NOUN: Readonly<Record<string, string>> = {
figure: 'target Figure', figureOrLocation: 'target Figure or Location', ownFigure: 'target Figure you control',
opponentFigure: 'target Figure you don\'t control', each: 'each Figure and each Location', self: 'this',
attached: 'adopted Figure', ownFigures: 'Figures you control',
};
function keywordsAsText(keywords: readonly Keyword[]): string {
return keywords.join(', ');
}
function staticText(e: EffectSpec): string {
const c = e.condition ?? {};
let who = 'Figures you control';
if (c.maxPower !== undefined) who = `Figures you control with power ${c.maxPower} or less`;
if (c.connected) who = 'Figures you control that are Connected to this';
if (c.samePortal) who = 'Other Figures you control of its Portal';
if (e.grant?.length && !e.power && !e.toughness) {
return `${who} have ${keywordsAsText(e.grant)}.`;
}
return `${who} get ${pt(e.power, e.toughness)}.`;
}
function tokenText(e: EffectSpec): string {
const t = e.token!;
const n = e.amount ?? 1;
const portal = t.portal === 'Neutral' ? '' : `${t.portal} `;
const body = `${t.power}/${t.toughness} ${portal}Figure token${n === 1 ? '' : 's'} named ${t.name}`;
return n === 1 ? `create a ${body}` : `create ${word(n)} ${body}`;
}
function actionText(e: EffectSpec): string {
switch (e.op) {
case 'damage': return `deal ${e.amount} damage to ${TARGET_NOUN[e.target ?? 'figure']}`;
case 'damageEach': return `deal ${e.amount} damage to each Figure and each Location`;
case 'createToken': return tokenText(e);
case 'pump': return `${TARGET_NOUN[e.target ?? 'figure']} gets ${pt(e.power, e.toughness)}${e.duration === 'turn' ? ' until end of turn' : ''}`;
case 'fight': return `it fights ${TARGET_NOUN[e.target ?? 'opponentFigure']}`;
case 'draw': return e.amount === 1 ? 'draw a card' : `draw ${word(e.amount ?? 1)} cards`;
case 'gainLife': return `you gain ${e.amount} Notability`;
case 'attach': return '';
case 'drain': return `each opponent loses ${e.amount} Notability and you gain ${e.amount} Notability`;
case 'destroy': return `destroy ${TARGET_NOUN[e.target ?? 'figure'] ?? 'target permanent'}`;
case 'bounce': return `return ${TARGET_NOUN[e.target ?? 'figure'] ?? 'target permanent'} to its owner's hand`;
case 'tap': return `tap ${TARGET_NOUN[e.target ?? 'figure'] ?? 'target permanent'}`;
}
}
const cap = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
/** One sentence per effect, in effect order. */
export function effectText(effects: readonly EffectSpec[]): string[] {
const lines: string[] = [];
for (let i = 0; i < effects.length; i++) {
const e = effects[i];
if (e.op === 'attach') {
// The Doctrine's buff is the static pump on `attached` that follows (DUEL_API "Doctrine").
const next = effects[i + 1];
const buff = next && next.trigger === 'static' && next.op === 'pump' && next.target === 'attached' ? next : null;
let line = `Attach to ${e.target === 'figure' ? 'target Figure' : 'target Figure you control'}.`;
if (buff) {
const grant = buff.grant?.length ? ` and has ${keywordsAsText(buff.grant)}` : '';
line += ` It gets ${pt(buff.power, buff.toughness)}${grant}.`;
i++;
}
lines.push(line);
continue;
}
if (e.op === 'fight' && i > 0 && effects[i - 1].op === 'pump') {
lines[lines.length - 1] = lines[lines.length - 1].replace(/\.$/, `, then ${actionText(e)}.`);
continue;
}
switch (e.trigger) {
case 'static': lines.push(staticText(e)); break;
case 'onPlay': lines.push(`${cap(actionText(e))}.`); break;
case 'onEnter': lines.push(`When this enters, ${actionText(e)}.`); break;
case 'onDestroyed': lines.push(`When this is destroyed, ${actionText(e)}.`); break;
case 'startOfTurn': {
const max = e.condition?.maxTokens;
const cond = max !== undefined ? `, if you control fewer than ${max} tokens` : '';
lines.push(`At the start of your turn${cond}, ${actionText(e)}.`);
break;
}
case 'onCombatDamageToPlayer':
// On a Doctrine the trigger belongs to the attached Figure ("it"); on a Figure, to "this".
lines.push(`Whenever ${effects.some(x => x.op === 'attach') ? 'it' : 'this'} deals combat damage to a player, ${actionText(e)}.`);
break;
}
}
return lines;
}
/** The type line, e.g. "Figure — Society" or "Event — History". */
export function typeLine(card: Pick<DuelCard, 'type' | 'portal'>): string {
return card.portal === 'Neutral' ? card.type : `${card.type} — ${card.portal}`;
}
/** Full rules text: keyword line, Location lines, effect lines. Newline-separated. */
export function rulesTextFor(card: Pick<DuelCard, 'type' | 'keywords' | 'effects' | 'territory' | 'toughness'>): string {
const lines: string[] = [];
if (card.keywords.length) lines.push(`${keywordsAsText(card.keywords)}.`);
if (card.type === 'Location') {
if (card.territory) lines.push('Territory.');
lines.push(`Garrison 0/${card.toughness}.`);
}
lines.push(...effectText(card.effects));
return lines.join('\n');
}
export const RULE_WORD_REMINDER: Readonly<Record<'Territory' | 'Garrison', string>> = {
Territory: 'Your Citation maximum is 1 higher while you control this, to at most +2 from Territory.',
Garrison: 'This can block like a Figure with power 0. It can\'t attack.',
};
/** Rules text with reminder text for keywords and rule words, for the tap-to-expand view. */
export function rulesTextWithReminders(card: Pick<DuelCard, 'type' | 'keywords' | 'effects' | 'territory' | 'toughness'>): string {
const lines = card.keywords.map(k => `${k}. (${KEYWORD_REMINDER[k]})`);
for (const line of rulesTextFor({ ...card, keywords: [] }).split('\n').filter(Boolean)) {
const word = line.match(/^(Territory|Garrison)\b/)?.[1] as keyof typeof RULE_WORD_REMINDER | undefined;
lines.push(word ? `${line} (${RULE_WORD_REMINDER[word]})` : line);
}
return lines.join('\n');
}