Track the TRK preview package and the newest value artifacts for FRA20261007, move the 17lands API notes under data, and ignore downloaded 17lands, card image and live directories.
119 lines
4.6 KiB
Python
119 lines
4.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Build enriched TRK card records: Chinese text, roles, scores, benchmarks."""
|
|
import json, os, re, sys, pickle, gzip
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
import roles
|
|
from build_pool import search
|
|
|
|
BASE = os.path.dirname(os.path.abspath(__file__))
|
|
V1 = r'C:/Code/Mtga/.tmp/trk_view1.json'
|
|
TRK = r'C:/Code/Mtga/.tmp/trk_api.json'
|
|
ZH = r'C:/Code/Mtga/data/mtgch/cardnames-zh-CN.txt'
|
|
|
|
def strip_html(h):
|
|
h = h or ''
|
|
h = re.sub(r'<br\s*/?>', '\n', h, flags=re.I)
|
|
h = re.sub(r'</p>\s*<p[^>]*>', '\n', h, flags=re.I)
|
|
h = re.sub(r'</?div[^>]*>', '\n', h, flags=re.I)
|
|
h = re.sub(r'<[^>]+>', '', h)
|
|
lines = [re.sub(r'[ \t]+', ' ', x).strip() for x in h.split('\n')]
|
|
return '\n'.join([x for x in lines if x])
|
|
|
|
def load_zh():
|
|
d = {}
|
|
with open(ZH, encoding='utf-8') as f:
|
|
for line in f:
|
|
p = line.rstrip('\r\n').split('|')
|
|
if len(p) >= 4:
|
|
d[p[0]] = {'name_zh': p[1], 'type_zh': p[2], 'text_zh': p[3]}
|
|
return d
|
|
|
|
COLOR_ORDER = ['W', 'U', 'B', 'R', 'G', 'M', 'C']
|
|
COLOR_ZH = {'W': '白色', 'U': '蓝色', 'B': '黑色', 'R': '红色', 'G': '绿色', 'M': '多色', 'C': '无色/地'}
|
|
|
|
def color_key(c):
|
|
cs = c.get('colors') or []
|
|
if not cs:
|
|
return 'C'
|
|
if len(cs) > 1:
|
|
return 'M'
|
|
return cs[0]
|
|
|
|
def main():
|
|
idx = pickle.load(open(os.path.join(BASE, 'pool.pkl'), 'rb'))
|
|
pool = idx['pool']
|
|
v1 = {c['name']: c for c in json.load(open(V1, encoding='utf-8'))['items']}
|
|
trk = json.load(open(TRK, encoding='utf-8'))
|
|
zh = load_zh()
|
|
by_name = {}
|
|
for c in pool:
|
|
by_name.setdefault(c['name'].lower(), []).append(c)
|
|
# authoritative reprint flag for TRK printings (mtgch's own flag is incomplete)
|
|
oracle_reprint = {}
|
|
with gzip.open(os.path.join(BASE, '..', '..', '.tmp', 'oracle.jsonl.gz'), 'rt', encoding='utf-8') as f:
|
|
for line in f:
|
|
oc = json.loads(line)
|
|
if oc.get('set') == 'trk':
|
|
oracle_reprint[oc['name'].lower()] = bool(oc.get('reprint'))
|
|
|
|
out = []
|
|
for c in trk:
|
|
c = roles.prepare(c)
|
|
v = v1[c['name']]
|
|
c['zh_name'] = v.get('name_atomic') or c['name']
|
|
c['zh_type'] = v.get('type_line_atomic') or ''
|
|
c['zh_text'] = strip_html(v.get('oracle_text_atomic_html'))
|
|
c['zh_flavor'] = strip_html(v.get('flavor_text_atomic_html'))
|
|
c['color_key'] = color_key(c)
|
|
c['tags'] = sorted(c['tags'])
|
|
c['name_zh_data'] = zh.get(c['name'])
|
|
c['reprint'] = None
|
|
if oracle_reprint.get(c['name'].lower(), c.get('reprint', False)):
|
|
c['reprint'] = {'set': 'old', 'rarity': c['rarity'], 'released_at': ''}
|
|
else:
|
|
for pc in by_name.get(c['name'].lower(), []):
|
|
if pc['set'] != 'trk':
|
|
c['reprint'] = {'set': pc['set'], 'rarity': pc['rarity'],
|
|
'released_at': pc['released_at']}
|
|
break
|
|
allowed = roles.ROLE_COMPAT.get(c['role'], {c['role']})
|
|
picked = []
|
|
seen = set()
|
|
for s, m in search(idx, c, topk=120):
|
|
key = m['name'].lower()
|
|
if key == c['name'].lower() or key in seen:
|
|
continue
|
|
mr = roles.primary_role(roles.prepare(m))
|
|
if mr not in allowed:
|
|
continue
|
|
if m['type_cat'] == 'other' and c['role'] != 'other':
|
|
continue
|
|
seen.add(key)
|
|
m2 = roles.prepare(m)
|
|
m2['tags'] = sorted(m2['tags'])
|
|
m2['sim'] = round(s, 2)
|
|
m2['name_zh'] = zh.get(m['name'], {}).get('name_zh') or m['name']
|
|
m2['rarity_zh'] = roles.RARITY_ZH.get(m['rarity'], m['rarity'])
|
|
m2['text_short'] = roles.txt(m)[:200]
|
|
picked.append(m2)
|
|
if len(picked) >= 4:
|
|
break
|
|
c['benchmarks'] = picked
|
|
out.append(c)
|
|
|
|
out.sort(key=lambda c: (COLOR_ORDER.index(c['color_key']), c['cmc'], c['name']))
|
|
for i, c in enumerate(out, 1):
|
|
c['idx'] = i
|
|
with open(os.path.join(BASE, 'data', 'cards.json'), 'w', encoding='utf-8') as f:
|
|
json.dump(out, f, ensure_ascii=False, indent=1)
|
|
print('cards', len(out))
|
|
with open(os.path.join(BASE, 'data', 'bench.txt'), 'w', encoding='utf-8') as f:
|
|
for c in out:
|
|
f.write(f"{c['idx']:03d} {c['zh_name']} | {c['mana_cost'] or '-'} | {c['rarity']} | {c['role']}\n")
|
|
for b in c['benchmarks']:
|
|
f.write(f" {b['sim']:.2f} [{b['rarity_zh']}] {b['name_zh']} | {b['mana_cost'] or '-'} | {b['type_line']} | {b['text_short'][:100]}\n")
|
|
print('done')
|
|
|
|
if __name__ == '__main__':
|
|
main()
|