forked from mhmurray/cloaca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_manager.py
320 lines (262 loc) · 9.1 KB
/
card_manager.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
"""
This module reads data from a json file and returns card properties as strings,
except card value, which is returned as an int.
There is barely any error handling!
"""
import json
import logging
from os import path
from collections import Counter
_deck = ()
def _get_deck():
"""Initialize or return the list of card names. Six Jacks plus
the Orders cards in alphabetical order.
"""
global _deck
if len(_deck) == 0:
orders = []
d = get_cards_dict_from_json_file()
for name, card_dict in d.items():
name = str(name)
n_cards = int(card_dict['card_count'])
orders.extend([name]*n_cards)
orders.sort(key=lambda x:x.lower())
_deck = tuple(['Jack']*6 + orders)
return _deck
def standard_deck():
"""Returns a tuple of the standard cards (strings) in a deck, Jacks first,
alphabetical after.
"""
return _get_deck()
def get_card(name, copy=0):
"""Return the card of the specified name and relative
index. If the first Dock card is 12, say, then
get_card('Dock', 2) returns Card(ident=14).
Raise IndexError if copy is larger than the number
of those cards.
"""
c = Card(standard_deck().index(name)+copy)
if c.name != name:
raise IndexError('There are not {0!s} copies of {1} in the deck.'
.format(copy+1, name))
else:
return c
def get_cards(card_names):
"""Return a tuple of cards with the specified names, and the first
ident numbers in the list.
> get_cards(['Road']*3)
> (Card(105), Card(106), Card(107))
Raises ValueError if too many cards of one type are requested. For
instance, there are only 3 'Statue' cards, so get_cards(['Statue']*4)
will raise.
"""
d = {name:list(cards(name)) for name in set(card_names)}
out=[]
for name in card_names:
try:
c = d[name].pop(0)
except IndexError:
raise ValueError(
'There are only {0} {1} cards in the game '
'({2} requested).'
.format(len(cards(name)), name, card_names.count(name)))
out.append(c)
return tuple(out)
def get_cards_of_material(material):
"""Gets all card of the specified material.
"""
return filter(lambda c: c.material == material, get_orders_card_set())
def card_ids(name):
"""Return generator of all card ids with the specified name.
"""
return (i for i,c in enumerate(standard_deck()) if c==name)
def cards(name):
"""Return generator of all Card objects with the specified name.
They will have different ids.
"""
return (Card(i) for i in card_ids(name))
def get_orders_card_set():
"""Returns a list with the default number of each Orders cards.
The elements are Card objects.
"""
return [Card(i) for i in range(6, len(_get_deck()))]
def get_cards_dict_from_json_file():
""" Return dict of data for ALL cards from the json file. """
# json should be in the GTR directory, with this module
gtr_dir = path.dirname(__file__)
json_file = file(path.join(gtr_dir,'GTR_cards.json'), 'r')
cards_dict = json.load(json_file)
json_file.close()
return cards_dict
def get_card_dict(card_name):
""" Return dict of data for ONE card. """
cards_dict = get_cards_dict_from_json_file()
if card_name in ['Jack','Card'] :
card_dict = {"card_count": "0",
"function": None,
"material": None
}
else:
card_dict = cards_dict[card_name]
return card_dict
def get_function_of_card(card_name):
""" Return function string for the specified card. """
card_dict = get_card_dict(card_name)
function = card_dict['function']
return str(function) if function else function
def get_material_of_card(card_name):
""" Return material string for the specified card. """
card_dict = get_card_dict(card_name)
material = card_dict['material']
return str(material) if material else material
def get_count_of_card(card_name):
""" Return card count for the specified card. """
card_dict = get_card_dict(card_name)
count = card_dict['card_count']
count = int(count)
return count
def get_materials():
materials = [
'Brick',
'Concrete',
'Marble',
'Rubble',
'Stone',
'Wood'
]
return materials
def get_role_of_material(material):
if material == 'Brick':
return 'Legionary'
elif material == 'Concrete':
return 'Architect'
elif material == 'Marble':
return 'Patron'
elif material == 'Rubble':
return 'Laborer'
elif material == 'Stone':
return 'Merchant'
elif material == 'Wood':
return 'Craftsman'
elif material is None:
return None
else:
logging.error('----> Role of {0} not found!'.format(material))
def get_role_of_card(card_name):
material = get_material_of_card(card_name)
role = get_role_of_material(material)
return role
def get_value_of_material(material):
if material == 'Brick':
return 2
elif material == 'Concrete':
return 2
elif material == 'Marble':
return 3
elif material == 'Rubble':
return 1
elif material == 'Stone':
return 3
elif material == 'Wood':
return 1
elif material is None:
return None
else:
logging.error('----> Value of {0} not found!'.format(material))
def get_value_of_card(card_name):
material = get_material_of_card(card_name)
value = get_value_of_material(material)
return value
def get_all_roles():
""" Returns a list of all 6 possible roles. """
return ['Patron', 'Laborer', 'Architect',
'Craftsman', 'Legionary', 'Merchant']
def get_all_materials():
""" Returns a list of all possible materials """
foundations = [
'Brick',
'Concrete',
'Marble',
'Rubble',
'Stone',
'Wood',
]
return foundations
def cmp_jacks_first(c1, c2):
""" Comparator that alphabetizes cards, but puts Jacks before all
Orders cards.
"""
if c1 == c2:
return 0
if c1 == 'Jack':
return -1
if c2 == 'Jack':
return 1
return cmp(c1,c2)
class Card(object):
"""Each card has an identity number, different for even duplicate cards.
Comparison between cards can use '==' for comparing names, or
identical() to compare ident numbers.
Card(123, 'Insula') == Card(101, 'Insula') # True
Card(123, 'Insula') == Card(123, 'Dock') # False
Card(123, 'Insula').identical(Card(123, 'Insula')) # True
Card(123, 'Insula').identical(Card(101, 'Insula')) # False
Comparisons between cards using the operators (==, <, etc.) compare
names alphabetically except that Jacks are always first.
Members:
ident -- unique id number, different for all cards. Negative values
are for anonymouse cards named 'Card', eg. for your opponent's
hand. Card with idents too large are set to -1.
Attributes (read-only):
name -- name of foundation, or 'Jack'
material -- string like 'Brick', 'Rubble'. None for Jacks.
value -- integer 1-3 or None for Jacks.
role -- string like 'Merchant', 'Laborer'. None for Jacks.
text -- rules text of the card. Empty string for Jacks.
"""
def __init__(self, ident):
if type(ident) is not int:
raise TypeError(
'Card.ident must be an integer, received \'{0!s}\''
.format(ident))
self.ident = ident
if self.ident >= len(standard_deck()):
raise TypeError('Card.ident out of range: {0}'.format(ident))
def get_name(self):
if self.ident < 0: return 'Card'
else: return standard_deck()[self.ident]
name = property(lambda self: standard_deck()[self.ident])
material = property(lambda self: get_material_of_card(self.name))
value = property(lambda self: get_value_of_card(self.name))
role = property(lambda self: get_role_of_card(self.name))
text = property(lambda self: get_function_of_card(self.name))
def __repr__(self):
rep = ('Card({ident!r})')
return rep.format(**self.__dict__)
def __str__(self):
return self.name
def __cmp__(self, other):
"""Custom comparison between cards. Uses the ident attribute.
If compared to a non-Card object, the comparison uses
cmp(id(self), id(other)).
"""
if not isinstance(other, Card):
return cmp(id(self), id(other))
return cmp(self.ident, other.ident)
def __hash__(self):
return self.ident
def compare_jacks_first(self, other):
if not isinstance(other, Card):
return cmp(id(self), id(other))
c1, c2 = self.name.lower(), other.name.lower()
if c1 == c2:
return cmp(self.ident, other.ident)
elif c1 == 'Jack':
return -1
elif c2 == 'Jack':
return 1
else:
return cmp(self, other)
def same_name(self, other):
return self.name == other.name