import re from django.db import models from mtgweb.lib.mtg import mtg # Create your models here. class CardType(models.Model): name = models.CharField(max_length=200, unique=True, db_index=True) def __unicode__(self): return self.name class Attribute(models.Model): name = models.CharField(max_length=200, unique=True, db_index=True) def __unicode__(self): return self.name class Card(models.Model, mtg.Card): name = models.CharField(max_length=200, unique=True) type = models.ForeignKey(CardType) attributes = models.ManyToManyField(Attribute) cost = models.CharField(max_length=80) converted_cost = models.IntegerField(default=0) power = models.CharField(max_length=10) toughness = models.CharField(max_length=10) rarity = models.CharField(max_length=1) text = models.TextField() def __unicode__(self): return self.name class Deck(models.Model): name = models.CharField(max_length=80) cards = models.ManyToManyField(Card, through='Included') def __unicode__(self): return self.name def lands(self): symbols = {} for symbol in mtg.Mana.types.keys(): symbols[symbol] = 0 for included in self.included_set.all(): card = included.card types = card.type.name.split(' ') attributes = [a.name for a in card.attributes.all()] if 'land' not in types: continue if 'forest' in attributes: symbols['G'] += included.count if 'mountain' in attributes: symbols['R'] += included.count if 'island' in attributes: symbols['U'] += included.count if 'plains' in attributes: symbols['W'] += included.count if 'swamp' in attributes: symbols['B'] += included.count # Get symbols from abilities pattern = '{%s}' % mtg.ManaCost.symbolPattern costs = [mtg.ManaCost(cost) for cost in re.findall(pattern, str(card.text))] for cost in costs: for color, count in cost.mana.mana.iteritems(): symbols[color] += count * included.count return symbols def symbols(self): symbols = {} for symbol in mtg.Mana.types.keys(): symbols[symbol] = 0 for included in self.included_set.all(): card = included.card # Get symbols from card cost cost = mtg.ManaCost(card.cost) for color, count in cost.mana.mana.iteritems(): symbols[color] += count * included.count # Get symbols from abilities pattern = '{%s}' % mtg.ManaCost.symbolPattern costs = [mtg.ManaCost(cost) for cost in re.findall(pattern, str(card.text))] for cost in costs: for color, count in cost.mana.mana.iteritems(): symbols[color] += count * included.count return symbols def mana_curve(self): curve = {} for included in self.included_set.all(): card = included.card if 'land' in card.type.name.split(' '): continue cost = mtg.ManaCost(card.cost).converted() if cost not in curve: curve[cost] = included.count else: curve[cost] += included.count return curve class Included(models.Model): card = models.ForeignKey(Card) deck = models.ForeignKey(Deck) count = models.IntegerField(default=0) def __unicode__(self): return '{0}x {1}'.format(self.count, self.card)