| 29 | | def possibleActions(self): |
| | 32 | |
| | 33 | def dealCard(self, card): |
| | 34 | self.cards.append(card) |
| | 35 | #returns money added to pot if any |
| | 36 | def doAction(self, action): |
| | 37 | self.action_history.append( action ) |
| | 38 | |
| | 39 | def inHand(self): |
| | 40 | #TODO need to change this |
| | 41 | return True |
| | 42 | |
| | 43 | PHASES = ["blinds", "flop", "turn", "river"] |
| | 44 | class holdemround: |
| | 45 | def __init__(self, players): |
| | 46 | self.deck = deck.deck() |
| | 47 | #keep both of these on hand so we can quickly find a player |
| | 48 | self.players = players |
| | 49 | self.player_array = players.values() |
| | 50 | self.player_array.sort( cmp=lambda x,y: x.seat - y.seat) ) |
| | 51 | self.action_on = None |
| | 52 | self.phase = 0 |
| | 53 | self.community_cards = None |
| | 54 | self.pot = None |
| | 55 | pass |
| | 56 | |
| | 57 | #return false if game is over |
| | 58 | def nextHand(self): |
| | 59 | self.deck.shuffle() |
| | 60 | self.action_on = 0 |
| | 61 | #rotate the players |
| | 62 | self.player_array = self.player_array.append(self.player_array.pop(0)) |
| | 63 | self.phase = 0 |
| | 64 | self.pot = 0.0 |
| | 65 | self.community_cards = [] |
| | 66 | for p in self.player_array: p.nextHand() |
| | 67 | #deal cards |
| | 68 | for _ in xrange(2): for p in self.player_array: |
| | 69 | p.dealCard(self.deck.deal()) |
| | 70 | return True |
| | 71 | |
| | 72 | #returns false if hand is over |
| | 73 | def nextPhase(self): |
| | 74 | self.phase += 1 |
| | 75 | if self.phase == len(PHASES): return False |
| | 76 | return True |
| | 77 | |
| | 78 | #returns false if phase is over |
| | 79 | def nextAction(self): |
| | 80 | self.action_on = ( self.button_on + 1 ) % len(self.player_array) |
| | 81 | return True |
| | 82 | |
| | 83 | #returns possible actions for a player |
| | 84 | def possibleActions(self, player_id): |
| | 86 | |
| | 87 | def getState(self): |
| | 88 | state = {"phase": PHASES[self.phase], |
| | 89 | "action_on": self.player_array[self.action_on].seat, |
| | 90 | "pot_before_phase": self.pot, |
| | 91 | "community_cards": self.community_cards } |
| | 92 | p_actions = {} |
| | 93 | |
| | 94 | for (n,d) in self.players.items(): |
| | 95 | if p.inHand(): p_actions[n] = d.publicData() |
| | 96 | state["player_actions"] = p_actions |
| | 97 | |
| | 98 | return state |
| | 99 | |
| | 100 | |
| | 133 | |
| | 134 | def genStatus(self, p_id): |
| | 135 | root = {} |
| | 136 | root["stat"] = "ok" |
| | 137 | root["type"] = "game_state" |
| | 138 | root["you"] = self.players[p_id].privateData() |
| | 139 | |
| | 140 | table_info = {} |
| | 141 | table_info["game_in_progress"] = self.game_started |
| | 142 | table_info["name"] = self.name |
| | 143 | table_info["blinds"] = self.blinds |
| | 144 | |
| | 145 | players = {} |
| | 146 | for (n,d) in self.players.items(): |
| | 147 | players[n] = d.publicData() |
| | 148 | table_info["players"] = players |
| | 149 | root["table_info"] = table_info |
| | 150 | |
| | 151 | if self.game_started: |
| | 152 | root["game_state"] = self.game_state.getState() |
| | 153 | |
| | 154 | def addDeferred(self, p_id, deferred): |
| | 155 | self.deferred.append((p_id, deferred)) |
| | 156 | |
| | 157 | #call this when the state changes to update all the deferred |
| | 158 | def stateChanged(self): |
| | 159 | for (p,d) in self.deferred: |
| | 160 | d.callback(self.genStatus(p)) |
| | 161 | self.deferred = [] |
| | 162 | |
| | 163 | |
| | 164 | |