From 79ab9c0f0d605f788074d31189bd4370e96476c9 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 22 Sep 2010 14:22:21 -0400 Subject: [PATCH] Deck color charts --- analyzer/models.py | 60 ++++++++++++++++++++++++++++------- analyzer/templatetags/math.py | 8 +++++ decks/views.py | 36 ++++++++++++++++++++- media/mtg.css | 11 +++++++ templates/decks/detail.html | 17 ++++++++++ templates/decks/index.html | 4 +-- 6 files changed, 122 insertions(+), 14 deletions(-) diff --git a/analyzer/models.py b/analyzer/models.py index 91dda5c..26c1fd6 100755 --- a/analyzer/models.py +++ b/analyzer/models.py @@ -29,26 +29,64 @@ class Deck(models.Model): cards = models.ManyToManyField(Card, through='Included') def __unicode__(self): return self.name - def colors(self): + def lands(self): symbols = {} for symbol in mtg.Mana.types.keys(): symbols[symbol] = 0 - total = 0 - for card in self.cards.all(): - # Get symbols from card cost - cost = mtg.ManaCost(card.cost) - for color, count in cost.mana.mana.iteritems(): - symbols[color] += count - total += count + 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 - total += count - return (symbols, total) + 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) diff --git a/analyzer/templatetags/math.py b/analyzer/templatetags/math.py index f08ce57..a251117 100644 --- a/analyzer/templatetags/math.py +++ b/analyzer/templatetags/math.py @@ -25,3 +25,11 @@ def getsum(value): return sum(value) else: 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 diff --git a/decks/views.py b/decks/views.py index ccb5133..fdebfd3 100755 --- a/decks/views.py +++ b/decks/views.py @@ -1,6 +1,8 @@ from django.http import Http404 from django.shortcuts import render_to_response from mtgweb.analyzer.models import Deck +from mtgweb.lib.mtg import mtg +from pygooglechart import PieChart3D def index(request): decks = Deck.objects.all() @@ -10,4 +12,36 @@ def detail(request, id): deck = Deck.objects.get(pk=id) except Deck.DoesNotExist: 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}) diff --git a/media/mtg.css b/media/mtg.css index 2b6d3aa..8b6478b 100644 --- a/media/mtg.css +++ b/media/mtg.css @@ -1,6 +1,7 @@ /* Page styles */ body { font-family: serif; + background-color: #eee; } h1, h2, h3, h4, h5, h6 { text-shadow: 2px 2px 2px #999; @@ -114,3 +115,13 @@ header { display: block; float: left; } +.mana-curve { + width: 50%; + border: 1px solid #999; +} +.mana-curve .cost { + width: 2em; +} +.mana-curve .bar { + background-color: #999; +} diff --git a/templates/decks/detail.html b/templates/decks/detail.html index 91699ae..1675c11 100755 --- a/templates/decks/detail.html +++ b/templates/decks/detail.html @@ -1,9 +1,26 @@ {% extends "main.html" %} +{% load get_range %} +{% load math %} {% block title %}Deck: {{ deck.name }}{% endblock %} {% block content %}

{{deck}}

+

Mana

+

Lands

+ +

Symbols

+ +

Curve

+ + {% for cost, count in curve.items %} + + + + + {% endfor %} +
{{ cost }}
{{ count }}
+

Cards