From 104ccdd259ae0db8d14f30dcc558a4ee567861c7 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sat, 26 May 2012 04:22:32 -0400 Subject: [PATCH] basic scoring --- fowl/game/models.py | 22 ++++++++++++++++++++-- fowl/game/tests.py | 27 ++++++++++++++------------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/fowl/game/models.py b/fowl/game/models.py index 114346e..39086fa 100644 --- a/fowl/game/models.py +++ b/fowl/game/models.py @@ -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()]) \ No newline at end of file + ret = ' & '.join([str(m) for m in self.members.all()]) + if self.victorious: + ret += ' (v)' + return ret \ No newline at end of file diff --git a/fowl/game/tests.py b/fowl/game/tests.py index 501deb7..f50b91b 100644 --- a/fowl/game/tests.py +++ b/fowl/game/tests.py @@ -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() \ No newline at end of file