Ejemplo n.º 1
0
class UploadPKResultHandler(tornado.web.RequestHandler):
    """RequestHandler for login"""
    def post(self):
        #=======need further modification for json dict structure========
        #=======it is easy to test with current writing

        #{
        #"request" : "/helloword/upload_result.json",
        #"sessionID" : "",
        #"gameID"  : "",
        #"userAnswer":[
        #  {
        #    "ans" : "a",
        #    "time"   : "5",
        #    },
        #  {
        #    //
        #  }
        #]
        #}
        sessionID = ''
        gameID = ''
        userAnswer = []
        try:
            paramStr = self.get_argument("params")

            params = json.loads(paramStr)
            if params['request'] == "/helloword/upload_pk_result.json":
                sessionID = params['sessionID']
                userAnswer = params['userAnswer']
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("10001", "params error")
            return

        #=======================================
        try:
            user = mysqlhelper.UserInfo()
            result = user.getUserInfoBySessionID(sessionID)
            if result == 0:
                ## 存储答案
                ans = [x['ans'] for x in userAnswer]
                pvpGameHander = mysqlhelper.PvpGameInfo(user._user)

                userResult = pvpGameHander.varifyUserAns(
                    user._user.userID, ans)
                if userResult != -1:
                    self.printSuccess(userResult)
                else:
                    self.printError("20501", "upload result error")
                return
            else:
                raise Exception
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("20101", "login failed")
Ejemplo n.º 2
0
class UploadResultHandler(tornado.web.RequestHandler):
    """RequestHandler for login"""
    def post(self):
        #=======need further modification for json dict structure========
        #=======it is easy to test with current writing

        #{
        #"request" : "/helloword/upload_result.json",
        #"sessionID" : "",
        #"gameID"  : "",
        #"userAnswer":[
        #  {
        #    "chosen" : "a",
        #    "time"   : "5",
        #    },
        #  {
        #    //
        #  }
        #]
        #}
        sessionID = ''
        gameID = ''
        userAnswer = []
        try:
            paramStr = self.get_argument("params")

            params = json.loads(paramStr)
            if params['request'] == "/helloword/upload_result.json":
                sessionID = params['sessionID']

                gameType = params['gameType']

                userAnswer = params['userAnswer']
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("10001", "params error")
            return

        #=======================================
        try:
            user = mysqlhelper.UserInfo()

            result = user.getUserIDBySession(sessionID)
            if result == 0:
                ## 存储答案
                self.printSuccess()
                return
            else:
                raise Exception
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("20101", "login failed")
Ejemplo n.º 3
0
class LoginHandler(tornado.web.RequestHandler):
    """RequestHandler for login"""
    def post(self):
        #=======need further modification for json dict structure========
        #=======it is easy to test with current writing

        #===============================
        #   {
        #        "request" : "/user/login.json",
        #        "loginInfo" : {
        #            "userName" : "userName",
        #            "password" : "password"
        #        }
        #    }
        userName = ''
        password = ''
        try:
            paramStr = self.get_argument("params")

            params = json.loads(paramStr)
            if params['request'] == "/user/login.json":
                userName = params['loginInfo']['userName']
                password = params['loginInfo']['password']
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("10001", "params error")
            return

        #=======================================
        try:
            user = mysqlhelper.UserInfo()

            result = user.getUserInfoByName(userName)
            if result == 0:
                if not user.varifyPassword(password):
                    raise Exception
                sessionID = str(uuid4())
                user.deleteSessionByUserID(user._user.userID)
                user.createSession(sessionID, user._user.userID)
                self.printSuccess(user._user.userID, user._user.userName,
                                  user._user.userNickname,
                                  user._user.userEmail,
                                  user._user.userAvatarType,
                                  user._user.userAvatar, sessionID)
                return
            else:
                raise Exception
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("20101", "login failed")
Ejemplo n.º 4
0
    def get_user_by_sessions(self):
        try:
            user = mysqlhelper.UserInfo()
            result = user.getUserIDBySession(self.sessionID)

            if result == 0:
                self.user = user
                return 0
                pass
            else:
                raise Exception
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("20301", "session error")
            return -1
Ejemplo n.º 5
0
class UserLogoutHandler(tornado.web.RequestHandler):
    """RequestHandler for login"""
    def post(self):
        #=======need further modification for json dict structure========
        #=======it is easy to test with current writing

        #{
        #"request" : "/helloword/user_logout.json",
        #"sessionID" : "",
        #"gameID"  : "",
        #"logout"  : "true"
        #}
        sessionID = ''
        gameID = ''
        logout = False
        try:
            paramStr = self.get_argument("params")

            params = json.loads(paramStr)
            if params['request'] == "/helloword/user_logout.json":
                sessionID = params['sessionID']
                gameID = params['gameID']
                logout = params['logout']
                if logout != True:
                    raise Exception
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("10001", "params error")
            return

        #=======================================
        try:
            user = mysqlhelper.UserInfo()

            result = user.getUserIDBySession(sessionID)
            if result == 0:
                ## 用户退出
                self.printSuccess()
                return
            else:
                raise Exception
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("20101", "login failed")
Ejemplo n.º 6
0
class RequestRankHandler(tornado.web.RequestHandler):
    """RequestHandler for login"""
    def post(self):
        #=======need further modification for json dict structure========
        #=======it is easy to test with current writing

        #{
        #"request" : "/helloword/request_rank.json",
        #"sessionID" : ""
        #}
        sessionID = ''
        try:
            paramStr = self.get_argument("params")

            params = json.loads(paramStr)
            if params['request'] == "/helloword/request_rank.json":
                sessionID = params['sessionID']
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("10001", "params error")
            return

        #=======================================
        try:
            user = mysqlhelper.UserInfo()

            result = user.getUserInfoBySessionID(sessionID)
            if result == 0:
                ## 获取积分
                pvpGameHander = mysqlhelper.PvpGameInfo(user._user)

                rankInfo = pvpGameHander.getUserRank()

                toprankInfo = pvpGameHander.getTopRank(5)

                self.printSuccess(rankInfo, toprankInfo)
                return
            else:
                raise Exception
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("20101", "login failed")
Ejemplo n.º 7
0
class RequestPKGameHandler(tornado.web.RequestHandler):
    """RequestHandler for login"""
    def post(self):
        #=======need further modification for json dict structure========
        #=======it is easy to test with current writing

        #{
        #"request" : "/helloword/request_game.json",
        #"sessionID"  : "",
        #"gameType" : ""
        #}
        sessionID = ''
        gameType = ''
        try:
            paramStr = self.get_argument("params")

            params = json.loads(paramStr)
            if params['request'] == "/helloword/request_pk_game.json":
                sessionID = params['sessionID']

                gameType = params['gameType']

        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("10001", "params error")
            return

        #=======================================
        try:
            user = mysqlhelper.UserInfo()
            result = user.getUserInfoBySessionID(sessionID)
            if result == 0:
                ## 产生题目
                pvpGameHander = mysqlhelper.PvpGameInfo(user._user)

                ans = pvpGameHander.getGame(gameType, 10)
                if ans != -1:

                    examList = []

                    for x in xrange(0, 10):
                        moreAns = self.getRandomAns(ans[x])
                        examNode = {
                            "description": moreAns['pro_description'],
                            "ans1": moreAns['pro_ans_a'],
                            "ans2": moreAns['pro_ans_b'],
                            "ans3": moreAns['pro_ans_c'],
                            "ans4": moreAns['pro_ans_d'],
                            "point": moreAns['pro_point'],  #分值
                            "ans": moreAns['pro_ans'],
                            "time": moreAns['pro_time'],
                            "enemyTime": "3",
                            "enemyAns": "a"
                        }
                        examList.append(examNode)

                    self.printSuccess(examList)

                return
            else:
                raise Exception
        except Exception, e:
            logging.warning(traceback.format_exc())
            self.printError("20101", "login failed")
Ejemplo n.º 8
0
            logging.warning(traceback.format_exc())
            self.printError('10001', 'params error!')
            return
        try:
            userEmail = params['userInfo']['userEmail']
        except Exception, e:
            userEmail = userName
            
        try:
            userAvatarType = params['userInfo']['userAvatarType']
            userAvatar     = params['userInfo']['userAvatar']
        except Exception, e:
            pass            

        try:
            user = mysqlhelper.UserInfo()
            user.setUserInfo(userName, userEmail, userNickname)
            user.setUserPassword(password)

            if userAvatarType != '':
                user.setUserAvatarInfo(userAvatarType,userAvatar)
            userID = user.saveUserInfo()
            if userID > 0:
                sessionID = str(uuid4())
                user.createSession(sessionID,userID)
                self.printSuccess(userID, user._user.userName, user._user.userNickname, user._user.userEmail,user._user.userAvatarType, user._user.userAvatar, sessionID)
                return
            else:
                raise Exception
        except Exception, e:
            self.printError('20101', 'register failed!')