2008-08-04 05:44:28 +02:00
#!/usr/bin/python
#Copyright 2008 Steffen Jobbagy-Felso
#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.
2009-07-29 07:17:51 +02:00
#parses an in-memory fpdb hand history and calls db routine to store it
import sys
2008-08-04 05:44:28 +02:00
import fpdb_simple
2009-06-19 22:15:52 +02:00
import Database
2009-07-26 02:42:09 +02:00
from time import time , strftime
2009-08-12 02:46:39 +02:00
from Exceptions import *
2008-08-04 05:44:28 +02:00
2009-07-29 07:17:51 +02:00
2008-08-04 05:44:28 +02:00
#parses a holdem hand
2009-07-31 22:24:21 +02:00
def mainParser ( settings , siteID , category , hand , config , db = None , writeq = None ) :
2009-07-29 07:17:51 +02:00
2009-07-26 02:42:09 +02:00
t0 = time ( )
2009-07-29 00:58:10 +02:00
#print "mainparser"
2009-06-17 22:43:30 +02:00
backend = settings [ ' db-backend ' ]
2009-07-31 22:24:21 +02:00
# Ideally db connection is passed in, if not use sql list if passed in, otherwise start from scratch
2009-06-26 00:14:32 +02:00
if db == None :
db = Database . Database ( c = config , sql = None )
2009-03-18 22:07:32 +01:00
category = fpdb_simple . recogniseCategory ( hand [ 0 ] )
2009-03-19 16:27:08 +01:00
2009-03-19 03:05:01 +01:00
base = " hold " if category == " holdem " or category == " omahahi " or category == " omahahilo " else " stud "
2009-03-19 16:27:08 +01:00
2008-12-08 02:38:06 +01:00
#part 0: create the empty arrays
2009-03-18 22:07:32 +01:00
lineTypes = [ ] #char, valid values: header, name, cards, action, win, rake, ignore
lineStreets = [ ] #char, valid values: (predeal, preflop, flop, turn, river)
2008-08-04 05:44:28 +02:00
2008-12-08 02:38:06 +01:00
cardValues , cardSuits , boardValues , boardSuits , antes , actionTypes , allIns , actionAmounts , actionNos , actionTypeByNo , seatLines , winnings , rakes = [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ]
2008-08-04 05:44:28 +02:00
2008-12-08 02:38:06 +01:00
#part 1: read hand no and check for duplicate
2009-03-18 22:07:32 +01:00
siteHandNo = fpdb_simple . parseSiteHandNo ( hand [ 0 ] )
2009-06-17 22:43:30 +02:00
#print "siteHandNo =", siteHandNo
2009-03-19 16:27:08 +01:00
handStartTime = fpdb_simple . parseHandStartTime ( hand [ 0 ] )
2008-12-08 02:38:06 +01:00
2009-03-18 22:07:32 +01:00
isTourney = fpdb_simple . isTourney ( hand [ 0 ] )
smallBlindLine = 0
for i , line in enumerate ( hand ) :
if ' posts small blind ' in line or ' posts the small blind ' in line :
if line [ - 2 : ] == " $0 " : continue
smallBlindLine = i
2008-12-08 02:38:06 +01:00
break
2009-03-19 16:27:08 +01:00
2009-07-29 00:58:10 +02:00
gametypeID = fpdb_simple . recogniseGametypeID ( backend , db , db . get_cursor ( ) , hand [ 0 ] , hand [ smallBlindLine ] , siteID , category , isTourney )
2008-12-08 02:38:06 +01:00
if isTourney :
2009-03-18 22:07:32 +01:00
siteTourneyNo = fpdb_simple . parseTourneyNo ( hand [ 0 ] )
buyin = fpdb_simple . parseBuyin ( hand [ 0 ] )
fee = fpdb_simple . parseFee ( hand [ 0 ] )
entries = - 1 #todo: parse this
prizepool = - 1 #todo: parse this
knockout = 0
tourneyStartTime = handStartTime #todo: read tourney start time
rebuyOrAddon = fpdb_simple . isRebuyOrAddon ( hand [ 0 ] )
2008-08-18 06:58:41 +02:00
2009-08-26 01:13:34 +02:00
## The tourney site id has to be searched because it may already be in db with a TourneyTypeId which is different from the one automatically calculated (Summary import first)
tourneyTypeId = fpdb_simple . recogniseTourneyTypeId ( db , siteID , siteTourneyNo , buyin , fee , knockout , rebuyOrAddon )
2009-07-29 07:17:51 +02:00
else :
siteTourneyNo = - 1
buyin = - 1
fee = - 1
entries = - 1
prizepool = - 1
knockout = 0
tourneyStartTime = None
rebuyOrAddon = - 1
tourneyTypeId = 1
2009-08-06 21:31:46 +02:00
fpdb_simple . isAlreadyInDB ( db , gametypeID , siteHandNo )
2008-12-08 02:38:06 +01:00
2009-03-27 16:54:13 +01:00
hand = fpdb_simple . filterCrap ( hand , isTourney )
2009-03-21 16:34:23 +01:00
2008-12-08 02:38:06 +01:00
#part 2: classify lines by type (e.g. cards, action, win, sectionchange) and street
fpdb_simple . classifyLines ( hand , category , lineTypes , lineStreets )
#part 3: read basic player info
#3a read player names, startcashes
2009-03-18 22:07:32 +01:00
for i , line in enumerate ( hand ) :
if lineTypes [ i ] == " name " :
seatLines . append ( line )
2009-03-19 16:27:08 +01:00
2009-03-18 22:07:32 +01:00
names = fpdb_simple . parseNames ( seatLines )
2009-08-08 07:56:53 +02:00
playerIDs = fpdb_simple . recognisePlayerIDs ( db , names , siteID ) # inserts players as needed
2009-03-19 16:27:08 +01:00
tmp = fpdb_simple . parseCashesAndSeatNos ( seatLines )
2009-03-18 22:07:32 +01:00
startCashes = tmp [ ' startCashes ' ]
seatNos = tmp [ ' seatNos ' ]
2008-12-08 02:38:06 +01:00
fpdb_simple . createArrays ( category , len ( names ) , cardValues , cardSuits , antes , winnings , rakes , actionTypes , allIns , actionAmounts , actionNos , actionTypeByNo )
#3b read positions
2009-03-18 22:07:32 +01:00
if base == " hold " :
positions = fpdb_simple . parsePositions ( hand , names )
2008-12-08 02:38:06 +01:00
#part 4: take appropriate action for each line based on linetype
2009-03-18 22:07:32 +01:00
for i , line in enumerate ( hand ) :
if lineTypes [ i ] == " cards " :
2009-03-19 16:27:08 +01:00
fpdb_simple . parseCardLine ( category , lineStreets [ i ] , line , names , cardValues , cardSuits , boardValues , boardSuits )
2008-12-08 02:38:06 +01:00
#if category=="studhilo":
# print "hand[i]:", hand[i]
# print "cardValues:", cardValues
# print "cardSuits:", cardSuits
2009-03-18 22:07:32 +01:00
elif lineTypes [ i ] == " action " :
2009-03-19 16:27:08 +01:00
fpdb_simple . parseActionLine ( base , isTourney , line , lineStreets [ i ] , playerIDs , names , actionTypes , allIns , actionAmounts , actionNos , actionTypeByNo )
2009-03-18 22:07:32 +01:00
elif lineTypes [ i ] == " win " :
2009-03-19 16:27:08 +01:00
fpdb_simple . parseWinLine ( line , names , winnings , isTourney )
2009-03-18 22:07:32 +01:00
elif lineTypes [ i ] == " rake " :
totalRake = 0 if isTourney else fpdb_simple . parseRake ( line )
2008-12-08 02:38:06 +01:00
fpdb_simple . splitRake ( winnings , rakes , totalRake )
2009-03-18 22:07:32 +01:00
elif lineTypes [ i ] == " header " or lineTypes [ i ] == " rake " or lineTypes [ i ] == " name " or lineTypes [ i ] == " ignore " :
2008-12-08 02:38:06 +01:00
pass
2009-03-18 22:07:32 +01:00
elif lineTypes [ i ] == " ante " :
2009-03-19 16:27:08 +01:00
fpdb_simple . parseAnteLine ( line , isTourney , names , antes )
2009-03-18 22:07:32 +01:00
elif lineTypes [ i ] == " table " :
2009-03-19 16:27:08 +01:00
tableResult = fpdb_simple . parseTableLine ( base , line )
2008-12-08 02:38:06 +01:00
else :
2009-08-12 02:46:39 +02:00
raise FpdbError ( " unrecognised lineType: " + lineTypes [ i ] )
2009-03-19 16:27:08 +01:00
2009-03-18 22:07:32 +01:00
maxSeats = tableResult [ ' maxSeats ' ]
tableName = tableResult [ ' tableName ' ]
2008-12-08 02:38:06 +01:00
#print "before part5, antes:", antes
2009-06-19 22:15:52 +02:00
#part 5: final preparations, then call Database.* with
2008-12-08 02:38:06 +01:00
# the arrays as they are - that file will fill them.
fpdb_simple . convertCardValues ( cardValues )
2009-03-18 22:07:32 +01:00
if base == " hold " :
2008-12-08 02:38:06 +01:00
fpdb_simple . convertCardValuesBoard ( boardValues )
fpdb_simple . convertBlindBet ( actionTypes , actionAmounts )
fpdb_simple . checkPositions ( positions )
2009-07-29 00:58:10 +02:00
c = db . get_cursor ( )
2009-08-06 21:31:46 +02:00
c . execute ( " SELECT limitType FROM Gametypes WHERE id= %s " % ( db . sql . query [ ' placeholder ' ] , ) , ( gametypeID , ) )
2009-07-29 00:58:10 +02:00
limit_type = c . fetchone ( ) [ 0 ]
2009-03-10 15:03:02 +01:00
fpdb_simple . convert3B4B ( category , limit_type , actionTypes , actionAmounts )
2008-12-08 02:38:06 +01:00
2009-03-18 22:07:32 +01:00
totalWinnings = sum ( winnings )
2008-12-08 02:38:06 +01:00
2009-03-18 22:07:32 +01:00
# if hold'em, use positions and not antes, if stud do not use positions, use antes
2009-06-17 22:43:30 +02:00
# this is used for handsplayers inserts, so still needed even if hudcache update is being skipped
2009-03-18 22:07:32 +01:00
if base == " hold " :
hudImportData = fpdb_simple . generateHudCacheData ( playerIDs , base , category , actionTypes
2008-12-04 00:14:03 +01:00
, allIns , actionTypeByNo , winnings , totalWinnings , positions
2008-12-13 03:02:07 +01:00
, actionTypes , actionAmounts , None )
2008-12-08 02:38:06 +01:00
else :
2009-03-18 22:07:32 +01:00
hudImportData = fpdb_simple . generateHudCacheData ( playerIDs , base , category , actionTypes
2008-12-04 00:14:03 +01:00
, allIns , actionTypeByNo , winnings , totalWinnings , None
2008-12-13 03:02:07 +01:00
, actionTypes , actionAmounts , antes )
2009-03-19 16:27:08 +01:00
2009-07-26 02:42:09 +02:00
#print "parse: hand data prepared" # only reads up to here apart from inserting new players
try :
2009-07-29 00:58:10 +02:00
db . commit ( ) # need to commit new players as different db connection used
2009-07-26 02:42:09 +02:00
# for other writes. maybe this will change maybe not ...
except :
2009-07-29 07:17:51 +02:00
print " parse: error during commit: " + str ( sys . exc_value )
2009-07-26 02:42:09 +02:00
2009-07-31 05:42:57 +02:00
# HERE's an ugly kludge to keep from failing when positions is undef
# We'll fix this by getting rid of the legacy importer. REB
try :
if positions : pass
except :
positions = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
2009-07-29 07:17:51 +02:00
# save data structures in a HandToWrite instance and then insert into database:
htw = Database . HandToWrite ( )
htw . set_all ( config , settings , base , category , siteTourneyNo , buyin
, fee , knockout , entries , prizepool , tourneyStartTime
, isTourney , tourneyTypeId , siteID , siteHandNo
, gametypeID , handStartTime , names , playerIDs , startCashes
, positions , antes , cardValues , cardSuits , boardValues , boardSuits
, winnings , rakes , actionTypes , allIns , actionAmounts
, actionNos , hudImportData , maxSeats , tableName , seatNos )
2009-07-31 22:24:21 +02:00
# save hand in db via direct call or via q if in a thread
if writeq == None :
result = db . store_the_hand ( htw )
else :
writeq . put ( htw )
result = - 999 # meaning unknown
2009-07-29 07:17:51 +02:00
2009-07-26 02:42:09 +02:00
t9 = time ( )
#print "parse and save=(%4.3f)" % (t9-t0)
2008-12-08 02:38:06 +01:00
return result
2008-08-04 05:44:28 +02:00
#end def mainParser