From 859106ad1a7b1c4287ad7013d4178f15cfd078e7 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 30 Jun 2010 07:52:01 -0400 Subject: [PATCH] Support for card rarities --- analyze.py | 9 +++++++++ database.py | 13 +++++++++---- mtg.py | 10 +++++++++- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/analyze.py b/analyze.py index bed21ba..4ca590e 100644 --- a/analyze.py +++ b/analyze.py @@ -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') diff --git a/database.py b/database.py index 7248ae4..8681197 100644 --- a/database.py +++ b/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 \ No newline at end of file + print c diff --git a/mtg.py b/mtg.py index bda6ce6..43c4568 100644 --- a/mtg.py +++ b/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 = []