def build_street_start_message(self, state): message = { "message_type": self.STREET_START_MESSAGE, "round_state": DataEncoder.encode_round_state(state) } message.update(DataEncoder.encode_street(state["street"])) return self.__build_notification_message(message)
def build_round_result_message(self, round_count, winners, hand_info, state): message = { "message_type": self.ROUND_RESULT_MESSAGE, "round_count": round_count, "hand_info" : hand_info, "round_state": DataEncoder.encode_round_state(state) } message.update(DataEncoder.encode_winners(winners)) return self.__build_notification_message(message)
def build_game_update_message(self, player_pos, action, amount, state): player = state["table"].seats.players[player_pos] message = { "message_type": self.GAME_UPDATE_MESSAGE, "action": DataEncoder.encode_action(player, action, amount), "round_state": DataEncoder.encode_round_state(state), "action_histories": DataEncoder.encode_action_histories(state["table"]) } return self.__build_notification_message(message)
def build_game_update_message(self, player_pos, action, amount, state): player = state['table'].seats.players[player_pos] message = { 'message_type': self.GAME_UPDATE_MESSAGE, 'action': DataEncoder.encode_action(player, action, amount), 'round_state': DataEncoder.encode_round_state(state), 'action_histories': DataEncoder.encode_action_histories(state['table']) } return self.__build_notification_message(message)
def build_round_result_message(self, round_count, winners, hand_info, state): message = { 'message_type': self.ROUND_RESULT_MESSAGE, 'round_count': round_count, 'hand_info': hand_info, 'round_state': DataEncoder.encode_round_state(state) } message.update(DataEncoder.encode_winners(winners)) return self.__build_notification_message(message)
def build_round_result_message(self, round_count, winners, hand_info, state): message = { "message_type": self.ROUND_RESULT_MESSAGE, "round_count": round_count, "hand_info": hand_info, "round_state": DataEncoder.encode_round_state(state, True) } message.update(DataEncoder.encode_winners(winners)) return self.__build_notification_message(message)
def build_round_start_message(self, round_count, player_pos, seats): player = seats.players[player_pos] hole_card = DataEncoder.encode_player(player, holecard=True)["hole_card"] message = { "message_type": self.ROUND_START_MESSAGE, "round_count": round_count, "hole_card": hole_card } message.update(DataEncoder.encode_seats(seats)) return self.__build_notification_message(message)
def test_ask_message(self): state = self.__setup_state() table = state["table"] message = MessageBuilder.build_ask_message(1, state) msg = message["message"] self.eq("ask", message["type"]) self.eq(MessageBuilder.ASK_MESSAGE, msg["message_type"]) self.eq(["CA", "C2"], msg["hole_card"]) self.eq(3, len(msg["valid_actions"])) self.eq(DataEncoder.encode_round_state(state), msg["round_state"]) self.eq(DataEncoder.encode_action_histories(table), msg["action_histories"])
def test_game_update_message(self): state = self.__setup_state() table = state['table'] player = table.seats.players[1] message = MessageBuilder.build_game_update_message(1, 'call', 10, state) msg = message['message'] self.eq('notification', message['type']) self.eq(MessageBuilder.GAME_UPDATE_MESSAGE, msg['message_type']) self.eq(DataEncoder.encode_action(player, 'call', 10), msg['action']) self.eq(DataEncoder.encode_round_state(state), msg['round_state']) self.eq(DataEncoder.encode_action_histories(table), msg['action_histories'])
def test_game_update_message(self): state = self.__setup_state() table = state["table"] player = table.seats.players[1] message = MessageBuilder.build_game_update_message(1, "call", 10, state) msg = message["message"] self.eq("notification", message["type"]) self.eq(MessageBuilder.GAME_UPDATE_MESSAGE, msg["message_type"]) self.eq(DataEncoder.encode_action(player, "call", 10), msg["action"]) self.eq(DataEncoder.encode_round_state(state), msg["round_state"]) self.eq(DataEncoder.encode_action_histories(table), msg["action_histories"])
def test_ask_message(self): state = self.__setup_state() table = state['table'] message = MessageBuilder.build_ask_message(1, state) msg = message['message'] self.eq('ask', message['type']) self.eq(MessageBuilder.ASK_MESSAGE, msg['message_type']) self.eq(['2d', '3d'], msg['hole_card']) self.eq(3, len(msg['valid_actions'])) self.eq(DataEncoder.encode_round_state(state), msg['round_state']) self.eq(DataEncoder.encode_action_histories(table), msg['action_histories'])
def test_round_result_message(self): state = self.__setup_state() winners = state['table'].seats.players[1:2] hand_info = ['dummy', 'info'] message = MessageBuilder.build_round_result_message(7, winners, hand_info, state) msg = message['message'] self.eq('notification', message['type']) self.eq(MessageBuilder.ROUND_RESULT_MESSAGE, msg['message_type']) self.eq(7, msg['round_count']) self.eq(hand_info, msg['hand_info']) self.eq(DataEncoder.encode_winners(winners)['winners'], msg['winners']) self.eq(DataEncoder.encode_round_state(state), msg['round_state'])
def test_round_result_message(self): state = self.__setup_state() winners = state["table"].seats.players[1:2] hand_info = ["dummy", "info"] message = MessageBuilder.build_round_result_message(7, winners, hand_info, state) msg = message["message"] self.eq("notification", message["type"]) self.eq(MessageBuilder.ROUND_RESULT_MESSAGE, msg["message_type"]) self.eq(7, msg["round_count"]) self.eq(hand_info, msg["hand_info"]) self.eq(DataEncoder.encode_winners(winners)["winners"], msg["winners"]) self.eq(DataEncoder.encode_round_state(state), msg["round_state"])
def build_round_result_message(self, round_count, winners, hand_info, state): winner_uuid = [winner.uuid for winner in winners] hand_info = [info for info in hand_info if info['uuid'] in winner_uuid] message = { "message_type": self.ROUND_RESULT_MESSAGE, "round_count": round_count, "hand_info": hand_info, "round_state": DataEncoder.encode_round_state(state, True) } message.update(DataEncoder.encode_winners(winners)) return self.__build_notification_message(message)
def test_encofe_game_information(self): config = { "initial_stack":100, "max_round":10, "small_blind_amount":5,\ "ante":1, "blind_structure": {1: {"ante": 3, "small_blind": 10} } } seats = setup_seats() hsh = DataEncoder.encode_game_information(config, seats) self.eq(3, hsh["player_num"]) self.eq(DataEncoder.encode_seats(seats)["seats"], hsh["seats"]) self.eq(config["small_blind_amount"], hsh["rule"]["small_blind_amount"]) self.eq(config["max_round"], hsh["rule"]["max_round"]) self.eq(config["initial_stack"], hsh["rule"]["initial_stack"]) self.eq(config["ante"], hsh["rule"]["ante"]) self.eq(config["blind_structure"], hsh["rule"]["blind_structure"])
def test_encofe_game_information(self): config = {"initial_stack": 100, "max_round": 10, "small_blind_amount": 5, \ "ante": 1, "blind_structure": {1: {"ante": 3, "small_blind": 10}}} seats = setup_seats() hsh = DataEncoder.encode_game_information(config, seats) self.eq(3, hsh["player_num"]) self.eq(DataEncoder.encode_seats(seats)["seats"], hsh["seats"]) self.eq(config["small_blind_amount"], hsh["rule"]["small_blind_amount"]) self.eq(config["max_round"], hsh["rule"]["max_round"]) self.eq(config["initial_stack"], hsh["rule"]["initial_stack"]) self.eq(config["ante"], hsh["rule"]["ante"]) self.eq(config["blind_structure"], hsh["rule"]["blind_structure"])
def build_ask_message(self, player_pos, state): players = state["table"].seats.players player = players[player_pos] hole_card = DataEncoder.encode_player(player, holecard=True)["hole_card"] valid_actions = ActionChecker.legal_actions(players, player_pos, state["small_blind_amount"]) message = { "message_type" : self.ASK_MESSAGE, "hole_card": hole_card, "valid_actions": valid_actions, "round_state": DataEncoder.encode_round_state(state), "action_histories": DataEncoder.encode_action_histories(state["table"]) } return self.__build_ask_message(message)
def test_encode_round_state(self): state = setup_round_state() state["table"].set_blind_pos(1, 3) hsh = DataEncoder.encode_round_state(state) self.eq("flop", hsh["street"]) self.eq(DataEncoder.encode_pot(state["table"].seats.players), hsh["pot"]) self.eq(DataEncoder.encode_seats(state["table"].seats)["seats"], hsh["seats"]) self.eq(["CA"], hsh["community_card"]) self.eq(state["table"].dealer_btn, hsh["dealer_btn"]) self.eq(state["next_player"], hsh["next_player"]) self.eq(1, hsh["small_blind_pos"]) self.eq(3, hsh["big_blind_pos"]) self.eq(DataEncoder.encode_action_histories(state["table"])["action_histories"], hsh["action_histories"]) self.eq(state["round_count"], hsh["round_count"]) self.eq(state["small_blind_amount"], hsh["small_blind_amount"])
def build_game_start_message(self, config, seats): message = { 'message_type': self.GAME_START_MESSAGE, 'game_information': DataEncoder.encode_game_information(config, seats) } return self.__build_notification_message(message)
def build_ask_message(self, player_pos, state): players = state['table'].seats.players player = players[player_pos] hole_card = DataEncoder.encode_player(player, holecard=True)['hole_card'] valid_actions = ActionChecker.legal_actions( players, player_pos, state['small_blind_amount']) message = { 'message_type': self.ASK_MESSAGE, 'hole_card': hole_card, 'valid_actions': valid_actions, 'round_state': DataEncoder.encode_round_state(state), 'action_histories': DataEncoder.encode_action_histories(state['table']) } return self.__build_ask_message(message)
def build_game_result_message(self, config, seats): message = { "message_type": self.GAME_RESULT_MESSAGE, "game_information": DataEncoder.encode_game_information(config, seats) } return self.__build_notification_message(message)
def build_ask_message(self, player_pos, state): players = state["table"].seats.players player = players[player_pos] hole_card = DataEncoder.encode_player(player, holecard=True)["hole_card"] valid_actions = ActionChecker.legal_actions( players, player_pos, state["small_blind_amount"]) message = { "message_type": self.ASK_MESSAGE, "hole_card": hole_card, "valid_actions": valid_actions, "round_state": DataEncoder.encode_round_state(state), "action_histories": DataEncoder.encode_action_histories(state["table"]) } return self.__build_ask_message(message)
def test_encode_player_without_holecard(self): player = setup_player() hsh = DataEncoder.encode_player(player) self.eq(player.name, hsh['name']) self.eq(player.uuid, hsh['uuid']) self.eq(player.stack, hsh['stack']) self.eq('folded', hsh['state']) self.false('hole_card' in hsh)
def test_encode_player_without_holecard(self): player = setup_player() hsh = DataEncoder.encode_player(player) self.eq(player.name, hsh["name"]) self.eq(player.uuid, hsh["uuid"]) self.eq(player.stack, hsh["stack"]) self.eq("folded", hsh["state"]) self.false("hole_card" in hsh)
def test_street_start_message(self): state = self.__setup_state() message = MessageBuilder.build_street_start_message(state) msg = message['message'] self.eq(MessageBuilder.STREET_START_MESSAGE, msg['message_type']) self.eq('notification', message['type']) self.eq('flop', msg['street']) self.eq(DataEncoder.encode_round_state(state), msg['round_state'])
def test_street_start_message(self): state = self.__setup_state() message = MessageBuilder.build_street_start_message(state) msg = message["message"] self.eq(MessageBuilder.STREET_START_MESSAGE, msg["message_type"]) self.eq("notification", message["type"]) self.eq("flop", msg["street"]) self.eq(DataEncoder.encode_round_state(state), msg["round_state"])
def test_game_result_message(self): config = self.__setup_config() seats = self.__setup_seats() state = self.__setup_state() message = MessageBuilder.build_game_result_message(config, seats) msg = message["message"] self.eq("notification", message["type"]) self.eq(MessageBuilder.GAME_RESULT_MESSAGE, msg["message_type"]) self.eq(DataEncoder.encode_game_information(config, seats), msg["game_information"])
def test_round_start_message(self): seats = self.__setup_seats() message = MessageBuilder.build_round_start_message(7, 1, seats) msg = message['message'] self.eq('notification', message['type']) self.eq(MessageBuilder.ROUND_START_MESSAGE, msg['message_type']) self.eq(7, msg['round_count']) self.eq(DataEncoder.encode_seats(seats)['seats'], msg['seats']) self.eq(['2d', '3d'], msg['hole_card'])
def test_round_start_message(self): seats = self.__setup_seats() message = MessageBuilder.build_round_start_message(7, 1, seats) msg = message["message"] self.eq("notification", message["type"]) self.eq(MessageBuilder.ROUND_START_MESSAGE, msg["message_type"]) self.eq(7, msg["round_count"]) self.eq(DataEncoder.encode_seats(seats)["seats"], msg["seats"]) self.eq(["CA", "C2"], msg["hole_card"])
def test_game_start_message(self): config = self.__setup_config() seats = self.__setup_seats() message = MessageBuilder.build_game_start_message(config, seats) msg = message['message'] self.eq('notification', message['type']) self.eq(MessageBuilder.GAME_START_MESSAGE, msg['message_type']) self.eq(MessageBuilder.GAME_START_MESSAGE, msg['message_type']) self.eq(DataEncoder.encode_game_information(config, seats), msg['game_information'])
def test_encode_valid_actions(self): hsh = DataEncoder.encode_valid_actions(10, 20, 100) acts = hsh["valid_actions"] self.eq("fold", acts[0]["action"]) self.eq(0, acts[0]["amount"]) self.eq("call", acts[1]["action"]) self.eq(10, acts[1]["amount"]) self.eq("raise", acts[2]["action"]) self.eq(20, acts[2]["amount"]["min"]) self.eq(100, acts[2]["amount"]["max"])
def test_encode_valid_actions(self): hsh = DataEncoder.encode_valid_actions(10, 20, 100) acts = hsh['valid_actions'] self.eq('fold', acts[0]['action']) self.eq(0, acts[0]['amount']) self.eq('call', acts[1]['action']) self.eq(10, acts[1]['amount']) self.eq('raise', acts[2]['action']) self.eq(20, acts[2]['amount']['min']) self.eq(100, acts[2]['amount']['max'])
def test_encode_round_state(self): state = setup_round_state() state["table"].set_blind_pos(1, 3) hsh = DataEncoder.encode_round_state(state) self.eq("flop", hsh["street"]) self.eq(DataEncoder.encode_pot(state["table"].seats.players), hsh["pot"]) self.eq( DataEncoder.encode_seats(state["table"].seats)["seats"], hsh["seats"]) self.eq(["CA"], hsh["community_card"]) self.eq(state["table"].dealer_btn, hsh["dealer_btn"]) self.eq(state["next_player"], hsh["next_player"]) self.eq(1, hsh["small_blind_pos"]) self.eq(3, hsh["big_blind_pos"]) self.eq( DataEncoder.encode_action_histories( state["table"])["action_histories"], hsh["action_histories"]) self.eq(state["round_count"], hsh["round_count"]) self.eq(state["small_blind_amount"], hsh["small_blind_amount"])
def test_encode_round_state(self): state = setup_round_state() state['table'].set_blind_pos(1, 3) hsh = DataEncoder.encode_round_state(state) self.eq('flop', hsh['street']) self.eq(DataEncoder.encode_pot(state['table'].seats.players), hsh['pot']) self.eq( DataEncoder.encode_seats(state['table'].seats)['seats'], hsh['seats']) self.eq(['2d'], hsh['community_card']) self.eq(state['table'].dealer_btn, hsh['dealer_btn']) self.eq(state['next_player'], hsh['next_player']) self.eq(1, hsh['small_blind_pos']) self.eq(3, hsh['big_blind_pos']) self.eq( DataEncoder.encode_action_histories( state['table'])['action_histories'], hsh['action_histories']) self.eq(state['round_count'], hsh['round_count']) self.eq(state['small_blind_amount'], hsh['small_blind_amount'])
def test_encode_pot(self): players = setup_players_for_pot() hsh = DataEncoder.encode_pot(players) main_pot = hsh["main"] side_pot1 = hsh["side"][0] side_pot2 = hsh["side"][1] self.eq(22, main_pot["amount"]) self.eq(9, side_pot1["amount"]) self.eq(3, len(side_pot1["eligibles"])) self.eq(['uuid1', 'uuid2', 'uuid3'], side_pot1['eligibles']) self.eq(4, side_pot2["amount"]) self.eq(2, len(side_pot2["eligibles"])) self.eq(['uuid1', 'uuid3'], side_pot2['eligibles'])
def test_encode_action_histories(self): table = setup_table() p1, p2, p3 = table.seats.players hsh = DataEncoder.encode_action_histories(table) hsty = hsh["action_histories"] self.eq(4, len(hsty["preflop"])) fetch_info = lambda info: (info["action"], info["amount"]) self.eq(("RAISE", 10), fetch_info(hsty["preflop"][0])) self.eq("FOLD", hsty["preflop"][1]["action"]) self.eq(("RAISE", 20), fetch_info(hsty["preflop"][2])) self.eq(("CALL", 20), fetch_info(hsty["preflop"][3])) self.eq(2, len(hsty["flop"])) self.eq(("CALL", 5), fetch_info(hsty["flop"][0])) self.eq(("RAISE", 5), fetch_info(hsty["flop"][1])) self.assertFalse("turn" in hsty) self.assertFalse("river" in hsty)
def check(arg, expected): self.eq(expected, DataEncoder.encode_street(arg)["street"])
def test_encode_seats(self): seats = setup_seats() hsh = DataEncoder.encode_seats(seats) self.eq(3, len(hsh["seats"])) self.eq(DataEncoder.encode_player(seats.players[0]), hsh["seats"][0])
def test_encode_player_with_holecard(self): player = setup_player() hsh = DataEncoder.encode_player(player, holecard=True) self.eq([str(card) for card in player.hole_card], hsh["hole_card"])
def test_encode_winners(self): winners = [setup_player() for _ in range(2)] hsh = DataEncoder.encode_winners(winners) self.eq(2, len(hsh["winners"])) self.eq([DataEncoder.encode_player(p) for p in winners], hsh["winners"])
def test_encode_action(self): player = setup_player() hsh = DataEncoder.encode_action(player, "raise", 20) self.eq(player.uuid, hsh["player_uuid"]) self.eq("raise", hsh["action"]) self.eq(20, hsh["amount"])