예제 #1
0
    def dispatch(self, request, *args, **kwargs):
        # get the game by the id
        self.game = Game.get_game(kwargs['game_id'])
        user = get_user(request)
        # check to see if the game is open and available for this user
        # if this player is the creator, just return
        if self.game.p1 == user or self.game.p2 == user:
            return super(GameView, self).dispatch(request, *args, **kwargs)

        # if there is no opponent and the game is not yet completed,
        # set the opponent as this user
        if not self.game.p2 and self.game.winner == 0:
            self.game.p2 = user
            Cell.create_new_board(self.game.id, self.game.num_rows,
                                  self.game.num_cols, self.game.p1,
                                  self.game.p2)
            self.game.save()
            return super(GameView, self).dispatch(request, *args, **kwargs)
        else:
            messages.add_message(request, messages.ERROR,
                                 'Sorry, the selected game is not available.')
            return redirect('/lobby/')