Support for card rarities
This commit is contained in:
parent
c6ba11ae51
commit
859106ad1a
3 changed files with 27 additions and 5 deletions
|
@ -52,6 +52,14 @@ def colors(deck):
|
|||
for color, value in card.cost.mana.mana.iteritems():
|
||||
data[Mana.types[color]] += 1 if value > 0 else 0
|
||||
return data
|
||||
def rarities(deck):
|
||||
data = {}
|
||||
for rarity in Card.rarities.values():
|
||||
data[rarity] = 0
|
||||
for card in deck:
|
||||
if not card.rarity: continue
|
||||
data[Card.rarities[card.rarity]] += 1
|
||||
return data
|
||||
|
||||
db = database.TextDB('db.txt')
|
||||
|
||||
|
@ -79,3 +87,4 @@ for filename in sys.argv[1:]:
|
|||
graph(mana_curve(deck), '\nMana Curve', hideEmpty=False)
|
||||
graph(types(deck), '\nCard Types')
|
||||
graph(colors(deck), '\nColors')
|
||||
graph(rarities(deck), '\nRarities')
|
||||
|
|
13
database.py
13
database.py
|
@ -29,11 +29,12 @@ class TextDB(Database):
|
|||
'attributes': [],
|
||||
'cost': '',
|
||||
'power': 0,
|
||||
'toughness': 0
|
||||
'toughness': 0,
|
||||
'rarity': None,
|
||||
}
|
||||
elif card and line:
|
||||
# Load the data into the card
|
||||
if not card['cost'] and re.match('^X?\d*[WBURG]*$', line):
|
||||
if not card['cost'] and re.match('^X?\d*[{0}]*$'.format(' '.join(Mana.types.keys())), line):
|
||||
card['cost'] = line
|
||||
continue
|
||||
if not card['type']:
|
||||
|
@ -43,6 +44,10 @@ class TextDB(Database):
|
|||
card['attributes'] = attributes
|
||||
if not card['power'] and not card['toughness'] and re.match('^\d+([+-]?\*)?\/\d+([+-]?\*)?$', line):
|
||||
(card['power'], card['toughness']) = line.split('/')
|
||||
if not card['rarity'] and re.match('^([A-Z0-9]+-[{0}](, )?)+$'.format(' '.join(Card.rarities.keys())), line):
|
||||
sets = line.split(', ')
|
||||
currentSet = sets.pop()
|
||||
card['rarity'] = currentSet.split('-').pop()
|
||||
elif not line:
|
||||
# Finished with current record
|
||||
if card:
|
||||
|
@ -51,9 +56,9 @@ class TextDB(Database):
|
|||
else:
|
||||
# Prepare to read in the next record
|
||||
inRecord = False
|
||||
return Card(card['name'], card['type'], card['attributes'], card['cost'], card['power'], card['toughness'])
|
||||
return Card(card['name'], card['type'], card['attributes'], card['cost'], card['power'], card['toughness'], card['rarity'])
|
||||
|
||||
if __name__ == '__main__':
|
||||
db = TextDB('db.txt')
|
||||
c = db.getCard('Aura Gnarlid')
|
||||
print c
|
||||
print c
|
||||
|
|
10
mtg.py
10
mtg.py
|
@ -163,13 +163,21 @@ class Player:
|
|||
return True
|
||||
|
||||
class Card:
|
||||
def __init__(self, name, type, attributes, cost=0, power=0, toughness=0, owner=None):
|
||||
rarities = {
|
||||
'C': 'Common',
|
||||
'U': 'Uncommon',
|
||||
'R': 'Rare',
|
||||
'M': 'Mythic Rare',
|
||||
}
|
||||
|
||||
def __init__(self, name, type, attributes, cost=0, power=0, toughness=0, rarity=None, owner=None):
|
||||
self.name = name
|
||||
self.type = type.lower()
|
||||
self.attributes = [a.lower() for a in attributes]
|
||||
self.cost = ManaCost(cost)
|
||||
self.power = power
|
||||
self.toughness = toughness
|
||||
self.rarity = rarity
|
||||
self.is_tapped = False
|
||||
self.abilities = []
|
||||
|
||||
|
|
Loading…
Reference in a new issue