예제 #1
0
파일: proto_bot.py 프로젝트: ahendy0/Holdem
def pb_action_to_action(pb_action):
    """
    Convert a Protocol Buffer Action into an Action object.
    """
    action = Action(type='fold')
    keys = [key for key in dir(protocol.Action) if key.isupper()]
    for key in keys:
        if getattr(protocol.Action, key) == pb_action.type:
            action.type = key.lower()
            break
    else:
        raise Exception("Could not decode action type %d" % pb_action.type)
    if hasattr(pb_action, 'amount'):
        action.amount = pb_action.amount
    return action
예제 #2
0
파일: proto_bot.py 프로젝트: bashia/Holdem
def pb_action_to_action(pb_action):
    """
    Convert a Protocol Buffer Action into an Action object.
    """
    action = Action(type='fold')
    keys = [key for key in dir(protocol.Action) if key.isupper()]
    for key in keys:
        if getattr(protocol.Action, key) == pb_action.type:
            action.type = key.lower()
            break
    else:
        raise Exception("Could not decode action type %d" % pb_action.type)
    if hasattr(pb_action, 'amount'):
        action.amount = pb_action.amount
    return action
예제 #3
0
 def run_turn(self, player):
     try:
         return player.turn()
     except Exception, e:
         self.game.send_event(
             player,
             Event('bad_bot',
                   message='bot threw an exception: ' + str(e),
                   action=None))
         return Action('fold')
예제 #4
0
 def action(self, type, amount=None):
     action = Action(type=type)
     if amount is not None:
         action.amount = amount
     return action
예제 #5
0
 def action(self, type, amount=None):
     action = Action(type=type)
     if amount is not None:
         action.amount = amount
     return action
예제 #6
0
    def betting_round(self, n, button, pot):
        player_bets = {}
        self.has_bet = {}
        # TODO: don't let player keep betting if it's just him and all_in guys
        for player in self.players:
            player_bets[player] = 0

        small_blind, big_blind = self.calculate_blinds(button)
        if n == 1:
            current_player = self.get_player(self.players.index(big_blind) + 1)
            player_bets[small_blind] = self.game.small_blind_amount
            player_bets[big_blind] = self.game.big_blind_amount
            current_bet = self.game.big_blind_amount
        else:
            current_player = self.get_player(self.players.index(button) + 1)
            current_bet = 0

        self.game.output("Start of betting round %d" % n)

        while True:
            if len(self.active_players) == 1:
                winner = self.active_players[0]
                self.game.output("Player %s won when everyone else folded" %
                                 winner)
                raise WinByDefault(winner)

            # figure out if this betting round is over
            for player in self.active_players:
                if player not in self.has_bet and player not in self.all_in:
                    break  # found a player that can still make an action
            else:
                break  # all players have bet, folded, or are all in, break out of the while loop

            # if player is all in, he does not get another turn
            if current_player in self.all_in:
                self.game.output("Player is all in, skipping %s" %
                                 current_player)
                current_player = self.next_player(current_player)
                continue

            if current_player not in self.active_players:
                self.game.output("Player has folded, skipping %s" %
                                 current_player)
                current_player = self.next_player(current_player)
                continue

            #GAMESTATE STUFF
            current_player.active_player_count = len(self.active_players)
            current_player.bet_to_player = current_bet - player_bets[
                current_player]
            current_player.pot = self.pot.total

            action = self.run_turn(current_player)

            def warn(message):
                self.game.send_event(
                    current_player,
                    Event('bad_bot', message=message, action=action))

            if action is None or \
                  getattr(action, 'type', None) not in ['fold', 'call', 'raise', 'check'] or \
                  action.type == 'raise' and not hasattr(action, 'amount'):
                warn('invalid action, folding')
                action = Action('fold')

            if action.type == 'raise':
                if action.amount <= 0 or self.game.credits[
                        current_player] < action.amount:
                    warn('invalid raise, calling')
                    action = Action('call')
                else:
                    amount_to_bet = action.amount + (
                        current_bet - player_bets[current_player])
                    if self.game.credits[current_player] < amount_to_bet:
                        warn(
                            'tried to raise more than player possesses, betting maximum'
                        )
                        amount_to_bet = self.game.credits[current_player]
                    if player_bets[
                            current_player] + amount_to_bet > current_bet:
                        current_bet = player_bets[
                            current_player] + amount_to_bet
                    self.bet(current_player, amount_to_bet)
                    player_bets[current_player] += amount_to_bet
                    self.has_bet = {current_player: True}

            if action.type == 'call':
                if current_bet == 0:
                    warn('tried to call on zero bet, checking')
                    action = Action('check')
                elif current_bet == player_bets[current_player]:
                    warn(
                        'tried to call but had already bet that amount, should have checked, checking'
                    )
                    action = Action('check')
                else:
                    amount_to_bet = current_bet - player_bets[current_player]
                    self.bet(current_player, amount_to_bet)
                    player_bets[current_player] += amount_to_bet
                    self.has_bet[current_player] = True

            if action.type == 'check':
                if current_bet != player_bets[current_player]:
                    warn('tried to check when not up to current bet, calling')
                    # TODO: action = Action('call')
                    amount_to_bet = current_bet - player_bets[current_player]
                    self.bet(current_player, amount_to_bet)
                    player_bets[current_player] += amount_to_bet
                    self.has_bet[current_player] = True
                else:
                    self.has_bet[current_player] = True

            if action.type == 'fold':
                self.active_players.remove(current_player)

            self.game.broadcast_event(
                Event('action',
                      action=action,
                      player_id=self.game.id[current_player]))
            current_player = self.next_player(current_player)

        self.game.output("End of betting round %d" % n)
예제 #7
0
 def turn(self):
     try:
         return self.bot.turn()
     except Exception, e:
         print "bot threw exception:",e
         return Action('fold')