def test_serialization(self): self.__sitdown_players() serial = self.seats.serialize() restored = Seats.deserialize(serial) for i in range(len(self.seats.players)): self.eq(Player.serialize(self.seats.players[i]), Player.serialize(restored.players[i]))
def setUp(self): self.p1 = Player(uuid='uuid1', initial_stack=100, name='hoge') self.p1_algo = Mock() self.p2 = Player(uuid='uuid2', initial_stack=100, name='fuga') self.p2_algo = Mock() self.mh = MessageHandler() self.mh.register_algorithm(self.p1.uuid, self.p1_algo) self.mh.register_algorithm(self.p2.uuid, self.p2_algo)
def __create_blind_player(self, small_blind=True): name = "sb" if small_blind else "bb" blind = 5 if small_blind else 10 player = Player("uuid", 100, name=name) player.add_action_history(Const.Action.RAISE, blind, 5) player.collect_bet(blind) player.pay_info.update_by_pay(blind) return player
def setUp(self): self.p1 = Player(uuid="uuid1", initial_stack=100, name="hoge") self.p1_algo = Mock() self.p2 = Player(uuid="uuid2", initial_stack=100, name="fuga") self.p2_algo = Mock() self.mh = MessageHandler() self.mh.register_algorithm(self.p1.uuid, self.p1_algo) self.mh.register_algorithm(self.p2.uuid, self.p2_algo)
def __setup_players_with_table(self): p1 = Player('uuid1', 100) p2 = Player('uuid2', 100) p3 = Player('uuid3', 100) p2.pay_info.update_to_fold() p3.pay_info.update_to_allin() table = Table() for player in [p1, p2, p3]: table.seats.sitdown(player) return table
def receive_game_start_message(self, game_info): player0_uuid = game_info['seats'][0]['uuid'] player0_stack = game_info['seats'][0]['stack'] player0_name = game_info['seats'][0]['name'] player1_uuid = game_info['seats'][1]['uuid'] player1_stack = game_info['seats'][1]['stack'] player1_name = game_info['seats'][1]['name'] player0 = Player(player0_uuid, player0_stack, player0_name) player1 = Player(player1_uuid, player1_stack, player1_name) self.players = [player0, player1] self.oppo_committed_amt = 0 self.player_committed_amt = 0 self.num_raises = 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 _restore_seats(seats_info, action_histories): players = [Player(info["uuid"], info["stack"], info["name"]) for info in seats_info] players_state = [info["state"] for info in seats_info] _restore_action_histories_on_players(players, action_histories) _restore_pay_info_on_players(players, players_state, action_histories) seats = Seats() seats.players = players return seats
def __setup_table(self): players = [Player("uuid%d" % i, 100) for i in range(3)] 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 = 2 table.set_blind_pos(0, 1) return table
def _restore_seats(seats_info, action_histories): players = [ Player(info['uuid'], info['stack'], info['name']) for info in seats_info ] players_state = [info['state'] for info in seats_info] _restore_action_histories_on_players(players, action_histories) _restore_pay_info_on_players(players, players_state, action_histories) seats = Seats() seats.players = players return seats
def test_serialization(self): player = self.__setup_player_for_serialization() serial = player.serialize() restored = Player.deserialize(serial) self.eq(player.name, restored.name) self.eq(player.uuid, restored.uuid) self.eq(player.stack, restored.stack) self.eq(player.hole_card, restored.hole_card) self.eq(player.action_histories, restored.action_histories) self.eq(player.round_action_histories, restored.round_action_histories) self.eq(player.pay_info.amount, restored.pay_info.amount) self.eq(player.pay_info.status, restored.pay_info.status)
def start_game(self, players_info, game_config): self.config = game_config # setup table table = Table() for uuid, name in players_info.items(): player = Player(uuid, game_config['initial_stack'], name) table.seats.sitdown(player) # start the first round state, msgs = self._start_new_round(1, game_config['blind_structure'], table) self.current_state = state return _parse_broadcast_destination(msgs, self.current_state['table'])
def generate_initial_game_state(self, players_info): table = Table() for uuid, info in players_info.items(): table.seats.sitdown(Player(uuid, info["stack"], info["name"])) table.dealer_btn = len(table.seats.players) - 1 return { "round_count": 0, "small_blind_amount": self.game_rule["sb_amount"], "street": Const.Street.PREFLOP, "next_player": None, "table": table }
def _create_blind_player(small_blind=True): name = 'sb' if small_blind else 'bb' blind = _SB_AMOUNT if small_blind else _SB_AMOUNT * 2 player = Player('uuid', 100, name=name) player.add_action_history(Const.Action.RAISE, blind, 5) player.collect_bet(blind) player.pay_info.update_by_pay(blind) return player
def test_need_amount_after_ante(self): # situation => SB=$5 (players[0]), BB=$10 (players[1]), ANTE=$3 players = [Player("uuid", 100, name="name") for _ in range(3)] for player in players: player.collect_bet(3) player.add_action_history(Const.Action.ANTE, 3) player.pay_info.update_by_pay(3) players[0].collect_bet(5) players[0].add_action_history(Const.Action.SMALL_BLIND, sb_amount=5) players[0].pay_info.update_by_pay(5) players[1].collect_bet(10) players[1].add_action_history(Const.Action.BIG_BLIND, sb_amount=5) players[1].pay_info.update_by_pay(10) def set_stack(stacks, ps): for stack, p in zip(stacks, ps): p.stack = stack set_stack([7, 7, 7], players) self.eq(("call", 10), ActionChecker.correct_action(players, 0, 5, "call", 10)) self.eq(("call", 10), ActionChecker.correct_action(players, 1, 5, "call", 10)) self.eq(("call", 7), ActionChecker.correct_action(players, 2, 5, "call", 10)) self.true(ActionChecker.is_allin(players[2], "call", 8)) self.false(ActionChecker.is_allin(players[2], "raise", 10)) self.eq(5, ActionChecker.need_amount_for_action(players[0], 10)) self.eq(0, ActionChecker.need_amount_for_action(players[1], 10)) self.eq(10, ActionChecker.need_amount_for_action(players[2], 10)) set_stack([12, 12, 12], players) actions = ActionChecker.legal_actions(players, 2, 5) self.eq(-1, actions[2]["amount"]["max"]) set_stack([10, 5, 12], players) self.eq(("raise", 15), ActionChecker.correct_action(players, 0, 5, "raise", 15)) self.eq(("raise", 15), ActionChecker.correct_action(players, 1, 5, "raise", 15)) self.eq(("fold", 0), ActionChecker.correct_action(players, 2, 5, "raise", 15))
def setup_table(): table = Table() players = [Player("uuid%d" % i, 100, "hoge") for i in range(3)] table.seats.players = players table.add_community_card(Card.from_id(1)) table.dealer_btn = 2 table.set_blind_pos(2, 0) p1, p2, p3 = table.seats.players p3.add_action_history(Const.Action.RAISE, 10, 5) p1.add_action_history(Const.Action.FOLD) p2.add_action_history(Const.Action.RAISE, 20, 10) p3.add_action_history(Const.Action.CALL, 20) [ p.save_street_action_histories(Const.Street.PREFLOP) for p in [p1, p2, p3] ] p3.add_action_history(Const.Action.CALL, 5) p2.add_action_history(Const.Action.RAISE, 5, 5) return table
def __create_player_with_pay_info(self, name, amount, status): player = Player('uuid', 100, name) player.pay_info.amount = amount player.pay_info.status = status return player
def __setup_player(self): return Player('uuid', 100, 'hoge')
def deserialize(self, serial): seats = self() seats.players = [Player.deserialize(s) for s in serial] return seats
def __escort_player_to_table(self, player_name): uuid = self.__fetch_uuid() player = Player(uuid, self.initial_stack, player_name) self.table.seats.sitdown(player) return uuid
def __setup_players(self): return reduce(lambda acc, _: acc + [Player("uuid", 100)], range(3), [])
def __setup_player(self): self.player = Player('uuid', 100) self.player.add_holecard([Card.from_id(cid + 1) for cid in range(2)]) self.player.add_action_history(Const.Action.CALL, 10) self.player.pay_info.update_to_fold()
def __setup_clean_players(self): return [Player("uuid", 100) for _ in range(2)]
def setUp(self): self.player = Player("uuid", 100)
class PlayerTest(BaseUnitTest): def setUp(self): self.player = Player("uuid", 100) def test_add_holecard(self): cards = [Card.from_id(cid) for cid in range(1,3)] self.player.add_holecard(cards) self.true(cards[0] in self.player.hole_card) self.true(cards[1] in self.player.hole_card) @raises(ValueError) def test_add_single_hole_card(self): self.player.add_holecard([Card.from_id(1)]) @raises(ValueError) def test_add_too_many_hole_card(self): self.player.add_holecard([Card.from_id(cid) for cid in range(1,4)]) @raises(ValueError) def test_add_hole_card_twice(self): self.player.add_holecard([Card.from_id(cid) for cid in range(1,3)]) self.player.add_holecard([Card.from_id(cid) for cid in range(1,3)]) def test_clear_holecard(self): self.player.add_holecard([Card.from_id(cid) for cid in range(1,3)]) self.player.clear_holecard() self.eq(0, len(self.player.hole_card)) def test_append_chip(self): self.player.append_chip(10) self.eq(110, self.player.stack) def test_collect_bet(self): self.player.collect_bet(10) self.eq(90, self.player.stack) @raises(ValueError) def test_collect_too_much_bet(self): self.player.collect_bet(200) def test_is_active(self): self.player.pay_info.update_by_pay(10) self.true(self.player.is_active()) def test_if_allin_player_is_active(self): self.player.pay_info.update_to_allin() self.true(self.player.is_active()) def test_if_folded_player_is_not_active(self): self.player.pay_info.update_to_fold() self.false(self.player.is_active()) def test_if_no_money_player_is_active(self): self.player.collect_bet(100) self.true(self.player.is_active()) def test_is_waiting_ask(self): self.player.pay_info.update_by_pay(10) self.true(self.player.is_waiting_ask()) def test_if_allin_player_is_not_waiting_ask(self): self.player.pay_info.update_to_allin() self.false(self.player.is_waiting_ask()) def test_if_folded_player_is_not_waiting_ask(self): self.player.pay_info.update_to_fold() self.false(self.player.is_waiting_ask()) def test_add_fold_action_history(self): self.player.add_action_history(Const.Action.FOLD) self.eq("FOLD", self.player.action_histories[-1]["action"]) def test_add_call_action_history(self): self.player.add_action_history(Const.Action.CALL, 10) action = self.player.action_histories[-1] self.eq("CALL", action["action"]) self.eq(10, action["amount"]) self.eq(10, action["paid"]) def test_add_call_action_history_after_paid(self): self.player.add_action_history(Const.Action.CALL, 10) self.player.add_action_history(Const.Action.CALL, 20) action = self.player.action_histories[-1] self.eq(20, action["amount"]) self.eq(10, action["paid"]) def test_add_raise_action_history(self): self.player.add_action_history(Const.Action.RAISE, 10, 5) action = self.player.action_histories[-1] self.eq("RAISE", action["action"]) self.eq(10, action["amount"]) self.eq(10, action["paid"]) self.eq(5, action["add_amount"]) def test_add_raise_action_history_after_paid(self): self.player.add_action_history(Const.Action.CALL, 10) self.player.add_action_history(Const.Action.RAISE, 20, 10) action = self.player.action_histories[-1] self.eq(20, action["amount"]) self.eq(10, action["paid"]) def test_add_small_blind_history(self): self.player.add_action_history(Const.Action.SMALL_BLIND, sb_amount=5) action = self.player.action_histories[-1] self.eq("SMALLBLIND", action["action"]) self.eq(5, action["amount"]) self.eq(5, action["add_amount"]) def test_add_big_blind_history(self): self.player.add_action_history(Const.Action.BIG_BLIND, sb_amount=5) action = self.player.action_histories[-1] self.eq("BIGBLIND", action["action"]) self.eq(10, action["amount"]) self.eq(5, action["add_amount"]) def test_add_ante_history(self): self.player.add_action_history(Const.Action.ANTE, 10) action = self.player.action_histories[-1] self.eq("ANTE", action["action"]) self.eq(10, action["amount"]) @raises(AssertionError) def test_add_empty_ante_history(self): self.player.add_action_history(Const.Action.ANTE, 0) def test_save_street_action_histories(self): self.assertIsNone(self.player.round_action_histories[Const.Street.PREFLOP]) self.player.add_action_history(Const.Action.BIG_BLIND, sb_amount=5) self.player.save_street_action_histories(Const.Street.PREFLOP) self.eq(1, len(self.player.round_action_histories[Const.Street.PREFLOP])) self.eq("BIGBLIND", self.player.round_action_histories[Const.Street.PREFLOP][0]["action"]) self.eq(0, len(self.player.action_histories)) def test_clear_action_histories(self): self.player.add_action_history(Const.Action.BIG_BLIND, sb_amount=5) self.player.save_street_action_histories(Const.Street.PREFLOP) self.player.add_action_history(Const.Action.CALL, 10) self.assertIsNotNone(0, len(self.player.round_action_histories[Const.Street.PREFLOP])) self.neq(0, len(self.player.action_histories)) self.player.clear_action_histories() self.assertIsNone(self.player.round_action_histories[Const.Street.PREFLOP]) self.eq(0, len(self.player.action_histories)) def test_paid_sum(self): self.eq(0, self.player.paid_sum()) self.player.add_action_history(Const.Action.BIG_BLIND, sb_amount=5) self.eq(10, self.player.paid_sum()) self.player.clear_action_histories() self.eq(0, self.player.paid_sum()) self.player.add_action_history(Const.Action.ANTE, 3) self.eq(0, self.player.paid_sum()) self.player.add_action_history(Const.Action.BIG_BLIND, sb_amount=5) self.eq(10, self.player.paid_sum()) def test_serialization(self): player = self.__setup_player_for_serialization() serial = player.serialize() restored = Player.deserialize(serial) self.eq(player.name, restored.name) self.eq(player.uuid, restored.uuid) self.eq(player.stack, restored.stack) self.eq(player.hole_card, restored.hole_card) self.eq(player.action_histories, restored.action_histories) self.eq(player.round_action_histories, restored.round_action_histories) self.eq(player.pay_info.amount, restored.pay_info.amount) self.eq(player.pay_info.status, restored.pay_info.status) def __setup_player_for_serialization(self): player = Player("uuid", 50, "hoge") player.add_holecard([Card.from_id(cid) for cid in range(1,3)]) player.add_action_history(Const.Action.SMALL_BLIND, sb_amount=5) player.save_street_action_histories(Const.Street.PREFLOP) player.add_action_history(Const.Action.CALL, 10) player.add_action_history(Const.Action.RAISE, 10, 5) player.add_action_history(Const.Action.FOLD) player.pay_info.update_by_pay(15) player.pay_info.update_to_fold() return player
def __setup_player_for_serialization(self): player = Player("uuid", 50, "hoge") player.add_holecard([Card.from_id(cid) for cid in range(1,3)]) player.add_action_history(Const.Action.SMALL_BLIND, sb_amount=5) player.save_street_action_histories(Const.Street.PREFLOP) player.add_action_history(Const.Action.CALL, 10) player.add_action_history(Const.Action.RAISE, 10, 5) player.add_action_history(Const.Action.FOLD) player.pay_info.update_by_pay(15) player.pay_info.update_to_fold() return player
def setUp(self): self.seats = Seats() self.p1 = Player("uuid1", 100) self.p2 = Player("uuid2", 100) self.p3 = Player("uuid3", 100)
def setup_player_with_payinfo(idx, name, amount, status): player = Player("uuid%d" % idx, 100, name) player.pay_info.amount = amount player.pay_info.status = status return player
def __setup_player(self): return Player("uuid", 100, "hoge")
class TableTest(BaseUnitTest): def setUp(self): self.__setup_table() self.__setup_player() self.table.seats.sitdown(self.player) def test_set_blind(self): self.assertIsNone(self.table._blind_pos) self.table.set_blind_pos(1, 2) self.assertIsNotNone(self.table._blind_pos) self.eq(1, self.table.sb_pos()) self.eq(2, self.table.bb_pos()) def test_set_blind_error(self): with self.assertRaises(Exception) as e1: self.table.sb_pos() with self.assertRaises(Exception) as e2: self.table.bb_pos() for e in [e1, e2]: self.eq('blind position is not yet set', str(e.exception)) def test_reset_deck(self): self.table.reset() self.eq(52, self.table.deck.size()) def test_reset_commynity_card(self): self.table.reset() for card in self.table.deck.draw_cards(5): self.table.add_community_card(card) def test_reset_player_status(self): self.table.reset() self.eq(0, len(self.player.hole_card)) self.eq(0, len(self.player.action_histories)) self.eq(PayInfo.PLAY_TILL_END, self.player.pay_info.status) @raises(ValueError) def test_community_card_exceed_size(self): self.table.add_community_card(Card.from_id(1)) def test_shift_dealer_btn_skip(self): table = self.__setup_players_with_table() table.shift_dealer_btn() self.eq(2, table.dealer_btn) table.shift_dealer_btn() self.eq(0, table.dealer_btn) def test_next_ask_waiting_player_pos(self): table = self.__setup_players_with_table() self.eq(0, table.next_ask_waiting_player_pos(0)) self.eq(0, table.next_ask_waiting_player_pos(1)) self.eq(0, table.next_ask_waiting_player_pos(2)) def test_next_ask_waitint_player_pos_when_no_one_waiting(self): table = self.__setup_players_with_table() table.seats.players[0].pay_info.update_to_allin() self.eq(table._player_not_found, table.next_ask_waiting_player_pos(0)) self.eq(table._player_not_found, table.next_ask_waiting_player_pos(1)) self.eq(table._player_not_found, table.next_ask_waiting_player_pos(2)) def test_serialization(self): table = self.__setup_players_with_table() for card in table.deck.draw_cards(3): table.add_community_card(card) table.shift_dealer_btn() table.set_blind_pos(1, 2) serial = table.serialize() restored = Table.deserialize(serial) self.eq(table.dealer_btn, restored.dealer_btn) self.eq(Seats.serialize(table.seats), Seats.serialize(restored.seats)) self.eq(Deck.serialize(table.deck), Deck.serialize(restored.deck)) self.eq(table.get_community_card(), restored.get_community_card()) self.eq(1, restored.sb_pos()) self.eq(2, restored.bb_pos()) def __setup_table(self): self.table = Table() for card in self.table.deck.draw_cards(5): self.table.add_community_card(card) def __setup_player(self): self.player = Player('uuid', 100) self.player.add_holecard([Card.from_id(cid + 1) for cid in range(2)]) self.player.add_action_history(Const.Action.CALL, 10) self.player.pay_info.update_to_fold() def __setup_players_with_table(self): p1 = Player('uuid1', 100) p2 = Player('uuid2', 100) p3 = Player('uuid3', 100) p2.pay_info.update_to_fold() p3.pay_info.update_to_allin() table = Table() for player in [p1, p2, p3]: table.seats.sitdown(player) return table