95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
// Safe JavaScript card source. A card implementation is a module-shaped
|
|
// expression that calls the builder API below:
|
|
//
|
|
// export default [
|
|
// triggered(when.enters(permanents.withType('creature')), () => {
|
|
// gainLife(you, 1)
|
|
// }),
|
|
// ]
|
|
//
|
|
// The source is tokenized, parsed and evaluated against that API only. It is
|
|
// never passed to eval or new Function, so a saved override cannot run code.
|
|
|
|
import type {
|
|
Ability,
|
|
Condition,
|
|
Cost,
|
|
Duration,
|
|
Effect,
|
|
KeywordAbility,
|
|
ManaSpec,
|
|
ObjectSelector,
|
|
PlayerRef,
|
|
SearchDestination,
|
|
StaticEffect,
|
|
Target,
|
|
TokenSpec,
|
|
TriggerEvent,
|
|
Value,
|
|
Zone,
|
|
} from './types.js'
|
|
|
|
import { SIMPLE_KEYWORDS } from './compile.js'
|
|
import { singularize } from './parse.js'
|
|
import { Selector, PLAYER_REFS, DURATIONS, ZONES, SEARCH_DESTINATIONS, COLOR_CODES, CHOICES, RESTRICTION_RULES, REPLACEMENT_WHATS, REPLACEMENT_RESULTS, TRIGGER_PLAYERS, VALUE_KINDS, EFFECT_OPS, STATIC_OPS, COST_KINDS, CONDITION_KINDS, TRIGGER_KINDS, ABILITY_KINDS, expectKind, isRecord, asRecord, asString, asNumber, asBoolean, optionalString, stringArray, asPlayer, asTriggerPlayer, asDuration, asZone, asSearchDestination, asValue, asSelector, isTarget, asTarget, asEffects, asEffect, asStaticEffects, asStaticEffect, asCosts, asCost, asCondition, asTrigger, asAbility, asAbilityArray, asOptions } from './source-validation.js'
|
|
|
|
|
|
export class CardSection {
|
|
readonly typeLine?: string
|
|
readonly abilities: Ability[]
|
|
|
|
constructor(typeLineOrAbilities: unknown, abilities?: unknown) {
|
|
if (abilities === undefined) {
|
|
this.abilities = asAbilityArray(typeLineOrAbilities)
|
|
} else {
|
|
this.typeLine = asString(typeLineOrAbilities, 'card type line')
|
|
this.abilities = asAbilityArray(abilities)
|
|
}
|
|
}
|
|
}
|
|
|
|
export class Face {
|
|
readonly name: string
|
|
readonly typeLine?: string
|
|
readonly abilities: Ability[]
|
|
|
|
constructor(name: unknown, typeLineOrAbilities: unknown, abilities?: unknown) {
|
|
this.name = asString(name, 'face name')
|
|
if (abilities === undefined) {
|
|
this.abilities = asAbilityArray(typeLineOrAbilities)
|
|
} else {
|
|
this.typeLine = asString(typeLineOrAbilities, 'face type line')
|
|
this.abilities = asAbilityArray(abilities)
|
|
}
|
|
}
|
|
}
|
|
|
|
export class Faces {
|
|
readonly faces: Face[]
|
|
|
|
constructor(faces: unknown[]) {
|
|
this.faces = faces.map(face => {
|
|
if (!(face instanceof Face)) throw new Error('faces() expects face(...) entries')
|
|
return face
|
|
})
|
|
}
|
|
}
|
|
|
|
/** Top-level card section: card('Creature — Human', [...abilities]). */
|
|
export function card(typeLineOrAbilities: unknown, abilities?: unknown): CardSection {
|
|
return new CardSection(typeLineOrAbilities, abilities)
|
|
}
|
|
|
|
export function face(name: unknown, typeLineOrAbilities: unknown, abilities?: unknown): Face {
|
|
return new Face(name, typeLineOrAbilities, abilities)
|
|
}
|
|
|
|
export function faces(...entries: unknown[]): Faces {
|
|
return new Faces(entries)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// API and entry point
|
|
// ---------------------------------------------------------------------------
|
|
|