Deck color charts

This commit is contained in:
Correl Roush 2010-09-22 14:22:21 -04:00
parent 59d4351565
commit 79ab9c0f0d
6 changed files with 122 additions and 14 deletions

View file

@ -29,26 +29,64 @@ class Deck(models.Model):
cards = models.ManyToManyField(Card, through='Included') cards = models.ManyToManyField(Card, through='Included')
def __unicode__(self): def __unicode__(self):
return self.name return self.name
def colors(self): def lands(self):
symbols = {} symbols = {}
for symbol in mtg.Mana.types.keys(): for symbol in mtg.Mana.types.keys():
symbols[symbol] = 0 symbols[symbol] = 0
total = 0 for included in self.included_set.all():
for card in self.cards.all(): card = included.card
# Get symbols from card cost types = card.type.name.split(' ')
cost = mtg.ManaCost(card.cost) attributes = [a.name for a in card.attributes.all()]
for color, count in cost.mana.mana.iteritems(): if 'land' not in types:
symbols[color] += count continue
total += count 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 # Get symbols from abilities
pattern = '{%s}' % mtg.ManaCost.symbolPattern pattern = '{%s}' % mtg.ManaCost.symbolPattern
costs = [mtg.ManaCost(cost) for cost in re.findall(pattern, str(card.text))] costs = [mtg.ManaCost(cost) for cost in re.findall(pattern, str(card.text))]
for cost in costs: for cost in costs:
for color, count in cost.mana.mana.iteritems(): for color, count in cost.mana.mana.iteritems():
symbols[color] += count symbols[color] += count * included.count
total += count return symbols
return (symbols, total) 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): class Included(models.Model):
card = models.ForeignKey(Card) card = models.ForeignKey(Card)
deck = models.ForeignKey(Deck) deck = models.ForeignKey(Deck)

View file

@ -25,3 +25,11 @@ def getsum(value):
return sum(value) return sum(value)
else: else:
return 0 return 0
@register.filter
def getmax(value):
if type(value) == dict:
return max(value.values())
elif type(value) == list:
return max(value)
else:
return 0

View file

@ -1,6 +1,8 @@
from django.http import Http404 from django.http import Http404
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from mtgweb.analyzer.models import Deck from mtgweb.analyzer.models import Deck
from mtgweb.lib.mtg import mtg
from pygooglechart import PieChart3D
def index(request): def index(request):
decks = Deck.objects.all() decks = Deck.objects.all()
@ -10,4 +12,36 @@ def detail(request, id):
deck = Deck.objects.get(pk=id) deck = Deck.objects.get(pk=id)
except Deck.DoesNotExist: except Deck.DoesNotExist:
raise Http404 raise Http404
return render_to_response('decks/detail.html', {'deck': deck})
# Generate charts
colors = {
'R': 'FF0000',
'G': '00FF00',
'U': '0000FF',
'W': 'FFFFFF',
'B': '000000',
}
land_chart = PieChart3D(250, 100)
lands = dict([(k, v) for k, v in deck.lands().items() if v])
land_chart.fill_solid(PieChart3D.BACKGROUND, 'FFFFFF00')
land_chart.add_data(lands.values())
land_chart.set_pie_labels([mtg.Mana.types[m] for m in lands.keys()])
land_chart.set_colours([colors[m] for m in lands.keys()])
symbol_chart = PieChart3D(250, 100)
symbols = dict([(k, v) for k, v in deck.symbols().items() if v])
symbol_chart.fill_solid(PieChart3D.BACKGROUND, 'FFFFFF00')
symbol_chart.add_data(symbols.values())
symbol_chart.set_pie_labels([mtg.Mana.types[m] for m in symbols.keys()])
symbol_chart.set_colours([colors[m] for m in symbols.keys()])
curve = deck.mana_curve()
for i in range(max(curve.keys()) + 1):
if i not in curve:
curve[i] = 0
return render_to_response('decks/detail.html', {
'deck': deck,
'land_chart': land_chart,
'symbol_chart': symbol_chart,
'curve': curve})

View file

@ -1,6 +1,7 @@
/* Page styles */ /* Page styles */
body { body {
font-family: serif; font-family: serif;
background-color: #eee;
} }
h1, h2, h3, h4, h5, h6 { h1, h2, h3, h4, h5, h6 {
text-shadow: 2px 2px 2px #999; text-shadow: 2px 2px 2px #999;
@ -114,3 +115,13 @@ header {
display: block; display: block;
float: left; float: left;
} }
.mana-curve {
width: 50%;
border: 1px solid #999;
}
.mana-curve .cost {
width: 2em;
}
.mana-curve .bar {
background-color: #999;
}

View file

@ -1,9 +1,26 @@
{% extends "main.html" %} {% extends "main.html" %}
{% load get_range %}
{% load math %}
{% block title %}Deck: {{ deck.name }}{% endblock %} {% block title %}Deck: {{ deck.name }}{% endblock %}
{% block content %} {% block content %}
<h2>{{deck}}</h2> <h2>{{deck}}</h2>
<h3>Mana</h3>
<h4>Lands</h4>
<img src="{{ land_chart.get_url }}" />
<h4>Symbols</h4>
<img src="{{ symbol_chart.get_url }}" />
<h4>Curve</h4>
<table class="mana-curve">
{% for cost, count in curve.items %}
<tr>
<td class="cost">{{ cost }}</td>
<td><div class="bar" style="text-align: right; width: {% widthratio count curve.values|getsum 100 %}%">{{ count }}</div></td>
</tr>
{% endfor %}
</table>
<h3>Cards</h3>
<ul> <ul>
{% for card in deck.included_set.all %} {% for card in deck.included_set.all %}
<li>{{card.count}}x <a href="{% url mtgweb.cards.views.display name=card.card.name %}">{{ card.card }}</a></li> <li>{{card.count}}x <a href="{% url mtgweb.cards.views.display name=card.card.name %}">{{ card.card }}</a></li>

View file

@ -7,9 +7,9 @@
{% for deck in decks %} {% for deck in decks %}
<li> <li>
<div class="manabar"> <div class="manabar">
{% for color, count in deck.colors.0.items %} {% for color, count in deck.symbols.items %}
{% if count > 0 %} {% if count > 0 %}
<span class="mana {{ color }}" style="width: {{ count|mult:100|div:deck.colors.1 }}%"></span> <span class="mana {{ color }}" style="width: {% widthratio count deck.symbols.values|getsum 100 %}%"></span>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</div> </div>