Improve session viewer. There were some off-by-one errors.
[fpdb-dooglus.git] / pyfpdb / PacificPokerToFpdb.py
blob54d3f412ae206b5bef6ec47b58216c53d5d4644f
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright 2008-2010, Carl Gherardi
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ########################################################################
21 import L10n
22 _ = L10n.get_translation()
24 # DONE: Holdem: nl, pl, fl
25 # TODO: Tournaments and SNG import
26 # TODO: bulkloading summary files hangs fpdb
27 # TODO: Ring player stats do not always show, cause in the hhc?
30 import logging
31 # logging has been set up in fpdb.py or HUD_main.py, use their settings:
32 log = logging.getLogger("888hhc")
33 log.info("PacificPokerToFpdb.py")
35 import sys
36 from HandHistoryConverter import *
37 from decimal_wrapper import Decimal
39 # PacificPoker HH Format
41 class PacificPoker(HandHistoryConverter):
43 # Class Variables
45 sitename = "PacificPoker"
46 filetype = "text"
47 codepage = ("utf8", "cp1252")
48 siteId = 13 # Needs to match id entry in Sites database
50 mixes = { 'HORSE': 'horse', '8-Game': '8game', 'HOSE': 'hose'} # Legal mixed games
51 sym = {'USD': "\$", 'CAD': "\$", 'T$': "", "EUR": "\xe2\x82\xac", "GBP": "\xa3", "play": ""} # ADD Euro, Sterling, etc HERE
52 substitutions = {
53 'LEGAL_ISO' : "USD|EUR|GBP|CAD|FPP", # legal ISO currency codes
54 'LS' : u"\$|\xe2\x82\xac|\u20ac|" # legal currency symbols - Euro(cp1252, utf-8)
57 # translations from captured groups to fpdb info strings
58 # not needed for PacificPoker
59 #Lim_Blinds = { '0.01': ('0.01', '0.02'),
60 # '0.02': ('0.02', '0.04'),
61 # '0.03': ('0.03', '0.06'),
62 # '0.05': ('0.05', '0.10'),
63 # '0.12': ('0.12', '0.25'),
64 # '0.25': ('0.25', '0.50'),
65 # '0.50': ('0.50', '1.00'),
66 # '1.00': ('1.00', '2.00'), '1': ('1.00', '2.00'),
67 # '2.00': ('2.00', '4.00'), '2': ('2.00', '4.00'),
68 # '3.00': ('3.00', '6.00'), '3': ('3.00', '6.00'),
69 # '5.00': ('5.00', '10.00'), '5': ('5.00', '10.00'),
70 # '10.00': ('10.00', '20.00'), '10': ('10.00', '20.00'),
71 # '15.00': ('15.00', '30.00'), '15': ('15.00', '30.00'),
72 # '30.00': ('30.00', '60.00'), '30': ('30.00', '60.00'),
73 # '50.00': ('50.00', '100.00'), '50': ('50.00', '100.00'),
74 # '75.00': ('75.00', '150.00'), '75': ('75.00', '150.00'),
75 # '100.00': ('100.00', '200.00'), '100': ('100.00', '200.00'),
76 # '200.00': ('200.00', '400.00'), '200': ('200.00', '400.00'),
77 # '250.00': ('250.00', '500.00'), '250': ('250.00', '500.00')
78 # }
80 limits = { 'No Limit':'nl', 'Pot Limit':'pl', 'Limit':'fl', 'LIMIT':'fl', 'Fix Limit':'fl' }
82 games = { # base, category
83 'Holdem' : ('hold','holdem'),
84 'Omaha' : ('hold','omahahi'),
85 'Omaha Hi/Lo' : ('hold','omahahilo'),
86 'OmahaHL' : ('hold','omahahilo'),
87 'Razz' : ('stud','razz'),
88 'RAZZ' : ('stud','razz'),
89 '7 Card Stud' : ('stud','studhi'),
90 '7 Card Stud Hi/Lo' : ('stud','studhilo'),
91 'Badugi' : ('draw','badugi'),
92 'Triple Draw 2-7 Lowball' : ('draw','27_3draw'),
93 'Single Draw 2-7 Lowball' : ('draw','27_1draw'),
94 '5 Card Draw' : ('draw','fivedraw')
97 currencies = { u'€':'EUR', '$':'USD', '':'T$' }
99 # Static regexes
100 re_GameInfo = re.compile(u"""
101 \#Game\sNo\s:\s(?P<HID>[0-9]+)\\n
102 \*\*\*\*\*\sCassava\sHand\sHistory\sfor\sGame\s[0-9]+\s\*\*\*\*\*\\n
103 (?P<CURRENCY>%(LS)s)?(?P<SB>[.,0-9]+)/(%(LS)s)?(?P<BB>[.,0-9]+)\sBlinds\s
104 (?P<LIMIT>No\sLimit|Fix\sLimit|Pot\sLimit)\s
105 (?P<GAME>Holdem|Omaha|OmahaHL)
106 \s-\s\*\*\*\s
107 (?P<DATETIME>.*$)
108 """ % substitutions, re.MULTILINE|re.VERBOSE)
110 re_PlayerInfo = re.compile(u"""
111 ^Seat\s(?P<SEAT>[0-9]+):\s
112 (?P<PNAME>.*)\s
113 \(\s(%(LS)s)?(?P<CASH>[.,0-9]+)\s\)""" % substitutions,
114 re.MULTILINE|re.VERBOSE)
116 re_HandInfo = re.compile("""
117 ^Table\s(?P<TABLE>[-\ \#a-zA-Z\d]+)\s
118 (\(Real\sMoney\))?
119 (?P<PLAY>\(Practice\sPlay\))?
121 Seat\s(?P<BUTTON>[0-9]+)\sis\sthe\sbutton
122 """, re.MULTILINE|re.VERBOSE)
124 re_SplitHands = re.compile('\n\n+')
125 re_TailSplitHands = re.compile('(\n\n\n+)')
126 re_Button = re.compile('Seat (?P<BUTTON>\d+) is the button', re.MULTILINE)
127 re_Board = re.compile(r"\[\s(?P<CARDS>.+)\s\]")
129 re_DateTime = re.compile("""(?P<D>[0-9]{2})\s(?P<M>[0-9]{2})\s(?P<Y>[0-9]{4})[\- ]+(?P<H>[0-9]+):(?P<MIN>[0-9]+):(?P<S>[0-9]+)""", re.MULTILINE)
131 short_subst = {'PLYR': r'(?P<PNAME>.+?)', 'CUR': '\$?'}
132 re_PostSB = re.compile(r"^%(PLYR)s posts small blind \[%(CUR)s(?P<SB>[.,0-9]+)\]" % short_subst, re.MULTILINE)
133 re_PostBB = re.compile(r"^%(PLYR)s posts big blind \[%(CUR)s(?P<BB>[.,0-9]+)\]" % short_subst, re.MULTILINE)
134 re_Antes = re.compile(r"^%(PLYR)s posts the ante \[%(CUR)s(?P<ANTE>[.,0-9]+)\]" % short_subst, re.MULTILINE)
135 # TODO: unknown in available hand histories for pacificpoker:
136 re_BringIn = re.compile(r"^%(PLYR)s: brings[- ]in( low|) for %(CUR)s(?P<BRINGIN>[.,0-9]+)" % short_subst, re.MULTILINE)
137 re_PostBoth = re.compile(r"^%(PLYR)s posts dead blind \[%(CUR)s(?P<SBBB>[.,0-9]+)\s\+\s%(CUR)s[.,0-9]+\]" % short_subst, re.MULTILINE)
138 re_HeroCards = re.compile(r"^Dealt to %(PLYR)s( \[\s(?P<NEWCARDS>.+?)\s\])" % short_subst, re.MULTILINE)
139 re_Action = re.compile(r"""
140 ^%(PLYR)s(?P<ATYPE>\sbets|\schecks|\sraises|\scalls|\sfolds|\sdiscards|\sstands\spat)
141 (\s\[(%(CUR)s)?(?P<BET>[.,0-9]+)\])?
142 (\s*and\sis\sall.in)?
143 (\s*and\shas\sreached\sthe\s[%(CUR)s\d\.]+\scap)?
144 (\s*cards?(\s\[(?P<DISCARDED>.+?)\])?)?\s*$"""
145 % short_subst, re.MULTILINE|re.VERBOSE)
146 re_ShowdownAction = re.compile(r"^%s shows \[(?P<CARDS>.*)\]" % short_subst['PLYR'], re.MULTILINE)
147 re_sitsOut = re.compile("^%s sits out" % short_subst['PLYR'], re.MULTILINE)
148 re_ShownCards = re.compile("^%s ?(?P<SHOWED>shows|mucks) \[ (?P<CARDS>.*) \]$" % short_subst['PLYR'], re.MULTILINE)
149 re_CollectPot = re.compile(r"^%(PLYR)s collected \[ %(CUR)s(?P<POT>[.,0-9]+) \]$" % short_subst, re.MULTILINE)
151 def compilePlayerRegexs(self, hand):
152 pass
154 def readSupportedGames(self):
155 return [["ring", "hold", "nl"],
156 ["ring", "hold", "pl"],
157 ["ring", "hold", "fl"],
159 ["ring", "stud", "fl"],
161 ["ring", "draw", "fl"],
162 ["ring", "draw", "pl"],
163 ["ring", "draw", "nl"],
165 ["tour", "hold", "nl"],
166 ["tour", "hold", "pl"],
167 ["tour", "hold", "fl"],
169 ["tour", "stud", "fl"],
171 ["tour", "draw", "fl"],
172 ["tour", "draw", "pl"],
173 ["tour", "draw", "nl"],
176 def determineGameType(self, handText):
177 info = {}
178 m = self.re_GameInfo.search(handText)
179 if not m:
180 tmp = handText[0:120]
181 log.error(_("Unable to recognise gametype from: '%s'") % tmp)
182 log.error("determineGameType: " + _("Raising FpdbParseError"))
183 raise FpdbParseError(_("Unable to recognise gametype from: '%s'") % tmp)
185 mg = m.groupdict()
186 #print "DEBUG: mg==", mg
187 if 'LIMIT' in mg:
188 #print "DEBUG: re_GameInfo[LIMIT] \'", mg['LIMIT'], "\'"
189 info['limitType'] = self.limits[mg['LIMIT']]
190 if 'GAME' in mg:
191 #print "DEBUG: re_GameInfo[GAME] \'", mg['GAME'], "\'"
192 (info['base'], info['category']) = self.games[mg['GAME']]
193 if 'SB' in mg:
194 #print "DEBUG: re_GameInfo[SB] \'", mg['SB'], "\'"
195 info['sb'] = mg['SB']
196 if 'BB' in mg:
197 #print "DEBUG: re_GameInfo[BB] \'", mg['BB'], "\'"
198 info['bb'] = mg['BB']
199 if 'CURRENCY' in mg:
200 #print "DEBUG: re_GameInfo[CURRENCY] \'", mg['CURRENCY'], "\'"
201 info['currency'] = self.currencies[mg['CURRENCY']]
203 if 'TOURNO' in mg and mg['TOURNO'] is not None:
204 info['type'] = 'tour'
205 else:
206 info['type'] = 'ring'
208 # Pacific Poker includes the blind levels in the gametype, the following is not needed.
209 #if info['limitType'] == 'fl' and info['bb'] is not None and info['type'] == 'ring' and info['base'] != 'stud':
210 # try:
211 # info['sb'] = self.Lim_Blinds[mg['BB']][0]
212 # info['bb'] = self.Lim_Blinds[mg['BB']][1]
213 # except KeyError:
214 # log.error(_("determineGameType: Lim_Blinds has no lookup for '%s'" % mg['BB']))
215 # log.error(_("determineGameType: Raising FpdbParseError"))
216 # raise FpdbParseError(_("Lim_Blinds has no lookup for '%s'") % mg['BB'])
218 return info
220 def readHandInfo(self, hand):
221 info = {}
222 m = self.re_HandInfo.search(hand.handText,re.DOTALL)
223 m2 = self.re_GameInfo.search(hand.handText)
224 if m is None or m2 is None:
225 log.error(_("No match in readHandInfo: '%s'") % hand.handText[0:100])
226 raise FpdbParseError(_("No match in readHandInfo: '%s'") % hand.handText[0:100])
228 info.update(m.groupdict())
229 info.update(m2.groupdict())
231 log.debug("readHandInfo: %s" % info)
232 for key in info:
233 if key == 'DATETIME':
234 #2008/11/12 10:00:48 CET [2008/11/12 4:00:48 ET] # (both dates are parsed so ET date overrides the other)
235 #2008/08/17 - 01:14:43 (ET)
236 #2008/09/07 06:23:14 ET
237 m1 = self.re_DateTime.finditer(info[key])
238 datetimestr = "2000/01/01 00:00:00" # default used if time not found
239 for a in m1:
240 datetimestr = "%s/%s/%s %s:%s:%s" % (a.group('Y'), a.group('M'),a.group('D'),a.group('H'),a.group('MIN'),a.group('S'))
241 #tz = a.group('TZ') # just assume ET??
242 #print " tz = ", tz, " datetime =", datetimestr
243 hand.startTime = datetime.datetime.strptime(datetimestr, "%Y/%m/%d %H:%M:%S") # also timezone at end, e.g. " ET"
244 hand.startTime = HandHistoryConverter.changeTimezone(hand.startTime, "ET", "UTC")
245 if key == 'HID':
246 hand.handid = info[key]
247 if key == 'TOURNO':
248 hand.tourNo = info[key]
249 if key == 'BUYIN':
250 if hand.tourNo!=None:
251 print "DEBUG: info['BUYIN']: %s" % info['BUYIN']
252 print "DEBUG: info['BIAMT']: %s" % info['BIAMT']
253 print "DEBUG: info['BIRAKE']: %s" % info['BIRAKE']
254 print "DEBUG: info['BOUNTY']: %s" % info['BOUNTY']
255 if info[key] == 'Freeroll':
256 hand.buyin = 0
257 hand.fee = 0
258 hand.buyinCurrency = "FREE"
259 else:
260 if info[key].find("$")!=-1:
261 hand.buyinCurrency="USD"
262 elif info[key].find(u"€")!=-1:
263 hand.buyinCurrency="EUR"
264 elif info[key].find("FPP")!=-1:
265 hand.buyinCurrency="PSFP"
266 else:
267 #FIXME: handle other currencies, FPP, play money
268 raise FpdbParseError(_("Failed to detect currency.") + " " + _("Hand ID: %s: '%s'") % (hand.handid, info[key]))
270 info['BIAMT'] = info['BIAMT'].strip(u'$€FPP')
272 if hand.buyinCurrency!="PSFP":
273 if info['BOUNTY'] != None:
274 # There is a bounty, Which means we need to switch BOUNTY and BIRAKE values
275 tmp = info['BOUNTY']
276 info['BOUNTY'] = info['BIRAKE']
277 info['BIRAKE'] = tmp
278 info['BOUNTY'] = info['BOUNTY'].strip(u'$€') # Strip here where it isn't 'None'
279 hand.koBounty = int(100*Decimal(info['BOUNTY']))
280 hand.isKO = True
281 else:
282 hand.isKO = False
284 info['BIRAKE'] = info['BIRAKE'].strip(u'$€')
286 hand.buyin = int(100*Decimal(info['BIAMT']))
287 hand.fee = int(100*Decimal(info['BIRAKE']))
288 else:
289 hand.buyin = int(Decimal(info['BIAMT']))
290 hand.fee = 0
291 if key == 'LEVEL':
292 hand.level = info[key]
294 if key == 'TABLE':
295 if hand.tourNo != None:
296 hand.tablename = re.split(" ", info[key])[1]
297 else:
298 hand.tablename = info[key]
299 if key == 'BUTTON':
300 hand.buttonpos = info[key]
301 if key == 'MAX' and info[key] != None:
302 hand.maxseats = int(info[key])
304 if key == 'MIXED':
305 hand.mixed = self.mixes[info[key]] if info[key] is not None else None
306 if key == 'PLAY' and info['PLAY'] is not None:
307 # hand.currency = 'play' # overrides previously set value
308 hand.gametype['currency'] = 'play'
310 def readButton(self, hand):
311 m = self.re_Button.search(hand.handText)
312 if m:
313 hand.buttonpos = int(m.group('BUTTON'))
314 else:
315 log.info(_('readButton: not found'))
317 def readPlayerStacks(self, hand):
318 log.debug("readPlayerStacks")
319 m = self.re_PlayerInfo.finditer(hand.handText)
320 for a in m:
321 #print "DEBUG: Seat[", a.group('SEAT'), "]; PNAME[", a.group('PNAME'), "]; CASH[", a.group('CASH'), "]"
322 hand.addPlayer(int(a.group('SEAT')), a.group('PNAME'), a.group('CASH'))
324 def markStreets(self, hand):
325 # PREFLOP = ** Dealing down cards **
326 # This re fails if, say, river is missing; then we don't get the ** that starts the river.
327 if hand.gametype['base'] in ("hold"):
328 m = re.search(r"\*\* Dealing down cards \*\*(?P<PREFLOP>.+(?=\*\* Dealing flop \*\*)|.+)"
329 r"(\*\* Dealing flop \*\* (?P<FLOP>\[ \S\S, \S\S, \S\S \].+(?=\*\* Dealing turn \*\*)|.+))?"
330 r"(\*\* Dealing turn \*\* (?P<TURN>\[ \S\S \].+(?=\*\* Dealing river \*\*)|.+))?"
331 r"(\*\* Dealing river \*\* (?P<RIVER>\[ \S\S \].+))?"
332 , hand.handText,re.DOTALL)
333 if m is None:
334 log.error("Didn't match markStreets")
335 raise FpdbParseError(_("No match in markStreets"))
336 else:
337 #print "DEBUG: Matched markStreets"
338 mg = m.groupdict()
339 # if 'PREFLOP' in mg:
340 # print "DEBUG: PREFLOP: ", [mg['PREFLOP']]
341 # if 'FLOP' in mg:
342 # print "DEBUG: FLOP: ", [mg['FLOP']]
343 # if 'TURN' in mg:
344 # print "DEBUG: TURN: ", [mg['TURN']]
345 # if 'RIVER' in mg:
346 # print "DEBUG: RIVER: ", [mg['RIVER']]
348 hand.addStreets(m)
350 def readCommunityCards(self, hand, street): # street has been matched by markStreets, so exists in this hand
351 if street in ('FLOP','TURN','RIVER'): # a list of streets which get dealt community cards (i.e. all but PREFLOP)
352 #print "DEBUG readCommunityCards:", street, hand.streets.group(street)
353 m = self.re_Board.search(hand.streets[street])
354 hand.setCommunityCards(street, m.group('CARDS').split(', '))
356 def readAntes(self, hand):
357 log.debug(_("reading antes"))
358 m = self.re_Antes.finditer(hand.handText)
359 for player in m:
360 #~ logging.debug("hand.addAnte(%s,%s)" %(player.group('PNAME'), player.group('ANTE')))
361 hand.addAnte(player.group('PNAME'), player.group('ANTE'))
363 def readBringIn(self, hand):
364 m = self.re_BringIn.search(hand.handText,re.DOTALL)
365 if m:
366 #~ logging.debug("readBringIn: %s for %s" %(m.group('PNAME'), m.group('BRINGIN')))
367 hand.addBringIn(m.group('PNAME'), m.group('BRINGIN'))
369 def readBlinds(self, hand):
370 liveBlind = True
371 for a in self.re_PostSB.finditer(hand.handText):
372 if liveBlind:
373 hand.addBlind(a.group('PNAME'), 'small blind', a.group('SB'))
374 liveBlind = False
375 else:
376 # Post dead blinds as ante
377 hand.addBlind(a.group('PNAME'), 'secondsb', a.group('SB'))
378 for a in self.re_PostBB.finditer(hand.handText):
379 hand.addBlind(a.group('PNAME'), 'big blind', a.group('BB'))
380 for a in self.re_PostBoth.finditer(hand.handText):
381 hand.addBlind(a.group('PNAME'), 'both', a.group('SBBB'))
383 def readHeroCards(self, hand):
384 # streets PREFLOP, PREDRAW, and THIRD are special cases beacause
385 # we need to grab hero's cards
386 for street in ('PREFLOP', 'DEAL'):
387 if street in hand.streets.keys():
388 m = self.re_HeroCards.finditer(hand.streets[street])
389 for found in m:
390 # if m == None:
391 # hand.involved = False
392 # else:
393 hand.hero = found.group('PNAME')
394 newcards = found.group('NEWCARDS').split(', ')
395 hand.addHoleCards(street, hand.hero, closed=newcards, shown=False, mucked=False, dealt=True)
397 for street, text in hand.streets.iteritems():
398 if not text or street in ('PREFLOP', 'DEAL'): continue # already done these
399 m = self.re_HeroCards.finditer(hand.streets[street])
400 for found in m:
401 player = found.group('PNAME')
402 if found.group('NEWCARDS') is None:
403 newcards = []
404 else:
405 newcards = found.group('NEWCARDS').split(', ')
406 if found.group('OLDCARDS') is None:
407 oldcards = []
408 else:
409 oldcards = found.group('OLDCARDS').split(', ')
411 if street == 'THIRD' and len(newcards) == 3: # hero in stud game
412 hand.hero = player
413 hand.dealt.add(player) # need this for stud??
414 hand.addHoleCards(street, player, closed=newcards[0:2], open=[newcards[2]], shown=False, mucked=False, dealt=False)
415 else:
416 hand.addHoleCards(street, player, open=newcards, closed=oldcards, shown=False, mucked=False, dealt=False)
419 def readAction(self, hand, street):
420 m = self.re_Action.finditer(hand.streets[street])
421 for action in m:
422 acts = action.groupdict()
423 #print "DEBUG: acts: %s" %acts
424 if action.group('ATYPE') == ' raises':
425 hand.addCallandRaise( street, action.group('PNAME'), action.group('BET').replace(',','') )
426 elif action.group('ATYPE') == ' calls':
427 hand.addCall( street, action.group('PNAME'), action.group('BET').replace(',','') )
428 elif action.group('ATYPE') == ' bets':
429 hand.addBet( street, action.group('PNAME'), action.group('BET').replace(',','') )
430 elif action.group('ATYPE') == ' folds':
431 hand.addFold( street, action.group('PNAME'))
432 elif action.group('ATYPE') == ' checks':
433 hand.addCheck( street, action.group('PNAME'))
434 elif action.group('ATYPE') == ' discards':
435 hand.addDiscard(street, action.group('PNAME'), action.group('BET').replace(',',''), action.group('DISCARDED'))
436 elif action.group('ATYPE') == ' stands pat':
437 hand.addStandsPat( street, action.group('PNAME'))
438 else:
439 print (_("DEBUG:") + " " + _("Unimplemented %s: '%s' '%s'") % ("readAction", action.group('PNAME'), action.group('ATYPE')))
442 def readShowdownActions(self, hand):
443 # TODO: pick up mucks also??
444 for shows in self.re_ShowdownAction.finditer(hand.handText):
445 cards = shows.group('CARDS').split(', ')
446 hand.addShownCards(cards, shows.group('PNAME'))
448 def readCollectPot(self,hand):
449 for m in self.re_CollectPot.finditer(hand.handText):
450 #print "DEBUG: hand.addCollectPot(player=", m.group('PNAME'), ", pot=", m.group('POT'), ")"
451 hand.addCollectPot(player=m.group('PNAME'),pot=m.group('POT').replace(',',''))
453 def readShownCards(self,hand):
454 for m in self.re_ShownCards.finditer(hand.handText):
455 if m.group('CARDS') is not None:
456 cards = m.group('CARDS')
457 cards = cards.split(', ') # needs to be a list, not a set--stud needs the order
459 (shown, mucked) = (False, False)
460 if m.group('SHOWED') == "showed": shown = True
461 elif m.group('SHOWED') == "mucked": mucked = True
463 #print "DEBUG: hand.addShownCards(%s, %s, %s, %s)" %(cards, m.group('PNAME'), shown, mucked)
464 hand.addShownCards(cards=cards, player=m.group('PNAME'), shown=shown, mucked=mucked)