-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcard.py
More file actions
138 lines (122 loc) · 4.69 KB
/
card.py
File metadata and controls
138 lines (122 loc) · 4.69 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
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
COLORMAP = {
"R": "Red",
"W": "White",
"B": "Black",
"U": "Blue",
"G": "Green"
}
class Card(object):
def __init__(self, name="", pretty_name="", cost=None, color_identity=None, card_type="", sub_types="",
abilities=None, set_id="", rarity="", collectible=True, set_number=-1, mtga_id=-1,
power=None, toughness=None):
self.name = name
self.set = set_id
self.pretty_name = pretty_name
if cost is None:
cost = []
self.cost = cost
if color_identity is None:
color_identity = []
self.color_identity = color_identity
self.card_type = card_type
self.sub_types = sub_types
self.set_number = set_number
self.mtga_id = mtga_id
self.rarity = rarity
self.collectible = collectible
if abilities is None:
abilities = []
self.abilities = abilities
self.power = power
self.toughness = toughness
@property
def abilities_decoded(self):
from ..set_data import all_mtga_abilities
return {ability_id: all_mtga_abilities[ability_id] for ability_id in self.abilities}
@property
def colors(self):
colors = []
for color_key in COLORMAP.keys():
if color_key in self.cost or color_key in self.color_identity:
colors.append(COLORMAP[color_key])
if not colors:
if self.card_type == "Basic Land":
if "Plains" in self.pretty_name:
colors = ["White"]
if "Swamp" in self.pretty_name:
colors = ["Black"]
if "Forest" in self.pretty_name:
colors = ["Green"]
if "Mountain" in self.pretty_name:
colors = ["Red"]
if "Island" in self.pretty_name:
colors = ["Blue"]
if not colors:
colors = ["Colorless"]
return colors
@property
def cmc(self):
'gets converted mana cost for a card'
cmc = 0
for symbol in self.cost:
if symbol.isdigit():
cmc += int(symbol)
elif symbol == "X":
continue
else:
cmc += 1
return cmc
def to_serializable(self):
return {
"name": self.name,
"set": self.set,
"colors": self.colors,
"pretty_name": self.pretty_name,
"cost": self.cost,
"color_identity": self.color_identity,
"card_type": self.card_type,
"sub_types": self.sub_types,
"rarity": self.rarity,
"set_number": self.set_number,
"mtga_id": self.mtga_id
}
@classmethod
def from_dict(cls, obj):
from ..set_data import all_mtga_cards
try:
return all_mtga_cards.find_one(obj["mtga_id"])
except ValueError:
new_unknown_card = cls("unknown_{}".format(obj["mtga_id"]), "{}: Unknown MTGA ID".format(obj["mtga_id"]), [], [], "unknown", "unknown", "unknown", -1, obj["mtga_id"])
all_mtga_cards.cards.append(new_unknown_card)
return new_unknown_card
def __repr__(self):
return "<Card: '{}' {} {} {}>".format(self.pretty_name, self.colors, self.set, self.mtga_id)
def __str__(self):
return self.__repr__()
class GameCard(Card):
def __init__(self, name, pretty_name, cost, color_identity, card_type, sub_types, set_id, rarity, set_number, mtga_id, owner_seat_id, game_id=-1, power=None, toughness=None):
super().__init__(name, pretty_name, cost, color_identity, card_type, sub_types, set_id, rarity, set_number, mtga_id, power, toughness)
self.game_id = game_id
self.previous_iids = []
self.owner_seat_id = owner_seat_id
def to_serializable(self):
serial = super(GameCard, self).to_serializable()
serial["iid"] = self.game_id
serial["owner_seat_id"] = self.owner_seat_id
return serial
def __repr__(self):
if self.mtga_id != -1:
return "<GameCard: {} {} iid={}>".format(self.name, self.mtga_id, self.game_id)
else:
return "<UnknownCard: iid={}>".format(self.game_id)
def transform_to(self, card_id):
from ..set_data import all_mtga_cards
new_card = all_mtga_cards.find_one(card_id)
self.name = new_card.name
self.pretty_name = new_card.pretty_name
self.cost = new_card.cost
self.card_type = new_card.card_type
self.sub_types = new_card.sub_types
self.set = new_card.set
self.set_number = new_card.set_number
self.mtga_id = new_card.mtga_id