2 # -*- coding: utf-8 -*-
4 # Copyright 2008-2011, Carl Gherardi
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 ########################################################################
22 _
= L10n
.get_translation()
28 # logging has been set up in fpdb.py or HUD_main.py, use their settings:
29 log
= logging
.getLogger("parser")
33 from HandHistoryConverter
import *
34 from decimal_wrapper
import Decimal
38 class OnGame(HandHistoryConverter
):
41 codepage
= ("utf8", "cp1252")
42 siteId
= 5 # Needs to match id entry in Sites database
44 mixes
= { } # Legal mixed games
45 sym
= {'USD': "\$", 'CAD': "\$", 'T$': "", "EUR": u
"\u20ac", "GBP": "\xa3"}
47 'LEGAL_ISO' : "USD|EUR|GBP|CAD|FPP", # legal ISO currency codes
48 'LS' : u
"\$|\xe2\x82\xac|\u20ac" # legal currency symbols - Euro(cp1252, utf-8)
50 currencies
= { u
'\u20ac':'EUR', u
'\xe2\x82\xac':'EUR', '$':'USD', '':'T$' }
52 limits
= { 'NO_LIMIT':'nl', 'POT_LIMIT':'pl', 'LIMIT':'fl'}
54 games
= { # base, category
55 "TEXAS_HOLDEM" : ('hold','holdem'),
56 'OMAHA_HI' : ('hold','omahahi'),
57 # 'Omaha Hi/Lo' : ('hold','omahahilo'),
58 # 'Razz' : ('stud','razz'),
59 # 'RAZZ' : ('stud','razz'),
60 'SEVEN_CARD_STUD' : ('stud','studhi'),
61 'SEVEN_CARD_STUD_HI_LO' : ('stud','studhilo'),
62 # 'Badugi' : ('draw','badugi'),
63 # 'Triple Draw 2-7 Lowball' : ('draw','27_3draw'),
64 'FIVE_CARD_DRAW' : ('draw','fivedraw')
68 # ***** End of hand R5-75443872-57 *****
69 re_SplitHands
= re
.compile(u
'\*\*\*\*\*\sEnd\sof\shand\s[-A-Z\d]+.*\n(?=\*)')
71 #TODO: detect play money
72 # "Play money" rather than "Real money" and set currency accordingly
73 re_HandInfo
= re
.compile(u
"""
74 \*\*\*\*\*\sHistory\sfor\shand\s(?P<HID>[-A-Z\d]+)
75 (\s\(TOURNAMENT:\s"[a-zA-Z ]+",\s(?P<TID>[-A-Z\d]+),\sbuy-in:\s[%(LS)s](?P<BUYIN>\d+))?
77 Start\shand:\s(?P<DATETIME>.*)
78 Table:\s(\[SPEED\]\s)?(?P<TABLE>[-\'\w\#\s\.]+)\s\[\d+\]\s\(
80 (?P<LIMIT>NO_LIMIT|Limit|LIMIT|Pot\sLimit|POT_LIMIT)\s
81 (?P<GAME>TEXAS_HOLDEM|OMAHA_HI|SEVEN_CARD_STUD|SEVEN_CARD_STUD_HI_LO|RAZZ|FIVE_CARD_DRAW)\s
82 (?P<CURRENCY>%(LS)s|)?(?P<SB>[.0-9]+)/
83 (%(LS)s)?(?P<BB>[.0-9]+)
85 """ % substitutions
, re
.MULTILINE|re
.DOTALL|re
.VERBOSE
)
87 re_TailSplitHands
= re
.compile(u
'(\*\*\*\*\*\sEnd\sof\shand\s[-A-Z\d]+.*\n)(?=\*)')
88 re_Button
= re
.compile('Button: seat (?P<BUTTON>\d+)', re
.MULTILINE
) # Button: seat 2
89 re_Board
= re
.compile(r
"\[(?P<CARDS>.+)\]")
91 # Wed Aug 18 19:45:30 GMT+0100 2010
92 re_DateTime
= re
.compile("""
96 (?P<H>[0-9]+):(?P<MIN>[0-9]+):(?P<S>[0-9]+)\s
97 (?P<OFFSET>\w+[-+]\d+)\s
99 """, re
.MULTILINE|re
.VERBOSE
)
101 #Seat 1: .Lucchess ($4.17 in chips)
102 #Seat 1: phantomaas ($27.11)
103 #Seat 5: mleo17 ($9.37)
104 #Seat 2: Montferat (1500)
105 re_PlayerInfo
= re
.compile(u
'Seat (?P<SEAT>[0-9]+):\s(?P<PNAME>.*)\s\((%(LS)s)?(?P<CASH>[.0-9]+)\)' % substitutions
)
107 def compilePlayerRegexs(self
, hand
):
108 players
= set([player
[1] for player
in hand
.players
])
109 if not players
<= self
.compiledPlayers
: # x <= y means 'x is subset of y'
110 # we need to recompile the player regexs.
111 # TODO: should probably rename re_HeroCards and corresponding method,
112 # since they are used to find all cards on lines starting with "Dealt to:"
113 # They still identify the hero.
114 self
.compiledPlayers
= players
117 #helander2222 posts blind ($0.25), lopllopl posts blind ($0.50).
118 player_re
= "(?P<PNAME>" + "|".join(map(re
.escape
, players
)) + ")"
119 subst
= {'PLYR': player_re
, 'CUR': self
.sym
[hand
.gametype
['currency']]}
120 self
.re_PostSB
= re
.compile('(?P<PNAME>.*) posts small blind \((%(CUR)s)?(?P<SB>[\.0-9]+)\)' % subst
, re
.MULTILINE
)
121 self
.re_PostBB
= re
.compile('(?P<PNAME>.*) posts big blind \((%(CUR)s)?(?P<BB>[\.0-9]+)\)' % subst
, re
.MULTILINE
)
122 self
.re_Antes
= re
.compile(r
"^%(PLYR)s: posts the ante (%(CUR)s)?(?P<ANTE>[\.0-9]+)" % subst
, re
.MULTILINE
)
123 self
.re_BringIn
= re
.compile(r
"^%(PLYR)s: brings[- ]in( low|) for (%(CUR)s)?(?P<BRINGIN>[\.0-9]+)" % subst
, re
.MULTILINE
)
124 self
.re_PostBoth
= re
.compile('(?P<PNAME>.*): posts small \& big blind \( (%(CUR)s)?(?P<SBBB>[\.0-9]+)\)' % subst
)
125 self
.re_PostDead
= re
.compile('(?P<PNAME>.*) posts dead blind \((%(CUR)s)?(?P<DEAD>[\.0-9]+)\)' % subst
, re
.MULTILINE
)
126 self
.re_HeroCards
= re
.compile('Dealing\sto\s%(PLYR)s:\s\[(?P<CARDS>.*)\]' % subst
)
128 self
.re_Action
= re
.compile('(, )?(?P<PNAME>.*?)(?P<ATYPE> bets| checks| raises| calls| folds)( (%(CUR)s)?(?P<BET>[\d\.]+))?( to (%(CUR)s)?(?P<BET2>[\d\.]+))?( and is all-in)?' % subst
)
129 #self.re_Board = re.compile(r"\[board cards (?P<CARDS>.+) \]")
131 #Uchilka shows [ KC,JD ]
132 self
.re_ShowdownAction
= re
.compile('(?P<PNAME>.*) shows \[ (?P<CARDS>.+) \]')
134 #Main pot: $3.57 won by mleo17 ($3.40)
135 #Side pot 1: $3.26 won by maac_5 ($3.10)
136 #Main pot: $2.87 won by maac_5 ($1.37), sagi34 ($1.36)
137 self
.re_Pot
= re
.compile('(Main|Side)\spot(\s\d+)?:\s.*won\sby\s(?P<POT>.*$)', re
.MULTILINE
)
138 self
.re_CollectPot
= re
.compile('\s*(?P<PNAME>.*)\s\((%(CUR)s)?(?P<POT>[\.\d]+)\)' % subst
)
139 #Seat 5: mleo17 ($3.40), net: +$2.57, [Jd, Qd] (TWO_PAIR QUEEN, JACK)
140 self
.re_ShownCards
= re
.compile("^Seat (?P<SEAT>[0-9]+): (?P<PNAME>.*) \(.*\), net:.* \[(?P<CARDS>.*)\].*" % subst
, re
.MULTILINE
)
141 self
.re_sitsOut
= re
.compile('(?P<PNAME>.*) sits out')
143 def readSupportedGames(self
):
145 ["ring", "hold", "fl"],
146 ["ring", "hold", "pl"],
147 ["ring", "hold", "nl"],
148 ["ring", "stud", "fl"],
149 ["ring", "draw", "fl"],
150 ["tour", "hold", "nl"],
153 def determineGameType(self
, handText
):
154 # Inspect the handText and return the gametype dict
155 # gametype dict is: {'limitType': xxx, 'base': xxx, 'category': xxx}
158 m
= self
.re_HandInfo
.search(handText
)
160 tmp
= handText
[0:100]
161 log
.error(_("Unable to recognise gametype from: '%s'") % tmp
)
162 log
.error("determineGameType: " + _("Raising FpdbParseError"))
163 raise FpdbParseError(_("Unable to recognise gametype from: '%s'") % tmp
)
166 #print "DEBUG: mg: %s" % mg
168 info
['type'] = 'ring'
169 if mg
['TID'] != None:
170 info
['type'] = 'tour'
173 info
['currency'] = self
.currencies
[mg
['CURRENCY']]
176 if mg
['LIMIT'] in self
.limits
:
177 info
['limitType'] = self
.limits
[mg
['LIMIT']]
179 tmp
= handText
[0:100]
180 log
.error(_("limit not found in self.limits(%s). hand: '%s'") % (str(mg
),tmp
))
181 log
.error("determineGameType: " + _("Raising FpdbParseError"))
182 raise FpdbParseError(_("limit not found in self.limits(%s). hand: '%s'") % (str(mg
),tmp
))
184 (info
['base'], info
['category']) = self
.games
[mg
['GAME']]
186 info
['sb'] = mg
['SB']
188 info
['bb'] = mg
['BB']
190 #log.debug("determinegametype: returning "+str(info))
193 def readHandInfo(self
, hand
):
195 m
= self
.re_HandInfo
.search(hand
.handText
)
198 info
.update(m
.groupdict())
200 #log.debug("readHandInfo: %s" % info)
202 if key
== 'DATETIME':
203 #'Wed Aug 18 19:45:30 GMT+0100 2010
204 # %a %b %d %H:%M:%S %z %Y
205 #hand.startTime = time.strptime(m.group('DATETIME'), "%a %b %d %H:%M:%S GMT%z %Y")
206 # Stupid library doesn't seem to support %z (http://docs.python.org/library/time.html?highlight=strptime#time.strptime)
207 # So we need to re-interpret te string to be useful
208 a
= self
.re_DateTime
.search(info
[key
])
210 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'))
211 tzoffset
= a
.group('OFFSET')
213 datetimestr
= "2010/Jan/01 01:01:01"
214 log
.error("readHandInfo: " + _("DATETIME not matched: '%s'") % info
[key
])
215 print (_("DEBUG:") + " readHandInfo: " + _("DATETIME not matched: '%s'") % info
[key
])
216 # TODO: Manually adjust time against OFFSET
217 hand
.startTime
= datetime
.datetime
.strptime(datetimestr
, "%Y/%b/%d %H:%M:%S") # also timezone at end, e.g. " ET"
218 hand
.startTime
= HandHistoryConverter
.changeTimezone(hand
.startTime
, tzoffset
, "UTC")
220 hand
.handid
= info
[key
]
221 # Need to remove non-alphanumerics for MySQL
222 hand
.handid
= hand
.handid
.replace('R','')
223 hand
.handid
= hand
.handid
.replace('-','')
225 hand
.tourNo
= info
[key
]
227 hand
.buyin
= info
[key
]
229 hand
.tablename
= info
[key
]
233 hand
.maxseats
= None # Set to None - Hand.py will guessMaxSeats()
236 def readPlayerStacks(self
, hand
):
237 #log.debug("readplayerstacks: re is '%s'" % self.re_PlayerInfo)
238 m
= self
.re_PlayerInfo
.finditer(hand
.handText
)
240 hand
.addPlayer(int(a
.group('SEAT')), a
.group('PNAME'), a
.group('CASH'))
242 def markStreets(self
, hand
):
243 if hand
.gametype
['base'] in ("hold"):
244 m
= re
.search(r
"pocket cards(?P<PREFLOP>.+(?= Dealing flop )|.+(?=Summary))"
245 r
"( Dealing flop (?P<FLOP>\[\S\S, \S\S, \S\S\].+(?= Dealing turn)|.+(?=Summary)))?"
246 r
"( Dealing turn (?P<TURN>\[\S\S\].+(?= Dealing river)|.+(?=Summary)))?"
247 r
"( Dealing river (?P<RIVER>\[\S\S\].+(?=Summary)))?", hand
.handText
, re
.DOTALL
)
248 elif hand
.gametype
['base'] in ("stud"):
249 m
= re
.search(r
"(?P<ANTES>.+(?=Dealing pocket cards)|.+)"
250 r
"(Dealing pocket cards(?P<THIRD>.+(?=Dealing 4th street)|.+))?"
251 r
"(Dealing 4th street(?P<FOURTH>.+(?=Dealing 5th street)|.+))?"
252 r
"(Dealing 5th street(?P<FIFTH>.+(?=Dealing 6th street)|.+))?"
253 r
"(Dealing 6th street(?P<SIXTH>.+(?=Dealing river)|.+))?"
254 r
"(Dealing river(?P<SEVENTH>.+))?", hand
.handText
,re
.DOTALL
)
255 elif hand
.gametype
['base'] in ("draw"):
256 m
= re
.search(r
"(?P<PREDEAL>.+(?=Dealing pocket cards)|.+)"
257 r
"(Dealing pocket cards(?P<DEAL>.+(?=\*\*\* FIRST DRAW \*\*\*)|.+))?"
258 r
"(\*\*\* FIRST DRAW \*\*\*(?P<DRAWONE>.+(?=\*\*\* SECOND DRAW \*\*\*)|.+))?"
259 r
"(\*\*\* SECOND DRAW \*\*\*(?P<DRAWTWO>.+(?=\*\*\* THIRD DRAW \*\*\*)|.+))?"
260 r
"(\*\*\* THIRD DRAW \*\*\*(?P<DRAWTHREE>.+))?", hand
.handText
,re
.DOTALL
)
264 #Needs to return a list in the format
265 # ['player1name', 'player2name', ...] where player1name is the sb and player2name is bb,
266 # addtional players are assumed to post a bb oop
268 def readButton(self
, hand
):
269 m
= self
.re_Button
.search(hand
.handText
)
271 hand
.buttonpos
= int(m
.group('BUTTON'))
273 log
.info(_('readButton: not found'))
275 # def readCommunityCards(self, hand, street):
276 # #print hand.streets.group(street)
277 # if street in ('FLOP','TURN','RIVER'): # a list of streets which get dealt community cards (i.e. all but PREFLOP)
278 # m = self.re_Board.search(hand.streets.group(street))
279 # hand.setCommunityCards(street, m.group('CARDS').split(','))
281 def readCommunityCards(self
, hand
, street
): # street has been matched by markStreets, so exists in this hand
282 if street
in ('FLOP','TURN','RIVER'): # a list of streets which get dealt community cards (i.e. all but PREFLOP)
283 #print "DEBUG readCommunityCards:", street, hand.streets.group(street)
284 m
= self
.re_Board
.search(hand
.streets
[street
])
285 hand
.setCommunityCards(street
, m
.group('CARDS').split(', '))
287 def readBlinds(self
, hand
):
289 m
= self
.re_PostSB
.search(hand
.handText
)
290 hand
.addBlind(m
.group('PNAME'), 'small blind', m
.group('SB'))
291 except exceptions
.AttributeError: # no small blind
292 log
.debug( _("readBlinds in noSB exception - no SB created")+str(sys
.exc_info()) )
293 #hand.addBlind(None, None, None)
294 for a
in self
.re_PostBB
.finditer(hand
.handText
):
295 hand
.addBlind(a
.group('PNAME'), 'big blind', a
.group('BB'))
296 for a
in self
.re_PostDead
.finditer(hand
.handText
):
297 #print "DEBUG: Found dead blind: addBlind(%s, 'secondsb', %s)" %(a.group('PNAME'), a.group('DEAD'))
298 hand
.addBlind(a
.group('PNAME'), 'secondsb', a
.group('DEAD'))
299 for a
in self
.re_PostBoth
.finditer(hand
.handText
):
300 hand
.addBlind(a
.group('PNAME'), 'small & big blinds', a
.group('SBBB'))
302 def readAntes(self
, hand
):
303 log
.debug(_("reading antes"))
304 m
= self
.re_Antes
.finditer(hand
.handText
)
306 #~ logging.debug("hand.addAnte(%s,%s)" %(player.group('PNAME'), player.group('ANTE')))
307 hand
.addAnte(player
.group('PNAME'), player
.group('ANTE'))
309 def readBringIn(self
, hand
):
310 m
= self
.re_BringIn
.search(hand
.handText
,re
.DOTALL
)
312 #~ logging.debug("readBringIn: %s for %s" %(m.group('PNAME'), m.group('BRINGIN')))
313 hand
.addBringIn(m
.group('PNAME'), m
.group('BRINGIN'))
315 def readHeroCards(self
, hand
):
316 # streets PREFLOP, PREDRAW, and THIRD are special cases beacause
317 # we need to grab hero's cards
318 for street
in ('PREFLOP', 'DEAL'):
319 if street
in hand
.streets
.keys():
320 m
= self
.re_HeroCards
.finditer(hand
.streets
[street
])
322 hand
.hero
= found
.group('PNAME')
323 newcards
= found
.group('CARDS').split(', ')
324 hand
.addHoleCards(street
, hand
.hero
, closed
=newcards
, shown
=False, mucked
=False, dealt
=True)
326 def readAction(self
, hand
, street
):
327 m
= self
.re_Action
.finditer(hand
.streets
[street
])
329 #acts = action.groupdict()
330 #print "readaction: acts: %s" %acts
331 if action
.group('ATYPE') == ' raises':
332 hand
.addRaiseTo( street
, action
.group('PNAME'), action
.group('BET2') )
333 elif action
.group('ATYPE') == ' calls':
334 hand
.addCall( street
, action
.group('PNAME'), action
.group('BET') )
335 elif action
.group('ATYPE') == ' bets':
336 hand
.addBet( street
, action
.group('PNAME'), action
.group('BET') )
337 elif action
.group('ATYPE') == ' folds':
338 hand
.addFold( street
, action
.group('PNAME'))
339 elif action
.group('ATYPE') == ' checks':
340 hand
.addCheck( street
, action
.group('PNAME'))
341 elif action
.group('ATYPE') == ' discards':
342 hand
.addDiscard(street
, action
.group('PNAME'), action
.group('BET'), action
.group('DISCARDED'))
343 elif action
.group('ATYPE') == ' stands pat':
344 hand
.addStandsPat( street
, action
.group('PNAME'))
346 print (_("DEBUG:") + " " + _("Unimplemented %s: '%s' '%s'") % ("readAction", action
.group('PNAME'), action
.group('ATYPE')))
348 def readShowdownActions(self
, hand
):
349 for shows
in self
.re_ShowdownAction
.finditer(hand
.handText
):
350 cards
= shows
.group('CARDS')
351 cards
= set(cards
.split(','))
352 hand
.addShownCards(cards
, shows
.group('PNAME'))
354 def readCollectPot(self
,hand
):
355 for m
in self
.re_Pot
.finditer(hand
.handText
):
356 for splitpot
in m
.group('POT').split(','):
357 for m
in self
.re_CollectPot
.finditer(splitpot
):
358 hand
.addCollectPot(player
=m
.group('PNAME'),pot
=m
.group('POT'))
360 def readShownCards(self
,hand
):
361 for m
in self
.re_ShownCards
.finditer(hand
.handText
):
362 cards
= m
.group('CARDS')
363 cards
= cards
.split(', ') # needs to be a list, not a set--stud needs the order
365 (shown
, mucked
) = (False, False)
366 if m
.group('CARDS') is not None:
368 hand
.addShownCards(cards
=cards
, player
=m
.group('PNAME'), shown
=shown
, mucked
=mucked
)