Files
wikideck/shared/duel/text.ts

145 lines
6.9 KiB
TypeScript
Raw Permalink 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.',
};
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: 'enchanted 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 '';
}
}
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');
}