示例#1
0
def initGame():
   global gameStarted, shownCards, state, chips, foldStatus, playersBet
   gameStarted = True
   print "generating deck..."
   generateDeck()
   print "Shuffling deck..."
   random.shuffle(deck)
   print "Randomizing player order..."
   random.shuffle(players)
   
   for player in players:
      chips.append(500)
      foldStatus.append(False)
      playersBet.append(0)
   
   print "sending player list: " + " ".join(players)
   traffic.sendText(PLAYER + " ".join(players), players)
   time.sleep(.1)
   
   deckString = ""
   for card in deck:
      deckString += " " + chr(card[0]) + " " + chr(card[1])
   traffic.sendText(DECK + deckString, players)
   
   for idx in range(len(players)*2+1,len(players)*2+4):
      shownCards.append(deck[idx])
   
   
   if myIP == players[turn]:
      #print "Your turn. Enter Action: "
      state = MY_TURN
   else:
      #print "Waiting for my turn..."
      state = WAITING_FOR_TURN
示例#2
0
文件: agave.py 项目: Bronan/Agave
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)
示例#3
0
def processAction(act):
   global state, currentBet, peers, pot, chips
   print "state is " + str(state)
   if act.lower().startswith(START):
      if state == WAITING_FOR_GAME:
         if server == myIP and len(players) > 1:
            #if len(players) > 1:
            print "I'm Starting game..."
            traffic.sendText(START, players)
            initGame()
         else:
            traffic.sendText(START, server)
   
   elif act.lower().startswith(WHOS):
      for ip in players:
         print peers(ip)
   
   elif act.lower().startswith(JOIN):
      if state == WAITING_FOR_GAME:
         players.append(myIP)
         traffic.sendText(JOIN, peers)
   
   elif act.lower().startswith(CHECK):
      if state == MY_TURN and currentBet == 0:
         changeTurn()
         traffic.sendText(CHECK, peers)
      else:
         print "Cannot check, current bet is " + currentBet
   
   elif act.lower().startswith(BET):
      myBet = int(act[4:])
      if state == MY_TURN and chips[turn] >= myBet:
         if myBet < currentBet:
            return True #= nothing will be done with the true, just exit func

         elif myBet > currentBet:
            if turn == 0:
               lastCheck = len(players)-1
            else:
               lastCheck = turn-1
            currentBet = myBet
            
         pot += myBet
         chips[turn] -= myBet
         traffic.sendText(BET + (str(myBet)), peers)
         changeTurn()
         
   elif act.lower().startswith(FOLD):   
      if lastCheck == turn:
         if lastCheck == 0:
            lastCheck = len(players)-1
         else:
            lastCheck -= 1
         
      changeTurn()
示例#4
0
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()
示例#5
0
      pass
      #print "Waiting for " + whosTurn

def generateDeck():
   for suit in range(3,7):
      for card in range(1,14):
         deck.append((suit,card))

#= MainCode
start_new_thread(processSocket, ())

#read from database
database.initialize(traffic.getIP())
userList = database.getGlobalList()
#greet users in database
traffic.sendText(GREET, userList)
print "my IP is " + myIP
while True:
   if gameStarted:
      if players[turn] == myIP:
         state = MY_TURN
      else:
         state = WAITING_FOR_TURN
   if state == WAITING_FOR_GAME:
      print "Waiting for players to join..."
      #print "I think server is " + server
      action = raw_input()
      processAction(action)
   elif state == MY_TURN:
      myPrint()
      #print "I think server is " + server
示例#6
0
文件: agave.py 项目: Bronan/Agave
def handleUser():
	"""Get and parse user input."""
	global botPower
	hiberCounter = 0
	
	# Get and process user input
	while True:
		# Get user input
		prompt = peers[LOCAL_IP] + ": "
		inputString = sleepPrompt(prompt)
		msgLog.append(prompt + inputString)
		
		# Process user input
		if len(inputString) > 0:
			commandString = inputString.lower()
			hiberCounter = 0
			if commandString.startswith(etc.AGAVEBOT):
				botPower = not botPower
				if botPower:
					printMsg("Agavebot: Agavebot is now activated.")
				else:
					printMsg("Agavebot: Why do you hate agavebot?")
			elif commandString.startswith(etc.CLEAR):
				ui.clear()
			elif commandString.startswith(etc.KICK):
				traffic.sendText(traffic.KICK, inputString[len(etc.KICK):])
			elif commandString.startswith(etc.HELP):
				printMsg(etc.help(ui.getCols(), VERSION, HIBER_NUM))
			elif commandString.startswith(etc.NICK):
				if len(inputString[len(etc.NICK):]) > 2:
					database.setNick(inputString[len(etc.NICK):])
					peers[LOCAL_IP] = inputString[len(etc.NICK):]
					sendStatus()
				else:
					printMsg("Nicknames must be at least 3 characters long.")
			elif (commandString.startswith(etc.QUIT) or commandString.startswith(etc.EXIT)):
				traffic.sendText(traffic.QUIT, peers.keys())
				opsys.exit()
			elif commandString.startswith(etc.WHOS):
				whosString = "\nPeer IP addresses  | Mode       | Peer nicknames\n" + "".center(34,"-")
				for ip in peers:
					whosString += "\n"
					if ip == LOCAL_IP:
						whosString += "-->"
					else:
						whosString += "   "
					whosString += ip.ljust(18) + MODE_TEXT[mode[ip]].ljust(13) + peers[ip]
				printMsg(whosString)
			elif commandString.startswith(etc.AWAY):
				setMode(AWAY)
				emulateIDU()
				setMode(CHAT)
			elif commandString.startswith("$test".lower()):
				printMsg(msgLog, log=False)
			else:
				if commandString.startswith(traffic.COMMAND_CHAR):
					inputString = traffic.COMMAND_CHAR + inputString
				traffic.sendText(inputString, peers.keys())
		else:
			msgLog.pop()
			hiberCounter += 1
			if hiberCounter >= HIBER_NUM:
				setMode(HIBERNATE)
				opsys.closePopup()
				emulateIDU()
				setMode(CHAT)
				opsys.resumePopup()
示例#7
0
文件: agave.py 项目: Bronan/Agave
def sendStatus(command=traffic.HEARTBEAT, ips=peers.keys()):
	"""Updates peers with current mode and nick and tell them I'm alive."""
	traffic.sendText(command + chr(mode[LOCAL_IP]) + peers[LOCAL_IP], ips)