def processMessage(): """Parse incoming commands and print messages.""" while True: msg, sendersIP = traffic.listenToSocket() # Update heartbeat heartbeats[sendersIP] = time.time() if not sendersIP in peers: peers[sendersIP] = sendersIP mode[sendersIP] = len(MODE_TEXT)-1 sendStatus(traffic.GREET, ips=sendersIP) printMsg(peers[sendersIP] + " has joined the party.") # Parse or print message if msg.startswith(traffic.QUIT): delPeer(sendersIP) elif msg.startswith(traffic.HEARTBEAT): parseStatus(msg[len(traffic.HEARTBEAT):], sendersIP) elif msg.startswith(traffic.GREET): parseStatus(msg[len(traffic.GREET):], sendersIP) sendStatus() elif msg.startswith(traffic.KICK): traffic.sendText(traffic.QUIT, peers.keys()) opsys.exit() else: if msg.startswith(traffic.AGAVEBOT): printMsg("Agavebot: " + msg[len(AGAVEBOT):]) elif msg.startswith(traffic.COMMAND_CHAR + traffic.COMMAND_CHAR): printMsg(peers[sendersIP] + ": " + msg[len(traffic.COMMAND_CHAR):]) else: printMsg(peers[sendersIP] + ": " + msg[:]) if botPower: msg = botResponses[random.randint(0, len(botResponses)-1)] traffic.sendText(traffic.AGAVEBOT + msg, peers.keys()) printMsg("Agavebot: " + msg)
def processSocket(): #process msg global server, state, players, chips, deck, shownCards, foldStatus, playersBet, gameStarted while True: msg, sendersIP = traffic.listenToSocket() if msg.startswith(GREET): print "GREET from " + sendersIP players.append(myIP) traffic.sendText(SERVER + server, sendersIP) elif msg.startswith(SERVER): server = msg[len(SERVER):] traffic.sendText(JOIN, server) players.append(sendersIP) print "\nSERVER from " + server print "now I feel like " + server + " is server" elif msg.lower().startswith(JOIN): if state == WAITING_FOR_GAME: players.append(sendersIP) elif msg.startswith(START): print sendersIP + " is starting the game." if server == myIP: print "I'm the server! Initializing game..." initGame() else: gameStarted = True state = WAITING_FOR_TURN #traffic.sendText(START, players) elif msg.startswith(PLAYER): #record the player list (ordered) print "receiving player list from server " + sendersIP players = msg[len(PLAYER):].split() #print players print "players list: " + " ".join(players) for player in players: chips.append(500) foldStatus.append(False) playersBet.append(0) elif msg.startswith(DECK): print "receiving deck from " + sendersIP tmpDeck = msg[len(DECK):].split() for idx in range(0, len(tmpDeck), 2): deck.append((tmpDeck[idx/2]),(tmpDeck[(idx/2)+1])) for idx in range(len(players)*2+1,len(players)*2+4): print str(idx) + "\n" shownCards.append(deck[idx]) print "Deck: " + deck print "Shown Cards: " + shownCards if players[turn] == myIP: state = MY_TURN else: state = WAITING_FOR_TURN elif msg.startswith(CHECK): print sendersIP + " has checked." changeTurn() elif msg.startswith(BET): print sendersIP + " bet " + newBet newBet = int(msg[5:]) pot += newBet chips[turn] -= newBet currentBet = newBet changeTurn() elif msg.startswith(FOLD): print sendersIP + " has folded." foldStatus[turn] = True playersBet[turn] = 0 changeTurn()