def lobbyLoop(data, sock): while True: comms.send(sock, data) received = comms.receive(sock) # received = sock.recv(1024) # received = json.loads(received) # print ("Received: {}".format(received)) userInput = raw_input( "\nLobby actions:\nReady\t\t(1)\nLeave Lobby\t(2)\n~> ") if userInput == "1": data = {} data['agenda'] = "ready" comms.send(sock, data) received = comms.receive(sock) while True: received = comms.receive(sock) #<- game start if received['agenda'] == 'roundStart': myHand = received['data'] board = [] print("myHand: {}".format(myHand)) while True: rec = comms.receive(sock) if rec['agenda'] == "BSblind": pass elif rec['agenda'] == "yourTurn": userInput = raw_input( "\nGame actions:\nRaise\t\t(1)\nFold\t\t(2)\nCheck\t\t(3)\nLeave\t\t(4)\n~> " ) if userInput == "1": data = {} data['agenda'] = "raise" data['data'] = raw_input("Raise ammount: ") comms.send(sock, data) elif userInput == "2": data = {} data['agenda'] = "fold" comms.send(sock, data) elif userInput == "3": data = {} data['agenda'] = "check" comms.send(sock, data) elif userInput == "4": data = {} data['agenda'] = "leave" comms.send(sock, data) elif rec['agenda'] == "cardReveal": board += rec['data'] print("BOARD: {}".format(board)) pass elif rec['agenda'] == "playerWonRound": #??? board = [] elif userInput == "2": data = {} data['agenda'] = "leaveLobby"
def handle(self, clientSock, address): packetSize = 1024 closeSocket = True try: reqData = comms.receive(clientSock) except socket.error as msg: clientSock.close() return responseData = {} responseData['agenda'] = reqData['agenda'] #login user if reqData['agenda'] == "login": print("Login attempt: {}\t{}".format(reqData['email'], reqData['password'])) user = User.getUser(reqData['email']) if user == 0: responseData['status'] = "nonExistentUser" else: user = User.login(reqData['email'], reqData['password']) if user == 0: responseData['status'] = "wrongPassword" else: #am i allready logged in? if user in users.values(): userHash = next((k for k, v in users.items() if v == user), None) responseData['status'] = "allreadyLoggedIn" responseData['data'] = userHash else: responseData['username'] = User.getUser(reqData['email'])[1] responseData['status'] = "ok" responseData['userId'] = str(uuid.uuid4()) users[responseData['userId']] = user print users #register user elif reqData['agenda'] == "register": print("Register attempt: {}\t{}".format(reqData['username'], reqData['email'], reqData['password'])) user = User.register(reqData['username'], reqData['email'],reqData['password']) if user == 0: responseData['status'] = "emailTaken" else: responseData['status'] = "ok" responseData['userId'] = str(uuid.uuid4()) users[responseData['userId']] = user #is user logged in legitely? elif reqData['userId'] not in users: responseData['status'] = "unauthenticated" else: #get lobbies if reqData['agenda'] == "getLobbies": responseData['data'] = lobby.getAllLobbiesForFront(self.lobbies) responseData['status'] = "ok" #create a lobby elif reqData['agenda'] == "createLobby": closeSocket = False responseData['data'] = "ok" createdLobbyId =lobby.createLobby(self.lobbies, reqData['lobbyName']) lobby.joinLobby(self.lobbies, createdLobbyId, users[reqData['userId']], clientSock, reqData) #join a lobby elif reqData['agenda'] == "joinLobby": if reqData['lobbyId'] in self.lobbies: closeSocket = False lobby.joinLobby(self.lobbies, reqData['lobbyId'], users[reqData['userId']], clientSock, reqData) else: responseData['status'] = "lobbyNotFound" #get profile data elif reqData['agenda'] == "profileData": responseData['data'] = "profileData request" elif reqData['agenda'] == "logout": if reqData['userId'] in users: del users[reqData['userId']] responseData['status'] = "ok" else: responseData['status'] = "notLoggedIn" else: print("Unknown request command {}".format(agenda)) responseData['status'] = "badRequest" if closeSocket: try: comms.send(clientSock, responseData) except socket.error as msg: print("Exception handled: {}".format(msg)) clientSock.close() return print "Closing socket for {}.".format(address) clientSock.close()
data['userId'] = userId sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((HOST, PORT)) lobbyLoop(data, sock) continue elif userInput == "6": data = {} data['agenda'] = "logout" else: print("Unknown user input {}".format(userInput)) continue data['userId'] = userId # Connect to server and send data # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((HOST, PORT)) comms.send(sock, data) # Receive data from the server and shut down received = comms.receive(sock) # received = sock.recv(1024) # received = json.loads(received) # print ("Received: {}".format(received)) if received['status'] != "allreadyLoggedIn" and received['status'] == "ok": if received['agenda'] == "login" or received['agenda'] == "register": userId = received['userId'] sock.close() # finally: # print "Sent: {}".format(data) # print "Received: {}".format(received)
def bettingPhase(self, gameObject, startPlayerIndex): playerCount=len(self.roundPlayers) currentPI=startPlayerIndex for p in range(0, playerCount): #init. playerStatus array-all to false, except those who folded (set to None) if(self.playerStatus[p]!=None): self.playerStatus[p]=False while(True): if(self.countActivePlayers(playerCount)==1): #check if more than 1 player is still active (un-folded) return False if(self.playerStatus[currentPI]==None): #skip player that folded currentPI=self.nextPlayer(currentPI, playerCount) else: print("Current min. bet: " + str(self.currentMinBet) + "\r\nCurrent pot: " + str(self.pot)) if(self.checkArray(self.playerStatus)): break print(" Player "+str(currentPI)) #tell everyone else its his turn data = {} data['agenda'] = "playersTurn" data['data'] = gameObject.players[currentPI].id comms.broadcastToEveryoneExcept(gameObject.players, gameObject.players[currentPI], data) data = {} data['agenda'] = "yourTurn" print("Sending yourTurn to {}".format(gameObject.players[currentPI].id)) comms.send(gameObject.players[currentPI].socket, data) rec = comms.receive(gameObject.players[currentPI].socket) action = rec['agenda'] if(action=="check"): self.roundPlayers[currentPI].placeBet(self, self.currentMinBet) self.playerStatus[currentPI]=True data = {} data['agenda'] = "playerCheck" data['data'] = gameObject.players[currentPI].id #player ki je raisal: SID comms.broadcastToPlayers(gameObject.players, data) currentPI=self.nextPlayer(currentPI, playerCount) elif(action=="raise"): #DONE(test) check, da raisa vec kot je min SERVER SIDE inputRaise = int(rec['data']) #if(inputRaise<=0): # inputRaise=minRaise #elif(inputRaise>self.roundPlayers[currentPI].money): # inputRaise=self.roundPlayers[currentPI].money #DONE-end betPlaced=self.roundPlayers[currentPI].placeBet(self, self.currentMinBet + inputRaise) self.playerStatus[currentPI]=True self.resetPlayerStatusAfterRaise(currentPI) #self.currentMinBet+=inputRaise self.currentMinBet=betPlaced print("RAISE: {}".format(inputRaise)) data = {} data['agenda'] = "playerRaise" data['data'] = [gameObject.players[currentPI].id, inputRaise] #player ki je raisal: SID, stevilo comms.broadcastToPlayers(gameObject.players, data) currentPI=self.nextPlayer(currentPI, playerCount) elif(action=="fold"): self.playerStatus[currentPI]=None data = {} data['agenda'] = "playerFold" data['data'] = gameObject.players[currentPI].id #player ki je raisal: SID comms.broadcastToPlayers(gameObject.players, data) currentPI=self.nextPlayer(currentPI, playerCount) elif(action=="leave"): #poslji mu ok za left? self.removePlayer(gameObject, currentPI) # print("\n><\n><\nround:{}, game:{}\n><\n><\n".format(len(self.roundPlayers), len(gameObject.players))) if currentPI > 0: if currentPI == playerCount - 1: currentPI -= 1 #fix index problems from leaver playerCount -= 1 return True