File size: 8,181 Bytes
6ee449a
 
 
 
 
 
 
 
de49ebf
 
6ee449a
 
 
 
 
 
de49ebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ee449a
 
 
 
 
de49ebf
6ee449a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de49ebf
6ee449a
 
de49ebf
6ee449a
 
 
 
 
 
 
de49ebf
 
6ee449a
 
 
 
 
 
 
 
 
1175a09
6ee449a
 
 
 
 
 
de49ebf
6ee449a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de49ebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from collections import defaultdict
import logging
import bridgebots
from pathlib import Path
from datetime import datetime
import pandas as pd
import tempfile
from collections import Counter
from dds_util import get_result_table
from typing import List, Dict


DEALER_LIST = ['N', 'E', 'S', 'W']
VULNERABILITY_LIST = ["None","NS","EW","All","NS","EW","All","None","EW","All","None","NS","All","None","NS","EW"]
SUITS = [bridgebots.Suit.SPADES, bridgebots.Suit.HEARTS, bridgebots.Suit.DIAMONDS, bridgebots.Suit.CLUBS]


def _build_record_dict(record_strings: List[str]) -> Dict:
    """
    Parse the pbn line by line. When a block section like "Auction" or "Play" is encountered, collect all the content of
     the block into a single entry
    :param record_strings: List of string lines for a single board
    :return: A dictionary mapping keys from the pbn to strings or other useful values (e.g. list of strings for the
    bidding record)
    """
    record_dict = {}
    # Janky while loop to handle non bracketed lines
    i = 0
    while i < len(record_strings):
        record_string = record_strings[i]
        if not (record_string.startswith("[") or record_string.startswith("{")):
            i += 1
            continue
        if record_string.startswith("{"):
            commentary = ""
            while i < len(record_strings):
                record_string = record_strings[i]
                if record_string.startswith("["):
                    break
                commentary += record_string + " "
                i += 1
            record_dict["Commentary"] = commentary.strip()
            continue
        if record_string.startswith("[") and "]" not in record_string:
            while "]" not in record_string:
                i += 1
                record_string = record_string + record_strings[i]
        record_string = record_string.replace("[", "").replace("]", "")
        key, value = record_string.split(maxsplit=1)
        value = value.replace('"', "")
        if key == "Note":
            number, message = value.split(":", maxsplit=1)
            key = key + "_" + number
            value = message
        record_dict[key] = value
        if key == "Auction":
            auction_record = []
            i += 1
            while i < len(record_strings):
                auction_str = record_strings[i]
                if "[" in auction_str:
                    break
                auction_record.extend(auction_str.split())
                i += 1
            record_dict["bidding_record"] = auction_record

        elif key == "Play":
            play_record = []
            i += 1
            while i < len(record_strings):
                play_str = record_strings[i]
                if "[" in play_str or play_str == "*":
                    break
                play_record.append(play_str.split())
                i += 1
            record_dict["play_record"] = play_record
        elif key == "OptimumResultTable":
            dds_tricks_table = []
            i += 1
            while i < len(record_strings):
                dds_str = record_strings[i]
                if "[" in dds_str or dds_str == "*":
                    break
                dds_tricks_table.append(dds_str.split())
                i += 1
            record_dict["dds_tricks_table"] = dds_tricks_table
        else:
            i += 1
    return record_dict

def parse_single_pbn_record(record_strings):
    """
    :param record_strings: One string per line of a single PBN deal record
    :return: Deal and BoardRecord corresponding to the PBN record
    """
    record_dict = _build_record_dict(record_strings)
    try:
        deal = bridgebots.pbn.from_pbn_deal(record_dict["Dealer"], record_dict["Vulnerable"], record_dict["Deal"])
    except KeyError as e:
        # if previous_deal:
        #     deal = previous_deal
        # else:
        raise ValueError("Missing deal fields and no previous_deal provided") from e
    # board_record = _parse_board_record(record_dict, deal)
    return deal, record_dict


def parse_pbn(file_path):
    """
    Split PBN file into boards then decompose those boards into Deal and BoardRecord objects. Only supports PBN v1.0
    See https://www.tistis.nl/pbn/pbn_v10.txt

    :param file_path: path to a PBN file
    :return: A list of DealRecords representing all the boards played
    """
    records_strings = bridgebots.pbn._split_pbn(file_path)
    # Maintain a mapping from deal to board records to create a single deal record per deal
    records = defaultdict(list)
    # Some PBNs have multiple board records per deal
    previous_deal = None
    for record_strings in records_strings:
        try:
            deal, board_record = parse_single_pbn_record(record_strings)
            records[deal].append(board_record)
            # previous_deal = deal
        except (KeyError, ValueError) as e:
            logging.warning(f"Malformed record {record_strings}: {e}")
    return [(deal, board_records) for deal, board_records in records.items()]


def create_single_pbn_string(
        data: pd.DataFrame,
        date=datetime.today(),
        board_no=1,
        event='',
        site='',
    ) -> str:
    year = date.strftime("%y")
    month = date.strftime("%m")
    day = date.strftime("%d")
    date_print = day + "." + month + "." + year
    dealer = DEALER_LIST[(board_no-1) % 4]
    vulnerability=VULNERABILITY_LIST[(board_no-1) % 16]
    deal = 'N:'
    deal += ' '.join(
        ['.'.join(data[col]) for col in data.columns[1:]]
    ) # sss.hhh.ddd.ccc sss.hhh.ddd.ccc......
    dd_tricks = get_result_table(deal)

    file = ''
    file += ("%This PBN was generated by Bridge Hand Scanner\n")
    file += f'[Event "{event}"]\n'
    file += f'[Site "{site}"]\n'
    file += f'[Date "{date_print}"]\n'
    file += f'[Board "{str(board_no)}"]\n'
    file += f'[Dealer "{dealer}"]\n'
    file += f'[Vulnerable "{vulnerability}"]\n'
    file += f'[Deal "{deal}"]\n'
    file += dd_tricks
    file += '\n'
    return file

def merge_pbn(pbn_paths):
    fd, fn = tempfile.mkstemp(suffix='.pbn', text=True)
    board_dict = {}
    for i, pbn_path in enumerate(pbn_paths):
        result = parse_pbn(pbn_path)[0]
        with open(pbn_path, 'r') as f2:
            pbn_str = f2.read()
        board_no = int(result[1][0]['Board'])
        board_dict[board_no] = pbn_str
        
    ordered_board_dict = dict(sorted(board_dict.items()))
    with open(fd, 'w') as f:
        for i, (k,v) in enumerate(ordered_board_dict.items()):
            if i != 0:
                f.write('\n')        
            f.write(v)
    return fn


def validate_pbn(pbn_string):
    try:
        deal, record = parse_single_pbn_record(pbn_string)
    except AssertionError:
        raise ValueError('Everyone should have 13 cards')
    except Exception as e:
        raise Exception(e)
    hands = deal.hands
    duplicated = set()
    missing = set()
    validation_dict = {}
    for suit in bridgebots.Suit:
        cards = [hands[direction].suits[suit] for direction in hands]
        # assert len(cards) == 13, 
        cards = [c for c in sum([hands[direction].suits[suit] for direction in hands], [])]
        duplicated.update([bridgebots.Card(suit,val) for val,cnt in Counter(cards).items() if cnt >1])

        cards_set = set(cards)
        missing.update([bridgebots.Card(suit,r) for r in bridgebots.Rank if not r in cards_set])

    err_msg = ''
    if len(duplicated) > 0:
        err_msg += f'Duplicated cards: {duplicated}. '
    if len(missing) > 0:
        err_msg += f'Missing cards: {missing}. '

    for direction in hands:
        num_cards = len(hands[direction].cards)
        if not num_cards == 13:
            err_msg += '{direction.name} has {num_cards} cards. '
    
    if err_msg:
        raise ValueError(err_msg)
    
def parse_dds_table(raw_list):
    table = []
    row = []
    tempchar = ''
    for x in raw_list:
        if len(x) > 0:
            tempchar += x[0]
        else:
            if len(tempchar) > 0:
                row.append(tempchar)
            tempchar = ''

        if len(row) == 3:
            table.append(row)
            row = []
    return table