Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

game model

By: a guest on Jul 30th, 2012  |  syntax: Python  |  size: 2.26 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class Game(models.Model):
  2.    
  3.     def __init__(self, *args, **kwargs):
  4.         super(Game, self).__init__(*args, **kwargs)
  5.         #Non-django-ORM instance variables
  6.         self.question_bank = []
  7.         self.answer_bank = []
  8.         self.object_id_list = []
  9.         self.qi = 0 # Question iterator
  10.         self.max_questions = 10
  11.         self.player_list = []
  12.         self.question_started = None
  13.  
  14.     GAME_CHOICES = (
  15.         ('solo', 'Trivia Solo'),
  16.         ('rtt', 'Real-Time Trivia'),
  17.         ('rtd', 'Real-Time Deathmatch'),
  18.         ('mtb', 'Multiplayer Turn-Based'),
  19.     )
  20.     category = models.ForeignKey('Category')
  21.     maxPlayers = models.IntegerField()
  22.     gameType = models.CharField("Game Type", max_length=255, choices=GAME_CHOICES)
  23.     started = models.DateTimeField(auto_now_add=True)
  24.     finished = models.DateTimeField(default=None, null=True)
  25.     players = models.ManyToManyField(User, through="Player")
  26.    
  27.     def __unicode__(self):
  28.         return str(self.id) + " | " + self.gameType + " | " + self.category.categoryName + " | " + str(self.started)
  29.    
  30.     def get_questions(self):
  31.         if len(self.object_id_list) != 0:
  32.             self.question_bank.append(Question.objects.filter(category__categoryName=self.category.categoryName).exclude(id__in=self.object_id_list).order_by('?')[:10])
  33.         else:
  34.             self.question_bank = Question.objects.filter(category__id=self.category.id).order_by('?')[:10]
  35.        
  36.         for q in self.question_bank:
  37.             self.object_id_list.append(q.id)
  38.    
  39.     def update_player_list(self):
  40.         player_list = []
  41.         the_players = self.players.all()
  42.         player_number = 1
  43.         for player in the_players:
  44.             single_player = player.player_set.get(game__id__exact=self.id)
  45.             #single_player.player_number = player_number
  46.             player_list.append(single_player)
  47.             player_number += 1
  48.         self.player_list = player_list
  49.        
  50.     def get_username_list(self):
  51.         username_arr = []
  52.         for player in self.player_list:
  53.             tmp_obj = {}
  54.             tmp_obj['name'] = player.base_user.username
  55.             tmp_obj['number'] = player.player_number
  56.             username_arr.append(tmp_obj)
  57.         return username_arr