First commit
This commit is contained in:
603
backend/src/effects/format.ts
Normal file
603
backend/src/effects/format.ts
Normal file
@@ -0,0 +1,603 @@
|
||||
import type {
|
||||
Ability,
|
||||
CompiledCard,
|
||||
Condition,
|
||||
Cost,
|
||||
Duration,
|
||||
Effect,
|
||||
ManaSpec,
|
||||
ObjectSelector,
|
||||
StaticEffect,
|
||||
Target,
|
||||
TokenSpec,
|
||||
TriggerEvent,
|
||||
Value,
|
||||
} from './types.js'
|
||||
|
||||
const COLOR_NAMES: Record<string, string> = { W: 'white', U: 'blue', B: 'black', R: 'red', G: 'green', C: 'colorless' }
|
||||
|
||||
function formatValue(value: Value): string {
|
||||
switch (value.kind) {
|
||||
case 'number':
|
||||
return String(value.value)
|
||||
case 'x':
|
||||
return 'X'
|
||||
case 'count':
|
||||
return `count(${formatSelector(value.selector)})`
|
||||
case 'source_power':
|
||||
return 'its power'
|
||||
case 'source_toughness':
|
||||
return 'its toughness'
|
||||
case 'cards_in_hand':
|
||||
return `cards in ${value.player === 'you' ? 'your' : "that player's"} hand`
|
||||
case 'max_cards_in_hand':
|
||||
return 'cards in the hand of the opponent with the most cards'
|
||||
case 'max_opponent_count':
|
||||
return `greatest number of ${formatSelector(value.selector)} an opponent controls`
|
||||
case 'total_mana_value':
|
||||
return `total mana value of ${formatSelector(value.selector)}`
|
||||
case 'total_power':
|
||||
return `total power of ${formatSelector(value.selector)}`
|
||||
case 'any_number':
|
||||
return 'any number'
|
||||
case 'event_amount':
|
||||
return 'that much'
|
||||
}
|
||||
}
|
||||
|
||||
function isBareSelector(selector: ObjectSelector): boolean {
|
||||
return selector.types?.length === 0 && Object.keys(selector).every(key => key === 'types')
|
||||
}
|
||||
|
||||
function formatSelector(selector: ObjectSelector): string {
|
||||
const parts: string[] = []
|
||||
if (selector.other) parts.push('other')
|
||||
if (selector.tapped === true) parts.push('tapped')
|
||||
if (selector.tapped === false) parts.push('untapped')
|
||||
if (selector.attacking) parts.push('attacking')
|
||||
if (selector.token) parts.push('token')
|
||||
if (selector.token === false) parts.push('nontoken')
|
||||
if (selector.attached) parts.push(selector.attached)
|
||||
if (selector.colors?.length) {
|
||||
parts.push(selector.colors.map(color => COLOR_NAMES[color] ?? color).join(' and '))
|
||||
}
|
||||
if (selector.supertypes?.length) parts.push(...selector.supertypes)
|
||||
if (selector.subtypes?.length) parts.push(selector.subtypes.join(' '))
|
||||
if (selector.types?.length) {
|
||||
parts.push(selector.types.join(selector.match === 'any' ? ' or ' : ' '))
|
||||
}
|
||||
if (selector.excludeTypes?.length) parts.push(`non-${selector.excludeTypes.join('/')}`)
|
||||
if (selector.counters) parts.push(`with ${selector.counters.atLeast}+ ${selector.counters.counter} counters`)
|
||||
if (selector.keywords?.length) parts.push(`with ${selector.keywords.join(' and ')}`)
|
||||
if (selector.excludeKeywords?.length) parts.push(`without ${selector.excludeKeywords.join(' and ')}`)
|
||||
if (selector.power?.atLeast !== undefined) parts.push(`power ${selector.power.atLeast}+`)
|
||||
if (selector.power?.atMost !== undefined) parts.push(`power ${selector.power.atMost} or less`)
|
||||
if (selector.toughness?.atLeast !== undefined) parts.push(`toughness ${selector.toughness.atLeast}+`)
|
||||
if (selector.toughness?.atMost !== undefined) parts.push(`toughness ${selector.toughness.atMost} or less`)
|
||||
if (selector.manaValue?.atLeast !== undefined) parts.push(`mana value ${selector.manaValue.atLeast}+`)
|
||||
if (selector.manaValue?.atMost !== undefined) parts.push(`mana value ${selector.manaValue.atMost} or less`)
|
||||
if (selector.name) parts.push(`named ${selector.name}`)
|
||||
|
||||
let text = parts.filter(Boolean).join(' ')
|
||||
if (selector.controller === 'you') text += ' you control'
|
||||
else if (selector.controller === 'opponent') text += ' an opponent controls'
|
||||
if (selector.owner === 'you') text += ' you own'
|
||||
else if (selector.owner === 'opponent') text += " you don't own"
|
||||
|
||||
if (!text) text = 'card'
|
||||
|
||||
if (selector.zone === 'graveyard') text += ' from a graveyard'
|
||||
else if (selector.zone === 'library') text += ' from a library'
|
||||
else if (selector.zone === 'hand') text += ' from a hand'
|
||||
else if (selector.zone === 'exile') text += ' in exile'
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
export function formatTarget(target: Target): string {
|
||||
switch (target.kind) {
|
||||
case 'self':
|
||||
return 'this'
|
||||
case 'trigger_subject':
|
||||
return 'the triggering object'
|
||||
case 'previous_target':
|
||||
return 'the chosen target'
|
||||
case 'any':
|
||||
return 'any target'
|
||||
case 'player':
|
||||
if (target.mode === 'each') {
|
||||
if (target.who === 'each_opponent') return 'each opponent'
|
||||
if (target.who === 'each_player') return 'each player'
|
||||
}
|
||||
if (target.who === 'you') return 'you'
|
||||
if (target.who === 'target_player') return 'target player'
|
||||
if (target.who === 'that_player') return 'that player'
|
||||
if (target.who === 'opponent') return 'target opponent'
|
||||
if (target.who === 'its_controller') return 'its controller'
|
||||
if (target.who === 'its_owner') return 'its owner'
|
||||
return target.who
|
||||
case 'player_or_planeswalker':
|
||||
return 'target player or planeswalker'
|
||||
case 'object': {
|
||||
const prefix = target.mode === 'target' ? 'target ' : target.mode === 'each' ? 'each ' : ''
|
||||
const count = target.count ? `${target.upTo ? 'up to ' : ''}${target.count} ` : ''
|
||||
const includeSelf = target.includeSelf ? 'this or ' : ''
|
||||
return `${prefix}${count}${includeSelf}${formatSelector(target.selector)}`
|
||||
}
|
||||
case 'spell':
|
||||
return isBareSelector(target.selector) ? 'target spell' : `target ${formatSelector(target.selector)} spell`
|
||||
case 'ability':
|
||||
return 'target ability'
|
||||
case 'divided':
|
||||
return `divided(${target.max === 'any' ? 'any number' : target.max}${target.selector ? `, ${formatSelector(target.selector)}` : ''})`
|
||||
}
|
||||
}
|
||||
|
||||
function formatMana(mana: ManaSpec): string {
|
||||
if (mana.kind === 'symbols') {
|
||||
return `${mana.symbols.join('')}${mana.restriction ? ` (only ${mana.restriction})` : ''}`
|
||||
}
|
||||
return `${formatValue(mana.amount)} mana of any${mana.oneColor ? ' one' : ''} color${mana.restriction ? ` (only ${mana.restriction})` : ''}`
|
||||
}
|
||||
|
||||
function formatCost(cost: Cost): string {
|
||||
switch (cost.kind) {
|
||||
case 'mana':
|
||||
return cost.symbols
|
||||
case 'tap':
|
||||
return '{T}'
|
||||
case 'untap':
|
||||
return '{Q}'
|
||||
case 'tap_other':
|
||||
return `tap ${formatSelector(cost.selector)}`
|
||||
case 'energy':
|
||||
return `{E} x${cost.amount}`
|
||||
case 'loyalty':
|
||||
return `${cost.amount > 0 ? '+' : ''}${cost.amount} loyalty`
|
||||
case 'pay_life':
|
||||
return `pay ${formatValue(cost.amount)} life`
|
||||
case 'discard':
|
||||
if (cost.amount.kind === 'cards_in_hand' && cost.amount.player === 'you') {
|
||||
return `discard your hand${cost.random ? ' at random' : ''}`
|
||||
}
|
||||
return `discard ${formatValue(cost.amount)} card(s)${cost.random ? ' at random' : ''}`
|
||||
case 'sacrifice':
|
||||
return `sacrifice ${formatSelector(cost.selector)}`
|
||||
case 'sacrifice_self':
|
||||
return 'sacrifice this'
|
||||
case 'exile_self':
|
||||
return 'exile this from a graveyard'
|
||||
case 'exile_from_graveyard':
|
||||
return `exile ${cost.amount ? `${formatValue(cost.amount)} ` : ''}${formatSelector(cost.selector)} from your graveyard`
|
||||
case 'reveal':
|
||||
return `reveal ${formatSelector(cost.selector)} from your hand`
|
||||
case 'choice':
|
||||
return `(${cost.options.map(option => option.map(formatCost).join(' + ')).join(' or ')})`
|
||||
case 'return_to_hand':
|
||||
return `return ${formatSelector(cost.selector)} to its owner's hand`
|
||||
case 'behold':
|
||||
return `behold ${formatSelector(cost.selector)}`
|
||||
case 'waterbend':
|
||||
return `waterbend ${formatValue(cost.amount)}`
|
||||
case 'blight':
|
||||
return `blight ${formatValue(cost.amount)}`
|
||||
case 'choose':
|
||||
return `choose ${cost.what}`
|
||||
case 'put_counter':
|
||||
return `put ${cost.counter} counter on ${formatSelector(cost.selector)}`
|
||||
case 'remove_counters':
|
||||
return `remove ${formatValue(cost.amount)} ${cost.counter} counter(s) from ${cost.selector ? formatSelector(cost.selector) : 'this'}`
|
||||
case 'forage':
|
||||
return 'forage'
|
||||
case 'collect_evidence':
|
||||
return `collect evidence ${cost.amount}`
|
||||
case 'mill':
|
||||
return `mill ${formatValue(cost.amount)} card(s)`
|
||||
}
|
||||
}
|
||||
|
||||
function formatDuration(duration: Duration): string {
|
||||
switch (duration) {
|
||||
case 'permanent':
|
||||
return ''
|
||||
case 'until_end_of_turn':
|
||||
return ', until end of turn'
|
||||
case 'until_your_next_turn':
|
||||
return ', until your next turn'
|
||||
case 'until_end_of_your_next_turn':
|
||||
return ', until the end of your next turn'
|
||||
case 'until_end_of_combat':
|
||||
return ', until end of combat'
|
||||
case 'until_controllers_next_untap_step':
|
||||
return ", until its controller's next untap step"
|
||||
case 'while_source_on_battlefield':
|
||||
return ', for as long as this remains on the battlefield'
|
||||
}
|
||||
}
|
||||
|
||||
function formatToken(token: TokenSpec): string {
|
||||
const parts: string[] = []
|
||||
if (token.tapped) parts.push('tapped')
|
||||
if (token.power && token.toughness) parts.push(`${formatValue(token.power)}/${formatValue(token.toughness)}`)
|
||||
if (token.colors?.length) parts.push(token.colors.map(color => COLOR_NAMES[color] ?? color).join(' and '))
|
||||
if (token.subtypes.length) parts.push(...token.subtypes)
|
||||
if (token.types.length) parts.push(...token.types)
|
||||
if (token.keywords?.length) parts.push(`with ${token.keywords.join(', ')}`)
|
||||
return parts.join(' ')
|
||||
}
|
||||
|
||||
export function formatEffect(effect: Effect): string {
|
||||
switch (effect.op) {
|
||||
case 'damage':
|
||||
return `damage(${formatTarget(effect.target)}, ${formatValue(effect.amount)}${effect.source ? `, from ${formatTarget(effect.source)}` : ''})`
|
||||
case 'draw':
|
||||
return `draw(${effect.player}, ${formatValue(effect.amount)})`
|
||||
case 'discard':
|
||||
return `discard(${effect.player}, ${formatValue(effect.amount)}${effect.random ? ', at random' : ''})`
|
||||
case 'mill':
|
||||
return `mill(${effect.player}, ${formatValue(effect.amount)})`
|
||||
case 'life':
|
||||
return `${effect.mode === 'gain' ? 'gain_life' : 'lose_life'}(${effect.player}, ${formatValue(effect.amount)})`
|
||||
case 'pump':
|
||||
return `pump(${formatTarget(effect.target)}, ${formatValue(effect.power)}/${formatValue(effect.toughness)}${formatDuration(effect.duration)})`
|
||||
case 'grant':
|
||||
return `grant(${formatTarget(effect.target)}, ${effect.keyword}${effect.value ? ` ${effect.value}` : ''}${formatDuration(effect.duration)})`
|
||||
case 'put_counters':
|
||||
return `put_counters(${formatTarget(effect.target)}, ${effect.counter}, ${formatValue(effect.amount)})`
|
||||
case 'remove_counters':
|
||||
return `remove_counters(${formatTarget(effect.target)}, ${effect.counter}, ${formatValue(effect.amount)})`
|
||||
case 'proliferate':
|
||||
return 'proliferate()'
|
||||
case 'create_token':
|
||||
return effect.copyOf
|
||||
? `create_token(${effect.player}, ${formatValue(effect.amount)}, copy of ${formatTarget(effect.copyOf)}${effect.notLegendary ? ', not legendary' : ''})`
|
||||
: `create_token(${effect.player}, ${formatValue(effect.amount)}, ${formatToken(effect.token)})`
|
||||
case 'destroy':
|
||||
return `destroy(${formatTarget(effect.target)})`
|
||||
case 'exile':
|
||||
return `exile(${formatTarget(effect.target)})`
|
||||
case 'sacrifice':
|
||||
return `sacrifice(${effect.player}, ${formatTarget(effect.target)}${effect.unless ? ` unless ${formatCost(effect.unless)}` : ''})`
|
||||
case 'counter':
|
||||
return `counter(${effect.what}, ${formatTarget(effect.target)}${effect.unless ? ` unless ${formatCost(effect.unless)}` : ''})`
|
||||
case 'return_to_hand':
|
||||
return `return_to_hand(${formatTarget(effect.target)})`
|
||||
case 'return_to_battlefield': {
|
||||
const counters = effect.counters
|
||||
? `, ${effect.counters.amount.kind === 'number' ? effect.counters.amount.value : formatValue(effect.counters.amount)} ${effect.counters.counter} counters`
|
||||
: ''
|
||||
return `return_to_battlefield(${formatTarget(effect.target)}${effect.tapped ? ', tapped' : ''}${effect.transformed ? ', transformed' : ''}${counters})`
|
||||
}
|
||||
case 'set_pt':
|
||||
return `set_pt(${formatTarget(effect.target)}, ${formatValue(effect.power)}/${formatValue(effect.toughness)}${formatDuration(effect.duration)})`
|
||||
case 'put_on_top':
|
||||
return `put_on_top(${formatTarget(effect.target)})`
|
||||
case 'tap':
|
||||
return `tap(${formatTarget(effect.target)})`
|
||||
case 'untap':
|
||||
return `untap(${formatTarget(effect.target)})`
|
||||
case 'scry':
|
||||
return `scry(${effect.player}, ${formatValue(effect.amount)})`
|
||||
case 'surveil':
|
||||
return `surveil(${effect.player}, ${formatValue(effect.amount)})`
|
||||
case 'search':
|
||||
return `search(${effect.player}, ${formatSelector(effect.selector)}, ${formatValue(effect.amount)}, to ${effect.destination}${effect.zones?.length === 2 ? ', library+graveyard' : ''}${effect.tapped ? ', tapped' : ''}${effect.shuffle ? ', shuffle' : ''})`
|
||||
case 'shuffle':
|
||||
return `shuffle(${effect.player})`
|
||||
case 'add_mana':
|
||||
return `add_mana(${effect.player}, ${formatMana(effect.mana)})`
|
||||
case 'fight':
|
||||
return `fight(${formatTarget(effect.a)}, ${formatTarget(effect.b)})`
|
||||
case 'regenerate':
|
||||
return `regenerate(${formatTarget(effect.target)})`
|
||||
case 'energy':
|
||||
return `energy(${effect.player}, ${formatValue(effect.amount)})`
|
||||
case 'attach':
|
||||
return `attach(this, ${formatTarget(effect.target)})`
|
||||
case 'restriction':
|
||||
return `restrict(${formatTarget(effect.target)}, ${effect.rule}${formatDuration(effect.duration)})`
|
||||
case 'keyword_action':
|
||||
return `${effect.action.replace(/ /g, '_')}(${effect.target ? formatTarget(effect.target) : ''}${effect.amount ? `${effect.target ? ', ' : ''}${formatValue(effect.amount)}` : ''}${effect.detail ? `, ${effect.detail}` : ''})`
|
||||
case 'level_up':
|
||||
return `level_up(${effect.level})`
|
||||
case 'put_from_among':
|
||||
return `put_from_among(${formatValue(effect.amount)}${effect.selector ? `, ${formatSelector(effect.selector)}` : ''}, to ${effect.destination})`
|
||||
case 'dig':
|
||||
return `dig(${formatValue(effect.amount)}, ${effect.upTo ? 'up to ' : ''}${formatValue(effect.pick)}${effect.pickSelector ? ` ${formatSelector(effect.pickSelector)}` : ''} to ${effect.to}${effect.tapped ? ' tapped' : ''}, rest ${effect.rest}${effect.order ? ` ${effect.order}` : ''}${effect.optional ? ', optional' : ''})`
|
||||
case 'prevent_damage':
|
||||
return `prevent_${effect.combatOnly ? 'combat_' : ''}damage(${formatTarget(effect.target)}, ${effect.amount === 'all' ? 'all' : formatValue(effect.amount)}${formatDuration(effect.duration)})`
|
||||
case 'gain_control':
|
||||
return `gain_control(${formatTarget(effect.target)}${formatDuration(effect.duration)})`
|
||||
case 'emblem':
|
||||
return `emblem("${effect.text}")`
|
||||
case 'transform':
|
||||
return `transform(${formatTarget(effect.target)})`
|
||||
case 'pay':
|
||||
return `pay(${effect.costs.map(formatCost).join(', ')})`
|
||||
case 'conditional':
|
||||
return `if ${formatCondition(effect.condition)}:\n${indent(effect.effects.map(formatEffect).join('\n'))}`
|
||||
case 'modal': {
|
||||
const choose = effect.min === effect.max ? String(effect.min) : `${effect.min}-${effect.max}`
|
||||
const body = effect.modes
|
||||
.map((mode, index) => `${index + 1}. ${mode.map(formatEffect).join('; ')}`)
|
||||
.join('\n')
|
||||
return `choose ${choose}:\n${indent(body)}`
|
||||
}
|
||||
case 'look_top':
|
||||
return `look_top(${effect.player}, ${formatValue(effect.amount)}${effect.reorder ? ', reorder' : ''})`
|
||||
case 'schedule':
|
||||
return `schedule(${effect.when}):\n${indent(effect.effects.map(formatEffect).join('\n'))}`
|
||||
case 'damage_divided':
|
||||
return `damage_divided(${formatValue(effect.amount)}, ${formatTarget(effect.target)})`
|
||||
}
|
||||
}
|
||||
|
||||
function formatCondition(condition: Condition): string {
|
||||
switch (condition.kind) {
|
||||
case 'you_control':
|
||||
return `you control ${condition.atLeast ? `at least ${condition.atLeast} ` : ''}${formatSelector(condition.selector)}`
|
||||
case 'opponent_controls':
|
||||
return `an opponent controls ${formatSelector(condition.selector)}`
|
||||
case 'your_turn':
|
||||
return 'it is your turn'
|
||||
case 'not_your_turn':
|
||||
return "it isn't your turn"
|
||||
case 'source_tapped':
|
||||
return 'this is tapped'
|
||||
case 'life_at_least':
|
||||
return `${condition.player} has at least ${condition.amount} life`
|
||||
case 'life_at_most':
|
||||
return `${condition.player} has at most ${condition.amount} life`
|
||||
case 'cards_in_graveyard':
|
||||
return `${condition.player} has ${condition.atLeast}+ cards in their graveyard`
|
||||
case 'source_untapped':
|
||||
return 'this is untapped'
|
||||
case 'source_attacking':
|
||||
return 'this is attacking'
|
||||
case 'life_above_starting':
|
||||
return `${condition.player} has at least ${condition.amount} life more than their starting life total`
|
||||
case 'hand_empty':
|
||||
return `${condition.player} has no cards in hand`
|
||||
case 'creature_died_this_turn':
|
||||
return `${condition.controller === 'you' ? 'a creature you control' : condition.controller === 'opponent' ? 'a creature an opponent controls' : 'a creature'} died this turn`
|
||||
case 'you_gained_life_this_turn':
|
||||
return `you gained ${condition.atLeast ? `at least ${condition.atLeast} ` : ''}life this turn`
|
||||
case 'you_drew_cards_this_turn':
|
||||
return `you drew ${condition.atLeast ? `${condition.atLeast}+ ` : 'a '}cards this turn`
|
||||
case 'spells_cast_last_turn':
|
||||
return `${condition.who} cast ${condition.atLeast}+ spells last turn`
|
||||
case 'no_spells_last_turn':
|
||||
return 'no spells were cast last turn'
|
||||
case 'attacked_this_turn':
|
||||
return 'this attacked this turn'
|
||||
case 'you_attacked_this_turn':
|
||||
return 'you attacked this turn'
|
||||
case 'defending_player_controls':
|
||||
return `defending player controls ${formatSelector(condition.selector)}`
|
||||
case 'targets':
|
||||
return `it targets ${formatSelector(condition.selector)}`
|
||||
case 'was_kicked':
|
||||
return 'this was kicked'
|
||||
case 'cast':
|
||||
return condition.fromHand ? 'you cast it from your hand' : 'you cast it'
|
||||
case 'in_graveyard':
|
||||
return 'this is in your graveyard'
|
||||
case 'on_battlefield':
|
||||
return 'this is on the battlefield'
|
||||
case 'monarch':
|
||||
return "you're the monarch"
|
||||
case 'initiative':
|
||||
return 'you have the initiative'
|
||||
case 'citys_blessing':
|
||||
return "you have the city's blessing"
|
||||
case 'suspended':
|
||||
return 'this is suspended'
|
||||
case 'bargained':
|
||||
return 'it was bargained'
|
||||
case 'was_creature':
|
||||
return 'it was a creature'
|
||||
case 'opponent_lost_life_this_turn':
|
||||
return 'an opponent lost life this turn'
|
||||
case 'life_changed_this_turn':
|
||||
return 'you gained or lost life this turn'
|
||||
case 'descended_this_turn':
|
||||
return 'you descended this turn'
|
||||
case 'permanent_left_battlefield_this_turn':
|
||||
return 'a permanent you controlled left the battlefield this turn'
|
||||
case 'nonland_permanents_entered_this_turn':
|
||||
return `${condition.atLeast}+ nonland permanents entered the battlefield under your control this turn`
|
||||
case 'attacked_with_total_power':
|
||||
return `you attacked with total power ${condition.atLeast}+ this combat`
|
||||
case 'total_power':
|
||||
return `creatures you control have total power ${condition.atLeast}+`
|
||||
case 'opponent_controls_more_lands':
|
||||
return 'an opponent controls more lands than you'
|
||||
case 'distinct_powers':
|
||||
return `you control ${condition.atLeast}+ creatures with different powers`
|
||||
case 'graveyard_card_types':
|
||||
return `there are ${condition.atLeast}+ card types in your graveyard`
|
||||
case 'no_objects':
|
||||
return `no ${formatSelector(condition.selector)} on the battlefield`
|
||||
case 'cards_in_hand_at_most':
|
||||
return `${condition.who} has ${condition.atMost} or fewer cards in hand`
|
||||
case 'not_their_turn':
|
||||
return 'it is not their turn'
|
||||
case 'no_attackers':
|
||||
return 'no creatures attacked this turn'
|
||||
case 'opponent_poison':
|
||||
return `an opponent has ${condition.atLeast}+ poison counters`
|
||||
case 'tribute_not_paid':
|
||||
return "tribute wasn't paid"
|
||||
case 'had_counters':
|
||||
return condition.counter ? `it had a ${condition.counter} counter on it` : 'it had counters on it'
|
||||
case 'mana_from_treasure':
|
||||
return 'mana from a Treasure was spent to cast it'
|
||||
case 'mana_spent':
|
||||
return `at least ${condition.atLeast} mana was spent to cast it`
|
||||
case 'no_mana_spent':
|
||||
return 'no mana was spent to cast it'
|
||||
case 'mana_symbol_spent':
|
||||
return `${condition.symbols} was spent to cast it`
|
||||
case 'kicked_with':
|
||||
return `it was kicked with ${condition.cost}`
|
||||
case 'more_cards_than_each_opponent':
|
||||
return 'you have more cards in hand than each opponent'
|
||||
case 'first_combat_phase':
|
||||
return 'it is the first combat phase of the turn'
|
||||
case 'not_token':
|
||||
return 'it is not a token'
|
||||
case 'cast_spell_this_turn':
|
||||
return `you've cast ${condition.noncreature ? 'a noncreature ' : 'a '}spell this turn`
|
||||
case 'full_party':
|
||||
return 'you have a full party'
|
||||
case 'graveyard_count':
|
||||
return `there are ${condition.atLeast}+ ${formatSelector(condition.selector)} in your graveyard`
|
||||
case 'creature_power_greater_than_base':
|
||||
return `you control a creature with ${condition.stat} greater than its base ${condition.stat}`
|
||||
case 'if_you_do':
|
||||
return 'you do'
|
||||
case 'if_you_dont':
|
||||
return "you don't"
|
||||
case 'otherwise':
|
||||
return 'otherwise'
|
||||
}
|
||||
}
|
||||
|
||||
function formatTrigger(event: TriggerEvent): string {
|
||||
switch (event.kind) {
|
||||
case 'enters':
|
||||
return `enters(${formatTarget(event.subject)})`
|
||||
case 'dies':
|
||||
return `dies(${formatTarget(event.subject)})`
|
||||
case 'attacks':
|
||||
return `attacks(${formatTarget(event.subject)})`
|
||||
case 'blocks':
|
||||
return `blocks(${formatTarget(event.subject)})`
|
||||
case 'attacks_or_blocks':
|
||||
return `attacks_or_blocks(${formatTarget(event.subject)})`
|
||||
case 'enters_or_attacks':
|
||||
return `enters_or_attacks(${formatTarget(event.subject)})`
|
||||
case 'enters_or_dies':
|
||||
return `enters_or_dies(${formatTarget(event.subject)})`
|
||||
case 'leaves_battlefield':
|
||||
return `leaves_battlefield(${formatTarget(event.subject)})`
|
||||
case 'deals_damage':
|
||||
return `deals_${event.combat ? 'combat_' : ''}damage(${formatTarget(event.subject)}, ${event.recipient})`
|
||||
case 'upkeep':
|
||||
return `upkeep(${event.who})`
|
||||
case 'end_step':
|
||||
return `end_step(${event.who})`
|
||||
case 'begin_combat':
|
||||
return `begin_combat(${event.who})`
|
||||
case 'main_phase':
|
||||
return `${event.phase}_main_phase(${event.who})`
|
||||
case 'cast':
|
||||
return `cast(${event.who}, ${isBareSelector(event.spell) ? 'any spell' : formatSelector(event.spell)}${event.targetsSelf ? ', targets this' : ''}${event.orCopy ? ', or copy' : ''}${event.ordinal ? `, #${event.ordinal} each turn` : ''})`
|
||||
case 'becomes_level':
|
||||
return `becomes_level(${event.level})`
|
||||
case 'draw':
|
||||
return `draw(${event.who}${event.ordinal ? `, #${event.ordinal} each turn` : ''})`
|
||||
case 'counter_placed':
|
||||
return `counter_placed(${formatTarget(event.subject)}${event.counter ? `, ${event.counter}` : ''})`
|
||||
case 'becomes_target':
|
||||
return `becomes_target(${formatTarget(event.subject)})`
|
||||
case 'life':
|
||||
return `${event.mode}_life(${event.who})`
|
||||
}
|
||||
}
|
||||
|
||||
function formatStaticEffect(effect: StaticEffect): string {
|
||||
switch (effect.op) {
|
||||
case 'pump':
|
||||
return `pump(${formatTarget(effect.target)}, ${formatValue(effect.power)}/${formatValue(effect.toughness)})`
|
||||
case 'grant':
|
||||
return `grant(${formatTarget(effect.target)}, ${effect.keyword}${effect.value ? ` ${effect.value}` : ''})`
|
||||
case 'restriction': {
|
||||
const extra = [
|
||||
effect.value ? formatValue(effect.value) : '',
|
||||
effect.comparison ? effect.comparison.replace(/_/g, ' ') : '',
|
||||
effect.detail ?? '',
|
||||
effect.condition ? `if ${formatCondition(effect.condition)}` : '',
|
||||
].filter(Boolean).join(' ')
|
||||
return `restrict(${formatTarget(effect.target)}, ${effect.rule}${extra ? ` ${extra}` : ''})`
|
||||
}
|
||||
case 'enters_tapped':
|
||||
return `enters_tapped(${formatTarget(effect.target)}${effect.unless ? `, unless ${formatCondition(effect.unless)}` : ''})`
|
||||
case 'enters_with_counters':
|
||||
return `enters_with_counters(${formatTarget(effect.target)}, ${effect.counter}, ${formatValue(effect.amount)})`
|
||||
case 'grant_ability':
|
||||
return `grant_ability(${formatTarget(effect.target)}):\n${indent(formatAbility(effect.ability))}`
|
||||
case 'set_pt':
|
||||
return `set_pt(${formatTarget(effect.target)}${effect.base ? ', base' : ''}, ${effect.power ? formatValue(effect.power) : '?'}/${effect.toughness ? formatValue(effect.toughness) : '?'})`
|
||||
case 'control':
|
||||
return `control(${formatTarget(effect.target)})`
|
||||
case 'choose':
|
||||
return `choose(${effect.what})`
|
||||
case 'replacement':
|
||||
return `replace(${effect.what}${effect.target ? `, ${formatTarget(effect.target)}` : ''}, instead ${effect.instead})`
|
||||
case 'cost_reduction':
|
||||
return `cost_reduction(${formatTarget(effect.target)}, ${formatValue(effect.amount)}${effect.condition ? `, if ${formatCondition(effect.condition)}` : ''})`
|
||||
}
|
||||
}
|
||||
|
||||
function indent(text: string, spaces = 2): string {
|
||||
const pad = ' '.repeat(spaces)
|
||||
return text
|
||||
.split('\n')
|
||||
.map(line => `${pad}${line}`)
|
||||
.join('\n')
|
||||
}
|
||||
|
||||
export function formatAbility(ability: Ability): string {
|
||||
switch (ability.kind) {
|
||||
case 'keyword':
|
||||
return `keyword ${ability.keyword}${ability.value ? ` ${ability.value}` : ''}`
|
||||
case 'keywords':
|
||||
return `keywords ${ability.keywords.map(keyword => `${keyword.keyword}${keyword.value ? ` ${keyword.value}` : ''}`).join(', ')}`
|
||||
case 'triggered': {
|
||||
const condition = ability.condition ? ` if ${formatCondition(ability.condition)}` : ''
|
||||
const body = ability.effects.map(formatEffect).join('\n')
|
||||
return `on ${formatTrigger(ability.event)}${condition}${ability.optional ? ' (may)' : ''}:\n${indent(body)}`
|
||||
}
|
||||
case 'activated': {
|
||||
const costs = ability.costs.map(formatCost).join(', ')
|
||||
const condition = ability.condition ? ` if ${formatCondition(ability.condition)}` : ''
|
||||
const limit = ability.activationsPerTurn ? ` (${ability.activationsPerTurn}/turn)` : ''
|
||||
const timing = ability.timing === 'sorcery' ? ' (sorcery speed)' : ''
|
||||
const body = ability.effects.map(formatEffect).join('\n')
|
||||
return `activate ${costs}${condition}${limit}${timing}${ability.optional ? ' (may)' : ''}:\n${indent(body)}`
|
||||
}
|
||||
case 'static': {
|
||||
const condition = ability.condition ? ` if ${formatCondition(ability.condition)}` : ''
|
||||
return `static${condition}:\n${indent(ability.effects.map(formatStaticEffect).join('\n'))}`
|
||||
}
|
||||
case 'spell':
|
||||
return `spell${ability.optional ? ' (may)' : ''}:\n${indent(ability.effects.map(formatEffect).join('\n'))}`
|
||||
case 'additional_cost':
|
||||
return `additional cost${ability.optional ? ' (may)' : ''}: ${ability.costs.map(formatCost).join(', ')}`
|
||||
case 'chapter':
|
||||
return `chapter ${ability.chapters.join(', ')}${ability.optional ? ' (may)' : ''}:\n${indent(ability.effects.map(formatEffect).join('\n'))}`
|
||||
case 'level_band': {
|
||||
const band = ability.max !== undefined ? `${ability.min}-${ability.max}` : `${ability.min}+`
|
||||
const pt = ability.power ? `: ${formatValue(ability.power)}/${formatValue(ability.toughness!)}` : ''
|
||||
return `level ${band}${pt}`
|
||||
}
|
||||
case 'level_pt':
|
||||
return `level_pt(${formatValue(ability.power)}/${formatValue(ability.toughness)})`
|
||||
case 'unsupported':
|
||||
return `unsupported (${ability.reason}): ${ability.text.replace(/\n/g, '\n ')}`
|
||||
}
|
||||
}
|
||||
|
||||
export function formatCard(card: CompiledCard): string {
|
||||
const lines = [`# ${card.name} [${card.status}]`]
|
||||
if (card.faces?.length) {
|
||||
for (const face of card.faces) {
|
||||
lines.push('', `## ${face.name} [${face.status}]`)
|
||||
lines.push(face.abilities.map(formatAbility).join('\n') || '(no abilities)')
|
||||
}
|
||||
} else {
|
||||
lines.push(card.abilities.map(formatAbility).join('\n') || '(no abilities)')
|
||||
}
|
||||
return lines.join('\n')
|
||||
}
|
||||
Reference in New Issue
Block a user