-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonParser.py
More file actions
74 lines (64 loc) · 2.49 KB
/
jsonParser.py
File metadata and controls
74 lines (64 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from card import *
import re
import json
class JSONParser:
def __init__(self):
self.cards = []
self.ignoredSets = ["UNH", "UST", "UNG"]
def ParseMTGJSON(self, mtgjsonFile):
jsonCards = json.load(open(mtgjsonFile, "r"))
for c in jsonCards:
cardInfo = jsonCards[c]
if any(s in self.ignoredSets for s in cardInfo["printings"]):
print("Ignoring", c, "- bad set")
continue
print("Processing", c)
card = Card()
card.name = c
if "manaCost" not in cardInfo:
card.cost = ManaCost()
else:
card.cost.FromString(cardInfo["manaCost"])
if "text" in cardInfo:
card.text = cardInfo["text"].splitlines()
card.types = cardInfo["supertypes"]
card.types += cardInfo["types"]
card.types += cardInfo["subtypes"]
if "power" in cardInfo:
card.power = cardInfo["power"]
#save on speed by not checking for toughness too
card.toughness = cardInfo["toughness"]
#card.DetermineQuality()
self.cards.append(card)
def UpdateCardQuality(self):
for c in self.cards:
c.DetermineQuality()
def TrimCards(self):
#Remove cards that had an error in quality (or aren't used at all)
self.cards = [c for c in self.cards if c.quality != 0]
#Remove reminder text and extraneous words
for c in self.cards:
text = []
for t in c.text:
line = re.sub("[\(].*?[\)]", "", t)
line = line.replace(" "," ")
line = line.replace(c.name, "THIS_CARD")
line = line.strip()
text.append(line)
c.text = text
def OutputTo(self, outFile):
json.dump(self.cards, open(outFile, "w"), indent=4, default=Card.ToJSON)
def ParseCondensedJSON(self, condensedFile):
jsonCards = json.load(open(condensedFile, "r"))
for c in jsonCards:
card = Card()
card.name = c["name"]
card.cost.FromString(c["cost"])
card.text = c["text"]
card.types = c["types"]
card.power = c["power"]
card.toughness = c["toughness"]
card.quality = c["quality"]
self.cards.append(card)
def PrintCards(self):
print(json.dumps(self.cards, indent=2, default=Card.ToJSON))