Esempio n. 1
0
    def submit(self, request):
        try:
            game = Game.getByID(self.cleaned_data['game_id'])
            if game.current_round_id:
                raise GameAlreadyStarted()
        except CassaNotFoundException:
            raise GameNotFound()
        
        if not GamePermissions.has_object_permission(request, game):
            raise PermissionDenied()
        
        # select randomly from the game members
        game_members = GameMember.filterByGame(game.game_id)
        selector = random.choice(game_members)
        
        now = datetime.datetime.now()
        
        round = Round(selector_id=selector.user_id, phrase_card_id=PhraseCard.getRandom(self.cleaned_data['deck_id']).phrase_card_id,
            game_id=game.game_id, date_created=now, last_modified=now)
        
        round.save()
        
        game.deck = self.cleaned_data['deck_id']
        game.current_round_id = round.round_id
        game.save()

        return RoundSerializer(round).data
Esempio n. 2
0
 def submit(self, request):
     try:
         game = Game.getByID(self.cleaned_data['game_id'])
     except CassaNotFoundException:
         raise GameNotFound()
     
     if not GamePermissions.has_object_permission(request, game):
         raise PermissionDenied()
     
     if not game.current_round_id:
         raise NoCurrentRound()
     try:
         round = Round.getByID(game.current_round_id)
     except CassaNotFoundException:
         raise RoundNotFound()
     
     if request.user.pk != str(round.selector_id):
         raise NotTheSelector()
     
     try:
         nomination = Nomination.getByID(self.cleaned_data['selection_id'])
     except CassaNotFoundException:
         raise InvalidNominationCard()
     
     round.selection_id = self.cleaned_data['selection_id']
     round.save()
     
     game_members = GameMember.filterByGame(game.game_id)
     # finds the game state that should be updated
     member = None
     for m in game_members:
         if str(m.user_id) == str(nomination.nominator_id):
             member = m
             break
     
     member.score += 10
     member.save()
     
     # creates a new round, saves it and the game
     now = datetime.datetime.now()
     selector = random.choice(game_members)
     
     new_round = Round(selector_id=selector.user_id, phrase_card_id=PhraseCard.getRandom(game.deck).phrase_card_id,
         game_id=game.game_id, date_created=now, last_modified=now)
     
     new_round.save()
     
     game.current_round_id = new_round.round_id
     game.save()
     
     return RoundSerializer(new_round).data