add_match

This commit is contained in:
James Turk 2012-05-26 17:19:53 -04:00
parent f2c168b0c9
commit 3829147bfb
3 changed files with 38 additions and 39 deletions

View File

@ -3,11 +3,23 @@ from django.db import models
from django.contrib import admin from django.contrib import admin
from django.contrib.auth.models import User from django.contrib.auth.models import User
WIN_TYPES = (('pin', 'pin'),
('DQ', 'DQ'),
('submission', 'submission'))
TITLES = (('wwe', 'WWE'),
('heavyweight', 'Heavyweight'),
('ic', 'Intercontinental'),
('us', 'United States'),
('tag', 'Tag Team'),
('diva', 'Divas'),
)
class Star(models.Model): class Star(models.Model):
id = models.CharField(max_length=100, primary_key=True) id = models.CharField(max_length=100, primary_key=True)
name = models.CharField(max_length=200) name = models.CharField(max_length=200)
photo_url = models.URLField() photo_url = models.URLField()
division = models.CharField(max_length=100) division = models.CharField(max_length=100)
title = models.CharField(max_length=20, choices=TITLES)
active = models.BooleanField() active = models.BooleanField()
def drafted(self, league): def drafted(self, league):
@ -77,32 +89,26 @@ class Event(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
date = models.DateField() date = models.DateField()
def points(self): def add_match(self, *teams, **kwargs):
total = defaultdict(int) winner = kwargs.get('winner', None)
for match in self.matches.all(): win_type = kwargs.get('win_type', None)
for star, points in match.points().iteritems(): title_at_stake = kwargs.get('title_at_stake', False)
total[star] += points
# PPV bonus match = Match.objects.create(event=self, title_at_stake=title_at_stake)
if self.name.lower() not in ('smackdown', 'raw'): for team in teams:
for star in total: if isinstance(team, (list, tuple)):
total[star] += 1 match.add_team(*team)
return total else:
match.add_team(team)
if winner:
match.record_win(winner, win_type)
return match
def __unicode__(self): def __unicode__(self):
return '{0} {1}'.format(self.name, self.date) return '{0} {1}'.format(self.name, self.date)
admin.site.register(Event) admin.site.register(Event)
WIN_TYPES = (('pin', 'pin'),
('DQ', 'DQ'),
('submission', 'submission'))
TITLES = (('wwe', 'WWE'),
('heavyweight', 'Heavyweight'),
('ic', 'Intercontinental'),
('us', 'United States'),
('tag', 'Tag Team'),
('diva', 'Divas'),
)
class Match(models.Model): class Match(models.Model):
event = models.ForeignKey(Event, related_name='matches') event = models.ForeignKey(Event, related_name='matches')
winner = models.ForeignKey(Star, null=True) winner = models.ForeignKey(Star, null=True)
@ -114,12 +120,15 @@ class Match(models.Model):
mt = MatchTeam.objects.create(match=self, title=title) mt = MatchTeam.objects.create(match=self, title=title)
for member in members: for member in members:
member = Star.objects.get(pk=member) member = Star.objects.get(pk=member)
if not mt.title and member.title:
mt.title = member.title
mt.save()
mt.members.add(member) mt.members.add(member)
def record_win(self, star, win_type): def record_win(self, star, win_type):
self.teams.filter(members__pk=star).update(victorious=True) self.teams.filter(members__pk=star).update(victorious=True)
self.win_type = win_type
self.winner_id = star self.winner_id = star
self.win_type = win_type
self.save() self.save()
def points(self): def points(self):

View File

@ -1,7 +1,7 @@
from django.test import TestCase from django.test import TestCase
from django.core.management import call_command from django.core.management import call_command
from django.contrib.auth.models import User from django.contrib.auth.models import User
from .models import Match, Event, League, Team, TeamPoints from .models import Match, Event, League, Team, TeamPoints, Star
class MatchTest(TestCase): class MatchTest(TestCase):
fixtures = ['testdata'] fixtures = ['testdata']
@ -225,18 +225,13 @@ class LeagueTest(TestCase):
self.johnny.add_star(pk='sin-cara') self.johnny.add_star(pk='sin-cara')
self.johnny.add_star(pk='markhenry') self.johnny.add_star(pk='markhenry')
event = Event.objects.create(name='smackdown', date='2012-01-01') event = Event.objects.create(name='smackdown', date='2012-01-01')
match1 = Match.objects.create(event=event) match1 = event.add_match('reymysterio', 'sin-cara', winner='sin-cara',
match1.add_team('reymysterio') win_type='pin')
match1.add_team('sin-cara') match2 = event.add_match('markhenry', ['santinomarella', 'mickfoley'],
match1.record_win('sin-cara', 'pin') winner='mickfoley', win_type='pin')
match2 = Match.objects.create(event=event) Star.objects.filter(pk='codyrhodes').update(title='ic')
match2.add_team('santinomarella', 'mickfoley') match3 = event.add_match('sin-cara', 'codyrhodes', title_at_stake=True,
match2.add_team('markhenry') winner='sin-cara', win_type='pin')
match2.record_win('mickfoley', 'pin')
match3 = Match.objects.create(event=event, title_at_stake=True)
match3.add_team('codyrhodes', title='ic')
match3.add_team('sin-cara')
match3.record_win('sin-cara', 'pin')
self.league.score_event(event) self.league.score_event(event)
# check TeamPoints objects # check TeamPoints objects
@ -245,8 +240,6 @@ class LeagueTest(TestCase):
self.assertEqual(TeamPoints.objects.get(team=self.johnny, match=match1, star__pk='sin-cara').points, 2) self.assertEqual(TeamPoints.objects.get(team=self.johnny, match=match1, star__pk='sin-cara').points, 2)
self.assertEqual(TeamPoints.objects.get(team=self.johnny, match=match3, star__pk='sin-cara').points, 12) self.assertEqual(TeamPoints.objects.get(team=self.johnny, match=match3, star__pk='sin-cara').points, 12)
self.assertEqual(TeamPoints.objects.get(team=self.johnny, star__pk='markhenry').points, 0) self.assertEqual(TeamPoints.objects.get(team=self.johnny, star__pk='markhenry').points, 0)
for obj in TeamPoints.objects.all():
print obj
# rename the event and rescore # rename the event and rescore
event.name = 'Wrestlemania' event.name = 'Wrestlemania'

View File

@ -16,17 +16,14 @@ cobra = Team.objects.create(name='COBRA!', login=erin, league=league)
punks = ('cmpunk', 'kane', 'rtruth', 'codyrhodes', 'sin-cara', 'antoniocesaro', punks = ('cmpunk', 'kane', 'rtruth', 'codyrhodes', 'sin-cara', 'antoniocesaro',
'wadebarrett', 'aj', 'bethphoenix') 'wadebarrett', 'aj', 'bethphoenix')
for person in punks: for person in punks:
print person
gm_punk.add_star(pk=person) gm_punk.add_star(pk=person)
awesomes = ('chrisjericho', 'sheamus', 'danielbryan', 'ryback', 'damien-sandow', awesomes = ('chrisjericho', 'sheamus', 'danielbryan', 'ryback', 'damien-sandow',
'kharma', 'kellykelly', 'brodusclay', 'johncena') 'kharma', 'kellykelly', 'brodusclay', 'johncena')
for person in awesomes: for person in awesomes:
print person
awesome.add_star(pk=person) awesome.add_star(pk=person)
cobras = ('santinomarella', 'dolphziggler', 'kofikingston', 'albertodelrio', cobras = ('santinomarella', 'dolphziggler', 'kofikingston', 'albertodelrio',
'randyorton', 'bigshow', 'christian', 'layla', 'natalya') 'randyorton', 'bigshow', 'christian', 'layla', 'natalya')
for person in cobras: for person in cobras:
print person
cobra.add_star(pk=person) cobra.add_star(pk=person)