コード例 #1
0
ファイル: views.py プロジェクト: noQ/TriviaOnline
    def post(self, request):
        token = TokenMiddleware.get_token(request)
        if token is None:
            raise NotAuthenticated("Token invalid or expired!")
        ''' check if token has user attribute '''
        if not token.has_key("user"):
            raise NotAuthenticated()
        user = token.get("user")
        uid  = user.uid
        
        """ get data from request """
        try:
            data = JSONParser().parse(request)
            data = data.get(PARAMETER_DATA)
        except JSONDecodeError:
            raise ParseError(detail="No data found on the request")
        
        session_name = WebRequestParameter.get_data(PARAMETER_SESSION, data)
        if session_name is None:
            raise ParseError(detail="Game session is not set on the request")
        
        ''' get session '''
        game_session = GameSession()
        ''' check if session exists '''
        if not game_session.find_session(session_name):
            return Error("Unable to join. Invalid session",
                         HttpStatus.SESSION_INVALID.value
                         ).show()
        ''' get plugin data '''
        session = game_session.get_session()
        if session is None:
            raise APIException("No session found with ID %s" % session_name)
        ''' load plugin data '''
        if session.game_name is None:
            raise APIException("Game is not set on session with ID %s" % session_name)
        
        ''' load plugin dictionary '''
        from TriviaOnline.settings import PLUGINS
        ''' get plugin by name '''
        plugin = PluginHandler.get_from_dict(PLUGINS, str(session.game_name))
        ''' check if session has game logic set '''
        game_logic = game_session.has_game_logic()
        if game_logic is None:
            ''' get game logic from plugin '''
            game_logic = plugin.game_logic()
            if game_logic:
                game_session.set_game_logic(game_logic)

        ''' get server info '''
        server_info = plugin.get_server_info()
        ''' check session on RT server '''
        try:
            action = Action.check_session(session_name)
            client = ServerActionHandler(
                                         (settings.GAME_SERVER_ADDRES, 
                                          settings.GAME_SERVER_PORT
                                          )
                                         )
            client.set_data(action)
            client.send()
            ''' retrieve data from server '''
            data = str(client.get_reveived_data())
            if data == ServerCommand.NO_DATA or data == ServerCommand.FAILED:
                raise APIException("Session not found on RT server!")
        except:
            traceback.print_exc()
            raise APIException("Unable to retrieve data from RT server!")
        
        response = {
                    'server' : server_info,
                    'game_logic' : game_logic
                    }
        ''' construct response '''
        return Response(response)