diff --git a/fowl/game/admin.py b/fowl/game/admin.py new file mode 100644 index 0000000..18aecf5 --- /dev/null +++ b/fowl/game/admin.py @@ -0,0 +1,18 @@ +from django.contrib import admin + +from .models import Star, Match, Event, League, Team + +class StarAdmin(admin.ModelAdmin): + list_display = ('name', 'division', 'active') + list_filter = ('division', 'active') + +admin.site.register(Star, StarAdmin) +admin.site.register(Match) +admin.site.register(Event) + +class TeamAdmin(admin.ModelAdmin): + list_display = ('name', 'league') + list_filter = ('league',) + +admin.site.register(League) +admin.site.register(Team, TeamAdmin) diff --git a/fowl/game/models.py b/fowl/game/models.py index e31d83b..e63ba5d 100644 --- a/fowl/game/models.py +++ b/fowl/game/models.py @@ -1,11 +1,13 @@ from collections import defaultdict from django.db import models -from django.contrib import admin from django.contrib.auth.models import User +# these things are independent of the game + WIN_TYPES = (('pin', 'pin'), ('DQ', 'DQ'), ('submission', 'submission')) + TITLES = (('wwe', 'WWE'), ('heavyweight', 'Heavyweight'), ('ic', 'Intercontinental'), @@ -19,9 +21,19 @@ class Star(models.Model): name = models.CharField(max_length=200) photo_url = models.URLField() division = models.CharField(max_length=100) - title = models.CharField(max_length=20, choices=TITLES) + title = models.CharField(max_length=20, choices=TITLES, null=True) active = models.BooleanField() + def win_title(self, title, tag_partner=None): + Star.objects.filter(title=title).update(title=None) + self.title = title + self.save() + if tag_partner: + if title != 'tag': + raise ValueError("can't have tag partner w/ non-tag belt") + tag_partner.title = title + tag_partner.save() + def drafted(self, league): return self.teams.filter(league=league).count() >= 1 @@ -29,66 +41,6 @@ class Star(models.Model): return self.name -class StarAdmin(admin.ModelAdmin): - list_display = ('name', 'division', 'active') - list_filter = ('division', 'active') -admin.site.register(Star, StarAdmin) - - -class League(models.Model): - name = models.CharField(max_length=100) - raw_picks = models.IntegerField(default=3) - smackdown_picks = models.IntegerField(default=3) - diva_picks = models.IntegerField(default=2) - wildcard_picks = models.IntegerField(default=1) - oldtimer_picks = models.IntegerField(default=2) - - def score_event(self, event): - ppv_bonus = 1 if event.name.lower() not in ('raw', 'smackdown') else 0 - TeamPoints.objects.filter(match__event=event).delete() - for match in event.matches.all(): - for star, points in match.points().iteritems(): - try: - team = self.teams.get(stars=star) - TeamPoints.objects.create(points=points + ppv_bonus, - team=team, - star_id=star, - match=match) - except Team.DoesNotExist: - pass - - def __unicode__(self): - return self.name - -admin.site.register(League) - - -class Team(models.Model): - name = models.CharField(max_length=100) - login = models.OneToOneField(User, related_name='team') - league = models.ForeignKey(League, related_name='teams') - stars = models.ManyToManyField(Star, related_name='teams') - - def add_star(self, pk): - member = Star.objects.get(pk=pk) - if member.drafted(self.league): - raise ValueError('cannot add {0}, already drafted in {1}'.format( - member, self.league)) - self.stars.add(member) - - def drop_star(self, pk): - member = Star.objects.get(pk=pk) - self.stars.remove(member) - - def __unicode__(self): - return self.name - -class TeamAdmin(admin.ModelAdmin): - list_display = ('name', 'league') - list_filter = ('league',) - -admin.site.register(Team, TeamAdmin) - class Event(models.Model): name = models.CharField(max_length=100) date = models.DateField() @@ -124,7 +76,6 @@ class Event(models.Model): def __unicode__(self): return '{0} {1}'.format(self.name, self.date) -admin.site.register(Event) class Match(models.Model): event = models.ForeignKey(Event, related_name='matches') @@ -220,8 +171,6 @@ class Match(models.Model): ret += ' (no contest)' return ret -admin.site.register(Match) - class MatchTeam(models.Model): members = models.ManyToManyField(Star) match = models.ForeignKey(Match, related_name='teams') @@ -236,6 +185,56 @@ class MatchTeam(models.Model): ret += ' (v)' return ret + +# fantasy stuff + +class League(models.Model): + name = models.CharField(max_length=100) + raw_picks = models.IntegerField(default=3) + smackdown_picks = models.IntegerField(default=3) + diva_picks = models.IntegerField(default=2) + wildcard_picks = models.IntegerField(default=1) + oldtimer_picks = models.IntegerField(default=2) + + def score_event(self, event): + ppv_bonus = 1 if event.name.lower() not in ('raw', 'smackdown') else 0 + TeamPoints.objects.filter(match__event=event).delete() + for match in event.matches.all(): + for star, points in match.points().iteritems(): + try: + team = self.teams.get(stars=star) + TeamPoints.objects.create(points=points + ppv_bonus, + team=team, + star_id=star, + match=match) + except Team.DoesNotExist: + pass + + def __unicode__(self): + return self.name + + +class Team(models.Model): + name = models.CharField(max_length=100) + login = models.OneToOneField(User, related_name='team') + league = models.ForeignKey(League, related_name='teams') + stars = models.ManyToManyField(Star, related_name='teams') + + def add_star(self, pk): + member = Star.objects.get(pk=pk) + if member.drafted(self.league): + raise ValueError('cannot add {0}, already drafted in {1}'.format( + member, self.league)) + self.stars.add(member) + + def drop_star(self, pk): + member = Star.objects.get(pk=pk) + self.stars.remove(member) + + def __unicode__(self): + return self.name + + class TeamPoints(models.Model): points = models.IntegerField() team = models.ForeignKey(Team, related_name='points') diff --git a/fowl/game/tests.py b/fowl/game/tests.py index a6cd8b7..6786e87 100644 --- a/fowl/game/tests.py +++ b/fowl/game/tests.py @@ -4,7 +4,33 @@ from django.contrib.auth.models import User from .models import Match, Event, League, Team, TeamPoints, Star def _give_belt(star, belt): - Star.objects.filter(pk=star).update(title=belt) + Star.objects.get(pk=star).win_title(belt) + +class StarTest(TestCase): + def test_win_belt(self): + cmpunk = Star.objects.create(pk='cmpunk', name='CM Punk', title='wwe') + dbry = Star.objects.create(pk='danielbryan', name='Daniel Bryan') + kofi = Star.objects.create(pk='kofi', name='Kofi Kingston', + title='tag') + rtruth = Star.objects.create(pk='rtruth', name='R Truth', title='tag') + swagger = Star.objects.create(pk='swagger', name='Jack Swagger') + ziggler = Star.objects.create(pk='ziggler', name='Dolph Ziggler') + + # belt win takes it away from original holder + self.assertEqual(Star.objects.get(pk='cmpunk').title, 'wwe') + dbry.win_title('wwe') + self.assertEqual(Star.objects.get(pk='cmpunk').title, None) + self.assertEqual(Star.objects.get(pk='danielbryan').title, 'wwe') + + # tag belt win + self.assertEqual(Star.objects.get(pk='kofi').title, 'tag') + self.assertEqual(Star.objects.get(pk='rtruth').title, 'tag') + ziggler.win_title('tag', tag_partner=swagger) + self.assertEqual(Star.objects.get(pk='kofi').title, None) + self.assertEqual(Star.objects.get(pk='rtruth').title, None) + self.assertEqual(Star.objects.get(pk='ziggler').title, 'tag') + self.assertEqual(Star.objects.get(pk='swagger').title, 'tag') + class MatchTest(TestCase): fixtures = ['testdata'] @@ -14,14 +40,14 @@ class MatchTest(TestCase): def test_basics(self): match = self.event.add_match('tripleh', 'undertaker') - self.assertEqual(unicode(match), 'Triple H vs. Undertaker') + self.assertEqual(unicode(match), + 'Triple H vs. Undertaker (no contest)') match.record_win('undertaker', 'pin') self.assertEqual(unicode(match), 'Triple H vs. Undertaker (v)') _give_belt('cmpunk', 'wwe') - match = self.event.add_match('cmpunk', 'reymysterio') - self.assertEqual(unicode(match), 'CM Punk (c) vs. Rey Mysterio') - match.record_win('cmpunk', 'pin') + match = self.event.add_match('cmpunk', 'reymysterio', winner='cmpunk', + win_type='pin') self.assertEqual(unicode(match), 'CM Punk (c) (v) vs. Rey Mysterio') @@ -34,10 +60,10 @@ class MatchTest(TestCase): # fatal 4 way: 6 points match = self.event.add_match('randyorton', 'sheamus', 'albertodelrio', 'chrisjericho', winner='sheamus', - win_type='pin') + win_type='pin') self.assertEqual(match.points(), {'sheamus': 6, 'randyorton': 0, 'albertodelrio': 0, 'chrisjericho': 0} - ) + ) # win stacked match: 1 point for team (bonuses can apply) match = self.event.add_match('santinomarella', ['markhenry', 'kane'], @@ -75,7 +101,7 @@ class MatchTest(TestCase): match = self.event.add_match(['kofikingston', 'rtruth'], ['jackswagger', 'dolphziggler'], winner='dolphziggler', - win_type='submission') + win_type='submission') self.assertEqual(match.points(), {'jackswagger': 2, 'dolphziggler': 4, 'kofikingston': 0, @@ -85,7 +111,7 @@ class MatchTest(TestCase): match = self.event.add_match(['kofikingston', 'rtruth'], ['jackswagger', 'dolphziggler'], winner='dolphziggler', - win_type='DQ') + win_type='DQ') self.assertEqual(match.points(), {'jackswagger': 1, 'dolphziggler': 1, 'kofikingston': 0, @@ -107,7 +133,7 @@ class MatchTest(TestCase): 'kharma': 0, 'kane': 0, 'albertodelrio': 0 - }) + }) def test_champ_scoring(self): _give_belt('cmpunk', 'wwe') @@ -117,7 +143,7 @@ class MatchTest(TestCase): # champ doesn't get a bonus just for winning match = self.event.add_match('cmpunk', 'reymysterio', winner='cmpunk', - win_type='pin') + win_type='pin') self.assertEqual(match.points(), {'cmpunk': 2, 'reymysterio': 0}) # +2 bonus for beating a champ in a non-title match @@ -127,7 +153,7 @@ class MatchTest(TestCase): # defending wwe belt is worth +5 match = self.event.add_match('cmpunk', 'reymysterio', winner='cmpunk', - win_type='pin', title_at_stake=True) + win_type='pin', title_at_stake=True) self.assertEqual(match.points(), {'cmpunk': 7, 'reymysterio': 0}) # winning wwe belt @@ -138,20 +164,20 @@ class MatchTest(TestCase): # defending other belt is worth +3 match = self.event.add_match('christian', 'codyrhodes', - winner='christian', win_type='pin', - title_at_stake=True) + winner='christian', win_type='pin', + title_at_stake=True) self.assertEqual(match.points(), {'christian': 5, 'codyrhodes': 0}) # winning other belt is worth +10 match = self.event.add_match('christian', 'codyrhodes', - winner='codyrhodes', win_type='pin', - title_at_stake=True) + winner='codyrhodes', win_type='pin', + title_at_stake=True) self.assertEqual(match.points(), {'codyrhodes': 12, 'christian': 0}) # title non-defense (DQ/countout) match = self.event.add_match('christian', 'codyrhodes', - winner='codyrhodes', win_type='DQ', - title_at_stake=True) + winner='codyrhodes', win_type='DQ', + title_at_stake=True) self.assertEqual(match.points(), {'codyrhodes': 1, 'christian': 1}) # no bonus in a tag match @@ -182,14 +208,14 @@ class LeagueTest(TestCase): def setUp(self): self.user = User.objects.create_user('me', 'test@example.com', - 'password') + 'password') self.user2 = User.objects.create_user('me2', 'test@example.com', 'password') self.league = League.objects.create(name='FOWL') self.teddy = Team.objects.create(name='Team Teddy', login=self.user, - league=self.league) + league=self.league) self.johnny = Team.objects.create(name='Team Johnny', login=self.user2, - league=self.league) + league=self.league) def test_team_add_star(self): self.teddy.add_star(pk='reymysterio') @@ -224,6 +250,9 @@ class LeagueTest(TestCase): # rename the event and rescore event.name = 'Wrestlemania' event.save() + # give cody rhodes his belt back for this one + _give_belt('codyrhodes', 'ic') + self.league.score_event(event) # all should be one higher than before self.assertEqual(TeamPoints.objects.get(team=self.teddy, star__pk='reymysterio').points, 1) diff --git a/requirements.txt b/requirements.txt index e606222..7e39553 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Django lxml scrapelib +django-debug-toolbar