mtg-web/analyzer/models.py
2010-09-21 21:37:00 -04:00

47 lines
1.6 KiB
Python
Executable file

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 colors(self):
mana = {}
symbols = 0
for card in self.cards.all():
cost = mtg.ManaCost(card.cost)
for color, count in cost.mana.mana.iteritems():
if not color in mana.keys():
mana[color] = 0
mana[color] += count
symbols += count
return mana
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)