mtg-web/decks/views.py

62 lines
2.5 KiB
Python
Executable file

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, SimpleLineChart, Axis
def index(request):
decks = Deck.objects.all()
return render_to_response('decks/index.html', {'decks': decks})
def detail(request, id):
try:
deck = Deck.objects.get(pk=id)
except Deck.DoesNotExist:
raise Http404
# 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()])
ramp_data = deck.ramp()
min_range = min([t['probability'] for t in ramp_data]) // 5 * 5
ramp_chart = SimpleLineChart(250, 100, y_range=[min_range, 100])
ramp_chart.fill_solid(SimpleLineChart.BACKGROUND, 'FFFFFF00')
ramp_chart.add_data([t['probability'] for t in ramp_data])
labels_left = range(min_range, 100, (100 - min_range) // 5)
labels_left[0] = ''
ramp_chart.set_axis_labels(Axis.LEFT, labels_left)
ramp_chart.set_axis_labels(Axis.BOTTOM, ['Turn {0}'.format(t['turn']) for t in ramp_data])
for i in range(len(ramp_data)):
p = ramp_data[i]['probability']
ramp_chart.add_marker(0, i, 'd', '000000', 5)
#ramp_chart.add_marker(0, i, 't{0}%'.format(round(p)), '000000', 10)
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,
'ramp_chart': ramp_chart,
'curve': curve})