mtgencode/jdecode.py

66 lines
2.1 KiB
Python
Raw Normal View History

2015-06-28 01:35:11 +00:00
import json
# to allow filtering of sets like un sets, etc...
def legal_set(set):
return not (set['type'] == 'un' or set['name'] == 'Celebration')
2015-06-28 01:35:11 +00:00
def mtg_open_json(fname, verbose = False):
f = open(fname, 'r')
jobj = json.load(f)
f.close()
allcards = {}
asides = {}
bsides = {}
for k_set in jobj:
set = jobj[k_set]
setname = set['name']
if legal_set(set):
for card in set['cards']:
card['setName'] = setname
cardnumber = None
if 'number' in card:
cardnumber = card['number']
# the lower avoids duplication of at least one card (Will-o/O'-the-Wisp)
cardname = card['name'].lower()
2015-06-28 01:35:11 +00:00
uid = set['code']
if cardnumber == None:
uid = uid + '_' + cardname + '_'
else:
uid = uid + '_' + cardnumber
# aggregate by name to avoid duplicates, not counting bsides
if not uid[-1] == 'b':
if cardname in allcards:
allcards[cardname] += [card]
else:
allcards[cardname] = [card]
# also aggregate aside cards by uid so we can add bsides later
if uid[-1:] == 'a':
asides[uid] = card
if uid[-1:] == 'b':
bsides[uid] = card
for uid in bsides:
aside_uid = uid[:-1] + 'a'
if aside_uid in asides:
# the second check handles the brothers yamazaki edge case
if not asides[aside_uid]['name'] == bsides[uid]['name']:
asides[aside_uid]['bside'] = bsides[uid]
else:
pass
# this exposes some coldsnap theme deck bsides that aren't
# really bsides; shouldn't matter too much
#print aside_uid
#print bsides[uid]
if verbose:
print 'Opened ' + str(len(allcards)) + ' uniquely named cards.'
return allcards