From 86d1f452ba78178f2e3df8e3e9505ad6ee4ec917 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sat, 26 May 2012 14:28:35 -0400 Subject: [PATCH] some team work --- fowl/game/models.py | 28 ++++++++++++++++++++++---- fowl/game/tests.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ setup_league.py | 24 ++++++++++++++++++---- 3 files changed, 93 insertions(+), 8 deletions(-) diff --git a/fowl/game/models.py b/fowl/game/models.py index ffeda1b..23aa63d 100644 --- a/fowl/game/models.py +++ b/fowl/game/models.py @@ -10,6 +10,9 @@ class Star(models.Model): division = models.CharField(max_length=100) active = models.BooleanField() + def drafted(self, league): + return self.teams.filter(league=league).count() >= 1 + def __unicode__(self): return self.name @@ -40,6 +43,13 @@ class Team(models.Model): league = models.ForeignKey(League, related_name='teams') stars = models.ManyToManyField(Star, related_name='teams') + def add_star(self, **kwargs): + member = Star.objects.get(**kwargs) + if member.drafted(self.league): + raise ValueError('cannot add {0}, already drafted in {1}'.format( + member, self.league)) + self.stars.add(member) + def __unicode__(self): return self.name @@ -53,6 +63,17 @@ class Event(models.Model): name = models.CharField(max_length=100) date = models.DateField() + def points(self): + total = defaultdict(int) + for match in self.matches.all(): + for star, points in match.points().iteritems(): + total[star] += points + # PPV bonus + if self.name.lower() not in ('smackdown', 'raw'): + for star in total: + total[star] += 1 + return total + def __unicode__(self): return '{0} {1}'.format(self.name, self.date) @@ -84,10 +105,9 @@ class Match(models.Model): def record_win(self, star, win_type): self.teams.filter(members__pk=star).update(victorious=True) self.win_type = win_type - self.winner__pk = star + self.winner_id = star self.save() - def points(self): points = {} winners = None @@ -129,9 +149,9 @@ class Match(models.Model): # if multiple people in this match and this person was credited # w/ win, give them the bonus points - if allies and w.id == self.winner__pk: + if allies and w.id == self.winner_id: points[w.id] += 1 - if w.id == self.winner__pk and self.win_type == 'submission': + if w.id == self.winner_id and self.win_type == 'submission': points[w.id] += 1 # look over all titles in this match diff --git a/fowl/game/tests.py b/fowl/game/tests.py index ed2be00..49a37c7 100644 --- a/fowl/game/tests.py +++ b/fowl/game/tests.py @@ -192,3 +192,52 @@ class MatchTest(TestCase): match.record_win('reymysterio', 'pin') self.assertEqual(match.points(), {'codyrhodes': 4, 'reymysterio': 5, 'cmpunk': 0, 'christian': 0}) + + +class EventTest(TestCase): + def setUp(self): + call_command('loadstars') + + def test_points(self): + smackdown = Event.objects.create(name='smackdown', date='2012-01-01') + match = Match.objects.create(event=smackdown) + match.add_team('reymysterio') + match.add_team('sin-cara') + match.record_win('sin-cara', 'pin') + match = Match.objects.create(event=smackdown) + match.add_team('santinomarella', 'mickfoley') + match.add_team('markhenry') + match.record_win('mickfoley', 'pin') + match = Match.objects.create(event=smackdown, title_at_stake=True) + match.add_team('codyrhodes', title='ic') + match.add_team('sin-cara') + match.record_win('sin-cara', 'pin') + self.assertEqual(smackdown.points(), {'reymysterio': 0, + 'sin-cara': 14, + 'santinomarella': 1, + 'mickfoley': 2, + 'markhenry': 0, + 'codyrhodes': 0 + }) + + # exact same matches on a PPV, +1 to everyone + ppv = Event.objects.create(name='In Your House', date='2012-01-01') + match = Match.objects.create(event=ppv) + match.add_team('reymysterio') + match.add_team('sin-cara') + match.record_win('sin-cara', 'pin') + match = Match.objects.create(event=ppv) + match.add_team('santinomarella', 'mickfoley') + match.add_team('markhenry') + match.record_win('mickfoley', 'pin') + match = Match.objects.create(event=ppv, title_at_stake=True) + match.add_team('codyrhodes', title='ic') + match.add_team('sin-cara') + match.record_win('sin-cara', 'pin') + self.assertEqual(ppv.points(), {'reymysterio': 1, + 'sin-cara': 15, + 'santinomarella': 2, + 'mickfoley': 3, + 'markhenry': 1, + 'codyrhodes': 1 + }) diff --git a/setup_league.py b/setup_league.py index b3a4bf6..9007cef 100644 --- a/setup_league.py +++ b/setup_league.py @@ -1,6 +1,10 @@ from django.contrib.auth.models import User from fowl.game.models import League, Star, Team, Event, Match +User.objects.all().delete() +League.objects.all().delete() +Team.objects.all().delete() + james = User.objects.create_superuser('james', 'james.p.turk@gmail.com', 'james') erin = User.objects.create_user('erin', 'erin.braswell@gmail.com', 'erin') kevin = User.objects.create_user('kevin', 'kevin.wohlgenant@gmail.com', 'kevin') @@ -9,8 +13,20 @@ gm_punk = Team.objects.create(name='GM Punk', login=james, league=league) awesome = Team.objects.create(name="I'm AWEsome!", login=kevin, league=league) cobra = Team.objects.create(name='COBRA!', login=erin, league=league) +punks = ('cmpunk', 'kane', 'rtruth', 'codyrhodes', 'sin-cara', 'antoniocesaro', + 'wadebarrett', 'aj', 'bethphoenix') +for person in punks: + print person + gm_punk.add_star(pk=person) -wm = Event.objects.create(name='Wrestlemania XXX', date='2013-01-01') -m1 = Match.objects.create(event=wm) -m1.add_team('dolphziggler') -m1.add_team('randysavage') \ No newline at end of file +awesomes = ('chrisjericho', 'sheamus', 'danielbryan', 'ryback', 'damien-sandow', + 'kharma', 'kellykelly', 'brodusclay', 'johncena') +for person in awesomes: + print person + awesome.add_star(pk=person) + +cobras = ('santinomarella', 'dolphziggler', 'kofikingston', 'albertodelrio', + 'randyorton', 'bigshow', 'christian', 'layla', 'natalya') +for person in cobras: + print person + cobra.add_star(pk=person) \ No newline at end of file