def GetMessagesFromHistory(self, history, serial2name = str, pocket_messages = False):
     self.game.turn_history = []
     self.game.turn_history_reduced = []
     self.game.turn_history_unreduced_position = 0
     for message in history:
         self.game.historyAdd(*message)
     return pokergame.history2messages(self.game, self.game.historyGet(), serial2name, pocket_messages)
Example #2
0
 def GetMessagesFromHistory(self, history, serial2name = str, pocket_messages = False):
     self.game.turn_history = []
     self.game.turn_history_reduced = []
     self.game.turn_history_unreduced_position = 0
     for message in history:
         self.game.historyAdd(*message)
     return pokergame.history2messages(self.game, self.game.historyGet(), serial2name, pocket_messages)
Example #3
0
 def test1(self):
     for (serial, seat) in ((1, 0), (2, 1), (3, 2), (4, 3)):
         self.make_new_player(serial, seat)
     self.game.beginTurn(1)
     self.assertEqual(
         str(
             history2messages(self.game,
                              self.game.turn_history,
                              pocket_messages=True)),
         "('hand #1, holdem, 1-2_20-200_limit', ['2 pays 1 blind', '3 pays 2 blind', 'pre-flop, 4 players', 'Cards player 1: 8s Ah', 'Cards player 2: 3h 6h', 'Cards player 3: As 6s', 'Cards player 4: 6d Qs', '4 calls 2', '1 calls 2', '2 calls 1', '3 checks', 'flop, 4 players', 'Board: 4s 7c 4h', 'Cards player 1: 8s Ah', 'Cards player 2: 3h 6h', 'Cards player 3: As 6s', 'Cards player 4: 6d Qs', '2 checks', '3 checks', '4 checks', '1 checks', 'turn, 4 players', 'Board: 4s 7c 4h 5c', 'Cards player 1: 8s Ah', 'Cards player 2: 3h 6h', 'Cards player 3: As 6s', 'Cards player 4: 6d Qs', '2 raises 4', '3 folds', '4 folds', '1 calls 4', 'river, 2 players', 'Board: 4s 7c 4h 5c Ks', 'Cards player 1: 8s Ah', 'Cards player 2: 3h 6h', '2 raises 4', '1 calls 4', 'Rake 1', 'Board: 4s 7c 4h 5c Ks', 'Cards player 1: 8s Ah', 'Cards player 2: 3h 6h', '2 shows Straight Seven to Trey ', '1 mucks loosing hand', '2 wins ', '2 receives 24'])"
     )
Example #4
0
    def explain(self, packet):
        self.log.debug("%s", packet)
        
        self.forward_packets = [ packet ]
        forward_packets = self.forward_packets
        
        if packet.type == PACKET_POKER_TABLE:
            if packet.id == 0:
                self.log.error("Too many open tables")
            else:
                if self.games.getGame(packet.id):
                    self.games.deleteGame(packet.id)
                new_game = self.games.getOrCreateGame(packet.id)
                new_game.prefix = self._prefix
                new_game.name = packet.name
                new_game.setTime(0)
                new_game.setVariant(packet.variant)
                new_game.setBettingStructure(packet.betting_structure)
                new_game.setMaxPlayers(packet.seats)
                new_game.registerCallback(self.gameEvent)
                new_game.level_skin = packet.skin
                new_game.currency_serial = packet.currency_serial
                new_game.history_index = 0
                new_game.log.addRef('Explain', self, lambda explain: explain.serial)
                self.updatePotsChips(new_game, [])
                new_game.position_info = [ 0, 0 ]
                self.forward_packets.append(self.currentGames())

        elif packet.type == PACKET_SERIAL:
            self.handleSerial(packet)

        elif packet.type == PACKET_ERROR:
            self.log.error("Server reported error : %s", packet)
            return False

        game = self.games.packet2game(packet)

        if game and packet.type in (PACKET_POKER_TABLE_DESTROY,PACKET_POKER_TABLE_QUIT):
            self.games.deleteGame(game.id)
            game = None

        if game:
            if packet.type == PACKET_POKER_START:
                if packet.hand_serial == 0:
                    self.log.error("game start was refused")
                    forward_packets.remove(packet)
                elif not game.isEndOrNull():
                    raise UserWarning, "you should not be here (state: %s)" % game.state
                else:
                    game.history_index = 0
                    game.setTime(packet.time)
                    game.setHandsCount(packet.hands_count)
                    game.setLevel(packet.level)
                    game.beginTurn(packet.hand_serial)
                    game.position_info[PokerNetworkGameClient.POSITION_OBSOLETE] = True
                    if not self.no_display_packets:
                        forward_packets.append(PacketPokerBoardCards(game_id = game.id, serial = self.getSerial()))
                        for serial in game.player_list:
                            forward_packets.append(self.updatePlayerChips(game, game.serial2player[serial]))
                            if self.what & PacketPokerExplain.CHIPSTACKS:
                                forward_packets.append(self.explainPlayerChips(game, game.serial2player[serial]))
                        forward_packets.extend(self.updatePotsChips(game, []))

            elif packet.type == PACKET_POKER_CANCELED:
                if not self.no_display_packets and packet.amount > 0:
                    player = game.getPlayer(packet.serial)
                    if player.bet > 0:
                        forward_packets.extend(self.chipsBet2Pot(game, player, player.bet, 0))
                    if packet.amount > 0:
                        forward_packets.append(self.chipsPot2Player(game, player, packet.amount, 0, "canceled"))
                game.canceled(packet.serial, packet.amount)
                forward_packets.append(PacketPokerPosition(game_id = game.id))

            elif packet.type == PACKET_POKER_PLAYER_ARRIVE:
                if game.addPlayer(packet.serial, seat=packet.seat, name=packet.name):
                    player = game.getPlayer(packet.serial)
                    player.setUserData(DEFAULT_PLAYER_USER_DATA.copy())
                    player.name = packet.name
                    player.url = packet.url
                    player.outfit = packet.outfit
                    player.auto_blind_ante = packet.auto_blind_ante
                    player.wait_for = packet.wait_for
                    player.auto = packet.auto
                if not self.no_display_packets:
                    self.forward_packets.append(PacketPokerSeats(game_id = game.id, seats = game.seats()))

            elif packet.type in (PACKET_POKER_PLAYER_LEAVE,PACKET_POKER_TABLE_MOVE) :
                game.removePlayer(packet.serial)
                if packet.type == PACKET_POKER_TABLE_MOVE:
                    if packet.serial == self.getSerial():
                        self.games.deleteGame(game.id)
                    forward_packets.append(PacketPokerPlayerLeave(
                        game_id = packet.game_id,
                        serial = packet.serial,
                        seat = packet.seat
                    ))
                if not self.no_display_packets:
                    self.forward_packets.append(PacketPokerSeats(game_id = game.id, seats = game.seats()))

            elif packet.type == PACKET_POKER_PLAYER_SELF:
                ( serial_in_position, position_is_obsolete ) = game.position_info
                if serial_in_position == self.getSerial():
                    game.position_info = [ 0, True ]
                forward_packets.extend(self.updateBetLimit(game))

            elif packet.type == PACKET_POKER_POSITION:
                if game.isBlindAnteRound():
                    game.setPosition(packet.position)
                forward_packets.remove(packet)

            elif packet.type == PACKET_POKER_SEATS:
                forward_packets.remove(packet)

            elif packet.type == PACKET_POKER_PLAYER_CARDS:
                player = game.getPlayer(packet.serial)
                player.hand.set(packet.cards)

            elif packet.type == PACKET_POKER_BOARD_CARDS:
                game.board.set(packet.cards)
                if not self.no_display_packets and game.isSit(self.getSerial()):
                    player = game.getPlayer(self.getSerial())
                    if len(packet.cards) > 0 and len(player.hand.tolist(True)):
                        forward_packets.append(PacketPokerPlayerHandStrength(
                            game_id = game.id,
                            serial = self.getSerial(),
                            hand = game.readablePlayerBestHands(self.getSerial())
                        ))

            elif packet.type == PACKET_POKER_DEALER:
                game.setDealer(packet.dealer)

            elif packet.type == PACKET_POKER_SIT_OUT:
                game.sitOut(packet.serial)

            elif packet.type == PACKET_POKER_AUTO_FOLD:
                game.autoPlayer(packet.serial)

            elif packet.type == PACKET_POKER_AUTO_BLIND_ANTE:
                game.autoBlindAnte(packet.serial)

            elif packet.type == PACKET_POKER_NOAUTO_BLIND_ANTE:
                game.noAutoBlindAnte(packet.serial)

            elif packet.type == PACKET_POKER_MUCK_REQUEST:                
                game.setMuckableSerials(packet.muckable_serials)
                
            elif packet.type == PACKET_POKER_SIT:
                game.sit(packet.serial)

            elif packet.type == PACKET_POKER_TIMEOUT_WARNING:
                if not self.setPlayerTimeout(game, packet):
                    forward_packets.remove(packet)
            
            elif packet.type == PACKET_POKER_TIMEOUT_NOTICE:
                self.unsetPlayerTimeout(game, packet.serial)

            elif packet.type == PACKET_POKER_WAIT_FOR:
                game.getPlayer(packet.serial).wait_for = packet.reason
                forward_packets.remove(packet)

            elif packet.type == PACKET_POKER_IN_GAME:
                game.setStaticPlayerList(packet.players)
                for serial in game.serialsAll():
                    player = game.getPlayer(serial)
                    wait_for = player.wait_for
                    in_game = serial in packet.players 
                    if in_game or wait_for:
                        auto = player.isAuto()
                        if not game.isSit(serial):
                            game.sit(serial)
                            forward_packets.append(PacketPokerSit(game_id = game.id, serial = serial))
                        if auto:
                            game.autoPlayer(serial)
                            forward_packets.append(PacketPokerAutoFold(game_id = game.id, serial = player.serial))
                        if wait_for:
                            if wait_for == True and not in_game and not game.isRunning():
                                #
                                # A player is waiting for the blind (big, late...)
                                # and the server says it will not participate to the
                                # blindAnte round. This only happens when the anteRound
                                # is already finished on the server (i.e. when connecting
                                # to a table in the middle of a game). 
                                player.wait_for = "first_round"
                            forward_packets.append(PacketPokerWaitFor(
                                game_id = game.id,
                                serial = serial,
                                reason = wait_for
                            ))
                    else:
                        if game.isSit(serial):
                            game.sitOut(serial)
                            forward_packets.append(PacketPokerSitOut(game_id = game.id,serial = serial))

            elif packet.type == PACKET_POKER_RAKE:
                if game.isBlindAnteRound():
                    game.blindAnteRoundEnd()
                game.setRakedAmount(packet.value)
                
            elif packet.type == PACKET_POKER_WIN:
                if not self.no_display_packets:
                    for serial in packet.serials:
                        forward_packets.append(PacketPokerPlayerWin(serial = serial, game_id = game.id))
                if game.winners:
                    #
                    # If we know the winners before an explicit call to the distributeMoney
                    # method, it means that there is no showdown.
                    if not self.no_display_packets:
                        if game.isGameEndInformationValid():
                            forward_packets.append(PacketPokerShowdown(game_id = game.id, showdown_stack = game.showdown_stack))
                        forward_packets.extend(self.packetsPot2Player(game))
                else:
                    game.distributeMoney()
                    winners = game.winners[:]
                    winners.sort()
                    packet.serials.sort()
                    if winners != packet.serials:
                        raise UserWarning, "game.winners %s != packet.serials %s" % (winners, packet.serials)
                    if not self.no_display_packets:
                        if game.isGameEndInformationValid():
                            forward_packets.extend(self.packetsShowdown(game))
                            forward_packets.append(PacketPokerShowdown(game_id = game.id, showdown_stack = game.showdown_stack))
                        forward_packets.extend(self.packetsPot2Player(game))
                    game.endTurn()
                forward_packets.append(PacketPokerPosition(game_id = game.id))

            elif packet.type == PACKET_POKER_REBUY:
                forward_packets.remove(packet)
                if game.rebuy(packet.serial, packet.amount):
                    #
                    # If the server says the player rebuys, assume he knows
                    # that the player can rightfully do so and therefore
                    # that the buy_in has already been paid. The client fail
                    # to notice that a player already paid the buy_in 
                    # when it connects to a table on which said player has
                    # no chips in front of him.
                    player = game.getPlayer(packet.serial)
                    player.buy_in_payed = True
                    forward_packets.append(self.updatePlayerChips(game, player))
                    if self.what & PacketPokerExplain.CHIPSTACKS:
                        forward_packets.append(self.explainPlayerChips(game, player))

            elif packet.type == PACKET_POKER_PLAYER_CHIPS:
                player = game.getPlayer(packet.serial)
                if player.buy_in_payed:
                    if player.bet != packet.bet:
                        self.log.error("server says player %d has a bet of %d chips and client thinks it has %d",
                            packet.serial,
                            packet.bet,
                            player.bet
                        )
                        player.bet = packet.bet
                    if player.money != packet.money:
                        self.log.error("server says player %d has a money of %d chips and client thinks it has %d",
                            packet.serial,
                            packet.money,
                            player.money
                        )
                        player.money = packet.money
                else:
                    #
                    # If server sends chips amount for a player that did not yet pay the buy in
                    player.bet = packet.bet
                    player.money = packet.money
                    if player.money > 0:
                        player.buy_in_payed = True
                if self.what & PacketPokerExplain.CHIPSTACKS:
                    forward_packets.append(self.explainPlayerChips(game, player))

            elif packet.type == PACKET_POKER_FOLD:
                game.fold(packet.serial)
                if game.isSitOut(packet.serial):
                    forward_packets.append(PacketPokerSitOut(game_id = game.id, serial = packet.serial))

            elif packet.type == PACKET_POKER_CALL:
                game.call(packet.serial)

            elif packet.type == PACKET_POKER_RAISE:
                game.callNraise(packet.serial, packet.amount)

            elif packet.type == PACKET_POKER_CHECK:
                game.check(packet.serial)

            elif packet.type == PACKET_POKER_BLIND:
                game.blind(packet.serial, packet.amount, packet.dead)

            elif packet.type == PACKET_POKER_BLIND_REQUEST:
                game.setPlayerBlind(packet.serial, packet.state)

            elif packet.type == PACKET_POKER_ANTE:
                game.ante(packet.serial, packet.amount)

            elif packet.type == PACKET_POKER_STATE:
                game.position_info[PokerNetworkGameClient.POSITION_OBSOLETE] = True
                if game.isBlindAnteRound():
                    game.blindAnteRoundEnd()
                if packet.string == "end" and game.state != "null":
                    game.endState()
                #
                # A state change is received at the begining of each
                # betting round. No state change is received when
                # reaching showdown or otherwise terminating the hand.
                if game.isFirstRound():
                    game.initRound()
                else:
                    if not self.no_display_packets:
                        if packet.string == "end" and game.isSingleUncalledBet(game.side_pots):
                            forward_packets.extend(self.moveBet2Player(game))
                        else:
                            forward_packets.extend(self.moveBet2Pot(game))
                    if packet.string != "end":
                        game.initRound()
                if not self.no_display_packets:
                    if game.isRunning() and game.cardsDealt() and game.downCardsDealtThisRoundCount() > 0:
                        forward_packets.append(PacketPokerDealCards(
                            game_id = game.id,
                            numberOfCards = game.downCardsDealtThisRoundCount(),
                            serials = game.serialsNotFold())
                        )
                if game.isRunning() and game.cardsDealt() and game.cardsDealtThisRoundCount() :
                    for player in game.playersNotFold():
                        cards = player.hand.toRawList()
                        forward_packets.append(PacketPokerPlayerCards(
                            game_id = game.id,
                            serial = player.serial,
                            cards = cards)
                        )
                if ( packet.string != "end" and not game.isBlindAnteRound() ):
                    if not self.no_display_packets:
                        forward_packets.extend(self.updateBetLimit(game))
                    forward_packets.append(PacketPokerBeginRound(game_id = game.id))

                if game.state != packet.string:
                    self.log.error("state = %s, expected %s instead ", game.state, packet.string)


            ( serial_in_position, position_is_obsolete ) = game.position_info
            if game.isRunning():
                #
                # Build position related packets
                position_changed = serial_in_position != game.getSerialInPosition()
                if position_is_obsolete or position_changed:
                    self_was_in_position = self.getSerial() != 0 and serial_in_position == self.getSerial()
                    serial_in_position = game.getSerialInPosition()
                    self_in_position = serial_in_position == self.getSerial()
                    if serial_in_position > 0:
                        if position_changed:
                            forward_packets.append(PacketPokerPosition(
                                game_id = game.id,
                                position = game.position,
                                serial = serial_in_position)
                            )
                        if self_was_in_position and not self_in_position:
                            self.unsetPlayerTimeout(game, self.getSerial())
                            if not game.isBlindAnteRound() or not game.getPlayer(self.getSerial()).isAutoBlindAnte():
                                forward_packets.append(PacketPokerSelfLostPosition(
                                    game_id = game.id,
                                    serial = serial_in_position)
                                )
                        if (not self_was_in_position or position_is_obsolete) and self_in_position:
                            if not game.isBlindAnteRound() or not game.getPlayer(self.getSerial()).isAutoBlindAnte():
                                forward_packets.append(PacketPokerSelfInPosition(
                                    game_id = game.id,
                                    serial = serial_in_position)
                                )
                    elif self_was_in_position:
                        self.unsetPlayerTimeout(game, self.getSerial())
                        if not game.isBlindAnteRound() or not game.getPlayer(self.getSerial()).isAutoBlindAnte():
                            forward_packets.append(PacketPokerSelfLostPosition(
                                game_id = game.id,
                                serial = self.getSerial())
                            )
            else:
                if serial_in_position > 0:
                    if not game.isBlindAnteRound() or not game.getPlayer(self.getSerial()).isAutoBlindAnte():
                        forward_packets.append(PacketPokerSelfLostPosition(
                            game_id = game.id,
                            serial = self.getSerial())
                        )
                    serial_in_position = 0
            position_is_obsolete = False
            game.position_info = [ serial_in_position, position_is_obsolete ]
            #
            # Build dealer messages
            # Skip state = end because information is missing and will be received by the next packet (WIN)
            if not (packet.type == PACKET_POKER_STATE and packet.string == "end"):
                (subject, messages) = history2messages(game, game.historyGet()[game.history_index:], serial2name = lambda serial: self.serial2name(game, serial))
                game.history_index = len(game.historyGet())
                if messages:
                    message = "".join("Dealer: %s\n" % line for line in messages)
                    forward_packets.append(PacketPokerChat(
                        game_id = game.id,
                        message = message)
                    )

        return True
Example #5
0
    def explain(self, packet):
        if self.verbose > 3: self.message("PokerExplain:explain: %s" % packet)

        self.forward_packets = [packet]
        forward_packets = self.forward_packets

        if packet.type == PACKET_POKER_TABLE:
            if packet.id == 0:
                self.error("Too many open tables")
            else:
                if self.games.getGame(packet.id):
                    self.games.deleteGame(packet.id)
                new_game = self.games.getOrCreateGame(packet.id)
                new_game.prefix = self._prefix
                new_game.name = packet.name
                new_game.setTime(0)
                new_game.setVariant(packet.variant)
                new_game.setBettingStructure(packet.betting_structure)
                new_game.setMaxPlayers(packet.seats)
                new_game.registerCallback(self.gameEvent)
                new_game.level_skin = packet.skin
                new_game.currency_serial = packet.currency_serial
                new_game.history_index = 0
                self.updatePotsChips(new_game, [])
                new_game.position_info = [0, 0]
                self.forward_packets.append(self.currentGames())

        elif packet.type == PACKET_SERIAL:
            self.handleSerial(packet)

        elif packet.type == PACKET_ERROR:
            self.error("Server reported error : %s" % packet)
            return False

        game = self.games.packet2game(packet)

        if game and packet.type == PACKET_POKER_TABLE_DESTROY:
            game = None

        if game:
            if packet.type == PACKET_POKER_START:
                if packet.hand_serial == 0:
                    self.error("game start was refused")
                    forward_packets.remove(packet)
                elif not game.isEndOrNull():
                    raise UserWarning, "you should not be here (state: %s)" % game.state
                else:
                    game.history_index = 0
                    game.setTime(packet.time)
                    game.setHandsCount(packet.hands_count)
                    game.setLevel(packet.level)
                    game.beginTurn(packet.hand_serial)
                    game.position_info[
                        PokerNetworkGameClient.POSITION_OBSOLETE] = True
                    if not self.no_display_packets:
                        forward_packets.append(
                            PacketPokerBoardCards(game_id=game.id,
                                                  serial=self.getSerial()))
                        for serial in game.player_list:
                            forward_packets.append(
                                self.updatePlayerChips(
                                    game, game.serial2player[serial]))
                            if self.what & PacketPokerExplain.CHIPSTACKS:
                                forward_packets.append(
                                    self.explainPlayerChips(
                                        game, game.serial2player[serial]))
                        forward_packets.extend(self.updatePotsChips(game, []))

            elif packet.type == PACKET_POKER_CANCELED:
                if not self.no_display_packets and packet.amount > 0:
                    player = game.getPlayer(packet.serial)
                    if player.bet > 0:
                        forward_packets.extend(
                            self.chipsBet2Pot(game, player, player.bet, 0))
                    if packet.amount > 0:
                        forward_packets.append(
                            self.chipsPot2Player(game, player, packet.amount,
                                                 0, "canceled"))
                game.canceled(packet.serial, packet.amount)
                forward_packets.append(PacketPokerPosition(game_id=game.id))

            elif packet.type == PACKET_POKER_PLAYER_ARRIVE:
                game.addPlayer(packet.serial, packet.seat)
                player = game.getPlayer(packet.serial)
                player.setUserData(DEFAULT_PLAYER_USER_DATA.copy())
                player.name = packet.name
                player.url = packet.url
                player.outfit = packet.outfit
                player.auto_blind_ante = packet.auto_blind_ante
                player.wait_for = packet.wait_for
                player.auto = packet.auto
                if not self.no_display_packets:
                    self.forward_packets.append(
                        PacketPokerSeats(game_id=game.id, seats=game.seats()))

            elif (packet.type == PACKET_POKER_PLAYER_LEAVE
                  or packet.type == PACKET_POKER_TABLE_MOVE):
                game.removePlayer(packet.serial)
                if packet.serial == self.getSerial():
                    self.games.deleteGame(game.id)
                if packet.type == PACKET_POKER_TABLE_MOVE:
                    forward_packets.append(
                        PacketPokerPlayerLeave(game_id=packet.game_id,
                                               serial=packet.serial,
                                               seat=packet.seat))
                if not self.no_display_packets:
                    self.forward_packets.append(
                        PacketPokerSeats(game_id=game.id, seats=game.seats()))

            elif packet.type == PACKET_POKER_PLAYER_SELF:
                (serial_in_position, position_is_obsolete) = game.position_info
                if serial_in_position == self.getSerial():
                    game.position_info = [0, True]
                forward_packets.extend(self.updateBetLimit(game))

            elif packet.type == PACKET_POKER_POSITION:
                if game.isBlindAnteRound():
                    game.setPosition(packet.position)
                forward_packets.remove(packet)

            elif packet.type == PACKET_POKER_SEATS:
                forward_packets.remove(packet)

            elif packet.type == PACKET_POKER_PLAYER_CARDS:
                player = game.getPlayer(packet.serial)
                player.hand.set(packet.cards)

            elif packet.type == PACKET_POKER_BOARD_CARDS:
                game.board.set(packet.cards)

            elif packet.type == PACKET_POKER_DEALER:
                game.setDealer(packet.dealer)

            elif packet.type == PACKET_POKER_SIT_OUT:
                game.sitOut(packet.serial)

            elif packet.type == PACKET_POKER_AUTO_FOLD:
                game.autoPlayer(packet.serial)

            elif packet.type == PACKET_POKER_AUTO_BLIND_ANTE:
                game.autoBlindAnte(packet.serial)

            elif packet.type == PACKET_POKER_NOAUTO_BLIND_ANTE:
                game.noAutoBlindAnte(packet.serial)

            elif packet.type == PACKET_POKER_MUCK_REQUEST:
                game.setMuckableSerials(packet.muckable_serials)

            elif packet.type == PACKET_POKER_SIT:
                game.sit(packet.serial)

            elif packet.type == PACKET_POKER_TIMEOUT_WARNING:
                if not self.setPlayerTimeout(game, packet):
                    forward_packets.remove(packet)

            elif packet.type == PACKET_POKER_TIMEOUT_NOTICE:
                self.unsetPlayerTimeout(game, packet.serial)

            elif packet.type == PACKET_POKER_WAIT_FOR:
                game.getPlayer(packet.serial).wait_for = packet.reason
                forward_packets.remove(packet)

            elif packet.type == PACKET_POKER_IN_GAME:
                game.setStaticPlayerList(packet.players)
                for serial in game.serialsAll():
                    player = game.getPlayer(serial)
                    wait_for = player.wait_for
                    in_game = serial in packet.players
                    if in_game or wait_for:
                        auto = player.isAuto()
                        if not game.isSit(serial):
                            game.sit(serial)
                            forward_packets.append(
                                PacketPokerSit(game_id=game.id, serial=serial))
                        if auto:
                            game.autoPlayer(serial)
                            forward_packets.append(
                                PacketPokerAutoFold(game_id=game.id,
                                                    serial=player.serial))

                        if wait_for:
                            if wait_for == True and not in_game and not game.isRunning(
                            ):
                                #
                                # A player is waiting for the blind (big, late...)
                                # and the server says it will not participate to the
                                # blindAnte round. This only happens when the anteRound
                                # is already finished on the server (i.e. when connecting
                                # to a table in the middle of a game).
                                #
                                player.wait_for = "first_round"
                            else:
                                player.wait_for = wait_for
                            forward_packets.append(
                                PacketPokerWaitFor(game_id=game.id,
                                                   serial=serial,
                                                   reason=wait_for))
                    else:
                        if game.isSit(serial):
                            game.sitOut(serial)
                            forward_packets.append(
                                PacketPokerSitOut(game_id=game.id,
                                                  serial=serial))

            elif packet.type == PACKET_POKER_RAKE:
                game.setRakedAmount(packet.value)

            elif packet.type == PACKET_POKER_WIN:
                if not self.no_display_packets:
                    for serial in packet.serials:
                        forward_packets.append(
                            PacketPokerPlayerWin(serial=serial,
                                                 game_id=game.id))

                if game.winners:
                    #
                    # If we know the winners before an explicit call to the distributeMoney
                    # method, it means that there is no showdown.
                    #
                    if not self.no_display_packets:
                        if game.isGameEndInformationValid():
                            forward_packets.append(
                                PacketPokerShowdown(
                                    game_id=game.id,
                                    showdown_stack=game.showdown_stack))
                        forward_packets.extend(self.packetsPot2Player(game))
                else:
                    game.distributeMoney()

                    winners = game.winners[:]
                    winners.sort()
                    packet.serials.sort()
                    if winners != packet.serials:
                        raise UserWarning, "game.winners %s != packet.serials %s" % (
                            winners, packet.serials)
                    if not self.no_display_packets:
                        if game.isGameEndInformationValid():
                            forward_packets.extend(self.packetsShowdown(game))
                            forward_packets.append(
                                PacketPokerShowdown(
                                    game_id=game.id,
                                    showdown_stack=game.showdown_stack))

                        forward_packets.extend(self.packetsPot2Player(game))
                    game.endTurn()
                forward_packets.append(PacketPokerPosition(game_id=game.id))

            elif packet.type == PACKET_POKER_REBUY:
                forward_packets.remove(packet)
                if game.rebuy(packet.serial, packet.amount):
                    #
                    # If the server says the player rebuys, assume he knows
                    # that the player can rightfully do so and therefore
                    # that the buy_in has already been paid. The client fail
                    # to notice that a player already paid the buy_in
                    # when it connects to a table on which said player has
                    # no chips in front of him.
                    #
                    player = game.getPlayer(packet.serial)
                    player.buy_in_payed = True
                    forward_packets.append(self.updatePlayerChips(
                        game, player))
                    if self.what & PacketPokerExplain.CHIPSTACKS:
                        forward_packets.append(
                            self.explainPlayerChips(game, player))

            elif packet.type == PACKET_POKER_PLAYER_CHIPS:
                player = game.getPlayer(packet.serial)
                if player.buy_in_payed:
                    if player.bet != packet.bet:
                        if self.verbose > 1:
                            self.error(
                                "server says player %d has a bet of %d chips and client thinks it has %d"
                                % (packet.serial, packet.bet, player.bet))
                        player.bet = packet.bet
                    if player.money != packet.money:
                        if self.verbose > 1:
                            self.error(
                                "server says player %d has a money of %d chips and client thinks it has %d"
                                % (packet.serial, packet.money, player.money))
                        player.money = packet.money
                else:
                    #
                    # If server sends chips amount for a player that did not yet pay the buy in
                    #
                    player.bet = packet.bet
                    player.money = packet.money
                    if player.money > 0:
                        player.buy_in_payed = True
                if self.what & PacketPokerExplain.CHIPSTACKS:
                    forward_packets.append(
                        self.explainPlayerChips(game, player))

            elif packet.type == PACKET_POKER_FOLD:
                game.fold(packet.serial)
                if game.isSitOut(packet.serial):
                    forward_packets.append(
                        PacketPokerSitOut(game_id=game.id,
                                          serial=packet.serial))

            elif packet.type == PACKET_POKER_CALL:
                game.call(packet.serial)

            elif packet.type == PACKET_POKER_RAISE:
                game.callNraise(packet.serial, packet.amount)

            elif packet.type == PACKET_POKER_CHECK:
                game.check(packet.serial)

            elif packet.type == PACKET_POKER_BLIND:
                game.blind(packet.serial, packet.amount, packet.dead)

            elif packet.type == PACKET_POKER_BLIND_REQUEST:
                game.setPlayerBlind(packet.serial, packet.state)

            elif packet.type == PACKET_POKER_ANTE:
                game.ante(packet.serial, packet.amount)

            elif packet.type == PACKET_POKER_STATE:
                game.position_info[
                    PokerNetworkGameClient.POSITION_OBSOLETE] = True

                if game.isBlindAnteRound():
                    game.blindAnteRoundEnd()

                if packet.string == "end" and game.state != "null":
                    game.endState()

                #
                # A state change is received at the begining of each
                # betting round. No state change is received when
                # reaching showdown or otherwise terminating the hand.
                #
                if game.isFirstRound():
                    game.initRound()
                else:
                    if not self.no_display_packets:
                        if (packet.string == "end"
                                and game.isSingleUncalledBet(game.side_pots)):
                            forward_packets.extend(self.moveBet2Player(game))
                        else:
                            forward_packets.extend(self.moveBet2Pot(game))

                    if packet.string != "end":
                        game.initRound()

                if not self.no_display_packets:
                    if game.isRunning() and game.cardsDealt(
                    ) and game.downCardsDealtThisRoundCount() > 0:
                        forward_packets.append(
                            PacketPokerDealCards(
                                game_id=game.id,
                                numberOfCards=game.
                                downCardsDealtThisRoundCount(),
                                serials=game.serialsNotFold()))

                if game.isRunning() and game.cardsDealt(
                ) and game.cardsDealtThisRoundCount():
                    for player in game.playersNotFold():
                        cards = player.hand.toRawList()
                        forward_packets.append(
                            PacketPokerPlayerCards(game_id=game.id,
                                                   serial=player.serial,
                                                   cards=cards))

                if not self.no_display_packets:
                    if game.isRunning() and game.cardsDealt(
                    ) and self.getSerial() != 0 and game.isPlaying(
                            self.getSerial()) and (packet.string == "flop" or
                                                   packet.string == "turn" or
                                                   packet.string == "river"):
                        forward_packets.append(
                            PacketPokerPlayerHandStrength(
                                game_id=game.id,
                                serial=self.getSerial(),
                                hand=game.readablePlayerBestHands(
                                    self.getSerial())))

                if (packet.string != "end" and not game.isBlindAnteRound()):
                    if not self.no_display_packets:
                        forward_packets.extend(self.updateBetLimit(game))
                    forward_packets.append(
                        PacketPokerBeginRound(game_id=game.id))

                if game.state != packet.string:
                    self.error("state = %s, expected %s instead " %
                               (game.state, packet.string))

            (serial_in_position, position_is_obsolete) = game.position_info
            if game.isRunning():
                #
                # Build position related packets
                #
                position_changed = serial_in_position != game.getSerialInPosition(
                )
                if position_is_obsolete or position_changed:
                    self_was_in_position = self.getSerial(
                    ) != 0 and serial_in_position == self.getSerial()
                    serial_in_position = game.getSerialInPosition()
                    self_in_position = serial_in_position == self.getSerial()
                    if serial_in_position > 0:
                        if position_changed:
                            forward_packets.append(
                                PacketPokerPosition(game_id=game.id,
                                                    position=game.position,
                                                    serial=serial_in_position))
                        if (self_was_in_position and not self_in_position):
                            self.unsetPlayerTimeout(game, self.getSerial())
                            if not game.isBlindAnteRound(
                            ) or not game.getPlayer(
                                    self.getSerial()).isAutoBlindAnte():
                                forward_packets.append(
                                    PacketPokerSelfLostPosition(
                                        game_id=game.id,
                                        serial=serial_in_position))
                        if ((not self_was_in_position or position_is_obsolete)
                                and self_in_position):
                            if not game.isBlindAnteRound(
                            ) or not game.getPlayer(
                                    self.getSerial()).isAutoBlindAnte():
                                forward_packets.append(
                                    PacketPokerSelfInPosition(
                                        game_id=game.id,
                                        serial=serial_in_position))
                    elif self_was_in_position:
                        self.unsetPlayerTimeout(game, self.getSerial())
                        if not game.isBlindAnteRound() or not game.getPlayer(
                                self.getSerial()).isAutoBlindAnte():
                            forward_packets.append(
                                PacketPokerSelfLostPosition(
                                    game_id=game.id, serial=self.getSerial()))
            else:
                if serial_in_position > 0:
                    if not game.isBlindAnteRound() or not game.getPlayer(
                            self.getSerial()).isAutoBlindAnte():
                        forward_packets.append(
                            PacketPokerSelfLostPosition(
                                game_id=game.id, serial=self.getSerial()))
                    serial_in_position = 0
            position_is_obsolete = False
            game.position_info = [serial_in_position, position_is_obsolete]
            #
            # Build dealer messages
            # Skip state = end because information is missing and will be received by the next packet (WIN)
            #
            if not (packet.type == PACKET_POKER_STATE
                    and packet.string == "end"):
                (subject, messages) = history2messages(
                    game,
                    game.historyGet()[game.history_index:],
                    serial2name=lambda serial: self.serial2name(game, serial))
                if messages or subject:
                    if messages:
                        message = "".join(
                            map(lambda line: "Dealer: " + line + "\n",
                                list(set(messages))))
                        forward_packets.append(
                            PacketPokerChat(game_id=game.id, message=message))
                game.history_index = len(game.historyGet())

        return True
Example #6
0
 def test1(self):
     for (serial, seat) in ((1, 0), (2, 1), (3, 2), (4, 3)):
         self.make_new_player(serial, seat)
     self.game.beginTurn(1)
     self.assertEqual(str(history2messages(self.game, self.game.turn_history, pocket_messages = True)), "('hand #1, holdem, 1-2_20-200_limit', ['2 pays 1 blind', '3 pays 2 blind', 'pre-flop, 4 players', 'Cards player 1: 8s Ah', 'Cards player 2: 3h 6h', 'Cards player 3: As 6s', 'Cards player 4: 6d Qs', '4 calls 2', '1 calls 2', '2 calls 1', '3 checks', 'flop, 4 players', 'Board: 4s 7c 4h', 'Cards player 1: 8s Ah', 'Cards player 2: 3h 6h', 'Cards player 3: As 6s', 'Cards player 4: 6d Qs', '2 checks', '3 checks', '4 checks', '1 checks', 'turn, 4 players', 'Board: 4s 7c 4h 5c', 'Cards player 1: 8s Ah', 'Cards player 2: 3h 6h', 'Cards player 3: As 6s', 'Cards player 4: 6d Qs', '2 raises 4', '3 folds', '4 folds', '1 calls 4', 'river, 2 players', 'Board: 4s 7c 4h 5c Ks', 'Cards player 1: 8s Ah', 'Cards player 2: 3h 6h', '2 raises 4', '1 calls 4', 'Rake 1.20', 'Board: 4s 7c 4h 5c Ks', 'Cards player 1: 8s Ah', 'Cards player 2: 3h 6h', '2 shows Straight Seven to Trey for hi ', '1 mucks loosing hand', '2 wins hi ', '2 receives 24'])")