# -*- coding: utf-8 -*- """Role tagging, heuristic scoring and benchmark lookup for the TRK set review.""" import re, math, json, pickle, os, random from collections import Counter BASE = os.path.dirname(os.path.abspath(__file__)) DATA = os.path.join(BASE, 'data') RARITY_ZH = {'common': '普通', 'uncommon': '非普通', 'rare': '稀有', 'mythic': '秘稀', 'special': '特殊', 'bonus': '额外'} def txt(c): return (c.get('oracle_text') or '').replace('\n', ' ') def has(c, *pats): t = txt(c).lower() return any(re.search(p, t) for p in pats) def num(pattern, t, default=None, cast=int): m = re.search(pattern, t) if not m: return default try: return cast(m.group(1)) except Exception: return default TAG_RULES = [ ('wipe', [r'destroy all', r'exile all', r'all creatures get -', r'deals? \w+ damage to each creature', r'damage to each creature and each player', r'sacrifices? .* creature of their choice.*each', r'each player sacrifices a creature', r'destroy each', r'exile each']), ('counterspell', [r'counter target spell', r'counter target .*spell']), ('removal', [r'destroy target', r'exile target', r'deals? (?:twice )?x damage to (?:any target|target creature)', r'deals? \d+ damage to (?:any target|target creature|each creature|target attacking)', r'target creature gets -\d+/-\d+', r'return target creature .* to its owner.s hand', r'target creature .* fights', r'each opponent sacrifices', r'sacrifice a creature', r'target creature .* can.t block', r'tap target creature .* stun counter']), ('bounce', [r'return target .* to (?:its|their) owner.s hand']), ('draw', [r'draw (?:a|two|three|four|five|x|\d+) cards?', r'draws? (?:a|two|three|x|\d+) cards?']), ('loot', [r'draw .* then discard', r'discard .* then draw', r'draw a card, then discard']), ('ramp', [r'search your library for .*land.*put (?:it|them|those) onto the battlefield', r'put (?:a|one|two) land card.*onto the battlefield', r'play an additional land', r'add (?:one|\{|\w) mana', r'add \{[^}]+\}', r'adds? one mana']), ('fixing', [r'add one mana of any color', r'add \{.+\}.*\{.+\}']), ('mana_rock', [r'\{t\}: add']), ('ritual', [r'add \{[wubrgc]\}\{[wubrgc]+\}']), ('reanimate', [r'assimilate target creature card', r'return target .*creature card from your graveyard to the battlefield', r'put target .*creature card .* from .*graveyard onto the battlefield', r'return .* from your graveyard to the battlefield']), ('recursion', [r'return target .*card from your graveyard to your hand', r'return target .* card from your graveyard to your hand', r'put target card from your graveyard on top']), ('gy_hate', [r'exile target card from a graveyard', r'exile target .* from a graveyard', r'exile .*graveyard', r'cards? from .* graveyard']), ('discard', [r'discards? (?:a|two|three|x|\d+) cards?', r'target player discards', r'each opponent discards']), ('sacrifice', [r'sacrifice a creature', r'sacrifice another', r'sacrifice an artifact', r'sacrifice a nontoken', r'sacrifice a permanent', r'sacrifice a land']), ('token', [r'create (?:a|two|three|four|x|\d+) .*token', r'create .* tokens?']), ('anthem', [r'creatures you control get \+', r'other creatures you control get \+', r'artifact creatures you control get \+', r'creatures you control have']), ('pump', [r'target creature gets \+', r'gets \+\d+/\+\d+ until end of turn']), ('lifegain', [r'you gain \d+ life', r'gain life', r'you gain life']), ('drain', [r'each opponent loses \d+ life', r'opponent loses \d+ life and you gain']), ('tutor', [r'search your library for']), ('blink', [r'exile .*return (?:that card|it) to the battlefield', r'return that card to the battlefield under its owner.s control at the beginning']), ('copy', [r'create a token that.s a copy', r'copy target', r'copy of target']), ('theft', [r'gain control of target']), ('extra_turn', [r'take an extra turn', r'extra combat']), ('protection', [r'hexproof', r'indestructible', r'protection from', r'can.t be the target', r'prevent all damage']), ('stax', [r'can.t attack', r'can.t block', r'can.t untap', r'can.t be blocked', r'players can.t', r'doesn.t untap', r'enters tapped']), ('surveil', [r'surveil']), ('mill', [r'\bmill \d+', r'put the top .* cards? of your library into your graveyard']), ('explore', [r'explores?']), ('discover', [r'discover \d+']), ('dilemma', [r'face a dilemma', r'faced a dilemma', r'choose one —', r'choose one or more']), ('station', [r'station']), ('beamup', [r'beam me up']), ('kick', [r'kicker']), ('cycling', [r'cycling']), ('landfall', [r'landfall']), ('counters_matter', [r'\+1/\+1 counter', r'counters? on']), ('treasure', [r'treasure token']), ('clue', [r'investigate', r'clue token']), ('food', [r'food token']), ('energy', [r'energy counter']), ] KEYWORDS = ['flying', 'menace', 'trample', 'deathtouch', 'lifelink', 'vigilance', 'reach', 'first strike', 'double strike', 'haste', 'ward', 'hexproof', 'indestructible', 'defender', 'flash', 'prowess', 'affinity', 'convoke', 'delve', 'cascade'] def tags(c): t = txt(c).lower() out = set() for tag, pats in TAG_RULES: if any(re.search(p, t) for p in pats): out.add(tag) # keyword abilities from card data (more reliable) for kw in c.get('keywords') or []: k = kw.lower() if k in KEYWORDS: out.add('kw_' + k.replace(' ', '_')) tl = (c.get('type_line') or '').lower() if 'equipment' in tl: out.add('equipment') if 'aura' in tl: out.add('aura') if 'spacecraft' in tl: out.add('spacecraft') if 'vehicle' in tl: out.add('vehicle') if 'land' in tl: out.add('land') if 'creature' in tl: out.add('creature') if 'artifact' in tl: out.add('artifact') if 'enchantment' in tl: out.add('enchantment') if 'planeswalker' in tl: out.add('planeswalker') if 'saga' in tl: out.add('saga') if 'instant' in tl: out.add('instant') if 'sorcery' in tl: out.add('sorcery') return out PRIMARY_ORDER = [ 'land', 'planeswalker', 'wipe', 'counterspell', 'removal', 'reanimate', 'extra_turn', 'theft', 'copy', 'tutor', 'ramp', 'draw', 'discard', 'sacrifice', 'token', 'anthem', 'equipment', 'aura', 'creature', 'artifact', 'enchantment', ] def primary_role(c): tg = c['tags'] tl = (c.get('type_line') or '').lower() if 'land' in tl: return 'land' if 'planeswalker' in tl: return 'planeswalker' if 'spacecraft' in tl and 'creature' not in tl: return 'spacecraft' if 'vehicle' in tl and 'creature' not in tl: return 'vehicle' if 'creature' in tl: # creatures are classified by their body + strongest utility if 'wipe' in tg: return 'creature_wipe' if 'removal' in tg and not ('ramp' in tg or 'draw' in tg): return 'creature_removal' if 'counterspell' in tg: return 'creature_counter' if 'reanimate' in tg: return 'creature_reanimate' if 'token' in tg and 'anthem' in tg: return 'creature_anthem' if 'ramp' in tg: return 'creature_ramp' if 'draw' in tg: return 'creature_draw' return 'creature' if 'wipe' in tg: return 'wipe' if 'counterspell' in tg: return 'counterspell' if 'removal' in tg: return 'removal' if 'reanimate' in tg: return 'reanimate' if 'extra_turn' in tg: return 'extra_turn' if 'theft' in tg: return 'theft' if 'copy' in tg: return 'copy' if 'tutor' in tg and 'ramp' not in tg: return 'tutor' if 'ramp' in tg: return 'ramp' if 'draw' in tg: return 'draw' if 'discard' in tg: return 'discard' if 'token' in tg: return 'go_wide' if 'anthem' in tg: return 'anthem' if 'equipment' in tl: return 'equipment' if 'aura' in tl: return 'aura' if 'artifact' in tl: return 'artifact' if 'enchantment' in tl: return 'enchantment' return 'other' ROLE_ZH = { 'spacecraft': '太空船', 'vehicle': '载具', 'land': '地', 'planeswalker': '鹏洛客', 'wipe': '扫场', 'counterspell': '反击', 'removal': '去除', 'reanimate': '掘坟', 'extra_turn': '额外回合', 'theft': '夺取', 'copy': '复制', 'tutor': '导师', 'ramp': '加速', 'draw': '抓牌', 'discard': '弃牌', 'go_wide': '铺场', 'anthem': '群体强化', 'equipment': '武具', 'aura': '灵气', 'artifact': '神器', 'enchantment': '结界', 'creature': '生物', 'creature_wipe': '生物·扫场', 'creature_removal': '生物·去除', 'creature_counter': '生物·反击', 'creature_reanimate': '生物·掘坟', 'creature_anthem': '生物·群体强化', 'creature_ramp': '生物·加速', 'creature_draw': '生物·抓牌', 'other': '其他', } # ---------------------------------------------------------------- scoring def pt(c): try: return float(c.get('power')), float(c.get('toughness')) except (TypeError, ValueError): return None, None def creature_score(c): p, t = pt(c) cmc = c.get('cmc') or 0 if p is None: return 5.0 s = 4.0 + (p + t - 2 * max(cmc, 1)) * 0.5 # extreme stats if p + t >= 2 * max(cmc, 1) + 3: s += 0.5 kw_bonus = {'kw_flying': 0.8, 'kw_trample': 0.4, 'kw_menace': 0.4, 'kw_deathtouch': 0.6, 'kw_lifelink': 0.6, 'kw_vigilance': 0.3, 'kw_reach': 0.2, 'kw_first_strike': 0.4, 'kw_double_strike': 1.2, 'kw_haste': 0.4, 'kw_ward': 0.5, 'kw_hexproof': 0.6, 'kw_indestructible': 0.8, 'kw_defender': -0.8, 'kw_flash': 0.4} for k, v in kw_bonus.items(): if k in c['tags']: s += v # static/triggered ability value if 'removal' in c['tags']: s += 1.6 if 'draw' in c['tags']: s += 1.2 if 'token' in c['tags']: s += 0.9 if 'anthem' in c['tags']: s += 1.0 if 'ramp' in c['tags']: s += 0.9 if 'recursion' in c['tags'] or 'reanimate' in c['tags']: s += 0.8 if 'protection' in c['tags']: s += 0.5 if 'counters_matter' in c['tags']: s += 0.4 if c.get('rarity') == 'mythic': s += 0.2 return max(1.0, min(9.8, s)) def removal_score(c): t = txt(c).lower() cmc = c.get('cmc') or 0 s = 9.2 - 0.85 * max(0, cmc - 1) if 'instant' in c['tags']: s += 0.4 if 'sorcery' in c['tags']: s -= 0.3 # unconditional destroy/exile unconditional = bool(re.search(r'(destroy|exile) target creature(?!.*(?:with|unless|that|if|power|mana value|non))', t)) if not unconditional: s -= 0.8 if re.search(r'destroy target creature with power \d+ or greater', t): s += 0.3 if 'exile target' in t: s += 0.3 if 'deals' in t and 'damage' in t: dmg = num(r'deals? (?:twice )?x damage', t) if dmg is None: dmg = num(r'deals? (\d+) damage', t, 0) if dmg: s = 4.6 + dmg * 1.15 - cmc * 0.9 if 'instant' in c['tags']: s += 0.4 if 'any target' in t: s += 0.3 if re.search(r'target creature gets -(\d+)/-(\d+)', t): x = num(r'target creature gets -(\d+)/-(\d+)', t, 2) s = 4.8 + x * 1.0 - cmc * 0.9 if 'instant' in c['tags']: s += 0.3 if 'each opponent sacrifices' in t or 'each player sacrifices' in t: s = 6.8 - cmc * 0.6 if 'fights' in t: s = 5.4 + (0.5 if 'instant' in c['tags'] else 0) - 0.3 * max(0, cmc - 2) # extra value extras = 0.0 if 'draw' in c['tags']: extras += 0.5 if 'surveil' in c['tags']: extras += 0.25 if 'lifegain' in c['tags']: extras += 0.2 if 'token' in c['tags']: extras += 0.35 if 'discard' in c['tags']: extras += 0.25 s += min(extras, 1.0) if 'kick' in c['tags']: s += 0.2 return max(1.0, min(9.8, s)) def draw_score(c): t = txt(c).lower() cmc = c.get('cmc') or 0 n = num(r'draw (?:a|one) card', t, 0) if n == 0: n = num(r'draw (two|three|four|five|x|\d+) cards', t, 1, cast=lambda x: {'two': 2, 'three': 3, 'four': 4, 'five': 5}.get(x, 2 if x == 'x' else int(x))) s = 4.5 + n * 0.9 - cmc * 0.55 if 'instant' in c['tags']: s += 0.3 if 'loot' in c['tags']: s -= 0.4 if 'surveil' in c['tags']: s += 0.3 if 'token' in c['tags']: s += 0.4 if c.get('cmc', 0) == 0: s += 0.8 return max(1.0, min(9.5, s)) def ramp_score(c): t = txt(c).lower() cmc = c.get('cmc') or 0 s = 5.5 if re.search(r'put (?:it|them|those|a|one).*onto the battlefield', t): s += 0.8 if re.search(r'search your library for .*land.*put .*onto the battlefield', t): s += 0.5 if 'add one mana of any color' in t or re.search(r'\{t\}: add \{.+\}', t): s += 0.8 s -= 0.35 * max(0, cmc - 2) if c.get('cmc', 0) <= 1: s += 0.8 if 'creature' in c['tags'] and (c.get('power') or '0').isdigit() and int(c.get('power') or 0) >= 1: s += 0.4 return max(1.0, min(9.0, s)) def counterspell_score(c): t = txt(c).lower() cmc = c.get('cmc') or 0 s = 8.6 - 0.85 * max(0, cmc - 2) if 'unless' in t: s -= 0.8 if re.search(r'mana value \d+ or less', t): s -= 0.9 if 'can.t be countered' in t: s += 0.2 if 'creature' in c['tags']: s += 1.2 return max(1.0, min(9.2, s)) def wipe_score(c): t = txt(c).lower() cmc = c.get('cmc') or 0 s = 8.2 - 0.55 * max(0, cmc - 4) if 'all creatures get -' in t: s -= 0.4 if 'nonartifact' in t: s -= 0.3 if 'each creature' in t and 'damage' in t: s -= 0.2 if 'destroy all' in t or 'exile all' in t: s += 0.3 return max(1.0, min(9.5, s)) def land_score(c): t = txt(c).lower() tl = (c.get('type_line') or '').lower() if 'basic land' in tl: return 2.0 s = 4.0 if 'pay 2 life' in t and ('swamp' in tl or 'mountain' in tl or 'forest' in tl or 'island' in tl or 'plains' in tl): return 8.5 # shock land reprint if 'enters tapped' in t and 'add' in t: s = 4.6 if 'gain 1 life' in t: s += 0.4 if 'enters tapped unless' in t: s = 5.3 if 'choose one' in t: s += 0.4 if re.search(r'\{t\}: add \{[wubrg]\}', t) and 'enters tapped' not in t and 'pay 2 life' not in t: s = 5.0 if 'sacrifice this land' in t: s += 0.2 return max(1.0, min(8.5, s)) def generic_score(c): tg = c['tags'] cmc = c.get('cmc') or 0 s = 4.5 if 'draw' in tg: s += 0.8 if 'removal' in tg: s += 1.2 if 'token' in tg: s += 0.8 if 'ramp' in tg: s += 0.8 if 'anthem' in tg: s += 0.8 if 'protection' in tg: s += 0.5 if 'treasure' in tg or 'clue' in tg or 'food' in tg: s += 0.4 if 'equipment' in tg: s += 0.3 s -= 0.25 * max(0, cmc - 3) return max(1.0, min(9.0, s)) def raw_score(c): r = c['role'] if r == 'land': return land_score(c) if r == 'planeswalker': return 7.5 if r == 'wipe': return wipe_score(c) if r == 'counterspell': return counterspell_score(c) if r in ('removal', 'creature_removal'): return removal_score(c) if r in ('creature', 'creature_wipe', 'creature_counter', 'creature_anthem', 'creature_ramp', 'creature_draw', 'creature_reanimate'): return creature_score(c) if r == 'draw': return draw_score(c) if r in ('ramp', 'tutor'): return ramp_score(c) return generic_score(c) # ------------------------------------------------------------- benchmarks ROLE_COMPAT = { 'spacecraft': {'spacecraft', 'vehicle', 'artifact'}, 'vehicle': {'vehicle', 'spacecraft', 'artifact'}, 'removal': {'removal', 'creature_removal', 'wipe'}, 'creature_removal': {'creature_removal', 'removal', 'creature'}, 'wipe': {'wipe', 'removal'}, 'counterspell': {'counterspell', 'creature_counter'}, 'creature_counter': {'creature_counter', 'counterspell'}, 'draw': {'draw', 'creature_draw'}, 'creature_draw': {'creature_draw', 'draw', 'creature'}, 'ramp': {'ramp', 'creature_ramp'}, 'creature_ramp': {'creature_ramp', 'ramp', 'creature'}, 'reanimate': {'reanimate', 'creature_reanimate'}, 'creature_reanimate': {'creature_reanimate', 'reanimate', 'creature'}, 'anthem': {'anthem', 'creature_anthem'}, 'creature_anthem': {'creature_anthem', 'anthem', 'creature'}, 'go_wide': {'go_wide', 'token', 'creature'}, 'equipment': {'equipment', 'aura'}, 'aura': {'aura', 'equipment'}, 'creature': {'creature', 'creature_removal', 'creature_draw', 'creature_ramp', 'creature_anthem'}, 'land': {'land'}, 'artifact': {'artifact'}, 'enchantment': {'enchantment'}, } def prepare(c): c = dict(c) c['tags'] = tags(c) c['role'] = primary_role(c) c['raw'] = raw_score(c) return c