-
Notifications
You must be signed in to change notification settings - Fork 2
/
handhistory.py
200 lines (161 loc) · 8.85 KB
/
handhistory.py
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
import os
import re
import simplejson as json
from decimal import Decimal
def Parse_bet(line):
match = re.match(bet_pattern, line)
if match == None:
if re.match(return_pattern, line) != None:
match = re.match(return_pattern, line)
action = {'player': match.group(2),
'action': 'bet_returned',
'amount': Decimal(match.group(1))}
else:
action = None
elif match.group(2) == 'folds':
action = {"player": match.group(1),
"action": "folds"}
elif match.group(2) == 'checks':
action = {"player": match.group(1),
"action": 'checks'}
elif match.group(2) == 'bets':
sub_match = re.match(' \$(\d*(?:\.\d\d)?)(?: and is all-in)?', match.group(3))
action = {"player": match.group(1),
"action": "bets",
"amount": Decimal(sub_match.group(1))}
elif match.group(2) == 'calls':
sub_match = re.match(' \$(\d*(?:\.\d\d)?)(?: and is all-in)?', match.group(3))
action = {"player": match.group(1),
"action": "calls",
"amount": Decimal(sub_match.group(1))}
elif match.group(2) == 'raises':
sub_match = re.match(' \$(\d*(?:\.\d\d)?) to \$(\d*(?:\.\d\d)?)(?: and is all-in)?', match.group(3))
action = {"player": match.group(1),
"action": "raises",
"amount": Decimal(sub_match.group(2)),
"raised_by": Decimal(sub_match.group(1))}
return action
def Parse_betting_round(file,contributions, players):
line = file.readline()
actions = []
while re.match(limit_pattern, line) == None:
action = Parse_bet(line)
if action:
actions.append(action)
if action["action"] in ["bets", "calls"]:
contributions[action["player"]] += action["amount"]
elif action["action"] == "raises":
contributions[action["player"]] = action["amount"]
elif action["action"] == 'bet_returned':
contributions[action["player"]] += -action["amount"]
line = file.readline()
if re.match(collect_pot_pattern, line) != None:
match = re.match(collect_pot_pattern, line)
actions.append({'player': match.group(1),
'action': 'collects_pot',
'amount': Decimal(match.group(2))})
while '*** SUMMARY ***' not in line:
line = file.readline()
return actions, True
return actions, False
def Parse_hands(path):
with open(path, "r", encoding='utf-8-sig') as file:
while True:
hand = {} ##dictionary for all data about hand
##read 2 header lines
match = re.match(header_pattern1 ,file.readline())
if match == None:
break
hand['hand_number'] = match.group(1)
hand['blinds'] = match.group(2)
hand['date'] = match.group(3)
hand['time'] = match.group(4)
match = re.match(header_pattern2 ,file.readline())
hand['table_name'] = match.group(1)
hand['table_max_size'] = match.group(2)
hand['button_pos'] = match.group(3)
line = file.readline()
hand["players"] = {}
contributions = {}
while re.match(seat_pattern, line) != None:
match = re.match(seat_pattern, line)
hand["players"][match.group(2)] = {"seat_num": match.group(1),
"chip_stack": Decimal(match.group(3)),
"into_pot": 0,
"winnings": 0}
contributions[match.group(2)] = 0
line = file.readline()
hand["blinds_posted"] = []
while line != '*** HOLE CARDS ***\n':
match = re.match(blinds_pattern, line)
if match != None:
hand["blinds_posted"].append({"player": match.group(1),
"blind_type": match.group(2),
"blind_size": Decimal(match.group(3))})
contributions[match.group(1)] += Decimal(match.group(3))
line = file.readline()
hand["my_cards"] = re.match(delt_pattern, file.readline()).group(1)
hand["betting_rounds"] = {}
for betting_round in betting_rounds:
hand["betting_rounds"][betting_round], Done = Parse_betting_round(file, contributions, hand["players"])
for player, amount in contributions.items():
hand["players"][player]["into_pot"] += amount
contributions[player] = 0
if Done == True:
hand["players"][hand["betting_rounds"][betting_round][-1]['player']]["winnings"]+= hand["betting_rounds"][betting_round][-1]['amount']
break
else:
line = file.readline()
hand["showdown_action"] = []
while '*** SUMMARY ***' not in line:
if re.match(mucks_pattern, line) != None:
hand["showdown_action"].append({'player': re.match(mucks_pattern, line).group(1),
'action': 'mucks'})
elif re.match(shows_pattern, line) != None:
hand["showdown_action"].append({'player': re.match(shows_pattern, line).group(1),
'action': 'shows',
'cards': re.match(shows_pattern, line).group(2)})
elif re.match(collect_pot_pattern, line) != None:
match = re.match(collect_pot_pattern, line)
hand["showdown_action"].append({'player': match.group(1),
'action': 'collects_pot',
'amount': Decimal(match.group(2))})
hand["players"][match.group(1)]["winnings"]+= Decimal(match.group(2))
elif re.match(cashout_pattern, line) != None:
match = re.match(cashout_pattern, line)
hand["showdown_action"].append({'player': match.group(1),
'action': 'collects_pot',
'amount': Decimal(match.group(2))})
hand["players"][match.group(1)]["winnings"]+= Decimal(match.group(2))
line = file.readline()
Parse_summary(hand, file)
yield hand
def Parse_summary(hand,file):
line = file.readline()
match = re.match("Total pot \$(\d*(?:\.\d\d)?)( Main pot \d*. Side pot \d*.)? \| Rake \$(\d*(?:\.\d\d)?)" , line)
hand["pot"] = match.group(1)
hand["rake"] = match.group(2)
line = file.readline()
match = re.match("Board \[([AKQJTschd2-9 ]*)\]" , line)
while file.readline() != "\n":
pass
file.readline()
file.readline()
return
my_name = ""
money_pattern = '\$\d*(?:\.\d\d)?' ## regex pattern for dollar amounts in the form $x.yz or $x
## regex patterns to parse the 2 header lines for each hand
header_pattern1 = "PokerStars Hand #(\d*): Hold'em No Limit \((\$\d*(?:\.\d\d)?/\$\d*(?:\.\d\d)? USD)\) - (\d{4}/\d{2}/\d{2}) (\d{2}:\d{2}:\d{2}) WET \[\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} ET\]"
header_pattern2 = "Table '([a-zA-Z\s]*)' (\d)-max Seat #(\d) is the button"
## regex for seat allocations, group 1: seat number, group 2: player name, group 3: chip stack
seat_pattern = "Seat (\d): (.*) \(\$(\d*(?:\.\d\d)?) in chips\) "#
blinds_pattern = "(.*): posts (big|small) blind \$(\d*(?:\.\d\d)?)"
delt_pattern = 'Dealt to ' + my_name + ' \[([AKQJT2-9][schd] [AKQJT2-9][schd])\]'
bet_pattern = '(.*): (folds|bets|calls|raises|checks)(.*)'
shows_pattern = '(.*): shows \[([AKQJT2-9][schd] [AKQJT2-9][schd])\] \(([a-zA-Z\s]*)\)'
mucks_pattern = '(.*): mucks hand'
return_pattern = 'Uncalled bet \(\$(\d*(?:\.\d\d)?)\) returned to (.*)'
collect_pot_pattern = '(.*) collected \$(\d*(?:\.\d\d)?) from pot'
limit_pattern = '\*\*\* (FLOP|TURN|RIVER|SHOW DOWN) \*\*\*'
cashout_pattern = '(.*) cashed out the hand for \$(\d*(?:\.\d\d)?) \| Cash Out Fee \$(\d*(?:\.\d\d)?)'
betting_rounds = ["pre_flop", "flop", "turn", "river"]