Use debian 2.7 only
[fpbd-bostik.git] / pyfpdb / BetfairToFpdb.py
blob15585817fb0c4a935b77fff443dbd9c606163e71
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright 2008-2011, 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 import sys
25 from HandHistoryConverter import *
27 # Betfair HH format
29 class Betfair(HandHistoryConverter):
31 sitename = 'Betfair'
32 filetype = "text"
33 codepage = "cp1252"
34 siteId = 7 # Needs to match id entry in Sites database
36 # Static regexes
37 re_GameInfo = re.compile("^(?P<LIMIT>NL|PL|) (?P<CURRENCY>\$|)?(?P<SB>[.0-9]+)/\$?(?P<BB>[.0-9]+) (?P<GAME>(Texas Hold\'em|Omaha Hi|Omaha|Razz))", re.MULTILINE)
38 re_SplitHands = re.compile(r'\n\n+')
39 re_HandInfo = re.compile("\*\*\*\*\* Betfair Poker Hand History for Game (?P<HID>[0-9]+) \*\*\*\*\*\n(?P<LIMIT>NL|PL|) (?P<CURRENCY>\$|)?(?P<SB>[.0-9]+)/\$?(?P<BB>[.0-9]+) (?P<GAMETYPE>(Texas Hold\'em|Omaha|Razz)) - (?P<DATETIME>[a-zA-Z]+, [a-zA-Z]+ \d+, \d\d:\d\d:\d\d GMT \d\d\d\d)\nTable (?P<TABLE>[ a-zA-Z0-9]+) \d-max \(Real Money\)\nSeat (?P<BUTTON>[0-9]+)", re.MULTILINE)
40 re_Button = re.compile(ur"^Seat (?P<BUTTON>\d+) is the button", re.MULTILINE)
41 re_PlayerInfo = re.compile("Seat (?P<SEAT>[0-9]+): (?P<PNAME>.*)\s\(\s(\$(?P<CASH>[.0-9]+)) \)")
42 re_Board = re.compile(ur"\[ (?P<CARDS>.+) \]")
45 def compilePlayerRegexs(self, hand):
46 players = set([player[1] for player in hand.players])
47 if not players <= self.compiledPlayers: # x <= y means 'x is subset of y'
48 # we need to recompile the player regexs.
49 self.compiledPlayers = players
50 player_re = "(?P<PNAME>" + "|".join(map(re.escape, players)) + ")"
51 log.debug("player_re: " + player_re)
52 self.re_PostSB = re.compile("^%s posts small blind \[\$?(?P<SB>[.0-9]+)" % player_re, re.MULTILINE)
53 self.re_PostBB = re.compile("^%s posts big blind \[\$?(?P<BB>[.0-9]+)" % player_re, re.MULTILINE)
54 self.re_Antes = re.compile("^%s antes asdf sadf sadf" % player_re, re.MULTILINE)
55 self.re_BringIn = re.compile("^%s antes asdf sadf sadf" % player_re, re.MULTILINE)
56 self.re_PostBoth = re.compile("^%s posts small \& big blinds \[\$?(?P<SBBB>[.0-9]+)" % player_re, re.MULTILINE)
57 self.re_HeroCards = re.compile("^Dealt to %s \[ (?P<CARDS>.*) \]" % player_re, re.MULTILINE)
58 self.re_Action = re.compile("^%s (?P<ATYPE>bets|checks|raises to|raises|calls|folds)(\s\[\$(?P<BET>[.\d]+)\])?" % player_re, re.MULTILINE)
59 self.re_ShowdownAction = re.compile("^%s shows \[ (?P<CARDS>.*) \]" % player_re, re.MULTILINE)
60 self.re_CollectPot = re.compile("^%s wins \$(?P<POT>[.\d]+) (.*?\[ (?P<CARDS>.*?) \])?" % player_re, re.MULTILINE)
61 self.re_SitsOut = re.compile("^%s sits out" % player_re, re.MULTILINE)
62 self.re_ShownCards = re.compile(r"%s (?P<SEAT>[0-9]+) (?P<CARDS>adsfasdf)" % player_re, re.MULTILINE)
64 def readSupportedGames(self):
65 return [["ring", "hold", "nl"],
66 ["ring", "hold", "pl"]
69 def determineGameType(self, handText):
70 info = {'type':'ring'}
72 m = self.re_GameInfo.search(handText)
73 if not m:
74 tmp = handText[0:200]
75 log.error(_("BetfairToFpdb.determineGameType: '%s'") % tmp)
76 raise FpdbParseError
78 mg = m.groupdict()
80 # translations from captured groups to our info strings
81 limits = { 'NL':'nl', 'PL':'pl', 'Limit':'fl' }
82 games = { # base, category
83 "Texas Hold'em" : ('hold','holdem'),
84 'Omaha Hi' : ('hold','omahahi'),
85 'Omaha' : ('hold','omahahi'),
86 'Razz' : ('stud','razz'),
87 '7 Card Stud' : ('stud','studhi')
89 currencies = { u' €':'EUR', '$':'USD', '':'T$' }
90 if 'LIMIT' in mg:
91 info['limitType'] = limits[mg['LIMIT']]
92 if 'GAME' in mg:
93 (info['base'], info['category']) = games[mg['GAME']]
94 if 'SB' in mg:
95 info['sb'] = mg['SB']
96 if 'BB' in mg:
97 info['bb'] = mg['BB']
98 if 'CURRENCY' in mg:
99 info['currency'] = currencies[mg['CURRENCY']]
101 return info
103 def readHandInfo(self, hand):
104 m = self.re_HandInfo.search(hand.handText)
105 if(m == None):
106 tmp = hand.handText[0:200]
107 log.error(_("BetfairToFpdb.readHandInfo: '%s'") % tmp)
108 raise FpdbParseError
109 log.debug("HID %s, Table %s" % (m.group('HID'), m.group('TABLE')))
110 hand.handid = m.group('HID')
111 hand.tablename = m.group('TABLE')
112 hand.startTime = datetime.datetime.strptime(m.group('DATETIME'), "%A, %B %d, %H:%M:%S GMT %Y")
113 #hand.buttonpos = int(m.group('BUTTON'))
115 def readPlayerStacks(self, hand):
116 m = self.re_PlayerInfo.finditer(hand.handText)
117 for a in m:
118 hand.addPlayer(int(a.group('SEAT')), a.group('PNAME'), a.group('CASH'))
120 #Shouldn't really dip into the Hand object, but i've no idea how to tell the length of iter m
121 if len(hand.players) < 2:
122 log.info(_("Less than 2 players found in hand %s.") % hand.handid)
124 def markStreets(self, hand):
125 m = re.search(r"\*\* Dealing down cards \*\*(?P<PREFLOP>.+(?=\*\* Dealing Flop \*\*)|.+)"
126 r"(\*\* Dealing Flop \*\*(?P<FLOP> \[ \S\S, \S\S, \S\S \].+(?=\*\* Dealing Turn \*\*)|.+))?"
127 r"(\*\* Dealing Turn \*\*(?P<TURN> \[ \S\S \].+(?=\*\* Dealing River \*\*)|.+))?"
128 r"(\*\* Dealing River \*\*(?P<RIVER> \[ \S\S \].+))?", hand.handText,re.DOTALL)
130 hand.addStreets(m)
133 def readCommunityCards(self, hand, street): # street has been matched by markStreets, so exists in this hand
134 if street in ('FLOP','TURN','RIVER'): # a list of streets which get dealt community cards (i.e. all but PREFLOP)
135 m = self.re_Board.search(hand.streets[street])
136 hand.setCommunityCards(street, m.group('CARDS').split(', '))
138 def readBlinds(self, hand):
139 try:
140 m = self.re_PostSB.search(hand.handText)
141 hand.addBlind(m.group('PNAME'), 'small blind', m.group('SB'))
142 except: # no small blind
143 hand.addBlind(None, None, None)
144 for a in self.re_PostBB.finditer(hand.handText):
145 hand.addBlind(a.group('PNAME'), 'big blind', a.group('BB'))
146 for a in self.re_PostBoth.finditer(hand.handText):
147 hand.addBlind(a.group('PNAME'), 'small & big blinds', a.group('SBBB'))
149 def readAntes(self, hand):
150 log.debug("reading antes")
151 m = self.re_Antes.finditer(hand.handText)
152 for player in m:
153 log.debug("hand.addAnte(%s,%s)" %(player.group('PNAME'), player.group('ANTE')))
154 hand.addAnte(player.group('PNAME'), player.group('ANTE'))
156 def readBringIn(self, hand):
157 m = self.re_BringIn.search(hand.handText,re.DOTALL)
158 if m:
159 log.debug(_("Player bringing in: %s for %s") % (m.group('PNAME'), m.group('BRINGIN')))
160 hand.addBringIn(m.group('PNAME'), m.group('BRINGIN'))
161 else:
162 log.warning(_("No bringin found"))
164 def readButton(self, hand):
165 hand.buttonpos = int(self.re_Button.search(hand.handText).group('BUTTON'))
167 def readHeroCards(self, hand):
168 # streets PREFLOP, PREDRAW, and THIRD are special cases beacause
169 # we need to grab hero's cards
170 for street in ('PREFLOP', 'DEAL'):
171 if street in hand.streets.keys():
172 m = self.re_HeroCards.finditer(hand.streets[street])
173 for found in m:
174 hand.hero = found.group('PNAME')
175 newcards = [c.strip() for c in found.group('CARDS').split(',')]
176 hand.addHoleCards(street, hand.hero, closed=newcards, shown=False, mucked=False, dealt=True)
178 def readStudPlayerCards(self, hand, street):
179 # balh blah blah
180 pass
182 def readAction(self, hand, street):
183 m = self.re_Action.finditer(hand.streets[street])
184 for action in m:
185 if action.group('ATYPE') == 'folds':
186 hand.addFold( street, action.group('PNAME'))
187 elif action.group('ATYPE') == 'checks':
188 hand.addCheck( street, action.group('PNAME'))
189 elif action.group('ATYPE') == 'calls':
190 hand.addCall( street, action.group('PNAME'), action.group('BET') )
191 elif action.group('ATYPE') == 'bets':
192 hand.addBet( street, action.group('PNAME'), action.group('BET') )
193 elif action.group('ATYPE') == 'raises to':
194 hand.addRaiseTo( street, action.group('PNAME'), action.group('BET') )
195 else:
196 sys.stderr.write(_("DEBUG:") + " " + _("Unimplemented %s: '%s' '%s'") % ("readAction", action.group('PNAME'), action.group('ATYPE')))
199 def readShowdownActions(self, hand):
200 for shows in self.re_ShowdownAction.finditer(hand.handText):
201 cards = shows.group('CARDS')
202 cards = cards.split(', ')
203 hand.addShownCards(cards, shows.group('PNAME'))
205 def readCollectPot(self,hand):
206 for m in self.re_CollectPot.finditer(hand.handText):
207 hand.addCollectPot(player=m.group('PNAME'),pot=m.group('POT'))
209 def readShownCards(self,hand):
210 for m in self.re_ShownCards.finditer(hand.handText):
211 if m.group('CARDS') is not None:
212 cards = m.group('CARDS')
213 cards = cards.split(', ')
214 hand.addShownCards(cards=None, player=m.group('PNAME'), holeandboard=cards)