2008-11-07 09:47:00 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
#Copyright 2008 Carl Gherardi
|
|
|
|
#This program is free software: you can redistribute it and/or modify
|
|
|
|
#it under the terms of the GNU Affero General Public License as published by
|
|
|
|
#the Free Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
#This program is distributed in the hope that it will be useful,
|
|
|
|
#but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
#GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
#You should have received a copy of the GNU Affero General Public License
|
|
|
|
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#In the "official" distribution you can find the license in
|
|
|
|
#agpl-3.0.txt in the docs folder of the package.
|
|
|
|
|
2008-11-09 01:46:14 +01:00
|
|
|
import Configuration
|
2008-11-09 12:57:58 +01:00
|
|
|
import FpdbRegex
|
|
|
|
import re
|
2008-11-09 01:46:14 +01:00
|
|
|
import sys
|
|
|
|
import traceback
|
2008-11-09 03:58:46 +01:00
|
|
|
import os
|
|
|
|
import os.path
|
2008-11-09 01:46:14 +01:00
|
|
|
import xml.dom.minidom
|
|
|
|
from xml.dom.minidom import Node
|
|
|
|
|
2008-11-07 09:47:00 +01:00
|
|
|
class HandHistoryConverter:
|
2008-11-09 01:46:14 +01:00
|
|
|
def __init__(self, config, file, sitename):
|
2008-11-09 00:49:05 +01:00
|
|
|
print "HandHistory init called"
|
|
|
|
self.c = config
|
2008-11-09 01:46:14 +01:00
|
|
|
self.sitename = sitename
|
2008-11-09 00:49:05 +01:00
|
|
|
self.obs = "" # One big string
|
|
|
|
self.filetype = "text"
|
|
|
|
self.doc = None # For XML based HH files
|
|
|
|
self.file = file
|
|
|
|
self.hhbase = self.c.get_import_parameters().get("hhArchiveBase")
|
2008-11-09 03:58:46 +01:00
|
|
|
self.hhbase = os.path.expanduser(self.hhbase)
|
|
|
|
self.hhdir = os.path.join(self.hhbase,sitename)
|
2008-11-09 06:33:42 +01:00
|
|
|
self.gametype = []
|
2008-11-09 04:29:58 +01:00
|
|
|
# self.ofile = os.path.join(self.hhdir,file)
|
2008-11-09 12:57:58 +01:00
|
|
|
self.rexx = FpdbRegex.FpdbRegex()
|
2008-11-09 00:49:05 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
tmp = "HandHistoryConverter: '%s'\n" % (self.sitename)
|
2008-11-09 01:46:14 +01:00
|
|
|
tmp = tmp + "\thhbase: '%s'\n" % (self.hhbase)
|
|
|
|
tmp = tmp + "\thhdir: '%s'\n" % (self.hhdir)
|
|
|
|
tmp = tmp + "\tfiletype: '%s'\n" % (self.filetype)
|
|
|
|
tmp = tmp + "\tinfile: '%s'\n" % (self.file)
|
2008-11-09 04:29:58 +01:00
|
|
|
# tmp = tmp + "\toutfile: '%s'\n" % (self.ofile)
|
2008-11-09 06:33:42 +01:00
|
|
|
tmp = tmp + "\tgametype: '%s'\n" % (self.gametype[0])
|
|
|
|
tmp = tmp + "\tgamebase: '%s'\n" % (self.gametype[1])
|
|
|
|
tmp = tmp + "\tlimit: '%s'\n" % (self.gametype[2])
|
2008-11-09 07:27:27 +01:00
|
|
|
tmp = tmp + "\tsb/bb: '%s/%s'\n" % (self.gametype[3], self.gametype[4])
|
2008-11-09 00:49:05 +01:00
|
|
|
return tmp
|
|
|
|
|
2008-11-10 07:41:04 +01:00
|
|
|
def processFile(self):
|
|
|
|
if not self.sanityCheck():
|
|
|
|
print "Cowardly refusing to continue after failed sanity check"
|
|
|
|
return
|
|
|
|
self.readFile(self.file)
|
|
|
|
self.gametype = self.determineGameType()
|
|
|
|
self.hands = self.splitFileIntoHands()
|
2008-11-10 14:02:56 +01:00
|
|
|
for hand in self.hands:
|
|
|
|
self.readHandInfo(hand)
|
|
|
|
self.writeHand("output file", hand)
|
2008-11-10 07:41:04 +01:00
|
|
|
|
2008-11-07 11:19:18 +01:00
|
|
|
# Functions to be implemented in the inheriting class
|
2008-11-07 09:47:00 +01:00
|
|
|
def readSupportedGames(self): abstract
|
2008-11-09 06:33:42 +01:00
|
|
|
|
|
|
|
# should return a list
|
|
|
|
# type base limit
|
|
|
|
# [ ring, hold, nl , sb, bb ]
|
|
|
|
# Valid types specified in docs/tabledesign.html in Gametypes
|
2008-11-07 09:47:00 +01:00
|
|
|
def determineGameType(self): abstract
|
2008-11-10 14:02:56 +01:00
|
|
|
def readHandInfo(self, hand): abstract
|
2008-11-07 09:47:00 +01:00
|
|
|
def readPlayerStacks(self): abstract
|
|
|
|
def readBlinds(self): abstract
|
|
|
|
def readAction(self): abstract
|
|
|
|
|
2008-11-09 03:58:46 +01:00
|
|
|
def sanityCheck(self):
|
|
|
|
sane = False
|
|
|
|
base_w = False
|
|
|
|
#Check if hhbase exists and is writable
|
|
|
|
#Note: Will not try to create the base HH directory
|
|
|
|
if not (os.access(self.hhbase, os.W_OK) and os.path.isdir(self.hhbase)):
|
|
|
|
print "HH Sanity Check: Directory hhbase '" + self.hhbase + "' doesn't exist or is not writable"
|
|
|
|
else:
|
|
|
|
#Check if hhdir exists and is writable
|
|
|
|
if not os.path.isdir(self.hhdir):
|
|
|
|
# In first pass, dir may not exist. Attempt to create dir
|
|
|
|
print "Creating directory: '%s'" % (self.hhdir)
|
|
|
|
os.mkdir(self.hhdir)
|
|
|
|
sane = True
|
|
|
|
elif os.access(self.hhdir, os.W_OK):
|
|
|
|
sane = True
|
|
|
|
else:
|
|
|
|
print "HH Sanity Check: Directory hhdir '" + self.hhdir + "' or its parent directory are not writable"
|
|
|
|
|
|
|
|
return sane
|
2008-11-09 00:49:05 +01:00
|
|
|
|
2008-11-07 11:19:18 +01:00
|
|
|
# Functions not necessary to implement in sub class
|
2008-11-09 00:49:05 +01:00
|
|
|
def setFileType(self, filetype = "text"):
|
|
|
|
self.filetype = filetype
|
|
|
|
|
2008-11-09 12:57:58 +01:00
|
|
|
def splitFileIntoHands(self):
|
|
|
|
hands = []
|
|
|
|
list = self.rexx.split_hand_re.split(self.obs)
|
2008-11-10 14:02:56 +01:00
|
|
|
list.pop() #Last entry is empty
|
2008-11-09 12:57:58 +01:00
|
|
|
for l in list:
|
2008-11-10 14:02:56 +01:00
|
|
|
# print "'" + l + "'"
|
2008-11-10 07:41:04 +01:00
|
|
|
hands = hands + [Hand(self.sitename, self.gametype, l)]
|
|
|
|
return hands
|
2008-11-09 00:49:05 +01:00
|
|
|
|
2008-11-07 09:47:00 +01:00
|
|
|
def readFile(self, filename):
|
|
|
|
"""Read file"""
|
2008-11-09 01:46:14 +01:00
|
|
|
print "Reading file: '%s'" %(filename)
|
2008-11-09 00:49:05 +01:00
|
|
|
if(self.filetype == "text"):
|
|
|
|
infile=open(filename, "rU")
|
2008-11-09 01:46:14 +01:00
|
|
|
self.obs = infile.read()
|
|
|
|
infile.close()
|
2008-11-09 00:49:05 +01:00
|
|
|
elif(self.filetype == "xml"):
|
|
|
|
try:
|
|
|
|
doc = xml.dom.minidom.parse(filename)
|
|
|
|
self.doc = doc
|
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stderr)
|
2008-11-07 09:47:00 +01:00
|
|
|
|
2008-11-09 12:57:58 +01:00
|
|
|
def writeHand(self, file, hand):
|
2008-11-07 09:47:00 +01:00
|
|
|
"""Write out parsed data"""
|
2008-11-10 14:02:56 +01:00
|
|
|
print "%s Game #%s: %s (%d/%d) - %s" %(hand.sitename, hand.handid, "XXXXhand.gametype", hand.sb, hand.bb, hand.starttime)
|
2008-11-10 14:29:49 +01:00
|
|
|
print "Table '%s' %d-max Seat #%s is the button" %(hand.tablename, hand.maxseats, hand.buttonpos)
|
2008-11-09 00:49:05 +01:00
|
|
|
#
|
|
|
|
# counter = 1
|
|
|
|
# for player in seating:
|
|
|
|
# print "Seat " + counter + ": " + playername + "($" + playermoney + " in chips"
|
|
|
|
#
|
|
|
|
# print playername + ": posts small blind " + sb
|
|
|
|
# print playername + ": posts big blind " + bb
|
|
|
|
#
|
2008-11-10 14:02:56 +01:00
|
|
|
print "*** HOLE CARDS ***"
|
2008-11-09 00:49:05 +01:00
|
|
|
# print "Dealt to " + hero + " [" + holecards + "]"
|
|
|
|
#
|
|
|
|
## ACTION STUFF
|
|
|
|
#
|
2008-11-10 14:02:56 +01:00
|
|
|
print "*** SUMMARY ***"
|
2008-11-09 00:49:05 +01:00
|
|
|
# print "Total pot $" + totalpot + " | Rake $" + rake
|
|
|
|
# print "Board [" + boardcards + "]"
|
|
|
|
#
|
|
|
|
## SUMMARY STUFF
|
2008-11-07 11:19:18 +01:00
|
|
|
|
2008-11-09 07:27:27 +01:00
|
|
|
#takes a poker float (including , for thousand seperator and converts it to an int
|
|
|
|
def float2int (self, string):
|
|
|
|
pos=string.find(",")
|
|
|
|
if (pos!=-1): #remove , the thousand seperator
|
|
|
|
string=string[0:pos]+string[pos+1:]
|
|
|
|
|
|
|
|
pos=string.find(".")
|
|
|
|
if (pos!=-1): #remove decimal point
|
|
|
|
string=string[0:pos]+string[pos+1:]
|
|
|
|
|
|
|
|
result = int(string)
|
|
|
|
if pos==-1: #no decimal point - was in full dollars - need to multiply with 100
|
|
|
|
result*=100
|
|
|
|
return result
|
|
|
|
#end def float2int
|
2008-11-09 12:57:58 +01:00
|
|
|
|
|
|
|
class Hand:
|
|
|
|
# def __init__(self, sitename, gametype, sb, bb, string):
|
2008-11-10 07:41:04 +01:00
|
|
|
def __init__(self, sitename, gametype, string):
|
|
|
|
self.sitename = sitename
|
|
|
|
self.gametype = gametype
|
2008-11-09 12:57:58 +01:00
|
|
|
self.string = string
|
|
|
|
|
2008-11-10 14:02:56 +01:00
|
|
|
self.handid = 0
|
|
|
|
self.sb = gametype[3]
|
|
|
|
self.bb = gametype[4]
|
2008-11-09 12:57:58 +01:00
|
|
|
self.tablename = "Slartibartfast"
|
|
|
|
self.maxseats = 10
|
|
|
|
self.counted_seats = 0
|
|
|
|
self.buttonpos = 0
|
|
|
|
self.seating = []
|
|
|
|
self.players = []
|
|
|
|
self.action = []
|
|
|
|
|