basic scoring

This commit is contained in:
James Turk 2012-05-26 04:22:32 -04:00
parent 2dd1b0c0c7
commit 104ccdd259
2 changed files with 34 additions and 15 deletions

View File

@ -71,10 +71,25 @@ class Match(models.Model):
mt.members.add(member)
def record_win(self, star, win_type):
self.teams.find(members__pk=star).update(victorious=True)
self.teams.filter(members__pk=star).update(victorious=True)
self.win_type = win_type
self.save()
def points(self):
winners = self.teams.filter(victorious=True)
points = {}
if winners:
winners = winners[0]
allies = winners.members.count() - 1
opponents = self.teams.filter(victorious=False).aggregate(
opponents=models.Count('members'))['opponents']
for w in winners.members.all():
points[w.id] = max((opponents - allies)*2, 1)
return points
def __unicode__(self):
return ' vs. '.join(str(t) for t in self.teams.all())
@ -86,4 +101,7 @@ class MatchTeam(models.Model):
victorious = models.BooleanField(default=False)
def __unicode__(self):
return ' & '.join([str(m) for m in self.members.all()])
ret = ' & '.join([str(m) for m in self.members.all()])
if self.victorious:
ret += ' (v)'
return ret

View File

@ -1,16 +1,17 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
from django.core.management import call_command
from .models import Match, Event
class MatchTest(TestCase):
def setUp(self):
call_command('loadstars')
self.wm29 = Event.objects.create(name='Wrestlemania 29', date='2012-04-01')
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
def test_basics(self):
match = Match.objects.create(event=self.wm29)
match.add_team('tripleh')
match.add_team('undertaker')
self.assertEqual(unicode(match), 'Triple H vs. Undertaker')
match.record_win('undertaker', 'pin')
self.assertEqual(unicode(match), 'Triple H vs. Undertaker (v)')
match.points()