If you only have games of a single limit type (fixed, pot, or no limit), but of more...
[fpdb-dooglus.git] / pyfpdb / TableWindow.py
blob05c6ce4e14ed1ba688a298a52d5e8a3794a03314
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """Base class for interacting with poker client windows.
5 There are currently subclasses for X and Windows.
7 The class queries the poker client window for data of interest, such as
8 size and location. It also controls the signals to alert the HUD when the
9 client has been resized, destroyed, etc.
10 """
11 # Copyright 2008 - 2010, Ray E. Barker
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 ########################################################################
29 # Standard Library modules
30 import re
32 # pyGTK modules
33 import gtk
34 import gobject
36 # FreePokerTools modules
37 from HandHistoryConverter import getTableTitleRe
38 from HandHistoryConverter import getTableNoRe
40 # Global used for figuring out the current game being played from the title.
41 # The dict key is a tuple of (limit type, category) for the game.
42 # The list is the names for those games used by the supported poker sites
43 # This is currently only used for mixed games, so it only needs to support those
44 # games on PokerStars and Full Tilt.
45 nlpl_game_names = { #fpdb name Stars Name FTP Name (if different)
46 ("nl", "holdem" ) : ("No Limit Hold\'em" , ),
47 ("pl", "holdem" ) : ("Pot Limit Hold\'em" , ),
48 ("pl", "omahahi" ) : ("Pot Limit Omaha" ,"Pot Limit Omaha Hi" ),
50 limit_game_names = { #fpdb name Stars Name FTP Name
51 ("fl", "holdem" ) : ("Limit Hold\'em" , ),
52 ("fl", "omahahilo" ) : ("Limit Omaha H/L" , ),
53 ("fl", "studhilo" ) : ("Limit Stud H/L" , ),
54 ("fl", "razz" ) : ("Limit Razz" , ),
55 ("fl", "studhi" ) : ("Limit Stud" , "Stud Hi"),
56 ("fl", "27_3draw" ) : ("Limit Triple Draw 2-7 Lowball", )
59 # A window title might have our table name + one of these words/
60 # phrases. If it has this word in the title, it is not a table.
61 bad_words = ('History for table:', 'HUD:', 'Chat:', 'FPDBHUD')
63 # Here are the custom signals we define for allowing the 'client watcher'
64 # thread to communicate with the gui thread. Any time a poker client is
65 # is moved, resized, or closed on of these signals is emitted to the
66 # HUD main window.
67 gobject.signal_new("client_moved", gtk.Window,
68 gobject.SIGNAL_RUN_LAST,
69 gobject.TYPE_NONE,
70 (gobject.TYPE_PYOBJECT,))
72 gobject.signal_new("client_resized", gtk.Window,
73 gobject.SIGNAL_RUN_LAST,
74 gobject.TYPE_NONE,
75 (gobject.TYPE_PYOBJECT,))
77 gobject.signal_new("client_destroyed", gtk.Window,
78 gobject.SIGNAL_RUN_LAST,
79 gobject.TYPE_NONE,
80 (gobject.TYPE_PYOBJECT,))
82 gobject.signal_new("game_changed", gtk.Window,
83 gobject.SIGNAL_RUN_LAST,
84 gobject.TYPE_NONE,
85 (gobject.TYPE_PYOBJECT,))
87 gobject.signal_new("table_changed", gtk.Window,
88 gobject.SIGNAL_RUN_LAST,
89 gobject.TYPE_NONE,
90 (gobject.TYPE_PYOBJECT,))
92 # Each TableWindow object must have the following attributes correctly populated:
93 # tw.name = the table name from the title bar, which must to match the table name
94 # from the corresponding hand record in the db.
95 # tw.number = This is the system id number for the client table window in the
96 # format that the system presents it. This is Xid in Xwindows and
97 # hwnd in Microsoft Windows.
98 # tw.title = The full title from the window title bar.
99 # tw.width, tw.height = The width and height of the window in pixels. This is
100 # the internal width and height, not including the title bar and
101 # window borders.
102 # tw.x, tw.y = The x, y (horizontal, vertical) location of the window relative
103 # to the top left of the display screen. This also does not include the
104 # title bar and window borders. To put it another way, this is the
105 # screen location of (0, 0) in the working window.
106 # tournament = Tournament number for a tournament or None for a cash game.
107 # table = Table number for a tournament.
108 # gdkhandle =
109 # window =
110 # parent =
111 # game =
112 # search_string =
114 class Table_Window(object):
115 def __init__(self, config, site, table_name = None, tournament = None, table_number = None):
117 self.config = config
118 self.site = site
119 self.hud = None # fill in later
120 self.gdkhandle = None
121 if tournament is not None and table_number is not None:
122 self.tournament = int(tournament)
123 self.table = int(table_number)
124 self.name = "%s - %s" % (self.tournament, self.table)
125 self.type = "tour"
126 table_kwargs = dict(tournament = self.tournament, table_number = self.table)
127 self.tableno_re = getTableNoRe(self.config, self.site, tournament = self.tournament)
128 elif table_name is not None:
129 self.name = table_name
130 self.type = "cash"
131 self.tournament = None
132 table_kwargs = dict(table_name = table_name)
134 else:
135 return None
137 self.search_string = getTableTitleRe(self.config, self.site, self.type, **table_kwargs)
138 self.find_table_parameters()
140 geo = self.get_geometry()
141 if geo is None: return None
142 self.width = geo['width']
143 self.height = geo['height']
144 self.x = geo['x']
145 self.y = geo['y']
146 self.oldx = self.x # attn ray: remove these two lines and update Hud.py::update_table_position()
147 self.oldy = self.y
149 self.game = self.get_game()
151 def __str__(self):
152 # __str__ method for testing
153 likely_attrs = ("number", "title", "site", "width", "height", "x", "y",
154 "tournament", "table", "gdkhandle", "window", "parent",
155 "key", "hud", "game", "search_string", "tableno_re")
156 temp = 'TableWindow object\n'
157 for a in likely_attrs:
158 if getattr(self, a, 0):
159 temp += " %s = %s\n" % (a, getattr(self, a))
160 return temp
162 ####################################################################
163 # "get" methods. These query the table and return the info to get.
164 # They don't change the data in the table and are generally used
165 # by the "check" methods. Most of the get methods are in the
166 # subclass because they are specific to X, Windows, etc.
167 def get_game(self):
168 # title = self.get_window_title()
169 # if title is None:
170 # return False
171 title = self.title
173 # check for nl and pl games first, to avoid bad matches
174 for game, names in nlpl_game_names.iteritems():
175 for name in names:
176 if name in title:
177 return game
178 for game, names in limit_game_names.iteritems():
179 for name in names:
180 if name in title:
181 return game
182 return False
184 def get_table_no(self):
185 new_title = self.get_window_title()
186 if new_title is None:
187 return False
189 try:
190 mo = re.search(self.tableno_re, new_title)
191 except AttributeError: #'Table' object has no attribute 'tableno_re'
192 return False
194 if mo is not None:
195 #print "get_table_no: mo=",mo.groups()
196 return int(mo.group(1))
197 return False
199 ####################################################################
200 # check_table() is meant to be called by the hud periodically to
201 # determine if the client has been moved or resized. check_table()
202 # also checks and signals if the client has been closed.
203 def check_table(self, hud):
204 result = self.check_size()
205 if result != False:
206 hud.parent.main_window.emit(result, hud)
207 if result == "client_destroyed":
208 return True
210 result = self.check_loc()
211 if result != False:
212 hud.parent.main_window.emit(result, hud)
213 if result == "client_destroyed":
214 return True
215 return True
217 ####################################################################
218 # "check" methods. They use the corresponding get method, update the
219 # table object and return the name of the signal to be emitted or
220 # False if unchanged. These do not signal for destroyed
221 # clients to prevent a race condition.
223 # These might be called by a Window.timeout, so they must not
224 # return False, or the timeout will be cancelled.
225 def check_game(self, hud):
226 new_game = self.get_game()
227 if new_game is not None and self.game != new_game:
228 self.game = new_game
229 hud.main_window.emit("game_changed", hud)
230 return "game_changed"
231 return True
233 def check_size(self):
234 new_geo = self.get_geometry()
235 if new_geo is None: # window destroyed
236 return "client_destroyed"
238 elif self.width != new_geo['width'] or self.height != new_geo['height']: # window resized
239 self.oldwidth = self.width
240 self.width = new_geo['width']
241 self.oldheight = self.height
242 self.height = new_geo['height']
243 return "client_resized"
244 return False # no change
246 def check_loc(self):
247 new_geo = self.get_geometry()
248 if new_geo is None: # window destroyed
249 return "client_destroyed"
251 if self.x != new_geo['x'] or self.y != new_geo['y']: # window moved
252 # print self.x, self.y, new_geo['x'], new_geo['y']
253 self.x = new_geo['x']
254 self.y = new_geo['y']
255 return "client_moved"
256 return False # no change
258 def check_table_no(self, hud):
259 result = self.get_table_no()
260 if result != False and result != self.table:
261 self.table = result
262 if hud is not None:
263 hud.parent.main_window.emit("table_changed", hud)
264 return True
266 def check_bad_words(self, title):
267 for word in bad_words:
268 if word in title: return True
269 return False