Expanded my entry in contributors.txt.
[fpdb-dooglus.git] / pyfpdb / TableWindow.py
blobb325a3e6308f745ee0593aeeff1a62a084b358d9
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 - 2011, 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 import L10n
30 _ = L10n.get_translation()
32 # Standard Library modules
33 import re
35 # pyGTK modules
36 import gtk
37 import gobject
39 # FreePokerTools modules
40 import Configuration
41 from HandHistoryConverter import getTableTitleRe
42 from HandHistoryConverter import getTableNoRe
44 c = Configuration.Config()
45 log = Configuration.get_logger("logging.conf", "hud", log_dir=c.dir_log, log_file='HUD-log.txt')
47 # Global used for figuring out the current game being played from the title.
48 # The dict key is a tuple of (limit type, category) for the game.
49 # The list is the names for those games used by the supported poker sites
50 # This is currently only used for mixed games, so it only needs to support those
51 # games on PokerStars and Full Tilt.
52 nlpl_game_names = { #fpdb name Stars Name FTP Name (if different)
53 ("nl", "holdem" ) : ("No Limit Hold\'em" , ),
54 ("pl", "holdem" ) : ("Pot Limit Hold\'em" , ),
55 ("pl", "omahahi" ) : ("Pot Limit Omaha" ,"Pot Limit Omaha Hi" ),
57 limit_game_names = { #fpdb name Stars Name FTP Name
58 ("fl", "holdem" ) : ("Limit Hold\'em" , ),
59 ("fl", "omahahilo" ) : ("Limit Omaha H/L" , ),
60 ("fl", "studhilo" ) : ("Limit Stud H/L" , ),
61 ("fl", "razz" ) : ("Limit Razz" , ),
62 ("fl", "studhi" ) : ("Limit Stud" , "Stud Hi"),
63 ("fl", "27_3draw" ) : ("Limit Triple Draw 2-7 Lowball", )
66 # A window title might have our table name + one of these words/
67 # phrases. If it has this word in the title, it is not a table.
68 bad_words = ('History for table:', 'HUD:', 'Chat:', 'FPDBHUD')
70 # Here are the custom signals we define for allowing the 'client watcher'
71 # thread to communicate with the gui thread. Any time a poker client is
72 # is moved, resized, or closed one of these signals is emitted to the
73 # HUD main window.
74 gobject.signal_new("client_moved", gtk.Window,
75 gobject.SIGNAL_RUN_LAST,
76 gobject.TYPE_NONE,
77 (gobject.TYPE_PYOBJECT,))
79 gobject.signal_new("client_resized", gtk.Window,
80 gobject.SIGNAL_RUN_LAST,
81 gobject.TYPE_NONE,
82 (gobject.TYPE_PYOBJECT,))
84 gobject.signal_new("client_destroyed", gtk.Window,
85 gobject.SIGNAL_RUN_LAST,
86 gobject.TYPE_NONE,
87 (gobject.TYPE_PYOBJECT,))
89 gobject.signal_new("game_changed", gtk.Window,
90 gobject.SIGNAL_RUN_LAST,
91 gobject.TYPE_NONE,
92 (gobject.TYPE_PYOBJECT,))
94 gobject.signal_new("table_changed", gtk.Window,
95 gobject.SIGNAL_RUN_LAST,
96 gobject.TYPE_NONE,
97 (gobject.TYPE_PYOBJECT,))
99 # Each TableWindow object must have the following attributes correctly populated:
100 # tw.name = the table name from the title bar, which must to match the table name
101 # from the corresponding hand record in the db.
102 # tw.number = This is the system id number for the client table window in the
103 # format that the system presents it. This is Xid in Xwindows and
104 # hwnd in Microsoft Windows.
105 # tw.title = The full title from the window title bar.
106 # tw.width, tw.height = The width and height of the window in pixels. This is
107 # the internal width and height, not including the title bar and
108 # window borders.
109 # tw.x, tw.y = The x, y (horizontal, vertical) location of the window relative
110 # to the top left of the display screen. This also does not include the
111 # title bar and window borders. To put it another way, this is the
112 # screen location of (0, 0) in the working window.
113 # tournament = Tournament number for a tournament or None for a cash game.
114 # table = Table number for a tournament.
115 # gdkhandle =
116 # window =
117 # parent =
118 # game =
119 # search_string =
121 class Table_Window(object):
122 def __init__(self, config, site, table_name = None, tournament = None, table_number = None):
124 self.config = config
125 self.site = site
126 self.hud = None # fill in later
127 self.gdkhandle = None
128 self.number = None
129 if tournament is not None and table_number is not None:
130 self.tournament = int(tournament)
131 self.table = int(table_number)
132 self.name = "%s - %s" % (self.tournament, self.table)
133 self.type = "tour"
134 table_kwargs = dict(tournament = self.tournament, table_number = self.table)
135 self.tableno_re = getTableNoRe(self.config, self.site, tournament = self.tournament)
136 elif table_name is not None:
137 self.name = table_name
138 self.type = "cash"
139 self.tournament = None
140 table_kwargs = dict(table_name = table_name)
142 else:
143 return None
145 self.search_string = getTableTitleRe(self.config, self.site, self.type, **table_kwargs)
146 trys = 0
147 while True:
148 self.find_table_parameters()
149 if self.number is not None: break
150 trys += 1
151 if trys > 4:
152 log.error(_("Can't find table %s") % table_name)
153 return None
155 geo = self.get_geometry()
156 if geo is None: return None
157 self.width = geo['width']
158 self.height = geo['height']
159 self.x = geo['x']
160 self.y = geo['y']
161 self.oldx = self.x # attn ray: remove these two lines and update Hud.py::update_table_position()
162 self.oldy = self.y
164 self.game = self.get_game()
166 def __str__(self):
167 # __str__ method for testing
168 likely_attrs = ("number", "title", "site", "width", "height", "x", "y",
169 "tournament", "table", "gdkhandle", "window", "parent",
170 "key", "hud", "game", "search_string", "tableno_re")
171 temp = 'TableWindow object\n'
172 for a in likely_attrs:
173 if getattr(self, a, 0):
174 temp += " %s = %s\n" % (a, getattr(self, a))
175 return temp
177 ####################################################################
178 # "get" methods. These query the table and return the info to get.
179 # They don't change the data in the table and are generally used
180 # by the "check" methods. Most of the get methods are in the
181 # subclass because they are specific to X, Windows, etc.
182 def get_game(self):
183 # title = self.get_window_title()
184 # if title is None:
185 # return False
186 title = self.title
188 # check for nl and pl games first, to avoid bad matches
189 for game, names in nlpl_game_names.iteritems():
190 for name in names:
191 if name in title:
192 return game
193 for game, names in limit_game_names.iteritems():
194 for name in names:
195 if name in title:
196 return game
197 return False
199 def get_table_no(self):
200 new_title = self.get_window_title()
201 if new_title is None:
202 return False
204 try:
205 mo = re.search(self.tableno_re, new_title)
206 except AttributeError: #'Table' object has no attribute 'tableno_re'
207 return False
209 if mo is not None:
210 #print "get_table_no: mo=",mo.groups()
211 return int(mo.group(1))
212 return False
214 ####################################################################
215 # check_table() is meant to be called by the hud periodically to
216 # determine if the client has been moved or resized. check_table()
217 # also checks and signals if the client has been closed.
218 def check_table(self, hud):
219 result = self.check_size()
220 if result != False:
221 hud.parent.main_window.emit(result, hud)
222 if result == "client_destroyed":
223 return True
225 result = self.check_loc()
226 if result != False:
227 hud.parent.main_window.emit(result, hud)
228 if result == "client_destroyed":
229 return True
230 return True
232 ####################################################################
233 # "check" methods. They use the corresponding get method, update the
234 # table object and return the name of the signal to be emitted or
235 # False if unchanged. These do not signal for destroyed
236 # clients to prevent a race condition.
238 # These might be called by a Window.timeout, so they must not
239 # return False, or the timeout will be cancelled.
240 def check_game(self, hud):
241 new_game = self.get_game()
242 if new_game is not None and self.game != new_game:
243 self.game = new_game
244 hud.main_window.emit("game_changed", hud)
245 return "game_changed"
246 return True
248 def check_size(self):
249 new_geo = self.get_geometry()
250 if new_geo is None: # window destroyed
251 return "client_destroyed"
253 elif self.width != new_geo['width'] or self.height != new_geo['height']: # window resized
254 self.oldwidth = self.width
255 self.width = new_geo['width']
256 self.oldheight = self.height
257 self.height = new_geo['height']
258 return "client_resized"
259 return False # no change
261 def check_loc(self):
262 new_geo = self.get_geometry()
263 if new_geo is None: # window destroyed
264 return "client_destroyed"
266 if self.x != new_geo['x'] or self.y != new_geo['y']: # window moved
267 # print self.x, self.y, new_geo['x'], new_geo['y']
268 self.x = new_geo['x']
269 self.y = new_geo['y']
270 return "client_moved"
271 return False # no change
273 def check_table_no(self, hud):
274 result = self.get_table_no()
275 if result != False and result != self.table:
276 self.table = result
277 if hud is not None:
278 hud.parent.main_window.emit("table_changed", hud)
279 return True
281 def check_bad_words(self, title):
282 for word in bad_words:
283 if word in title: return True
284 return False