def test_start_new_round_exclude_no_money_players(self): uuids = ["ruypwwoqwuwdnauiwpefsw", "sqmfwdkpcoagzqxpxnmxwm", "uxrdiwvctvilasinweqven"] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) original = reduce(lambda state, uuid: attach_hole_card_from_deck(state, uuid), uuids, game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) [self.emu.register_player(uuid, FoldMan()) for uuid in uuids] # case1: second player cannot pay small blind finish_state, events = self.emu.apply_action(original, "fold") finish_state["table"].seats.players[0].stack = 11 stacks = [p.stack for p in finish_state["table"].seats.players] game_state, events = self.emu.start_new_round(finish_state) self.eq(2, game_state["table"].dealer_btn) self.eq(1, game_state["next_player"]) self.eq(stacks[1]-sb_amount-ante, game_state["table"].seats.players[1].stack) self.eq(stacks[2]-sb_amount*2-ante, game_state["table"].seats.players[2].stack) self.eq(PayInfo.FOLDED, game_state["table"].seats.players[0].pay_info.status) self.eq(sb_amount*3 + ante*2, GameEvaluator.create_pot(game_state["table"].seats.players)[0]["amount"]) # case2: third player cannot pay big blind finish_state, events = self.emu.apply_action(original, "fold") finish_state["table"].seats.players[1].stack = 16 stacks = [p.stack for p in finish_state["table"].seats.players] game_state, events = self.emu.start_new_round(finish_state) self.eq(2, game_state["table"].dealer_btn) self.eq(0, game_state["next_player"]) self.eq(stacks[0]-sb_amount-ante, game_state["table"].seats.players[0].stack) self.eq(stacks[2]-sb_amount*2-ante, game_state["table"].seats.players[2].stack) self.eq(PayInfo.FOLDED, game_state["table"].seats.players[1].pay_info.status) self.eq(PayInfo.PAY_TILL_END, game_state["table"].seats.players[0].pay_info.status) self.eq(sb_amount*3 + ante*2, GameEvaluator.create_pot(game_state["table"].seats.players)[0]["amount"])
def test_start_new_round_exclude_no_money_players(self): uuids = ["ruypwwoqwuwdnauiwpefsw", "sqmfwdkpcoagzqxpxnmxwm", "uxrdiwvctvilasinweqven"] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) original = reduce(lambda state, uuid: attach_hole_card_from_deck(state, uuid), uuids, game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) [self.emu.register_player(uuid, FoldMan()) for uuid in uuids] # case1: second player cannot pay small blind finish_state, events = self.emu.apply_action(original, "fold") finish_state["table"].seats.players[0].stack = 11 stacks = [p.stack for p in finish_state["table"].seats.players] game_state, events = self.emu.start_new_round(finish_state) self.eq(2, game_state["table"].dealer_btn) self.eq(1, game_state["next_player"]) self.eq(stacks[1] - sb_amount - ante, game_state["table"].seats.players[1].stack) self.eq(stacks[2] - sb_amount * 2 - ante, game_state["table"].seats.players[2].stack) self.eq(PayInfo.FOLDED, game_state["table"].seats.players[0].pay_info.status) self.eq(sb_amount * 3 + ante * 2, GameEvaluator.create_pot(game_state["table"].seats.players)[0]["amount"]) # case2: third player cannot pay big blind finish_state, events = self.emu.apply_action(original, "fold") finish_state["table"].seats.players[1].stack = 16 stacks = [p.stack for p in finish_state["table"].seats.players] game_state, events = self.emu.start_new_round(finish_state) self.eq(2, game_state["table"].dealer_btn) self.eq(0, game_state["next_player"]) self.eq(stacks[0] - sb_amount - ante, game_state["table"].seats.players[0].stack) self.eq(stacks[2] - sb_amount * 2 - ante, game_state["table"].seats.players[2].stack) self.eq(PayInfo.FOLDED, game_state["table"].seats.players[1].pay_info.status) self.eq(PayInfo.PAY_TILL_END, game_state["table"].seats.players[0].pay_info.status) self.eq(sb_amount * 3 + ante * 2, GameEvaluator.create_pot(game_state["table"].seats.players)[0]["amount"])
def encode_pot(self, players): pots = GameEvaluator.create_pot(players) main = { "amount": pots[0]["amount"] } gen_hsh = lambda sidepot: \ { "amount": sidepot["amount"], "eligibles": [p.uuid for p in sidepot["eligibles"]] } side = [ gen_hsh(sidepot) for sidepot in pots[1:] ] return { "main": main, "side": side }
def __showdown(self, state): winners, hand_info, prize_map = GameEvaluator.judge(state["table"]) self.__prize_to_winners(state["table"].seats.players, prize_map) result_message = MessageBuilder.build_round_result_message(state["round_count"], winners, hand_info, state) state["table"].reset() state["street"] += 1 return state, [(-1, result_message)]
def encode_pot(self, players): pots = GameEvaluator.create_pot(players) main = {"amount": pots[0]["amount"]} gen_hsh = lambda sidepot: \ {"amount": sidepot["amount"], "eligibles": [p.uuid for p in sidepot["eligibles"]]} side = [gen_hsh(sidepot) for sidepot in pots[1:]] return {"main": main, "side": side}
def test_find_a_winner(self): mock_eval_hand_return = [0, 1, 0] dummy_players = self.__setup_players() dummy_community = [] with patch('pypokerengine.engine.hand_evaluator.HandEvaluator.eval_hand', side_effect=mock_eval_hand_return): winner = GameEvaluator._GameEvaluator__find_winners_from(dummy_community, dummy_players) self.eq(1, len(winner)) self.true(dummy_players[1] in winner)
def test_find_a_winner(self): mock_eval_hand_return = [0, 1, 0] dummy_players = self.__setup_players() dummy_community = [] with patch('pypokerengine.engine.hand_evaluator.HandEvaluator.eval_hand', side_effect=mock_eval_hand_return): winner = GameEvaluator._GameEvaluator__find_winners_from(dummy_community, dummy_players) self.eq(1, len(winner)) self.true(dummy_players[1] in winner)
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 test_case2(self): players = { "A": self.__create_player_with_pay_info("A", 10, PayInfo.PAY_TILL_END), "B": self.__create_player_with_pay_info("B", 10, PayInfo.PAY_TILL_END), "C": self.__create_player_with_pay_info("C", 7, PayInfo.ALLIN), } pots = GameEvaluator.create_pot(players.values()) self.eq(2, len(pots)) self.__sidepot_check(players, pots[0], 21, ["A", "B", "C"]) self.__sidepot_check(players, pots[1], 6, ["A", "B"])
def test_case2(self): players = { "A": self.__create_player_with_pay_info("A", 10, PayInfo.PAY_TILL_END), "B": self.__create_player_with_pay_info("B", 10, PayInfo.PAY_TILL_END), "C": self.__create_player_with_pay_info("C", 7, PayInfo.ALLIN), } pots = GameEvaluator.create_pot(players.values()) self.eq(2, len(pots)) self.__sidepot_check(players, pots[0], 21, ["A", "B", "C"]) self.__sidepot_check(players, pots[1], 6, ["A", "B"])
def test_judge_without_allin(self): gen_player = lambda acc, _: acc + [self.__create_player_with_pay_info("", 5, PayInfo.PAY_TILL_END)] players = reduce(gen_player, range(3), []) table = self.__setup_table(players) mock_eval_hand_return = [0,1,0]*3 with patch('pypokerengine.engine.hand_evaluator.HandEvaluator.eval_hand', side_effect=mock_eval_hand_return): winner, hand_info, prize_map = GameEvaluator.judge(table) self.eq(1, len(winner)) self.true(players[1] in winner) self.eq(15, prize_map[1])
def test_case3(self): players = { "A": self.__create_player_with_pay_info("A", 20, PayInfo.FOLDED), "B": self.__create_player_with_pay_info("B", 30, PayInfo.PAY_TILL_END), "C": self.__create_player_with_pay_info("C", 7, PayInfo.ALLIN), "D": self.__create_player_with_pay_info("D", 30, PayInfo.PAY_TILL_END), } pots = GameEvaluator.create_pot(players.values()) self.eq(2, len(pots)) self.__sidepot_check(players, pots[0], 28, ["B", "C", "D"]) self.__sidepot_check(players, pots[1], 59, ["B", "D"])
def test_case3(self): players = { "A": self.__create_player_with_pay_info("A", 20, PayInfo.FOLDED), "B": self.__create_player_with_pay_info("B", 30, PayInfo.PAY_TILL_END), "C": self.__create_player_with_pay_info("C", 7, PayInfo.ALLIN), "D": self.__create_player_with_pay_info("D", 30, PayInfo.PAY_TILL_END), } pots = GameEvaluator.create_pot(players.values()) self.eq(2, len(pots)) self.__sidepot_check(players, pots[0], 28, ["B", "C", "D"]) self.__sidepot_check(players, pots[1], 59, ["B", "D"])
def encode_pot(self, players): pots = GameEvaluator.create_pot(players) main = {'amount': pots[0]['amount']} def gen_hsh(sidepot): return { 'amount': sidepot['amount'], 'eligibles': [p.uuid for p in sidepot['eligibles']] } side = [gen_hsh(sidepot) for sidepot in pots[1:]] return {'main': main, 'side': side}
def test_judge_without_allin(self): def gen_player(acc, _): return acc + [self.__create_player_with_pay_info('', 5, PayInfo.PLAY_TILL_END)] players = reduce(gen_player, range(3), []) table = self.__setup_table(players) mock_eval_hand_return = [0, 1, 0] * 3 with patch('pypokerengine.engine.hand_evaluator.HandEvaluator.eval_hand', side_effect=mock_eval_hand_return): winner, hand_info, prize_map = GameEvaluator.judge(table) self.eq(1, len(winner)) self.true(players[1] in winner) self.eq(15, prize_map[1])
def test_judge_with_allin_when_allin_does_not_win(self): players = self.__setup_players_for_judge() table = self.__setup_table(players) mock_eval_hand_return = [2,1,0]*3 + [2,0] + [2] with patch('pypokerengine.engine.hand_evaluator.HandEvaluator.eval_hand', side_effect=mock_eval_hand_return): winner, hand_info, prize_map = GameEvaluator.judge(table) self.eq(2, hand_info[0]["hand"]["hole"]["low"]) self.eq(1, hand_info[1]["hand"]["hole"]["low"]) self.eq(0, hand_info[2]["hand"]["hole"]["low"]) self.eq(100, prize_map[0]) self.eq(0, prize_map[1]) self.eq(0, prize_map[2])
def test_case1(self): players = { 'A': self.__create_player_with_pay_info('A', 50, PayInfo.PLAY_TILL_END), 'B': self.__create_player_with_pay_info('B', 20, PayInfo.ALLIN), 'C': self.__create_player_with_pay_info('C', 30, PayInfo.ALLIN), } pots = GameEvaluator.create_pot(players.values()) self.eq(3, len(pots)) self.__sidepot_check(players, pots[0], 60, ['A', 'B', 'C']) self.__sidepot_check(players, pots[1], 20, ['A', 'C']) self.__sidepot_check(players, pots[2], 20, ['A'])
def test_judge_with_allin_when_allin_does_not_win(self): players = self.__setup_players_for_judge() table = self.__setup_table(players) mock_eval_hand_return = [2, 1, 0] * 3 + [2, 0] + [2] with patch('pypokerengine.engine.hand_evaluator.HandEvaluator.eval_hand', side_effect=mock_eval_hand_return): winner, hand_info, prize_map = GameEvaluator.judge(table) self.eq(2, hand_info[0]['hand']['hole']['low']) self.eq(1, hand_info[1]['hand']['hole']['low']) self.eq(0, hand_info[2]['hand']['hole']['low']) self.eq(100, prize_map[0]) self.eq(0, prize_map[1]) self.eq(0, prize_map[2])
def test_case5(self): players = { "A": self.__create_player_with_pay_info("A", 5, PayInfo.ALLIN), "B": self.__create_player_with_pay_info("B", 10, PayInfo.PAY_TILL_END), "C": self.__create_player_with_pay_info("C", 8, PayInfo.ALLIN), "D": self.__create_player_with_pay_info("D", 10, PayInfo.PAY_TILL_END), "E": self.__create_player_with_pay_info("E", 2, PayInfo.FOLDED) } pots = GameEvaluator.create_pot(players.values()) self.eq(3, len(pots)) self.__sidepot_check(players, pots[0], 22, ["A", "B", "C", "D"]) self.__sidepot_check(players, pots[1], 9, ["B", "C", "D"]) self.__sidepot_check(players, pots[2], 4, ["B", "D"])
def test_case5(self): players = { "A": self.__create_player_with_pay_info("A", 5, PayInfo.ALLIN), "B": self.__create_player_with_pay_info("B", 10, PayInfo.PAY_TILL_END), "C": self.__create_player_with_pay_info("C", 8, PayInfo.ALLIN), "D": self.__create_player_with_pay_info("D", 10, PayInfo.PAY_TILL_END), "E": self.__create_player_with_pay_info("E", 2, PayInfo.FOLDED) } pots = GameEvaluator.create_pot(players.values()) self.eq(3, len(pots)) self.__sidepot_check(players, pots[0], 22, ["A", "B", "C", "D"]) self.__sidepot_check(players, pots[1], 9, ["B", "C", "D"]) self.__sidepot_check(players, pots[2], 4, ["B", "D"])
def test_judge_without_allin_but_winner_folded(self): gen_player = lambda acc, _: acc + [self.__create_player_with_pay_info("", 5, PayInfo.PAY_TILL_END)] players = reduce(gen_player, range(3), []) players[1].pay_info.update_to_fold() table = self.__setup_table(players) mock_eval_hand_return = [0,0]*4 with patch('pypokerengine.engine.hand_evaluator.HandEvaluator.eval_hand', side_effect=mock_eval_hand_return): winner, hand_info, prize_map = GameEvaluator.judge(table) self.eq(2, len(winner)) self.eq("HIGHCARD", hand_info[0]["hand"]["hand"]["strength"]) self.eq("HIGHCARD", hand_info[1]["hand"]["hand"]["strength"]) self.eq(7, prize_map[0]) self.eq(0, prize_map[1]) self.eq(7, prize_map[2])
def test_judge_with_allin_when_allin_wins_case2(self): players = self.__setup_players_for_judge() table = self.__setup_table(players) mock_eval_hand_return = [1, 2, 0] * 3 + [1, 0] + [0] with patch( 'pypokerengine.engine.hand_evaluator.HandEvaluator.eval_hand', side_effect=mock_eval_hand_return): winner, hand_info, prize_map = GameEvaluator.judge(table) self.eq(1, hand_info[0]["hand"]["hole"]["low"]) self.eq(2, hand_info[1]["hand"]["hole"]["low"]) self.eq(0, hand_info[2]["hand"]["hole"]["low"]) self.eq(40, prize_map[0]) self.eq(60, prize_map[1]) self.eq(0, prize_map[2])
def test_case3(self): players = { 'A': self.__create_player_with_pay_info('A', 20, PayInfo.FOLDED), 'B': self.__create_player_with_pay_info('B', 30, PayInfo.PLAY_TILL_END), 'C': self.__create_player_with_pay_info('C', 7, PayInfo.ALLIN), 'D': self.__create_player_with_pay_info('D', 30, PayInfo.PLAY_TILL_END), } pots = GameEvaluator.create_pot(players.values()) self.eq(2, len(pots)) self.__sidepot_check(players, pots[0], 28, ['B', 'C', 'D']) self.__sidepot_check(players, pots[1], 59, ['B', 'D'])
def test_case5(self): players = { 'A': self.__create_player_with_pay_info('A', 5, PayInfo.ALLIN), 'B': self.__create_player_with_pay_info('B', 10, PayInfo.PLAY_TILL_END), 'C': self.__create_player_with_pay_info('C', 8, PayInfo.ALLIN), 'D': self.__create_player_with_pay_info('D', 10, PayInfo.PLAY_TILL_END), 'E': self.__create_player_with_pay_info('E', 2, PayInfo.FOLDED) } pots = GameEvaluator.create_pot(players.values()) self.eq(3, len(pots)) self.__sidepot_check(players, pots[0], 22, ['A', 'B', 'C', 'D']) self.__sidepot_check(players, pots[1], 9, ['B', 'C', 'D']) self.__sidepot_check(players, pots[2], 4, ['B', 'D'])
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_judge_without_allin_but_winner_folded(self): def gen_player(acc, _): return acc + [self.__create_player_with_pay_info('', 5, PayInfo.PLAY_TILL_END)] players = reduce(gen_player, range(3), []) players[1].pay_info.update_to_fold() table = self.__setup_table(players) mock_eval_hand_return = [0, 0] * 4 with patch('pypokerengine.engine.hand_evaluator.HandEvaluator.eval_hand', side_effect=mock_eval_hand_return): winner, hand_info, prize_map = GameEvaluator.judge(table) self.eq(2, len(winner)) self.eq('HIGHCARD', hand_info[0]['hand']['hand']['strength']) self.eq('HIGHCARD', hand_info[1]['hand']['hand']['strength']) self.eq(7.5, prize_map[0]) self.eq(0, prize_map[1]) self.eq(7.5, prize_map[2])
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_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 pot_amount(state): return GameEvaluator.create_pot( state['table'].seats.players)[0]['amount']