Carbon Poker offers a 5 card stud game that wasn't listed here. It's not available...
[fpdb-dooglus.git] / pyfpdb / EverleafToFpdb.py
blobb66dd36cce42e091e061f7fac6190693522c181a
1 #!/usr/bin/env python
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 ########################################################################
21 import L10n
22 _ = L10n.get_translation()
24 import sys
25 import logging
26 from HandHistoryConverter import *
28 # Class for converting Everleaf HH format.
30 class Everleaf(HandHistoryConverter):
32 sitename = 'Everleaf'
33 filetype = "text"
34 codepage = "cp1252"
35 siteId = 3 # Needs to match id entry in Sites database
37 substitutions = {
38 'LEGAL_ISO' : "USD|EUR|GBP|CAD|FPP", # legal ISO currency codes
39 'LS' : u"\$|\u20AC|\xe2\x82\xac|\x80|", # legal currency symbols - Euro(cp1252, utf-8) #TODO change \x80 to \x20\x80, update all regexes accordingly
40 'TAB' : u"-\u2013'\s\da-zA-Z#_", # legal characters for tablename
41 'NUM' : u".,\d", # legal characters in number format
44 # Static regexes
45 re_SplitHands = re.compile(r"\n\n\n+")
46 re_TailSplitHands = re.compile(r"(\n\n\n+)")
47 re_GameInfo = re.compile(ur"^(Blinds )? ?(?P<CURRENCY>[%(LS)s]?)(?P<SB>[.0-9]+) ?/ ? ?[%(LS)s]?(?P<BB>[.0-9]+) (?P<LIMIT>NL|PL|) ?(?P<GAME>(Hold\'em|Omaha|7 Card Stud))" % substitutions, re.MULTILINE)
49 #re_HandInfo = re.compile(ur".*#(?P<HID>[0-9]+)\n.*\n(Blinds )?(?P<CURRENCY>[$€])?(?P<SB>[.0-9]+)/(?:[$€])?(?P<BB>[.0-9]+) (?P<GAMETYPE>.*) - (?P<DATETIME>\d\d\d\d/\d\d/\d\d - \d\d:\d\d:\d\d)\nTable (?P<TABLE>.+$)", re.MULTILINE)
51 re_HandInfo = re.compile(ur".*\n(.*#|.* partie )(?P<HID>[0-9]+).*(\n|\n\n)(Blinds )? ?(?P<CURRENCY>[%(LS)s])?(?P<SB>[.0-9]+) ?/ ?(?:[%(LS)s])?(?P<BB>[.0-9]+) (?P<GAMETYPE>.*) - (?P<DATETIME>\d\d\d\d/\d\d/\d\d - \d\d:\d\d:\d\d)\nTable (?P<TABLE>.+$)" % substitutions, re.MULTILINE)
54 #re_HandInfo = re.compile(ur"(.*#|.*\n.* partie )(?P<HID>[0-9]+).*(\n|\n\n)(Blinds )?(?:\$| €|)(?P<SB>[.0-9]+)/(?:\$| €|)(?P<BB>[.0-9]+) (?P<GAMETYPE>.*) - (?P<DATETIME>\d\d\d\d/\d\d/\d\d - \d\d:\d\d:\d\d)\nTable (?P<TABLE>.+$)", re.MULTILINE)
57 re_Button = re.compile(ur"^Seat (?P<BUTTON>\d+) is the button$", re.MULTILINE)
58 re_PlayerInfo = re.compile(ur"""^Seat\s(?P<SEAT>[0-9]+):\s(?P<PNAME>.*)\s+
60 \s+[%(LS)s]?\s?(?P<CASH>[.0-9]+)
61 (\s(USD|EURO|EUR|Chips)?(new\splayer|All-in)?)?
62 \s?\)$
63 """ % substitutions, re.MULTILINE|re.VERBOSE)
64 re_Board = re.compile(ur"\[ (?P<CARDS>.+) \]")
65 re_TourneyInfoFromFilename = re.compile(ur".*TID_(?P<TOURNO>[0-9]+)-(?P<TABLE>[0-9]+)\.txt")
68 def compilePlayerRegexs(self, hand):
69 players = set([player[1] for player in hand.players])
70 if not players <= self.compiledPlayers: # x <= y means 'x is subset of y'
71 # we need to recompile the player regexs.
72 self.compiledPlayers = players
73 player_re = "(?P<PNAME>" + "|".join(map(re.escape, players)) + ")"
74 logging.debug("player_re: "+ player_re)
75 self.re_PostSB = re.compile(ur"^%s: posts small blind \[ ?[%s]? (?P<SB>[.0-9]+)\s.*\]$" % (player_re, self.substitutions["LS"]), re.MULTILINE)
76 self.re_PostBB = re.compile(ur"^%s: posts big blind \[ ?[%s]? (?P<BB>[.0-9]+)\s.*\]$" % (player_re, self.substitutions["LS"]), re.MULTILINE)
77 self.re_PostBoth = re.compile(ur"^%s: posts both blinds \[ ?[%s]? (?P<SBBB>[.0-9]+)\s.*\]$" % (player_re, self.substitutions["LS"]), re.MULTILINE)
78 self.re_Antes = re.compile(ur"^%s: posts ante \[ ?[%s]? (?P<ANTE>[.0-9]+)\s.*\]$" % (player_re, self.substitutions["LS"]), re.MULTILINE)
79 self.re_BringIn = re.compile(ur"^%s posts bring-in ?[%s]? (?P<BRINGIN>[.0-9]+)\." % (player_re, self.substitutions["LS"]), re.MULTILINE)
80 self.re_HeroCards = re.compile(ur"^Dealt to %s \[ (?P<CARDS>.*) \]$" % player_re, re.MULTILINE)
81 # ^%s(?P<ATYPE>: bets| checks| raises| calls| folds)(\s\[(?:\$| €|) (?P<BET>[.,\d]+) (USD|EURO|EUR|Chips)\])?
82 self.re_Action = re.compile(ur"^%s(?P<ATYPE>: bets| checks| raises| calls| folds)(\s\[(?: ?[%s]?) (?P<BET>[.,\d]+)\s?(USD|EURO|EUR|Chips|)\])?" % (player_re, self.substitutions["LS"]), re.MULTILINE)
83 self.re_ShowdownAction = re.compile(ur"^%s shows \[ (?P<CARDS>.*) \]" % player_re, re.MULTILINE)
84 self.re_CollectPot = re.compile(ur"^%s wins ?(?: ?[%s]?)\s?(?P<POT>[.\d]+) (USD|EURO|EUR|chips)(.*?\[ (?P<CARDS>.*?) \])?" % (player_re, self.substitutions["LS"]), re.MULTILINE)
85 self.re_SitsOut = re.compile(ur"^%s sits out" % player_re, re.MULTILINE)
87 def readSupportedGames(self):
88 return [
89 ["ring", "hold", "nl"],
90 ["ring", "hold", "pl"],
91 ["ring", "hold", "fl"],
92 ["ring", "stud", "fl"],
93 #["ring", "omahahi", "pl"],
94 #["ring", "omahahilo", "pl"],
95 ["tour", "hold", "nl"],
96 ["tour", "hold", "fl"],
97 ["tour", "hold", "pl"]
100 def determineGameType(self, handText):
101 """return dict with keys/values:
102 'type' in ('ring', 'tour')
103 'limitType' in ('nl', 'cn', 'pl', 'cp', 'fl')
104 'base' in ('hold', 'stud', 'draw')
105 'category' in ('holdem', 'omahahi', omahahilo', 'razz', 'studhi', 'studhilo', 'fivedraw', '27_1draw', '27_3draw', 'badugi')
106 'hilo' in ('h','l','s')
107 'smallBlind' int?
108 'bigBlind' int?
109 'smallBet'
110 'bigBet'
111 'currency' in ('USD', 'EUR', 'T$', <countrycode>)
112 or None if we fail to get the info """
113 # Blinds $0.50/$1 PL Omaha - 2008/12/07 - 21:59:48
114 # Blinds $0.05/$0.10 NL Hold'em - 2009/02/21 - 11:21:57
115 # $0.25/$0.50 7 Card Stud - 2008/12/05 - 21:43:59
117 # Tourney:
118 # Everleaf Gaming Game #75065769
119 # ***** Hand history for game #75065769 *****
120 # Blinds 10/20 NL Hold'em - 2009/02/25 - 17:30:32
121 # Table 2
122 info = {'type':'ring'}
125 m = self.re_GameInfo.search(handText)
126 if not m:
127 tmp = handText[0:150]
128 log.error(_("Unable to recognise gametype from: '%s'") % tmp)
129 log.error("determineGameType: " + _("Raising FpdbParseError"))
130 raise FpdbParseError(_("Unable to recognise gametype from: '%s'") % tmp)
132 mg = m.groupdict()
134 # translations from captured groups to our info strings
135 limits = { 'NL':'nl', 'PL':'pl', '':'fl' }
136 games = { # base, category
137 "Hold'em" : ('hold','holdem'),
138 'Omaha' : ('hold','omahahi'),
139 'Razz' : ('stud','razz'),
140 '7 Card Stud' : ('stud','studhi')
142 currencies = { u'€':'EUR', '$':'USD', '':'T$'}
143 if 'LIMIT' in mg:
144 info['limitType'] = limits[mg['LIMIT']]
145 if 'GAME' in mg:
146 (info['base'], info['category']) = games[mg['GAME']]
147 if 'SB' in mg:
148 info['sb'] = mg['SB']
149 if 'BB' in mg:
150 info['bb'] = mg['BB']
151 if 'CURRENCY' in mg:
152 info['currency'] = currencies[mg['CURRENCY']]
153 if info['currency'] == 'T$':
154 info['type'] = 'tour'
155 # NB: SB, BB must be interpreted as blinds or bets depending on limit type.
157 return info
160 def readHandInfo(self, hand):
161 m = self.re_HandInfo.search(hand.handText)
162 if(m == None):
163 logging.info(_("No match in readHandInfo: '%s'") % hand.handText[0:100])
164 logging.info(hand.handText)
165 return None
166 logging.debug("HID %s, Table %s" % (m.group('HID'), m.group('TABLE')))
167 hand.handid = m.group('HID')
168 hand.tablename = m.group('TABLE')
169 hand.maxseats = 4 # assume 4-max unless we have proof it's a larger/smaller game, since everleaf doesn't give seat max info
171 currencies = { u'€':'EUR', '$':'USD', '':'T$', None:'T$' }
172 mg = m.groupdict()
173 hand.gametype['currency'] = currencies[mg['CURRENCY']]
176 t = self.re_TourneyInfoFromFilename.search(self.in_path)
177 if t:
178 tourno = t.group('TOURNO')
179 hand.tourNo = tourno
180 hand.tablename = t.group('TABLE')
181 #TODO we should fetch info including buyincurrency, buyin and fee from URL:
182 # https://www.poker4ever.com/tourney/%TOURNEY_NUMBER%
184 # Believe Everleaf time is GMT/UTC, no transation necessary
185 hand.startTime = datetime.datetime.strptime(m.group('DATETIME'), "%Y/%m/%d - %H:%M:%S")
186 return
188 def readPlayerStacks(self, hand):
189 m = self.re_PlayerInfo.finditer(hand.handText)
190 for a in m:
191 seatnum = int(a.group('SEAT'))
192 hand.addPlayer(seatnum, a.group('PNAME'), a.group('CASH'))
193 if seatnum > 8:
194 hand.maxseats = 10 # they added 8-seat games now
195 elif seatnum > 6:
196 hand.maxseats = 8 # everleaf currently does 2/6/10 games, so if seats > 6 are in use, it must be 10-max.
197 # TODO: implement lookup list by table-name to determine maxes, then fall back to 6 default/10 here, if there's no entry in the list?
198 elif seatnum > 4:
199 hand.maxseats = 6 # they added 4-seat games too!
202 def markStreets(self, hand):
203 if hand.gametype['base'] == 'hold':
204 m = re.search(r"\*\* Dealing down cards \*\*(?P<PREFLOP>.+(?=\*\* Dealing Flop \*\*)|.+)"
205 r"(\*\* Dealing Flop \*\*(?P<FLOP> \[ \S\S, \S\S, \S\S \].+(?=\*\* Dealing Turn \*\*)|.+))?"
206 r"(\*\* Dealing Turn \*\*(?P<TURN> \[ \S\S \].+(?=\*\* Dealing River \*\*)|.+))?"
207 r"(\*\* Dealing River \*\*(?P<RIVER> \[ \S\S \].+))?", hand.handText,re.DOTALL)
208 elif hand.gametype['base'] == 'stud':
209 m = re.search(r"(?P<ANTES>.+(?=\*\* Dealing down cards \*\*)|.+)"
210 r"(\*\* Dealing down cards \*\*(?P<THIRD>.+(?=\*\*\*\* dealing 4th street \*\*\*\*)|.+))?"
211 r"(\*\*\*\* dealing 4th street \*\*\*\*(?P<FOURTH>.+(?=\*\*\*\* dealing 5th street \*\*\*\*)|.+))?"
212 r"(\*\*\*\* dealing 5th street \*\*\*\*(?P<FIFTH>.+(?=\*\*\*\* dealing 6th street \*\*\*\*)|.+))?"
213 r"(\*\*\*\* dealing 6th street \*\*\*\*(?P<SIXTH>.+(?=\*\*\*\* dealing river \*\*\*\*)|.+))?"
214 r"(\*\*\*\* dealing river \*\*\*\*(?P<SEVENTH>.+))?", hand.handText,re.DOTALL)
215 hand.addStreets(m)
217 def readCommunityCards(self, hand, street): # street has been matched by markStreets, so exists in this hand
218 # If this has been called, street is a street which gets dealt community cards by type hand
219 # but it might be worth checking somehow.
220 # if street in ('FLOP','TURN','RIVER'): # a list of streets which get dealt community cards (i.e. all but PREFLOP)
221 logging.debug("readCommunityCards (%s)" % street)
222 m = self.re_Board.search(hand.streets[street])
223 cards = m.group('CARDS')
224 cards = [card.strip() for card in cards.split(',')]
225 hand.setCommunityCards(street=street, cards=cards)
227 def readAntes(self, hand):
228 logging.debug(_("reading antes"))
229 m = self.re_Antes.finditer(hand.handText)
230 for player in m:
231 logging.debug("hand.addAnte(%s,%s)" %(player.group('PNAME'), player.group('ANTE')))
232 hand.addAnte(player.group('PNAME'), player.group('ANTE'))
234 def readBringIn(self, hand):
235 m = self.re_BringIn.search(hand.handText,re.DOTALL)
236 if m:
237 logging.debug("Player bringing in: %s for %s" %(m.group('PNAME'), m.group('BRINGIN')))
238 hand.addBringIn(m.group('PNAME'), m.group('BRINGIN'))
239 else:
240 logging.warning(_("No bringin found."))
242 def readBlinds(self, hand):
243 m = self.re_PostSB.search(hand.handText)
244 if m is not None:
245 hand.addBlind(m.group('PNAME'), 'small blind', m.group('SB'))
246 else:
247 logging.debug(_("No small blind"))
248 hand.addBlind(None, None, None)
249 for a in self.re_PostBB.finditer(hand.handText):
250 hand.addBlind(a.group('PNAME'), 'big blind', a.group('BB'))
251 for a in self.re_PostBoth.finditer(hand.handText):
252 hand.addBlind(a.group('PNAME'), 'both', a.group('SBBB'))
254 def readButton(self, hand):
255 hand.buttonpos = int(self.re_Button.search(hand.handText).group('BUTTON'))
257 def readHeroCards(self, hand):
258 m = self.re_HeroCards.search(hand.handText)
259 if m:
260 hand.hero = m.group('PNAME')
261 # "2c, qh" -> ["2c","qc"]
262 # Also works with Omaha hands.
263 cards = m.group('CARDS')
264 cards = [card.strip() for card in cards.split(',')]
265 # hand.addHoleCards(cards, m.group('PNAME'))
266 hand.addHoleCards('PREFLOP', hand.hero, closed=cards, shown=False, mucked=False, dealt=True)
268 else:
269 #Not involved in hand
270 hand.involved = False
273 def readStudPlayerCards(self, hand, street):
274 logging.warning(_("%s cannot read all stud/razz hands yet.") % hand.sitename)
277 def readAction(self, hand, street):
278 logging.debug("readAction (%s)" % street)
279 m = self.re_Action.finditer(hand.streets[street])
280 for action in m:
281 logging.debug("%s %s" % (action.group('ATYPE'), action.groupdict()))
282 if action.group('ATYPE') == ' raises':
283 hand.addCallandRaise( street, action.group('PNAME'), action.group('BET') )
284 elif action.group('ATYPE') == ' calls':
285 hand.addCall( street, action.group('PNAME'), action.group('BET') )
286 elif action.group('ATYPE') == ': bets':
287 hand.addBet( street, action.group('PNAME'), action.group('BET') )
288 elif action.group('ATYPE') == ' folds':
289 hand.addFold( street, action.group('PNAME'))
290 elif action.group('ATYPE') == ' checks':
291 hand.addCheck( street, action.group('PNAME'))
292 elif action.group('ATYPE') == ' complete to':
293 hand.addComplete( street, action.group('PNAME'), action.group('BET'))
294 else:
295 logging.debug(_("Unimplemented %s: '%s' '%s'") % ("readAction", action.group('PNAME'), action.group('ATYPE')))
298 def readShowdownActions(self, hand):
299 """Reads lines where holecards are reported in a showdown"""
300 logging.debug("readShowdownActions")
301 for shows in self.re_ShowdownAction.finditer(hand.handText):
302 cards = shows.group('CARDS')
303 cards = cards.split(', ')
304 logging.debug("readShowdownActions %s %s" % (cards, shows.group('PNAME')))
305 hand.addShownCards(cards, shows.group('PNAME'))
308 def readCollectPot(self,hand):
309 for m in self.re_CollectPot.finditer(hand.handText):
310 hand.addCollectPot(player=m.group('PNAME'),pot=m.group('POT'))
312 def readShownCards(self,hand):
313 """Reads lines where hole & board cards are mixed to form a hand (summary lines)"""
314 for m in self.re_CollectPot.finditer(hand.handText):
315 if m.group('CARDS') is not None:
316 cards = m.group('CARDS')
317 cards = cards.split(', ')
318 player = m.group('PNAME')
319 logging.debug("readShownCards %s cards=%s" % (player, cards))
320 # hand.addShownCards(cards=None, player=m.group('PNAME'), holeandboard=cards)
321 hand.addShownCards(cards=cards, player=m.group('PNAME'))
323 @staticmethod
324 def getTableTitleRe(type, table_name=None, tournament = None, table_number=None):
325 if tournament:
326 return "%s - Tournament ID: %s -" % (table_number, tournament)
327 return "%s -" % (table_name)