-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagiccard.py
289 lines (243 loc) · 8.63 KB
/
magiccard.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# magiccard.py
import re
import tools
CARD_ATTRS = ['name', 'artist', 'multiverseid', 'number', 'rarity',
'type_oracle', 'rules_printed', 'type_printed', 'rules_oracle',
'manacost', 'power', 'toughness', 'flavor_text', 'loyalty']
class DataConverter:
def convert(self, name, node, value):
method_name = "do_" + name
f = getattr(self, method_name, None)
if f:
return f(node, value)
else:
return value
def do_number(self, node, value):
try:
return int(value)
except:
return tools.null
do_loyalty = do_number # only for Planeswalkers
def do_power(self, node, value):
try:
return int(value)
except:
return tools.Abstract(value) # probably '*'
do_toughness = do_power
def do_manacost(self, node, value):
parts = []
for n in node.findall('symbol'):
parts.append(n.text)
return parts
class MagicSet(object):
@classmethod
def from_xml(cls, xml):
s = cls()
for name in ['name', 'shortname', 'release_date']:
setattr(s, name, xml.find(name).text)
s.tags = []
tag_node = xml.find('tags')
if tag_node:
for child in tag_node.getchildren():
s.tags.append(child.tag)
return s
def has_tag(self, tag):
return tag.lower() in self.tags
class MagicCard(object):
def __init__(self):
self._data = {}
self._dc = DataConverter()
@classmethod
def from_xml(cls, xml):
c = cls()
for name in CARD_ATTRS:
node = xml.find(name)
if node is not None:
value = xml.find(name)
value = c._dc.convert(name, node, value.text)
c._data[name] = value
else:
c._data[name] = tools.null
c._post_process()
return c
def _post_process(self):
# add some attributes that make certain queries easier.
before, sep, after = self._data['type_oracle'].partition("--")
self._data['_types'] = before.lower().strip().split()
self._data['_subtypes'] = after.lower().strip().split()
# do the same thing for the printed rules/types, so we can find e.g.
# interrupts or type 'Falcon'
typep = self._data['type_printed']
if ' - ' in typep:
before, sep, after = typep.partition(" - ")
else:
before, sep, after = typep.partition("--")
self._data['_printed_types'] = before.lower().strip().split()
self._data['_printed_subtypes'] = after.lower().strip().split()
def __getitem__(self, name):
try:
return self._data[name]
except KeyError:
try:
return getattr(self, name) # for properties
except AttributeError:
# we need to raise a KeyError here so eval() tries to look the
# name up in the global namespace next
raise KeyError, name
def type(self, name):
return name.lower() in self._data['_types']
def subtype(self, name):
return name.lower() in self._data['_subtypes']
def anytype(self, name):
return self.type(name) or self.subtype(name)
# we support old-school types as well
def printed_type(self, name):
return name.lower() in self._data['_printed_types']
def printed_subtype(self, name):
return name.lower() in self._data['_printed_subtypes']
def has_color(self, color):
for x in self._data['manacost']:
if tools.contains_any(x, color): return True
return False
@property
def colorless(self):
if self._data['name'] in force_color['colorless']:
return True
for x in self._data['manacost']:
if tools.contains_any(x, "RBWGU"):
return False
return True
@property
def multicolor(self):
colors_found = 0
for color in "RBWGU":
if self.has_color(color): colors_found += 1
return colors_found > 1
def name_like(self, s):
return s.lower() in self['name'].lower()
def flavor_like(self, s):
return s.lower() in self['flavor_text'].lower()
def text_like(self, s):
return s.lower() in self['rules_oracle'].lower()
def printed_text_like(self, s):
return s.lower() in self._data['rules_printed'].lower()
rules_like = text_like # alias
printed_rules_like = printed_text_like # alias
def name_match(self, regex, case_sensitive=1):
return re.search(regex, self['name'], 0 if case_sensitive else re.I)
def flavor_match(self, regex, case_sensitive=1):
return re.search(regex, self['flavor_text'],
0 if case_sensitive else re.I)
def text_match(self, regex, case_sensitive=1):
return re.search(regex, self['rules_oracle'],
0 if case_sensitive else re.I)
def printed_text_match(self, regex, case_sensitive=1):
return re.search(regex, self._data['rules_printed'],
0 if case_sensitive else re.I)
rules_match = text_match # alias
printed_rules_match = printed_text_match # alias
def has(self, keyword, arg=None):
# E.g. has('protection')
# has('protection', 'white')
for kw in self['keywords']:
parts = kw.split(':')
if parts[0] == keyword:
if arg is None:
return True
else:
if parts[1:] and parts[1].lower() == arg.lower():
return True
return False
def grants(self, keyword):
for kw in self['keywords']:
if kw.startswith('>'):
parts = kw[1:].split(':')
if parts[0] == keyword: return True
return False
# TODO: maybe_has or something, for ?keywords
def protection(self, x):
for kw in self['keywords']:
parts = kw.split(":")
if parts[0] == "protection" and x == parts[1]:
return True
return False
@property
def has_hybrid_mana(self):
for x in self._data['manacost']:
if len(x) == 2 and (x[0] in "WUGBR" or x[1] in "WUGBR") \
and x[0] != "P":
return True
return False
@property
def has_phyrexian_mana(self):
for x in self._data['manacost']:
if len(x) == 2 and x[0] == 'P' and x[1] in "WUGBR":
return True
return False
@property
def power_varies(self):
return isinstance(self['power'], tools.Abstract)
@property
def toughness_varies(self):
return isinstance(self['toughness'], tools.Abstract)
@property
def cmc(self):
cmc = 0
for x in self._data['manacost']:
if x in "RUWGBS":
cmc += 1
elif x in "XYZ":
pass # counts as zero
elif len(x) == 2 and x[0] == "2" and x[1] in "WURGB":
cmc += 2 # {2R} etc counts as 2
elif len(x) == 2 and x[0] in "WURGB" and x[1] in "WURGB":
cmc += 1 # hybrid mana
elif len(x) == 2 and x[0] == "P" and x[1] in "WURGB":
cmc += 1 # phyrexian mana
else:
try:
cmc += int(x)
except:
pass
return cmc
#
# some cards have a color but no mana cost
force_color = {
'B': ['Slaughter Pact',
'Living End'],
'G': ["Summoner's Pact",
"Dryad Arbor",
"Hypergenesis"],
'R': ['Pact of the Titan',
'Crimson Kobolds',
'Crookshank Kobolds',
'Kobolds of Kher Keep'],
'U': ['Pact of Negation',
'Evermind',
'Ancestral Vision'],
'W': ['Intervention Pact',
'Restore Balance'],
'colorless': ["Ghostfire"],
}
#
# set color properties dynamically
COLORS = [('red', 'R'), ('white', 'W'), ('blue', 'U'), ('green', 'G'),
('black', 'B')]
def add_color_property(color, symbol):
def f(self):
return self.has_color(symbol) or self['name'] in force_color[symbol]
f.__name__ = color
setattr(MagicCard, color, property(f))
for color, symbol in COLORS:
add_color_property(color, symbol)
#
# ditto for selected types
TYPE_PROPERTIES = ["creature", "artifact", "instant", "land", "enchantment",
"planeswalker", "sorcery"]
def add_type_property(prop):
def f(self):
return self.type(prop)
f.__name__ = prop
setattr(MagicCard, prop, property(f))
for prop in TYPE_PROPERTIES:
add_type_property(prop)