Changeset 12
- Timestamp:
- 02/03/08 16:52:57 (4 years ago)
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
client.py
r11 r12 11 11 from twisted.internet import defer 12 12 from pprint import PrettyPrinter 13 from string import split 13 14 14 15 pprinter = PrettyPrinter().pprint 15 16 16 def dealWithData(data): 17 pprinter(data) 17 def makeAction( action, value): 18 msg = { "method":"action", 19 "action":action, 20 "value":value } 18 21 19 22 class PokerClientProtocol(NetstringReceiver): 23 def dealWithData(self,data): 24 print "---------------" 25 pprinter(data) 26 if data["you"]["required_action"]: 27 print "---------------" 28 print "We need some input. Possible actions:" 29 p_acts = data["you"]["possible_actions"] 30 vals = [None,None] 31 while not vals[0] in p_acts: 32 pprinter(p_acts) 33 str = raw_input('Action? format: <action> OR <action>,<value>:\n') 34 vals = split(str,",") 35 vals.append(None) 36 self.sendString( cjson.encode(makeAction(vals[0], vals[1]))) 37 20 38 def __init__(self,table_name, user_id, client_id ): 21 39 self.table_name = table_name … … 33 51 34 52 decoded = cjson.decode(line) 35 dealWithData(decoded)53 self.dealWithData(decoded) 36 54 37 55 def connectionLost(self, reason): -
holdemtable.py
r10 r12 11 11 self.money = self.starting_money 12 12 self.cards = [] 13 self.money_in_pot = None #money in pot before previous round 14 self.__action_history = [] 15 self.status = "need implementing" 16 17 def nextHand(self): 18 self.__in_hand = True 19 self.money_in_pot = 0.0 20 self.cards = [] 21 self.__action_history = [] 22 23 def moneyInPhase(self): 24 m = 0.0 25 for (a,b) in self.action_history if b: 26 m += b 27 return m 28 29 def nextPhase(self): 13 30 self.action_history = [] 14 self.status = "need implementing" 15 16 def nextHand(self): 17 self.cards = [] 18 self.actionHistory = [] 31 self.money_in_pot += self.moneyInPhase() 32 19 33 20 34 def publicData(self): … … 28 42 "cards" : self.cards} 29 43 return dat 44 30 45 def actionHistory(self): 31 return self. action_history46 return self.__action_history 32 47 33 48 def dealCard(self, card): 34 49 self.cards.append(card) 35 50 #returns money added to pot if any 36 def doAction(self, action): 37 self.action_history.append( action ) 51 def doAction(self, action, value): 52 if value and value > self.money: return False 53 else: 54 self.money -= value 55 self.__action_history.append((action,value)) 56 return True 38 57 39 58 def inHand(self): 40 59 #TODO need to change this 41 return True60 return self.__in_hand 42 61 43 62 PHASES = ["blinds", "flop", "turn", "river"] 44 63 class holdemround: 45 def __init__(self, players):64 def __init__(self,players,blinds): 46 65 self.deck = deck.deck() 66 self.blinds = blinds 47 67 #keep both of these on hand so we can quickly find a player 48 68 self.players = players … … 53 73 self.phase = 0 54 74 self.community_cards = None 55 self.po t = None75 self.possible_actions = None #to cache possible actions 56 76 pass 77 78 #hand over? 79 80 def isHandOver(self): 81 total = sum([ 1 for p in self.player_array if p.inHand() ]) 82 if total == 0: return True 83 84 self.phase == len(PHASES): return True 85 return False 57 86 58 87 #return false if game is over 59 88 def nextHand(self): 60 89 self.deck.shuffle() 61 self.action_on = 062 90 #rotate the players 63 91 self.player_array.append(self.player_array.pop(0)) 64 self.phase = 092 self.phase = None 65 93 self.pot = 0.0 66 94 self.community_cards = [] … … 68 96 for p in self.player_array: p.nextHand() 69 97 #deal cards 70 for _ in xrange(2):98 for _ in range(2): 71 99 for p in self.player_array: 72 100 p.dealCard(self.deck.deal()) 73 return True 101 102 return nextPhase() 74 103 75 104 #returns false if hand is over 76 105 def nextPhase(self): 77 self.phase += 1 78 if self.phase == len(PHASES): return False 79 return True 106 self.action_on = None 107 108 if self.phase != None: self.phase += 1 109 else: self.phase = 0 110 111 if self.isHandOver(): return False 112 else: return nextAction() 80 113 114 81 115 #returns false if phase is over 82 116 def nextAction(self): 83 self.action_on = ( self.button_on + 1 ) % len(self.player_array) 117 self.possible_actions = None 118 119 #first check if we have just one player left. then we have an easy winner 120 121 if self.isHandOver(): return False 122 123 #increment the acction 124 if self.action != None: 125 self.action_on = ( self.button_on + 1 ) % len(self.player_array) 126 while not self.player_array[self.action_on].inHand(): 127 self.action_on = ( self.button_on + 1 ) % len(self.player_array) 128 else: self.action = 0 84 129 return True 85 130 86 def requiredAction( self, player_id):87 self.action_onand player_id == self.player_array[self.action_on].id131 def requiredAction(self, player_id): 132 return self.action_on != None and player_id == self.player_array[self.action_on].id 88 133 89 134 #returns possible actions for a player 90 135 def possibleActions(self, player_id): 91 if player_id != self.player_array[self.action_on].id: return None 92 else: return {} #TODO add stuff here 136 if possible_actions != None: 137 return possible_actions 138 139 me = self.players[player_id] 140 possible_actions = {"fold":None} 141 142 max_money = max([p.moneyInPhase() for p in self.player_array]) 143 144 money_in_phase = me.moneyInPhase() 145 if max_money == money_in_phase: possible_actions["check"] = None 146 if max_money > money: possible_actions["call"] = max_money 147 #TODO this needs to change if it's the river 148 if max_money == 0: possible_actions["bet"] = self.blinds[1] 149 #TODO this needs to change to reflect the last bet 150 else: possible_actions["raise"] = self.blinds[1] + max_money 151 152 return possible_actions 153 #TODO fix so unlimited raises aren't allowed 154 93 155 94 156 def getState(self): … … 100 162 101 163 for (n,p) in self.players.items(): 102 if p.inHand(): p_actions[n] = p. publicData()164 if p.inHand(): p_actions[n] = p.actionHistory() 103 165 state["player_actions"] = p_actions 104 166 … … 109 171 110 172 class holdemtable: 111 max_players = 2112 def __init__(self, name):173 def __init__(self, name, max_players): 174 self.max_players = max_players 113 175 self.players={} 114 176 self.game_started = False … … 145 207 you = self.players[p_id].privateData() 146 208 147 req_action = self.game_started and\ 148 self.game_state.requiredAction(p_id) 149 150 you["reqired_action"] = req_action 209 req_action = self.game_started and self.game_state.requiredAction(p_id) 210 if self.game_started: print self.game_state.requiredAction(p_id) 211 212 print req_action 213 you["required_action"] = req_action 151 214 if req_action: you["possible_actions"] = self.game_state.possibleActions(p_id) 152 215 … … 168 231 return root 169 232 233 def action(self, p_id, action, value): 234 pass 235 170 236 def addDeferred(self, p_id, deferred): 171 237 print "Adding player id(%s) to deferred" % p_id -
tabled.py
r11 r12 13 13 from twisted.internet import defer 14 14 15 tables = { "test_table":holdemtable("test_table")}15 tables = {} 16 16 17 17 class PokerTableProtocol(NetstringReceiver): … … 42 42 protocol.MakeError(103,"Already connected")) ) 43 43 return 44 table_name = decoded["table_name"] 44 45 45 self.table = tables[decoded["table_name"]] 46 if not table_name in tables: 47 tables[table_name]=holdemtable(table_name) 48 49 self.table = tables[table_name] 46 50 if self.table.isFull(): 47 51 self.sendString( cjson.encode(
