mtg-calculator
Version:
an MtG calculator for computing the probability of being able to draw and play cards from a deck
92 lines (91 loc) • 3.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.scryfallToCard = scryfallToCard;
function cardType(card) {
if ("type_line" in card) {
return card["type_line"].toLowerCase().includes("land") ? "Land" : "Nonland";
}
else {
return null;
}
}
function producedMana(card) {
var _a, _b, _c;
if (cardType(card) !== "Land") {
return null;
}
else if ("card_faces" in card) {
const colors = [];
for (const face of card["card_faces"]) {
const match = (_a = face.oracle_text) === null || _a === void 0 ? void 0 : _a.match(/^(.*?|\n)?\{T\}:.*?[Aa]dd(.+?\{(([A-Z]\/?)+?)\})+/);
if (match) {
const newColors = ((_b = face.oracle_text
.substring(match.index, match.index + match[0].length)
.match(/\{([^T]+)\}/g)) === null || _b === void 0 ? void 0 : _b.map(mana => mana.substring(1, mana.length - 1)).flatMap(mana => mana.split("/"))) || [];
colors.push(...newColors);
}
}
return [...new Set(colors)].join(",") || null;
}
else if ("produced_mana" in card) {
return (_c = card["produced_mana"]) === null || _c === void 0 ? void 0 : _c.join(",");
}
else {
return null;
}
}
function tapLand(card) {
if (cardType(card) !== "Land") {
return null;
}
const hasTapLandPhrase = (c) => {
return c.oracle_text ?
RegExp(`(NAME enters the battlefield tapped.(?!unless))|(Hideaway)|(NAME enters tapped)`)
.test(c.oracle_text.replace(c.name, "NAME")) :
false;
};
let isTapLand = null;
if ("card_faces" in card) {
for (const face of card["card_faces"]) {
if (cardType(face) === "Land") {
isTapLand = hasTapLandPhrase(face);
}
}
}
else {
isTapLand = hasTapLandPhrase(card);
}
return isTapLand;
}
function manaCost(card) {
let cost = null;
if ("card_faces" in card) {
for (const face of card["card_faces"]) {
if ("mana_cost" in face && face["mana_cost"]) {
cost = face["mana_cost"];
}
}
}
else if ("mana_cost" in card) {
cost = card["mana_cost"];
}
return cost;
}
const transformationLookup = {
"mana_cost": manaCost,
"types": (card) => "type_line" in card ? card["type_line"] : null,
"type": cardType,
"producible_mana_colors": producedMana,
"tap_land": tapLand
};
function scryfallToCard(partialScryfallCard) {
var _a;
const card = { name: partialScryfallCard.name, quantity: (_a = partialScryfallCard.quantity) !== null && _a !== void 0 ? _a : 1 };
for (const key in transformationLookup) {
const transformedValue = transformationLookup[key](partialScryfallCard);
if (transformedValue !== null) {
card[key] = transformedValue;
}
}
return card;
}