Exemplo n.º 1
0
def check_valid_split(set1_code, set2_code, set3_code):
    """[summary]

  Args:
      set1_code (str or tuple): [description]
      set2_code (str or tuple): [description]
      set3_code (str or tuple): [description]

  Returns:
      [type]: [description]
  """
    if isinstance(set1_code, str):
        set1_code = ast.literal_eval(set1_code)
        set1_code = CardGroupCode(set1_code)
    if isinstance(set2_code, str):
        set2_code = ast.literal_eval(set2_code)
        set2_code = CardGroupCode(set2_code)
    if isinstance(set3_code, str):
        set3_code = ast.literal_eval(set3_code)
        set3_code = CardGroupCode(set3_code)

    if not set1_code.code or not set2_code.code or not set3_code.code:
        return False

    #print(set1_code)
    #print(set2_code)
    #print(set3_code)

    if (set1_code > set2_code) | (set2_code > set3_code):
        return False
    else:
        return True
def gen_code_combs_set(strategy=None, score_round_fac=1):
  """Generate code combs across sets, with optional filtering
  

  Args:
      strategy ([type]): [description]
      round_fac (int, optional): [description]. Defaults to 1.

  Returns:
      [type]: [description]
  """
  
  if strategy is None:
    strategy = ChinesePokerPctileScoreStrategy()

  combs = []
  existing_scores = [[], [], []]
  #prev_scores = (None, None, None)
  
  for code_set1, score_set1 in strategy.score_dicts[0].items():
    print(score_set1)
    if round(score_set1/score_round_fac) in existing_scores[0]:
      continue
    univ_score1 = CardGroupCode.get_universal_score(code_set1)

    existing_scores[1] = []
    for code_set2, score_set2 in strategy.score_dicts[1].items():
      if (round(score_set2/score_round_fac) in existing_scores[1]) or (CardGroupCode(code_set2) < CardGroupCode(code_set1)):
        continue
      univ_score2 = CardGroupCode.get_universal_score(code_set2)

      existing_scores[2] = []
      for code_set3, score_set3 in strategy.score_dicts[2].items():
        if (round(score_set3/score_round_fac) in existing_scores[2]) or (CardGroupCode(code_set3) < CardGroupCode(code_set2)):
          continue
        univ_score3 = CardGroupCode.get_universal_score(code_set3)

        combs.append(
          (
            code_set1, 
            code_set2, 
            code_set3, 
            score_set1, 
            score_set2, 
            score_set3, 
            univ_score1,
            univ_score2,
            univ_score3,
          )
        )
        existing_scores[2].append(round(score_set3/score_round_fac))
      
      existing_scores[1].append(round(score_set2/score_round_fac))
    existing_scores[0].append(round(score_set1/score_round_fac))
  return combs
Exemplo n.º 3
0
def classify_group(cards=None, group_code=None):
    """[summary]

  Args:
      cards (list): List of card strings
  """

    classifier = CardGroupClassifier()

    if group_code is not None and cards is None:
        group_sorted_inds = None
        if isinstance(group_code, str):
            group_code = ast.literal_eval(group_code)
        if isinstance(group_code, tuple):
            group_code = CardGroupCode(group_code)
    else:
        group_code, group_sorted_inds = classifier.classify_card_group(cards)
    group_desc = classifier.get_code_description(group_code)

    return group_code, group_desc, group_sorted_inds
Exemplo n.º 4
0
def play_hand(user_codes, com1_codes, com2_codes, com3_codes):

    codes = [user_codes, com1_codes, com2_codes, com3_codes]
    for cI, code_split in enumerate(codes):
        if isinstance(code_split[0], str):
            code_split = [ast.literal_eval(code) for code in code_split]

        codes[cI] = [CardGroupCode(code) for code in code_split]

    print(f'codes: {codes}')
    compare_combs = combinations(range(4), 2)
    comp_res = [[[] for _ in range(4)] for _ in range(4)]
    game_scores = [[-999 for _ in range(4)] for _ in range(4)]
    tot_game_scores = [0 for _ in range(4)]

    for i1, i2 in compare_combs:
        temp_comp_res = [None, None, None]
        for sI in range(3):
            if codes[i1][sI] > codes[i2][sI]:
                temp_comp_res[sI] = 1
            elif codes[i1][sI] < codes[i2][sI]:
                temp_comp_res[sI] = -1
            elif codes[i1][sI] == codes[i2][sI]:
                temp_comp_res[sI] = 0
        comp_res[i1][i2] = temp_comp_res
        comp_res[i2][i1] = [-score for score in temp_comp_res]

        temp_game_score = sum(temp_comp_res)
        if abs(temp_game_score) == 3:
            temp_game_score *= 2

        game_scores[i1][i2] = temp_game_score
        game_scores[i2][i1] = -temp_game_score

        tot_game_scores[i1] += temp_game_score
        tot_game_scores[i2] -= temp_game_score

    #print(comp_res)
    #print(game_scores)

    return comp_res, game_scores, tot_game_scores
Exemplo n.º 5
0
def get_splits_data_for_single_game_and_seat_from_db(
    game_id,
    seat_id,
    cards=None,
):
    """[summary]

  Args:
      game_id (int): GameID
      seat_id (int): Between 1 and 4
      cards (List): List of Card objects or card strings. 
  """

    if cards is None:
        hands = next(yield_dealt_hands_from_db(game_id, game_id))[1]
        cards = hands[seat_id - 1]
    elif isinstance(cards[0], str):
        deck = Deck()
        cards = deck.deal_custom_hand(cards)

    splits_table = GameC.CHINESE_POKER_db_consts['splits_table']
    codes_table = GameC.CHINESE_POKER_db_consts['split_codes_table']
    query = f'SELECT SplitSeqNo, SplitStr, + ' \
            f'c1.L1Code AS S1L1Code, c1.L2Code AS S1L2Code, c1.L3Code AS S1L3Code, c1.L4Code AS S1L4Code, c1.L5Code AS S1L5Code, c1.L6Code AS S1L6Code, ' + \
            f'c2.L1Code AS S2L1Code, c2.L2Code AS S2L2Code, c2.L3Code AS S2L3Code, c2.L4Code AS S2L4Code, c2.L5Code AS S2L5Code, c2.L6Code AS S2L6Code, ' + \
            f'c3.L1Code AS S3L1Code, c3.L2Code AS S3L2Code, c3.L3Code AS S3L3Code, c3.L4Code AS S3L4Code, c3.L5Code AS S3L5Code, c3.L6Code AS S3L6Code ' + \
            f'FROM {splits_table} s ' + \
            f'JOIN {codes_table} c1 ON s.SplitID=c1.SplitID ' + \
            f'JOIN {codes_table} c2 ON s.SplitID=c2.SplitID ' + \
            f'JOIN {codes_table} c3 ON s.SplitID=c3.SplitID ' + \
            f'WHERE s.GameID={game_id} AND s.SeatID={seat_id} ' + \
            f'AND c1.SetNo=1 AND c2.SetNo=2 AND c3.SetNo=3'
    db_output, _ = DBF.select_query(query)
    hand_splits = []
    for row in db_output:
        split_seq_no, split_str, s1c1, s1c2, s1c3, s1c4, s1c5, s1c6, s2c1, s2c2, s2c3, s2c4, s2c5, s2c6, s3c1, s3c2, s3c3, s3c4, s3c5, s3c6 = row

        split_cards, split_inds = _convert_split_str_to_split_cards(
            cards, split_str)
        s1code = [s1c1, s1c2, s1c3, s1c4, s1c5, s1c6]
        s2code = [s2c1, s2c2, s2c3, s2c4, s2c5, s2c6]
        s3code = [s3c1, s3c2, s3c3, s3c4, s3c5, s3c6]

        s1code = CardGroupCode([code for code in s1code if code is not None])
        s2code = CardGroupCode([code for code in s2code if code is not None])
        s3code = CardGroupCode([code for code in s3code if code is not None])

        split_info_factory = ChinesePokerStrategy.ranked_split_info_factory

        split_info = split_info_factory(
            split_inds,
            split_cards,
            (s1code, s2code, s3code),
            None,
            None,
            None,
            None,
            None,
            split_seq_no,
        )
        hand_splits.append(split_info)

    hand_splits = sorted(hand_splits, key=lambda x: x.SeqNo)
    return hand_splits
Exemplo n.º 6
0
    def yield_splits_data_from_db(
        self,
        start_game_id=None,
        end_game_id=None,
    ):

        #db_connector = DBF.connect_to_db()
        splits_table = GameC.CHINESE_POKER_db_consts['splits_table']
        codes_table = GameC.CHINESE_POKER_db_consts['split_codes_table']
        base_query = f'SELECT SplitSeqNo, SplitStr, + ' \
                     f'c1.L1Code AS S1L1Code, c1.L2Code AS S1L2Code, c1.L3Code AS S1L3Code, c1.L4Code AS S1L4Code, c1.L5Code AS S1L5Code, c1.L6Code AS S1L6Code, ' + \
                     f'c2.L1Code AS S2L1Code, c2.L2Code AS S2L2Code, c2.L3Code AS S2L3Code, c2.L4Code AS S2L4Code, c2.L5Code AS S2L5Code, c2.L6Code AS S2L6Code, ' + \
                     f'c3.L1Code AS S3L1Code, c3.L2Code AS S3L2Code, c3.L3Code AS S3L3Code, c3.L4Code AS S3L4Code, c3.L5Code AS S3L5Code, c3.L6Code AS S3L6Code ' + \
                     f'FROM {splits_table} s ' + \
                     f'JOIN {codes_table} c1 ON s.SplitID=c1.SplitID ' + \
                     f'JOIN {codes_table} c2 ON s.SplitID=c2.SplitID ' + \
                     f'JOIN {codes_table} c3 ON s.SplitID=c3.SplitID ' + \
                     f'WHERE s.GameID=%s AND s.SeatID=%s ' + \
                     f'AND c1.SetNo=1 AND c2.SetNo=2 AND c3.SetNo=3'
        for game_id in range(start_game_id, end_game_id + 1):
            hands = next(self.yield_dealt_hands_from_db(game_id, game_id))[1]
            game_splits = []

            for seat_id in range(1, 5):
                query = base_query % (game_id, seat_id)
                cards = hands[seat_id - 1]
                db_output, _ = DBF.select_query(query)

                hand_splits = []
                for row in db_output:
                    split_seq_no, split_str, s1c1, s1c2, s1c3, s1c4, s1c5, s1c6, s2c1, s2c2, s2c3, s2c4, s2c5, s2c6, s3c1, s3c2, s3c3, s3c4, s3c5, s3c6 = row

                    split_cards = self._convert_split_str_to_split_cards(
                        cards, split_str)
                    s1code = [s1c1, s1c2, s1c3, s1c4, s1c5, s1c6]
                    s2code = [s2c1, s2c2, s2c3, s2c4, s2c5, s2c6]
                    s3code = [s3c1, s3c2, s3c3, s3c4, s3c5, s3c6]

                    s1code = CardGroupCode(
                        [code for code in s1code if code is not None])
                    s2code = CardGroupCode(
                        [code for code in s2code if code is not None])
                    s3code = CardGroupCode(
                        [code for code in s3code if code is not None])

                    split_info_factory = ChinesePokerStrategy.ranked_split_info_factory

                    split_info = split_info_factory(
                        None,
                        split_cards,
                        (s1code, s2code, s3code),
                        None,
                        None,
                        None,
                        None,
                        None,
                        split_seq_no,
                    )
                    hand_splits.append(split_info)

                game_splits.append(hand_splits)

            yield game_id, game_splits

            # TODO Convert each row to RankedSplitInfo named tuple
            # (Inds, Cards, Codes, )
        return
Exemplo n.º 7
0
 def _load_single_scoring_table(self, scoring_method):
     return CardGroupCode.load_set_scoring_file(scoring_method)