Changeset 13
- Timestamp:
- 02/03/08 18:29:26 (4 years ago)
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
client.py
r12 r13 19 19 "action":action, 20 20 "value":value } 21 return msg 21 22 22 23 class PokerClientProtocol(NetstringReceiver): … … 24 25 print "---------------" 25 26 pprinter(data) 27 if data["stat"] == "fail": 28 print "error?" 29 return 26 30 if data["you"]["required_action"]: 27 31 print "---------------" … … 34 38 vals = split(str,",") 35 39 vals.append(None) 36 self.sendString( cjson.encode(makeAction(vals[0], vals[1]))) 40 str = cjson.encode(makeAction(vals[0], vals[1])) 41 print "Sending %s" % str 42 self.sendString( str ) 37 43 38 44 def __init__(self,table_name, user_id, client_id ): -
holdemtable.py
r12 r13 2 2 import random 3 3 from twisted.internet import defer 4 5 class InvalidActionException(Exception): 6 pass 7 8 class InsufficientFundsException(Exception): 9 pass 4 10 5 11 class player: … … 11 17 self.money = self.starting_money 12 18 self.cards = [] 13 self. money_in_pot = None #money in pot before previous round19 self.__money_in_pot = None #money in pot before previous round 14 20 self.__action_history = [] 15 21 self.status = "need implementing" … … 17 23 def nextHand(self): 18 24 self.__in_hand = True 19 self. money_in_pot = 0.025 self.__money_in_pot = 0.0 20 26 self.cards = [] 21 27 self.__action_history = [] 22 28 29 def moneyInPot(self): 30 return __money_in_pot 31 23 32 def moneyInPhase(self): 24 m = 0.0 25 for (a,b) in self.action_history if b: 26 m += b 27 return m 33 return sum([ b for a,b in self.__action_history if b != None]) 28 34 29 35 def nextPhase(self): 30 self. action_history = []31 self. money_in_pot += self.moneyInPhase()36 self.__action_history = [] 37 self.__money_in_pot += self.moneyInPhase() 32 38 33 39 … … 50 56 #returns money added to pot if any 51 57 def doAction(self, action, value): 52 if value and value > self.money: return False 58 print value 59 print self.money 60 if value != None and value > self.money: 61 raise InsufficientFundsException("Insufficient Funds") 53 62 else: 54 63 self.money -= value 55 64 self.__action_history.append((action,value)) 56 return True57 65 58 66 def inHand(self): 59 67 #TODO need to change this 60 68 return self.__in_hand 69 61 70 62 71 PHASES = ["blinds", "flop", "turn", "river"] … … 70 79 self.player_array.sort( cmp=lambda x,y: x.seat - y.seat) 71 80 print self.player_array 72 self. action_on = None81 self.__action_on = None 73 82 self.phase = 0 74 83 self.community_cards = None … … 76 85 pass 77 86 87 def actionOn(self): 88 return self.__action_on 78 89 #hand over? 79 90 … … 82 93 if total == 0: return True 83 94 84 self.phase == len(PHASES): return True95 if self.phase == len(PHASES): return True 85 96 return False 86 97 … … 100 111 p.dealCard(self.deck.deal()) 101 112 102 return nextPhase()113 return self.nextPhase() 103 114 104 115 #returns false if hand is over 105 116 def nextPhase(self): 106 self. action_on = None117 self.__action_on = None 107 118 108 119 if self.phase != None: self.phase += 1 109 120 else: self.phase = 0 110 121 122 pn = PHASES[self.phase] 123 if pn == "blinds": 124 self.player_array[0].doAction("small_blinds",self.blinds[0]) 125 self.player_array[1].doAction("large_blinds",self.blinds[1]) 126 self.__action_on = 1 127 elif pn == "flop": 128 for _ in range(3): 129 self.community_cards.append(self.deck.deal) 130 elif pn == "turn": 131 self.community_cards.append(self.deck.deal) 132 elif pn == "river": 133 self.community_cards.append(self.deck.deal) 134 135 111 136 if self.isHandOver(): return False 112 else: return nextAction()137 else: return self.nextAction() 113 138 114 139 … … 121 146 if self.isHandOver(): return False 122 147 123 #increment the ac ction124 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)148 #increment the action 149 if self.__action_on != None: 150 self.__action_on = ( self.__action_on + 1 ) % len(self.player_array) 151 while not self.player_array[self.__action_on].inHand(): 152 self.__action_on = ( self.__action_on + 1 ) % len(self.player_array) 128 153 else: self.action = 0 129 154 return True 130 155 131 156 def requiredAction(self, player_id): 132 return self. action_on != None and player_id == self.player_array[self.action_on].id157 return self.__action_on != None and player_id == self.player_array[self.__action_on].id 133 158 134 159 #returns possible actions for a player 135 def possibleActions(self , player_id):136 if possible_actions != None:137 return possible_actions138 139 me = self.player s[player_id]140 possible_actions = {"fold":None}160 def possibleActions(self): 161 if self.possible_actions != None: 162 return self.possible_actions 163 164 me = self.player_array[self.__action_on] 165 self.possible_actions = {"fold":(None,None)} 141 166 142 167 max_money = max([p.moneyInPhase() for p in self.player_array]) 143 168 144 169 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 170 if max_money == money_in_phase: self.possible_actions["check"] = (None,None) 171 if max_money > money_in_phase: self.possible_actions["call"] =\ 172 (max_money - money_in_phase, max_money - money_in_phase) 147 173 #TODO this needs to change if it's the river 148 if max_money == 0: possible_actions["bet"] = self.blinds[1]174 if max_money == 0: self.possible_actions["bet"] = (self.blinds[1], me.money) 149 175 #TODO this needs to change to reflect the last bet 150 else: possible_actions["raise"] = self.blinds[1] + max_money151 152 return possible_actions176 else: self.possible_actions["raise"] = (self.blinds[1] + max_money - money_in_phase, me.money) 177 178 return self.possible_actions 153 179 #TODO fix so unlimited raises aren't allowed 154 180 … … 156 182 def getState(self): 157 183 state = {"phase": PHASES[self.phase], 158 "action_on": self.player_array[self. action_on].seat,184 "action_on": self.player_array[self.__action_on].seat, 159 185 "pot_before_phase": self.pot, 160 186 "community_cards": self.community_cards } … … 167 193 return state 168 194 195 def potSize(self): 196 return sum([ p.moneyInPot for p in self.player_array ]) 197 198 def doAction(self, p_id, action, value): 199 if p_id != self.player_array[self.__action_on].id: 200 raise InvalidActionException("Action not on player " + p_id) 201 202 pa = self.possibleActions() 203 if not action in pa: 204 raise InvalidActionException("Action not in possible list") 205 206 range = pa[action] 207 if not (range[0] <= value <= range[1]): 208 print range[0] 209 print value 210 print range[1] 211 raise InvalidActionException("Value out of range") 212 self.player_array[self.__action_on].doAction(action,value) 213 self.nextAction() 214 215 216 217 218 169 219 170 220 171 221 172 222 class holdemtable: 173 def __init__(self, name, max_players ):223 def __init__(self, name, max_players = 4): 174 224 self.max_players = max_players 175 225 self.players={} … … 190 240 def checkStart(self): 191 241 if self.isFull(): 192 self.game_state = holdemround(self.players )242 self.game_state = holdemround(self.players,self.blinds) 193 243 self.game_started = True 194 244 self.game_state.nextHand() … … 212 262 print req_action 213 263 you["required_action"] = req_action 214 if req_action: you["possible_actions"] = self.game_state.possibleActions( p_id)264 if req_action: you["possible_actions"] = self.game_state.possibleActions() 215 265 216 266 root["you"] = you … … 232 282 233 283 def action(self, p_id, action, value): 234 pass 284 self.game_state.doAction(p_id,action,value) 285 self.stateChanged("Actioned") 235 286 236 287 def addDeferred(self, p_id, deferred): 237 print "Adding player id(%s) to deferred" % p_id238 288 self.deferred.append((p_id, deferred)) 239 289 -
tabled.py
r12 r13 1 1 """ 2 usage: tabled.py < gamename>2 usage: tabled.py <port> 3 3 """ 4 4 import sys … … 32 32 try: 33 33 decoded = cjson.decode(line) 34 if "method" not in decoded:34 if decoded == None or "method" not in decoded: 35 35 self.sendString( cjson.encode( 36 36 protocol.MakeError(101,"Messages must contain methods")) ) … … 57 57 d.addCallback(self.sendMessage) 58 58 self.table.addPlayer(decoded, d) 59 elif method == "action": 60 value = decoded["value"] 61 if value != None: value = float(value) 62 self.table.action(self.id, decoded["action"],value) 59 63 else: 60 64 self.sendString( cjson.encode(protocol.MakeError(102,"Unknown Method")) ) … … 77 81 sys.exit(0) 78 82 79 gamename = sys.argv[1]83 port = int(sys.argv[1]) 80 84 81 85 factory = Factory() … … 83 87 84 88 # 8007 is the port you want to run under. Choose something >1024 85 reactor.listenTCP(8007, factory) 89 reactor.listenTCP(port, factory) 90 print "Listening on localhost:%d" % port 86 91 reactor.run() 87 92
