def __start_round(self): table = self.__setup_table() round_count = 1 small_blind_amount = 5 ante = 0 return RoundManager.start_new_round(round_count, small_blind_amount, ante, table)
def test_add_amount_calculation_when_raise_on_ante(self): table = self.__setup_table() def pot_amount(state): return GameEvaluator.create_pot( state['table'].seats.players)[0]['amount'] def stack_check(expected, state): return self.eq(expected, [p.stack for p in state['table'].seats.players]) start_state, _ = RoundManager.start_new_round(1, 10, 5, table) self.eq(45, pot_amount(start_state)) stack_check([85, 75, 95], start_state) folded_state, _ = RoundManager.apply_action(start_state, 'fold', 0) called_state, _ = RoundManager.apply_action(folded_state, 'call', 20) self.eq(55, pot_amount(called_state)) stack_check([85, 75, 95], start_state) called_state, _ = RoundManager.apply_action(start_state, 'call', 20) self.eq( 20, called_state['table'].seats.players[2].action_histories[-1] ['paid']) self.eq(65, pot_amount(called_state)) raised_state, _ = RoundManager.apply_action(start_state, 'raise', 40) self.eq( 40, raised_state['table'].seats.players[2].action_histories[-1] ['paid']) self.eq(85, pot_amount(raised_state))
def test_everyone_agree_logic_regression(self): players = [Player("uuid%d" % i, 100) for i in range(4)] players[0].stack = 150 players[1].stack = 150 players[2].stack = 50 players[3].stack = 50 deck = Deck(cheat=True, cheat_card_ids=range(1, 53)) table = Table(cheat_deck=deck) for player in players: table.seats.sitdown(player) table.dealer_btn = 3 table.set_blind_pos(0, 1) state, _ = RoundManager.start_new_round(1, 5, 0, table) state, _ = RoundManager.apply_action(state, "raise", 15) state, _ = RoundManager.apply_action(state, "raise", 20) state, _ = RoundManager.apply_action(state, "raise", 25) state, _ = RoundManager.apply_action(state, "raise", 30) state, _ = RoundManager.apply_action(state, "raise", 50) state, _ = RoundManager.apply_action(state, "call", 50) state, _ = RoundManager.apply_action(state, "raise", 125) state, _ = RoundManager.apply_action(state, "call", 125) state, _ = RoundManager.apply_action(state, "fold", 0) state, _ = RoundManager.apply_action(state, "fold", 0) self.eq(Const.Street.FINISHED, state["street"])
def test_collect_ante_skip_loser(self): ante = 10 sb_amount = 5 table = self.__setup_table() table.seats.players[2].stack = 0 table.seats.players[2].pay_info.status = PayInfo.FOLDED state, _ = RoundManager.start_new_round(1, sb_amount, ante, table) players = state["table"].seats.players self.eq(sb_amount+sb_amount*2+ante*2, GameEvaluator.create_pot(players)[0]["amount"])
def test_collect_ante_skip_loser(self): ante = 10 sb_amount = 5 table = self.__setup_table() table.seats.players[2].stack = 0 table.seats.players[2].pay_info.status = PayInfo.FOLDED state, _ = RoundManager.start_new_round(1, sb_amount, ante, table) players = state["table"].seats.players self.eq(sb_amount + sb_amount * 2 + ante * 2, GameEvaluator.create_pot(players)[0]["amount"])
def _start_next_round(self, round_count, blind_structure, table): table.shift_dealer_btn() small_blind, ante = _get_forced_bet_amount(round_count, blind_structure) table = _exclude_short_of_money_players(table, ante, small_blind) if self._has_game_finished(round_count, table, self.config['max_round']): finished_state = { 'table': table } game_result_msg = _gen_game_result_message(table, self.config) msgs = _parse_broadcast_destination([game_result_msg], table) return finished_state, msgs else: return RoundManager.start_new_round(round_count, small_blind, ante, table)
def play_round(self, round_count, blind_amount, ante, table): state, msgs = RoundManager.start_new_round(round_count, blind_amount, ante, table) while True: self.__message_check(msgs, state['street']) if state['street'] != Const.Street.FINISHED: # continue the round action, bet_amount = self.__publish_messages(msgs) state, msgs = RoundManager.apply_action(state, action, bet_amount) else: # finish the round after publish round result self.__publish_messages(msgs) break return state['table']
def play_round(self, round_count, blind_amount, ante, table): state, msgs = RoundManager.start_new_round(round_count, blind_amount, ante, table) while True: self.__message_check(msgs, state["street"]) if state["street"] != Const.Street.FINISHED: # continue the round action, bet_amount = self.__publish_messages(msgs) state, msgs = RoundManager.apply_action(state, action, bet_amount) else: # finish the round after publish round result self.__publish_messages(msgs) break return state["table"]
def _start_next_round(self, round_count, blind_structure, table): table.shift_dealer_btn() small_blind, ante = _get_forced_bet_amount(round_count, blind_structure) table = _exclude_short_of_money_players(table, ante, small_blind) if self._has_game_finished(round_count, table, self.config['max_round']): finished_state = {'table': table} game_result_msg = _gen_game_result_message(table, self.config) msgs = _parse_broadcast_destination([game_result_msg], table) return finished_state, msgs else: return RoundManager.start_new_round(round_count, small_blind, ante, table)
def test_collect_ante(self): ante = 10 sb_amount = 5 table = self.__setup_table() state, _ = RoundManager.start_new_round(1, sb_amount, ante, table) players = state["table"].seats.players self.eq(100-sb_amount-ante, players[0].stack) self.eq(100-sb_amount*2-ante, players[1].stack) self.eq(100-ante, players[2].stack) self.eq("ANTE", players[0].action_histories[0]["action"]) self.eq("ANTE", players[1].action_histories[0]["action"]) self.eq("ANTE", players[2].action_histories[0]["action"]) self.eq(sb_amount+ante, players[0].pay_info.amount) self.eq(sb_amount*2+ante, players[1].pay_info.amount) self.eq(ante, players[2].pay_info.amount) self.eq(sb_amount+sb_amount*2+ante*3, GameEvaluator.create_pot(players)[0]["amount"])
def test_collect_ante(self): ante = 10 sb_amount = 5 table = self.__setup_table() state, _ = RoundManager.start_new_round(1, sb_amount, ante, table) players = state["table"].seats.players self.eq(100 - sb_amount - ante, players[0].stack) self.eq(100 - sb_amount * 2 - ante, players[1].stack) self.eq(100 - ante, players[2].stack) self.eq("ANTE", players[0].action_histories[0]["action"]) self.eq("ANTE", players[1].action_histories[0]["action"]) self.eq("ANTE", players[2].action_histories[0]["action"]) self.eq(sb_amount + ante, players[0].pay_info.amount) self.eq(sb_amount * 2 + ante, players[1].pay_info.amount) self.eq(ante, players[2].pay_info.amount) self.eq(sb_amount + sb_amount * 2 + ante * 3, GameEvaluator.create_pot(players)[0]["amount"])
def start_new_round(self, game_state): round_count = game_state["round_count"] + 1 ante, sb_amount = self.game_rule["ante"], self.game_rule["sb_amount"] deepcopy = deepcopy_game_state(game_state) deepcopy_table = deepcopy["table"] deepcopy_table.shift_dealer_btn() ante, sb_amount = update_blind_level(ante, sb_amount, round_count, self.blind_structure) deepcopy_table = exclude_short_of_money_players(deepcopy_table, ante, sb_amount) is_game_finished = len([1 for p in deepcopy_table.seats.players if p.is_active()])==1 if is_game_finished: return deepcopy, self._generate_game_result_event(deepcopy) new_state, messages = RoundManager.start_new_round(round_count, sb_amount, ante, deepcopy_table) events = [self.create_event(message[1]["message"]) for message in messages] events = [e for e in events if e] return new_state, events
def test_add_amount_calculationl_when_raise_on_ante(self): table = self.__setup_table() pot_amount = lambda state: GameEvaluator.create_pot(state["table"].seats.players)[0]["amount"] stack_check = lambda expected, state: self.eq(expected, [p.stack for p in state["table"].seats.players]) start_state, _ = RoundManager.start_new_round(1, 10, 5, table) self.eq(45, pot_amount(start_state)) stack_check([85, 75, 95], start_state) folded_state, _ = RoundManager.apply_action(start_state, "fold", 0) called_state, _ = RoundManager.apply_action(folded_state, "call", 20) self.eq(55, pot_amount(called_state)) stack_check([85, 75, 95], start_state) called_state, _ = RoundManager.apply_action(start_state, "call", 20) self.eq(20, called_state["table"].seats.players[2].action_histories[-1]["paid"]) self.eq(65, pot_amount(called_state)) raised_state, _ = RoundManager.apply_action(start_state, "raise", 30) self.eq(30, raised_state["table"].seats.players[2].action_histories[-1]["paid"]) self.eq(75, pot_amount(raised_state))
def test_add_amount_calculationl_when_raise_on_ante(self): table = self.__setup_table() pot_amount = lambda state: GameEvaluator.create_pot(state["table"].seats.players)[0]["amount"] stack_check = lambda expected, state: self.eq(expected, [p.stack for p in state["table"].seats.players]) start_state, _ = RoundManager.start_new_round(1, 10, 5, table) self.eq(45, pot_amount(start_state)) stack_check([85, 75, 95], start_state) folded_state, _ = RoundManager.apply_action(start_state, "fold", 0) called_state, _ = RoundManager.apply_action(folded_state, "call", 20) self.eq(55, pot_amount(called_state)) stack_check([85, 75, 95], start_state) called_state, _ = RoundManager.apply_action(start_state, "call", 20) self.eq(20, called_state["table"].seats.players[2].action_histories[-1]["paid"]) self.eq(65, pot_amount(called_state)) raised_state, _ = RoundManager.apply_action(start_state, "raise", 30) self.eq(30, raised_state["table"].seats.players[2].action_histories[-1]["paid"]) self.eq(75, pot_amount(raised_state))
def play_round(self, round_count, blind_amount, ante, table): state, msgs = RoundManager.start_new_round(round_count, blind_amount, ante, table) while True: self.__message_check(msgs, state["street"]) if state["street"] != Const.Street.FINISHED: # continue the round answer = self.__publish_messages(msgs) bot_info = None if len(answer) == 3: action, bet_amount, bot_info = answer else: action, bet_amount = answer state, msgs = RoundManager.apply_action(state, action, bet_amount, bot_info=bot_info) else: # finish the round after publish round result self.__publish_messages(msgs) break return state["table"]
def test_skip_asking_to_allin_player(self): state, _ = self.__start_round() # Round 1 state, _ = RoundManager.apply_action(state, 'call', 10) state, _ = RoundManager.apply_action(state, 'fold', 0) state, _ = RoundManager.apply_action(state, 'call', 10) state, _ = RoundManager.apply_action(state, 'raise', 50) state, _ = RoundManager.apply_action(state, 'call', 50) state, _ = RoundManager.apply_action(state, 'fold', 0) self.eq([95, 40, 165], [p.stack for p in state['table'].seats.players]) # Round 2 state['table'].shift_dealer_btn() state['table'].set_blind_pos(1, 2) state, _ = RoundManager.start_new_round(2, 5, 0, state['table']) state, _ = RoundManager.apply_action(state, 'raise', 40) state, _ = RoundManager.apply_action(state, 'call', 40) state, _ = RoundManager.apply_action(state, 'raise', 70) state, msgs = RoundManager.apply_action(state, 'call', 70) self.eq([25, 0, 95], [p.stack for p in state['table'].seats.players]) self.eq(1, state['street']) self.eq('uuid2', msgs[-1][0])
def test_skip_asking_to_allin_player(self): state, _ = self.__start_round() # Round 1 state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "fold", 0) state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "raise", 50) state, _ = RoundManager.apply_action(state, "call", 50) state, _ = RoundManager.apply_action(state, "fold", 0) self.eq([95, 40, 165], [p.stack for p in state["table"].seats.players]) # Round 2 state["table"].shift_dealer_btn() state["table"].set_blind_pos(1, 2) state, _ = RoundManager.start_new_round(2, 5, 0, state["table"]) state, _ = RoundManager.apply_action(state, "raise", 40) state, _ = RoundManager.apply_action(state, "call", 40) state, _ = RoundManager.apply_action(state, "raise", 70) state, msgs = RoundManager.apply_action(state, "call", 70) self.eq([25, 0, 95], [p.stack for p in state["table"].seats.players]) self.eq(1, state["street"]) self.eq("uuid2", msgs[-1][0])
def test_skip_asking_to_allin_player(self): state, _ = self.__start_round() # Round 1 state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "fold", 0) state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "raise", 50) state, _ = RoundManager.apply_action(state, "call", 50) state, _ = RoundManager.apply_action(state, "fold", 0) self.eq([95, 40, 165], [p.stack for p in state["table"].seats.players]) # Round 2 state["table"].shift_dealer_btn() state["table"].set_blind_pos(1, 2) state, _ = RoundManager.start_new_round(2, 5, 0, state["table"]) state, _ = RoundManager.apply_action(state, "raise", 40) state, _ = RoundManager.apply_action(state, "call", 40) state, _ = RoundManager.apply_action(state, "raise", 70) state, msgs = RoundManager.apply_action(state, "call", 70) self.eq([25, 0, 95], [p.stack for p in state["table"].seats.players]) self.eq(1, state["street"]) self.eq("uuid2", msgs[-1][0])
def test_when_only_one_player_is_waiting_ask(self): state, _ = self.__start_round() # Round 1 state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "fold", 0) state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "call", 0) state, _ = RoundManager.apply_action(state, "raise", 50) state, _ = RoundManager.apply_action(state, "call", 50) state, _ = RoundManager.apply_action(state, "fold", 0) self.eq([95, 40, 165], [p.stack for p in state["table"].seats.players]) # Round 2 state["table"].shift_dealer_btn() state, _ = RoundManager.start_new_round(2, 5, 0, state["table"]) state, _ = RoundManager.apply_action(state, "raise", 40) state, _ = RoundManager.apply_action(state, "call", 40) state, _ = RoundManager.apply_action(state, "raise", 70) state, _ = RoundManager.apply_action(state, "call", 70) state, _ = RoundManager.apply_action(state, "call", 0) state, _ = RoundManager.apply_action(state, "raise", 10) state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "raise", 85) state, _ = RoundManager.apply_action(state, "call", 85)
def test_when_only_one_player_is_waiting_ask(self): state, _ = self.__start_round() # Round 1 state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "fold", 0) state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "call", 0) state, _ = RoundManager.apply_action(state, "raise", 50) state, _ = RoundManager.apply_action(state, "call", 50) state, _ = RoundManager.apply_action(state, "fold", 0) self.eq([95, 40, 165], [p.stack for p in state["table"].seats.players]) # Round 2 state["table"].shift_dealer_btn() state, _ = RoundManager.start_new_round(2, 5, 0, state["table"]) state, _ = RoundManager.apply_action(state, "raise", 40) state, _ = RoundManager.apply_action(state, "call", 40) state, _ = RoundManager.apply_action(state, "raise", 70) state, _ = RoundManager.apply_action(state, "call", 70) state, _ = RoundManager.apply_action(state, "call", 0) state, _ = RoundManager.apply_action(state, "raise", 10) state, _ = RoundManager.apply_action(state, "call", 10) state, _ = RoundManager.apply_action(state, "raise", 85) state, _ = RoundManager.apply_action(state, "call", 85)
def test_when_only_one_player_is_waiting_ask(self): state, _ = self.__start_round() # Round 1 state, _ = RoundManager.apply_action(state, 'call', 10) state, _ = RoundManager.apply_action(state, 'fold', 0) state, _ = RoundManager.apply_action(state, 'call', 10) state, _ = RoundManager.apply_action(state, 'call', 0) state, _ = RoundManager.apply_action(state, 'raise', 50) state, _ = RoundManager.apply_action(state, 'call', 50) state, _ = RoundManager.apply_action(state, 'fold', 0) self.eq([95, 40, 165], [p.stack for p in state['table'].seats.players]) # Round 2 state['table'].shift_dealer_btn() state, _ = RoundManager.start_new_round(2, 5, 0, state['table']) state, _ = RoundManager.apply_action(state, 'raise', 40) state, _ = RoundManager.apply_action(state, 'call', 40) state, _ = RoundManager.apply_action(state, 'fold', 0) state, _ = RoundManager.apply_action(state, 'call', 70) state, _ = RoundManager.apply_action(state, 'call', 0) state, _ = RoundManager.apply_action(state, 'raise', 10) state, _ = RoundManager.apply_action(state, 'call', 10) state, _ = RoundManager.apply_action(state, 'raise', 85) state, _ = RoundManager.apply_action(state, 'fold', 0)
def test_everyone_agree_logic_regression(self): players = [Player("uuid%d" % i, 100) for i in range(4)] players[0].stack = 150 players[1].stack = 150 players[2].stack = 50 players[3].stack = 50 deck = Deck(cheat=True, cheat_card_ids=range(1,53)) table = Table(cheat_deck=deck) for player in players: table.seats.sitdown(player) table.dealer_btn = 3 table.set_blind_pos(0, 1) state, _ = RoundManager.start_new_round(1, 5, 0, table) state, _ = RoundManager.apply_action(state, "raise", 15) state, _ = RoundManager.apply_action(state, "raise", 20) state, _ = RoundManager.apply_action(state, "raise", 25) state, _ = RoundManager.apply_action(state, "raise", 30) state, _ = RoundManager.apply_action(state, "raise", 50) state, _ = RoundManager.apply_action(state, "call", 50) state, _ = RoundManager.apply_action(state, "raise", 125) state, _ = RoundManager.apply_action(state, "call", 125) state, _ = RoundManager.apply_action(state, "fold", 0) state, _ = RoundManager.apply_action(state, "fold", 0) self.eq(Const.Street.FINISHED, state["street"])
def __start_round(self): table = self.__setup_table() round_count = 1 small_blind_amount = 5 ante = 0 return RoundManager.start_new_round(round_count, small_blind_amount, ante, table)