If you only have games of a single limit type (fixed, pot, or no limit), but of more...
[fpdb-dooglus.git] / pyfpdb / EverleafToFpdb.py
blobfced271a64bc92aef4a3d9eb20c8d9ea263592f4
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright 2008-2010, 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 # Static regexes
38 re_SplitHands = re.compile(r"\n\n\n+")
39 re_TailSplitHands = re.compile(r"(\n\n\n+)")
40 re_GameInfo = re.compile(ur"^(Blinds )?(?P<CURRENCY>[$€]?)(?P<SB>[.0-9]+)/[$€]?(?P<BB>[.0-9]+) (?P<LIMIT>NL|PL|) ?(?P<GAME>(Hold\'em|Omaha|7 Card Stud))", re.MULTILINE)
41 #re.compile(ur"^(Blinds )?(?P<CURRENCY>\$| €|)(?P<SB>[.0-9]+)/(?:\$| €)?(?P<BB>[.0-9]+) (?P<LIMIT>NL|PL|) ?(?P<GAME>(Hold\'em|Omaha|7 Card Stud))", re.MULTILINE)
42 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)
43 re_Button = re.compile(ur"^Seat (?P<BUTTON>\d+) is the button$", re.MULTILINE)
44 re_PlayerInfo = re.compile(ur"^Seat (?P<SEAT>[0-9]+): (?P<PNAME>.*) \(\s+([$€]? (?P<CASH>[.0-9]+) (USD|EURO|Chips)|new player|All-in) \)$", re.MULTILINE)
45 re_Board = re.compile(ur"\[ (?P<CARDS>.+) \]")
46 re_TourneyInfoFromFilename = re.compile(ur".*TID_(?P<TOURNO>[0-9]+)-(?P<TABLE>[0-9]+)\.txt")
49 def compilePlayerRegexs(self, hand):
50 players = set([player[1] for player in hand.players])
51 if not players <= self.compiledPlayers: # x <= y means 'x is subset of y'
52 # we need to recompile the player regexs.
53 self.compiledPlayers = players
54 player_re = "(?P<PNAME>" + "|".join(map(re.escape, players)) + ")"
55 logging.debug("player_re: "+ player_re)
56 self.re_PostSB = re.compile(ur"^%s: posts small blind \[[$€]? (?P<SB>[.0-9]+)\s.*\]$" % player_re, re.MULTILINE)
57 self.re_PostBB = re.compile(ur"^%s: posts big blind \[[$€]? (?P<BB>[.0-9]+)\s.*\]$" % player_re, re.MULTILINE)
58 self.re_PostBoth = re.compile(ur"^%s: posts both blinds \[[$€]? (?P<SBBB>[.0-9]+)\s.*\]$" % player_re, re.MULTILINE)
59 self.re_Antes = re.compile(ur"^%s: posts ante \[[$€]? (?P<ANTE>[.0-9]+)\s.*\]$" % player_re, re.MULTILINE)
60 self.re_BringIn = re.compile(ur"^%s posts bring-in [$€]? (?P<BRINGIN>[.0-9]+)\." % player_re, re.MULTILINE)
61 self.re_HeroCards = re.compile(ur"^Dealt to %s \[ (?P<CARDS>.*) \]$" % player_re, re.MULTILINE)
62 # ^%s(?P<ATYPE>: bets| checks| raises| calls| folds)(\s\[(?:\$| €|) (?P<BET>[.,\d]+) (USD|EURO|Chips)\])?
63 self.re_Action = re.compile(ur"^%s(?P<ATYPE>: bets| checks| raises| calls| folds)(\s\[(?:[$€]?) (?P<BET>[.,\d]+)\s?(USD|EURO|Chips|)\])?" % player_re, re.MULTILINE)
64 self.re_ShowdownAction = re.compile(ur"^%s shows \[ (?P<CARDS>.*) \]" % player_re, re.MULTILINE)
65 self.re_CollectPot = re.compile(ur"^%s wins (?:[$€]?)\s?(?P<POT>[.\d]+) (USD|EURO|chips)(.*?\[ (?P<CARDS>.*?) \])?" % player_re, re.MULTILINE)
66 self.re_SitsOut = re.compile(ur"^%s sits out" % player_re, re.MULTILINE)
68 def readSupportedGames(self):
69 return [
70 ["ring", "hold", "nl"],
71 ["ring", "hold", "pl"],
72 ["ring", "hold", "fl"],
73 ["ring", "stud", "fl"],
74 #["ring", "omahahi", "pl"],
75 #["ring", "omahahilo", "pl"],
76 ["tour", "hold", "nl"],
77 ["tour", "hold", "fl"],
78 ["tour", "hold", "pl"]
81 def determineGameType(self, handText):
82 """return dict with keys/values:
83 'type' in ('ring', 'tour')
84 'limitType' in ('nl', 'cn', 'pl', 'cp', 'fl')
85 'base' in ('hold', 'stud', 'draw')
86 'category' in ('holdem', 'omahahi', omahahilo', 'razz', 'studhi', 'studhilo', 'fivedraw', '27_1draw', '27_3draw', 'badugi')
87 'hilo' in ('h','l','s')
88 'smallBlind' int?
89 'bigBlind' int?
90 'smallBet'
91 'bigBet'
92 'currency' in ('USD', 'EUR', 'T$', <countrycode>)
93 or None if we fail to get the info """
94 #(TODO: which parts are optional/required?)
96 # Blinds $0.50/$1 PL Omaha - 2008/12/07 - 21:59:48
97 # Blinds $0.05/$0.10 NL Hold'em - 2009/02/21 - 11:21:57
98 # $0.25/$0.50 7 Card Stud - 2008/12/05 - 21:43:59
100 # Tourney:
101 # Everleaf Gaming Game #75065769
102 # ***** Hand history for game #75065769 *****
103 # Blinds 10/20 NL Hold'em - 2009/02/25 - 17:30:32
104 # Table 2
105 info = {'type':'ring'}
107 m = self.re_GameInfo.search(handText)
108 if not m:
109 tmp = handText[0:100]
110 log.error(_("determineGameType: Unable to recognise gametype from: '%s'") % tmp)
111 log.error(_("determineGameType: Raising FpdbParseError"))
112 raise FpdbParseError(_("Unable to recognise gametype from: '%s'") % tmp)
114 mg = m.groupdict()
116 # translations from captured groups to our info strings
117 limits = { 'NL':'nl', 'PL':'pl', '':'fl' }
118 games = { # base, category
119 "Hold'em" : ('hold','holdem'),
120 'Omaha' : ('hold','omahahi'),
121 'Razz' : ('stud','razz'),
122 '7 Card Stud' : ('stud','studhi')
124 currencies = { u'€':'EUR', '$':'USD', '':'T$'}
125 if 'LIMIT' in mg:
126 info['limitType'] = limits[mg['LIMIT']]
127 if 'GAME' in mg:
128 (info['base'], info['category']) = games[mg['GAME']]
129 if 'SB' in mg:
130 info['sb'] = mg['SB']
131 if 'BB' in mg:
132 info['bb'] = mg['BB']
133 if 'CURRENCY' in mg:
134 info['currency'] = currencies[mg['CURRENCY']]
135 if info['currency'] == 'T$':
136 info['type'] = 'tour'
137 # NB: SB, BB must be interpreted as blinds or bets depending on limit type.
139 return info
142 def readHandInfo(self, hand):
143 m = self.re_HandInfo.search(hand.handText)
144 if(m == None):
145 logging.info(_("Didn't match re_HandInfo"))
146 logging.info(hand.handText)
147 return None
148 logging.debug("HID %s, Table %s" % (m.group('HID'), m.group('TABLE')))
149 hand.handid = m.group('HID')
150 hand.tablename = m.group('TABLE')
151 hand.maxseats = 4 # assume 4-max unless we have proof it's a larger/smaller game, since everleaf doesn't give seat max info
153 currencies = { u'€':'EUR', '$':'USD', '':'T$', None:'T$' }
154 mg = m.groupdict()
155 hand.gametype['currency'] = currencies[mg['CURRENCY']]
158 t = self.re_TourneyInfoFromFilename.search(self.in_path)
159 if t:
160 tourno = t.group('TOURNO')
161 hand.tourNo = tourno
162 hand.tablename = t.group('TABLE')
163 #TODO we should fetch info including buyincurrency, buyin and fee from URL:
164 # https://www.poker4ever.com/tourney/%TOURNEY_NUMBER%
166 # Believe Everleaf time is GMT/UTC, no transation necessary
167 # Stars format (Nov 10 2008): 2008/11/07 12:38:49 CET [2008/11/07 7:38:49 ET]
168 # or : 2008/11/07 12:38:49 ET
169 # Not getting it in my HH files yet, so using
170 # 2008/11/10 3:58:52 ET
171 #TODO: Need some date functions to convert to different timezones (Date::Manip for perl rocked for this)
172 hand.startTime = datetime.datetime.strptime(m.group('DATETIME'), "%Y/%m/%d - %H:%M:%S")
173 return
175 def readPlayerStacks(self, hand):
176 m = self.re_PlayerInfo.finditer(hand.handText)
177 for a in m:
178 seatnum = int(a.group('SEAT'))
179 hand.addPlayer(seatnum, a.group('PNAME'), a.group('CASH'))
180 if seatnum > 8:
181 hand.maxseats = 10 # they added 8-seat games now
182 elif seatnum > 6:
183 hand.maxseats = 8 # everleaf currently does 2/6/10 games, so if seats > 6 are in use, it must be 10-max.
184 # 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?
185 elif seatnum > 4:
186 hand.maxseats = 6 # they added 4-seat games too!
189 def markStreets(self, hand):
190 # PREFLOP = ** Dealing down cards **
191 # This re fails if, say, river is missing; then we don't get the ** that starts the river.
192 #m = re.search('(\*\* Dealing down cards \*\*\n)(?P<PREFLOP>.*?\n\*\*)?( Dealing Flop \*\* \[ (?P<FLOP1>\S\S), (?P<FLOP2>\S\S), (?P<FLOP3>\S\S) \])?(?P<FLOP>.*?\*\*)?( Dealing Turn \*\* \[ (?P<TURN1>\S\S) \])?(?P<TURN>.*?\*\*)?( Dealing River \*\* \[ (?P<RIVER1>\S\S) \])?(?P<RIVER>.*)', hand.handText,re.DOTALL)
193 if hand.gametype['base'] == 'hold':
194 m = re.search(r"\*\* Dealing down cards \*\*(?P<PREFLOP>.+(?=\*\* Dealing Flop \*\*)|.+)"
195 r"(\*\* Dealing Flop \*\*(?P<FLOP> \[ \S\S, \S\S, \S\S \].+(?=\*\* Dealing Turn \*\*)|.+))?"
196 r"(\*\* Dealing Turn \*\*(?P<TURN> \[ \S\S \].+(?=\*\* Dealing River \*\*)|.+))?"
197 r"(\*\* Dealing River \*\*(?P<RIVER> \[ \S\S \].+))?", hand.handText,re.DOTALL)
198 elif hand.gametype['base'] == 'stud':
199 m = re.search(r"(?P<ANTES>.+(?=\*\* Dealing down cards \*\*)|.+)"
200 r"(\*\* Dealing down cards \*\*(?P<THIRD>.+(?=\*\*\*\* dealing 4th street \*\*\*\*)|.+))?"
201 r"(\*\*\*\* dealing 4th street \*\*\*\*(?P<FOURTH>.+(?=\*\*\*\* dealing 5th street \*\*\*\*)|.+))?"
202 r"(\*\*\*\* dealing 5th street \*\*\*\*(?P<FIFTH>.+(?=\*\*\*\* dealing 6th street \*\*\*\*)|.+))?"
203 r"(\*\*\*\* dealing 6th street \*\*\*\*(?P<SIXTH>.+(?=\*\*\*\* dealing river \*\*\*\*)|.+))?"
204 r"(\*\*\*\* dealing river \*\*\*\*(?P<SEVENTH>.+))?", hand.handText,re.DOTALL)
205 hand.addStreets(m)
207 def readCommunityCards(self, hand, street): # street has been matched by markStreets, so exists in this hand
208 # If this has been called, street is a street which gets dealt community cards by type hand
209 # but it might be worth checking somehow.
210 # if street in ('FLOP','TURN','RIVER'): # a list of streets which get dealt community cards (i.e. all but PREFLOP)
211 logging.debug("readCommunityCards (%s)" % street)
212 m = self.re_Board.search(hand.streets[street])
213 cards = m.group('CARDS')
214 cards = [card.strip() for card in cards.split(',')]
215 hand.setCommunityCards(street=street, cards=cards)
217 def readAntes(self, hand):
218 logging.debug(_("reading antes"))
219 m = self.re_Antes.finditer(hand.handText)
220 for player in m:
221 logging.debug("hand.addAnte(%s,%s)" %(player.group('PNAME'), player.group('ANTE')))
222 hand.addAnte(player.group('PNAME'), player.group('ANTE'))
224 def readBringIn(self, hand):
225 m = self.re_BringIn.search(hand.handText,re.DOTALL)
226 if m:
227 logging.debug("Player bringing in: %s for %s" %(m.group('PNAME'), m.group('BRINGIN')))
228 hand.addBringIn(m.group('PNAME'), m.group('BRINGIN'))
229 else:
230 logging.warning(_("No bringin found."))
232 def readBlinds(self, hand):
233 m = self.re_PostSB.search(hand.handText)
234 if m is not None:
235 hand.addBlind(m.group('PNAME'), 'small blind', m.group('SB'))
236 else:
237 logging.debug(_("No small blind"))
238 hand.addBlind(None, None, None)
239 for a in self.re_PostBB.finditer(hand.handText):
240 hand.addBlind(a.group('PNAME'), 'big blind', a.group('BB'))
241 for a in self.re_PostBoth.finditer(hand.handText):
242 hand.addBlind(a.group('PNAME'), 'both', a.group('SBBB'))
244 def readButton(self, hand):
245 hand.buttonpos = int(self.re_Button.search(hand.handText).group('BUTTON'))
247 def readHeroCards(self, hand):
248 m = self.re_HeroCards.search(hand.handText)
249 if m:
250 hand.hero = m.group('PNAME')
251 # "2c, qh" -> ["2c","qc"]
252 # Also works with Omaha hands.
253 cards = m.group('CARDS')
254 cards = [card.strip() for card in cards.split(',')]
255 # hand.addHoleCards(cards, m.group('PNAME'))
256 hand.addHoleCards('PREFLOP', hand.hero, closed=cards, shown=False, mucked=False, dealt=True)
258 else:
259 #Not involved in hand
260 hand.involved = False
263 def readStudPlayerCards(self, hand, street):
264 # lol. see Plymouth.txt
265 logging.warning(_("Everleaf readStudPlayerCards is only a stub."))
266 #~ if street in ('THIRD', 'FOURTH', 'FIFTH', 'SIXTH'):
267 #~ hand.addPlayerCards(player = player.group('PNAME'), street = street, closed = [], open = [])
270 def readAction(self, hand, street):
271 logging.debug("readAction (%s)" % street)
272 m = self.re_Action.finditer(hand.streets[street])
273 for action in m:
274 logging.debug("%s %s" % (action.group('ATYPE'), action.groupdict()))
275 if action.group('ATYPE') == ' raises':
276 hand.addCallandRaise( street, action.group('PNAME'), action.group('BET') )
277 elif action.group('ATYPE') == ' calls':
278 hand.addCall( street, action.group('PNAME'), action.group('BET') )
279 elif action.group('ATYPE') == ': bets':
280 hand.addBet( street, action.group('PNAME'), action.group('BET') )
281 elif action.group('ATYPE') == ' folds':
282 hand.addFold( street, action.group('PNAME'))
283 elif action.group('ATYPE') == ' checks':
284 hand.addCheck( street, action.group('PNAME'))
285 elif action.group('ATYPE') == ' complete to':
286 hand.addComplete( street, action.group('PNAME'), action.group('BET'))
287 else:
288 logging.debug(_("Unimplemented readAction: %s %s" %(action.group('PNAME'),action.group('ATYPE'),)))
291 def readShowdownActions(self, hand):
292 """Reads lines where holecards are reported in a showdown"""
293 logging.debug("readShowdownActions")
294 for shows in self.re_ShowdownAction.finditer(hand.handText):
295 cards = shows.group('CARDS')
296 cards = cards.split(', ')
297 logging.debug(_("readShowdownActions %s %s" %(cards, shows.group('PNAME'))))
298 hand.addShownCards(cards, shows.group('PNAME'))
301 def readCollectPot(self,hand):
302 for m in self.re_CollectPot.finditer(hand.handText):
303 hand.addCollectPot(player=m.group('PNAME'),pot=m.group('POT'))
305 def readShownCards(self,hand):
306 """Reads lines where hole & board cards are mixed to form a hand (summary lines)"""
307 for m in self.re_CollectPot.finditer(hand.handText):
308 if m.group('CARDS') is not None:
309 cards = m.group('CARDS')
310 cards = cards.split(', ')
311 player = m.group('PNAME')
312 logging.debug("readShownCards %s cards=%s" % (player, cards))
313 # hand.addShownCards(cards=None, player=m.group('PNAME'), holeandboard=cards)
314 hand.addShownCards(cards=cards, player=m.group('PNAME'))
316 @staticmethod
317 def getTableTitleRe(type, table_name=None, tournament = None, table_number=None):
318 if tournament:
319 return "%s - Tournament ID: %s -" % (table_number, tournament)
320 return "%s -" % (table_name)
324 if __name__ == "__main__":
325 parser = OptionParser()
326 parser.add_option("-i", "--input", dest="ipath", help=_("parse input hand history"), default="-")
327 parser.add_option("-o", "--output", dest="opath", help=_("output translation to"), default="-")
328 parser.add_option("-f", "--follow", dest="follow", help=_("follow (tail -f) the input"), action="store_true", default=False)
329 parser.add_option("-q", "--quiet",
330 action="store_const", const=logging.CRITICAL, dest="verbosity", default=logging.INFO)
331 parser.add_option("-v", "--verbose",
332 action="store_const", const=logging.INFO, dest="verbosity")
333 parser.add_option("--vv",
334 action="store_const", const=logging.DEBUG, dest="verbosity")
336 (options, args) = parser.parse_args()
338 LOG_FILENAME = './logging.out'
339 logging.basicConfig(filename=LOG_FILENAME,level=options.verbosity)
341 e = Everleaf(in_path = options.ipath, out_path = options.opath, follow = options.follow, autostart=True)