Last active
August 3, 2016 23:20
-
-
Save ProfPh/b05e486e90d5ee383508053e1f533243 to your computer and use it in GitHub Desktop.
play money to real money hand history converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
from glob import glob | |
import regex as re | |
import time | |
import os | |
file_count = 0 | |
line_count = 0 | |
first_run = True | |
hand = "" | |
def formatter(capture, multi=False): | |
# regex capture group 0 is the full match, e.g. "posts small blind 50" | |
full = capture.group(0) | |
# regex capture group 1 and 2 are the numbers inside the match, e.g. "50" | |
num = capture.group(2) if multi else capture.group(1) | |
if num is None: | |
return "" | |
# "REPL~" is our dummy string while we convert between old play money string and new real money string | |
# bad code... | |
string = full.replace(num, "REPL~") | |
# Dividing all values by 100 to make it more realistic: 10 NL Play Money becomes $0.10 BB table | |
num = round(int(num) / 100, 2) | |
# Add 2 decimal places if necessary | |
num = "${:.2f}".format(num) | |
formatted = string.replace("REPL~", num) | |
# If there are multiple replacements, such as "raises 500 to 600" | |
if multi: | |
num_2 = capture.group(3) | |
formatted = formatted.replace(num_2, "REPL2~") | |
num_2 = round(int(num_2) / 100, 2) | |
num_2 = "${:.2f}".format(num_2) | |
formatted.replace(num_2, "REPL2~") | |
return formatted.replace("REPL2~", num_2) | |
return formatted | |
def convert(): | |
global file_count | |
global line_count | |
global hand | |
# For every .txt file found by glob, try and convert play money to real money | |
for file in hh: | |
existing_lines = 0 | |
source = open(file, 'r', encoding='utf-8-sig') | |
# Converted files have their file name appended with "HM2" and are copied to a "HM" subdirectory | |
destination_file_name = "HM\HM2 " + file.replace("Play Money ", "") | |
# If file already exists, update new hands | |
try: | |
# Get lines of existing file | |
destination_read = open(destination_file_name, 'r', encoding='utf-8-sig') | |
existing_lines = sum(1 for _ in destination_read) | |
destination_read.close() | |
except FileNotFoundError: | |
# Make "HM2" directory if it doesn't exist | |
os.makedirs(os.path.dirname(destination_file_name), exist_ok=True) | |
pass | |
destination = open(destination_file_name, 'a+', encoding='utf-8-sig') | |
# Skip to where we left off for an ongoing session | |
for i in range(existing_lines): | |
next(source) | |
for line in source: | |
# Actions and events where we need to convert unit-less play money to "real" money | |
regex_sub = ("posts small blind (\d+)", lambda m: formatter(m)), \ | |
("posts big blind (\d+)", lambda m: formatter(m)), \ | |
("posts the ante (\d+)", lambda m: formatter(m)), \ | |
("calls (\d+)", lambda m: formatter(m)), \ | |
("bets (\d+)", lambda m: formatter(m)), \ | |
("Main pot (\d+)", lambda m: formatter(m)), \ | |
("Side pot (\d+)", lambda m: formatter(m)), \ | |
("Total pot (\d+)", lambda m: formatter(m)), \ | |
("| Rake (\d+)", lambda m: formatter(m)), \ | |
("and received (\d+)", lambda m: formatter(m)), \ | |
("and receives (\d+)", lambda m: formatter(m)), \ | |
("(\d+) in chips", lambda m: formatter(m)), \ | |
("\(((\d+)/(\d+))\)", lambda m: formatter(m, True)), \ | |
("(raises (\d+) to (\d+))", lambda m: formatter(m, True)), \ | |
("\((\d+)\)", lambda m: formatter(m)), \ | |
("collected (\d+)", lambda m: formatter(m)), \ | |
# Regex substitution | |
line = reduce(lambda a, kv: re.sub(*kv, a), regex_sub, line) | |
# Find latest hand | |
if line.startswith("Dealt to "): | |
hand = re.search(r'\[([^]]*)\]', line).group(1) | |
line_count += 1 | |
destination.write(line) | |
destination.close() | |
source.close() | |
if line_count != 0: | |
file_count += 1 | |
print("Hand history will be checked every 3 seconds\nNew hands will automatically be converted") | |
while True: | |
# Find any .txt file that starts with HH and convert it | |
hh = glob("HH*.txt") | |
convert() | |
if line_count != 0: | |
# Output number of conversions and time of conversion | |
file_string = "file" if file_count == 1 else "files" | |
current_time = time.strftime("%H:%M:%S", time.localtime(time.time())) | |
print(current_time + ": Converted {0} {1} updating {2} lines, last hand [{3}]".format(file_count, file_string, line_count, hand)) | |
first_run = False | |
if first_run: | |
current_time = time.strftime("%H:%M:%S", time.localtime(time.time())) | |
print(current_time + ": No new hands found") | |
first_run = False | |
file_count = 0 | |
line_count = 0 | |
# 3 second delay between scans for new/updated files | |
time.sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment