class PacketProcess(): def __init__(self,mmC,rloc): self.accessLock = rloc self.tid = str(random.getrandbits(128)) #System vars self.userID = "1" self.authenticated = False self.disconnect = False self.randomHash = str(random.getrandbits(64)) #MM vars self.searchingClash = False self.searchingClashFound = False #Game vars self.questionsRequested = False #db connection self.dbM = ServiceDBMinor() #MM self.mmControl = mmC debugElo = 1000 #Player ELo self.mmClient = MMGameClient(debugElo,self.tid) def processPacket(self,state,command,arg): # # General commands # if(state == States.system): return self.genSystemResponse(command,arg) elif(state == States.general): return self.genGeneralResponse(command,arg) elif(state == States.game and self.authenticated): return self.genGameResponse(command,arg) elif(state == States.match and self.authenticated): return self.genMatchResponse(command,arg) elif(state == States.levelAccess): return self.genLevelsResponse(command,arg) def genLevelsResponse(self,command,arg): #Request to start category game if(command == Commands.instantiate): #arg is cat name to load, Math #this is not working as only one category self.mmClient.startSingleRound(arg) return self.prepareResponse(States.levelAccess,Commands.instantiate,"Go") elif(command == Commands.ping): #Control when to terminate if(self.mmClient.gameScore >= 10 or not self.mmClient.alive): if(int(self.detailsLevelsMath) == 0): self.detailsLevelsMath = 1 self.dbM.updateUserSkill(self.userID,str(self.detailsLevelsMath),str(self.detailsLevelsLogic),str(self.detailsLevelsDifferences)) return self.prepareResponse(States.levelAccess,Commands.ping,"Terminate") else: return self.prepareResponse(States.levelAccess,Commands.ping,"Go") elif(command == Commands.question): if(self.questionsRequested == False): self.questionsRequested = True #Supply links if(arg == "A"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("A")) elif(arg == "B"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("B")) elif(arg == "C"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("C")) elif(arg == "D"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("D")) elif(arg == "E"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("E")) elif(arg == "F"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("F")) elif(arg == "G"): self.mmClient.logTimeLastLinkPosted() return self.prepareResponse(States.match,Commands.questionMain,self.mmClient.getLink("G")) #Update round end elif(command == Commands.time): #Client side time submitted return self.prepareResponse(States.match,Commands.time,"Time Received") #submit selection elif(command == Commands.selection): #Answer selection on the client side self.mmClient.endRound(arg) self.questionsRequested = False return self.prepareResponse(States.match,Commands.score,str(self.mmClient.getScore())) def genSystemResponse(self,command,arg): if(command == Commands.ping and not self.authenticated): return self.prepareResponse(States.system,Commands.pong,"none") elif(command == Commands.ping and arg == self.randomHash and self.authenticated): #self.randomHash = str(random.getrandbits(64)) return self.prepareResponse(States.system,Commands.pong,"randomHash") elif(command == Commands.disconnect): self.mmControl.unregisterMM(self.mmClient,self.accessLock) #Log a loss if game was running #if self.mmClient.ready: #Log a loss for this player self.mmClient.clientDC() self.disconnect = True return self.prepareResponse(States.system,Commands.disconnect,"none") elif(command == Commands.pong): (uPlay,uOnline) = self.mmControl.getUsersOnline(self.accessLock) return self.prepareResponse(States.system,Commands.ping,str(uPlay) + "]"+str(uOnline)) def genGeneralResponse(self,command,arg): if(command == Commands.loginID): #Validate loginID self.username = arg return self.prepareResponse(States.general,Commands.loginID,"none") elif(command == Commands.tpass): #Validate if user is legit, else register self.userID = self.dbM.getUser(self.username,arg) if self.userID != -1: self.mmClient.setUserID(self.userID); self.randomHash = str(random.getrandbits(64)) self.authenticated = True #Retrieve user details (self.detailsElo,self.detailsKnockouts,self.detailsGames,self.detailsWins,self.detailsGamesRemaining,self.detailsSupporterIs,self.detailsSupporterIcon,self.detailsSpecialIcon,self.detailsLevelsMath,self.detailsLevelsLogic,self.detailsLevelsDifferences) = self.dbM.getUserDetails(self.userID) return self.prepareResponse(States.general,Commands.tpass,self.randomHash) else: return self.prepareResponse(States.general,Commands.tpass,"rejected") elif(command == Commands.elo): return self.prepareResponse(States.general,Commands.elo,self.detailsElo) elif(command == Commands.knockouts): return self.prepareResponse(States.general,Commands.knockouts,self.detailsKnockouts) elif(command == Commands.games): return self.prepareResponse(States.general,Commands.games,self.detailsGames) elif(command == Commands.wins): return self.prepareResponse(States.general,Commands.wins,self.detailsWins) elif(command == Commands.gamesRemaining): return self.prepareResponse(States.general,Commands.gamesRemaining,self.detailsGamesRemaining) elif(command == Commands.supporterIs): return self.prepareResponse(States.general,Commands.supporterIs,self.detailsSupporterIs) elif(command == Commands.supporterIcon): return self.prepareResponse(States.general,Commands.supporterIcon,self.detailsSupporterIcon) elif(command == Commands.specialIcon): return self.prepareResponse(States.general,Commands.specialIcon,self.detailsSpecialIcon) elif(command == Commands.levelsMath): return self.prepareResponse(States.general,Commands.levelsMath,self.detailsLevelsMath) elif(command == Commands.levelsLogic): return self.prepareResponse(States.general,Commands.levelsLogic,self.detailsLevelsLogic) elif(command == Commands.levelsDifference): return self.prepareResponse(States.general,Commands.levelsDifference,self.detailsLevelsDifferences) def genGameResponse(self,command,arg): if(self.mmClient.mmEnemy is not None and self.searchingClashFound == False): self.searchingClashFound = True if(command == Commands.loginID and arg == "Reg"): self.searchingClash = True self.searchingClashFound = False #Connect to MM server self.mmControl.registerMM(self.mmClient,self.accessLock) return self.prepareResponse(States.game,Commands.loginID,"Searching") elif(command == Commands.loginID and arg == "Update" and self.searchingClash and self.searchingClashFound == False): #Verify how it is going return self.prepareResponse(States.game,Commands.loginID,"Updated") elif(command == Commands.loginID and arg == "Update" and self.searchingClash and self.searchingClashFound): #Match found, instantiate game mode self.searchingClash = False self.searchingClashFound = False return self.prepareResponse(States.game,Commands.loginID,"Matched") elif(command == Commands.loginID and arg == "Unreg" and self.searchingClash): self.mmControl.unregisterMM(self.mmClient,self.accessLock) self.searchingClash = False self.searchingClashFound = False return self.prepareResponse(States.game,Commands.loginID,"Left") def genMatchResponse(self,command,arg): if(command == Commands.question): if(self.questionsRequested == False): self.questionsRequested = True #Generate questions self.mmClient.startRound() #Supply links if(arg == "A"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("A")) elif(arg == "B"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("B")) elif(arg == "C"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("C")) elif(arg == "D"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("D")) elif(arg == "E"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("E")) elif(arg == "F"): return self.prepareResponse(States.match,Commands.question,self.mmClient.getLink("F")) elif(arg == "G"): self.mmClient.logTimeLastLinkPosted() return self.prepareResponse(States.match,Commands.questionMain,self.mmClient.getLink("G")) #Update round state elif(command == Commands.ping): #In game loop update if(arg == "Enemy"): if self.mmClient.checkIfEnemyLive(): return self.prepareResponse(States.match,Commands.ping,"Enemy1") else: return self.prepareResponse(States.match,Commands.ping,"Enemy0") #Waiting for enemy elif(arg == "Ping"): resp = self.mmClient.update() if res == "Terminate" and self.mmClient.mmEnemy is not None: if self.detailsSupporterIs >= 1: self.mmClient.finalOutcomeUpdate(self.detailsWins,self.detailsGames,self.detailsKnockouts,1000) else: self.mmClient.finalOutcomeUpdate(self.detailsWins,self.detailsGames,self.detailsKnockouts,self.detailsGamesRemaining) if not self.mmClient.alive: self.mmControl.purgeClient(self.mmClient,self.accessLock) #Remove from all stacks self.mmClient = MMGameClient(self.detailsElo,self.tid) return self.prepareResponse(States.match,Commands.ping,resp) #Update round end elif(command == Commands.time): #Client side time submitted return self.prepareResponse(States.match,Commands.time,"Time Received") #submit selection elif(command == Commands.selection): #Answer selection on the client side self.mmClient.endRound(arg) self.questionsRequested = False return self.prepareResponse(States.match,Commands.score,str(self.mmClient.getScore())) #Update score again and again to be in sync elif(command == Commands.score): return self.prepareResponse(States.match,Commands.score,str(self.mmClient.getScore())) #Check if ready for new round elif(command == Commands.ready): self.mmClient.markAsReady() #Start round if self.mmClient.checkIfBothReadyForRound(): return self.prepareResponse(States.match,Commands.ready,"Ready") #Not yet else: return self.prepareResponse(States.match,Commands.ready,"None") def prepareResponse(self,state,command,value): return str(str(state.value)+":"+str(command.value)+":"+str(value)) def failPacket(self): return self.prepareResponse(Commands.failed,Commands.failed,"none")
le = 10031 #dbObj.updateServerOnline(le,serverID) #dbObj.unregisterServerOnline(serverID) #dbMinor dbM = ServiceDBMinor() uid = "001" upass = "******" #Create user uidS = dbM.getUser(uid,upass) print uidS #details ? (elo,knockouts,games,wins,gamesRemaining,supporterIs,supporterIcon,specialIcon,levelsMath,levelsLogic,levelsDifferences) = dbM.getUserDetails(uidS) print elo print knockouts print games print wins print gamesRemaining print supporterIs print supporterIcon print specialIcon print levelsMath print levelsLogic print levelsDifferences