Use debian 2.7 only
[fpbd-bostik.git] / pyfpdb / TournamentTracker.py
blob9add5e3e985ac43336569dd02de86c8270a06887
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """TourneyTracker.py
4 Based on HUD_main .. who knows if we want to actually use this or not
5 """
6 # Copyright (c) 2009-2011 Eric Blade, and the FPDB team.
8 #This program is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, version 3 of the License.
12 #This program is distributed in the hope that it will be useful,
13 #but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 #GNU General Public License for more details.
17 #You should have received a copy of the GNU Affero General Public License
18 #along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #In the "official" distribution you can find the license in agpl-3.0.txt.
22 import L10n
23 _ = L10n.get_translation()
25 # to do allow window resizing
26 # to do hud to echo, but ignore non numbers
27 # to do no stat window for hero
28 # to do things to add to config.xml
30 # Standard Library modules
31 import sys
32 import os
33 import Options
34 import traceback
36 (options, argv) = Options.fpdb_options()
38 if not options.errorsToConsole:
39 print (_("Note: error output is being diverted to %s.") % "tourneyerror.txt"),
40 _("Any major error will be reported there _only_.")
41 errorFile = open('tourneyerror.txt', 'w', 0)
42 sys.stderr = errorFile
44 import thread
45 import time
46 import string
47 import re
49 # pyGTK modules
50 import pygtk
51 import gtk
52 import gobject
54 # FreePokerTools modules
55 import Configuration
56 import Database
57 import SummaryEverleaf
59 class Tournament:
60 """Tournament will hold the information about a tournament, I guess ? Remember I'm new to this language, so I don't know the best ways to do things"""
62 def __init__(self, parent, site, tid): # site should probably be something in the config object, but i don't know how the config object works right now, so we're going to make it a str ..
63 print "Tournament init"
64 self.parent = parent
65 self.window = None
66 self.site = site
67 self.id = tid
68 self.starttime = time.time()
69 self.endtime = None
70 self.game = None
71 self.structure = None
72 self.buyin = 0
73 self.fee = 0
74 self.rebuys = False
75 self.numrebuys = 0 # this should probably be attached to the players list...
76 self.numplayers = 0
77 self.prizepool = 0
78 self.players = {} # eventually i'd guess we'd probably want to fill this with playername:playerid's
79 self.results = {} # i'd guess we'd want to load this up with playerid's instead of playernames, too, as well, also
81 # if site == "Everleaf": # this should be attached to a button that says "retrieve tournament info" or something for sites that we know how to do it for
82 summary = SummaryEverleaf.EverleafSummary()
83 self.site = summary.parser.SiteName
84 self.id = summary.parser.TourneyId
85 self.starttime = summary.parser.TourneyStartTime
86 self.endtime = summary.parser.TourneyEndTime
87 self.game = summary.parser.TourneyGameType
88 self.structure = summary.parser.TourneyStructure
89 self.buyin = summary.parser.TourneyBuyIn # need to remember to parse the Fee out of this and move it to self.fee
90 self.rebuys = (summary.parser.TourneyRebuys == "yes")
91 self.prizepool = summary.parser.TourneyPool
92 self.numplayers = summary.parser.TourneysPlayers
94 self.openwindow() # let's start by getting any info we need.. meh
96 def openwindow(self, widget=None):
97 if self.window is not None:
98 self.window.show() # isn't there a better way to bring something to the front? not that GTK focus works right anyway, ever
99 else:
100 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
101 #print("tournament edit window:", self.window)
102 self.window.connect("delete_event", self.delete_event)
103 self.window.connect("destroy", self.destroy)
104 self.window.set_title(_("FPDB Tournament Entry"))
105 self.window.set_border_width(1)
106 self.window.set_default_size(480,640)
107 self.window.set_resizable(True)
109 self.main_vbox = gtk.VBox(False, 1)
110 self.main_vbox.set_border_width(1)
111 self.window.add(self.main_vbox)
112 self.window.show()
114 def addrebuy(self, widget=None):
115 t = self
116 t.numrebuys += 1
117 t.mylabel.set_label("%s - %s - %s - %s - %s %s - %s - %s - %s - %s - %s" % (t.site, t.id, t.starttime, t.endtime, t.structure, t.game, t.buyin, t.fee, t.numrebuys, t.numplayers, t.prizepool))
119 def delete_event(self, widget, event, data=None):
120 return False
122 def destroy(self, widget, data=None):
123 return False
124 #end def destroy
127 class ttracker_main(object):
128 """A main() object to own both the read_stdin thread and the gui."""
129 # This class mainly provides state for controlling the multiple HUDs.
131 def __init__(self, db_name = 'fpdb'):
132 self.db_name = db_name
133 self.config = Configuration.Config(file=options.config, dbname=options.dbname)
134 self.tourney_list = []
136 # a thread to read stdin
137 gobject.threads_init() # this is required
138 thread.start_new_thread(self.read_stdin, ()) # starts the thread
140 # a main window
141 self.main_window = gtk.Window()
142 self.main_window.connect("destroy", self.destroy)
143 self.vb = gtk.VBox()
144 self.label = gtk.Label(_('Closing this window will stop the Tournament Tracker'))
145 self.vb.add(self.label)
146 self.addbutton = gtk.Button(label=_("Add Tournament"))
147 self.addbutton.connect("clicked", self.addClicked, "add tournament")
148 self.vb.add(self.addbutton)
150 self.main_window.add(self.vb)
151 self.main_window.set_title(_("FPDB Tournament Tracker"))
152 self.main_window.show_all()
154 def addClicked(self, widget, data): # what is "data"? i'm guessing anything i pass in after the function name in connect() but unsure because the documentation sucks
155 print "addClicked", widget, data
156 t = Tournament(self, None, None)
157 if t is not None:
158 print "new tournament=", t
159 self.tourney_list.append(t)
160 mylabel = gtk.Label("%s - %s - %s - %s - %s %s - %s - %s - %s - %s - %s" % (t.site, t.id, t.starttime, t.endtime, t.structure, t.game, t.buyin, t.fee, t.numrebuys, t.numplayers, t.prizepool))
161 print "new label=", mylabel
162 editbutton = gtk.Button(label=_("Edit"))
163 print "new button=", editbutton
164 editbutton.connect("clicked", t.openwindow)
165 rebuybutton = gtk.Button(label=_("Rebuy"))
166 rebuybutton.connect("clicked", t.addrebuy)
167 self.vb.add(rebuybutton)
168 self.vb.add(editbutton) # These should probably be put in.. a.. h-box? i don't know..
169 self.vb.add(mylabel)
170 self.main_window.resize_children()
171 self.main_window.show()
172 mylabel.show()
173 editbutton.show()
174 rebuybutton.show()
175 t.mylabel = mylabel
176 t.editbutton = editbutton
177 t.rebuybutton = rebuybutton
178 self.vb.show()
179 print self.tourney_list
181 return True
182 else:
183 return False
184 # when we move the start command over to the main program, we can have the main program ask for the tourney id, and pipe it into the stdin here
185 # at least that was my initial thought on it
187 def destroy(*args): # call back for terminating the main eventloop
188 gtk.main_quit()
190 def create_HUD(self, new_hand_id, table, table_name, max, poker_game, stat_dict, cards):
192 def idle_func():
194 gtk.gdk.threads_enter()
195 try:
196 newlabel = gtk.Label("%s - %s" % (table.site, table_name))
197 self.vb.add(newlabel)
198 newlabel.show()
199 self.main_window.resize_children()
201 self.hud_dict[table_name].tablehudlabel = newlabel
202 self.hud_dict[table_name].create(new_hand_id, self.config, stat_dict, cards)
203 for m in self.hud_dict[table_name].aux_windows:
204 m.create()
205 m.update_gui(new_hand_id)
206 self.hud_dict[table_name].update(new_hand_id, self.config)
207 self.hud_dict[table_name].reposition_windows()
208 return False
209 finally:
210 gtk.gdk.threads_leave()
212 self.hud_dict[table_name] = Hud.Hud(self, table, max, poker_game, self.config, self.db_connection)
213 self.hud_dict[table_name].table_name = table_name
214 self.hud_dict[table_name].stat_dict = stat_dict
215 self.hud_dict[table_name].cards = cards
216 [aw.update_data(new_hand_id, self.db_connection) for aw in self.hud_dict[table_name].aux_windows]
217 gobject.idle_add(idle_func)
219 def update_HUD(self, new_hand_id, table_name, config):
220 """Update a HUD gui from inside the non-gui read_stdin thread."""
221 # This is written so that only 1 thread can touch the gui--mainly
222 # for compatibility with Windows. This method dispatches the
223 # function idle_func() to be run by the gui thread, at its leisure.
224 def idle_func():
225 gtk.gdk.threads_enter()
226 try:
227 self.hud_dict[table_name].update(new_hand_id, config)
228 [aw.update_gui(new_hand_id) for aw in self.hud_dict[table_name].aux_windows]
229 return False
230 finally:
231 gtk.gdk.threads_leave()
232 gobject.idle_add(idle_func)
234 def read_stdin(self): # This is the thread function
235 """Do all the non-gui heavy lifting for the HUD program."""
237 # This db connection is for the read_stdin thread only. It should not
238 # be passed to HUDs for use in the gui thread. HUD objects should not
239 # need their own access to the database, but should open their own
240 # if it is required.
241 self.db_connection = Database.Database(self.config, self.db_name, 'temp')
242 # self.db_connection.init_hud_stat_vars(hud_days)
243 tourny_finder = re.compile('(\d+) (\d+)')
245 while 1: # wait for a new hand number on stdin
246 new_hand_id = sys.stdin.readline()
247 new_hand_id = string.rstrip(new_hand_id)
248 if new_hand_id == "": # blank line means quit
249 self.destroy()
250 break # this thread is not always killed immediately with gtk.main_quit()
251 # get basic info about the new hand from the db
252 # if there is a db error, complain, skip hand, and proceed
253 try:
254 (table_name, max, poker_game, type, site_id, numseats) = self.db_connection.get_table_name(new_hand_id)
255 stat_dict = self.db_connection.get_stats_from_hand(new_hand_id, aggregate_stats[type]
256 ,hud_style, agg_bb_mult)
258 cards = self.db_connection.get_cards(new_hand_id)
259 comm_cards = self.db_connection.get_common_cards(new_hand_id)
260 if comm_cards != {}: # stud!
261 cards['common'] = comm_cards['common']
262 except Exception, err:
263 err = traceback.extract_tb(sys.exc_info()[2])[-1]
264 #print _("db error: skipping ")+str(new_hand_id)+" "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
265 if new_hand_id: # new_hand_id is none if we had an error prior to the store
266 sys.stderr.write(_("Database error %s in hand %d. Skipping.") % (err, int(new_hand_id)) + "\n")
267 continue
269 if type == "tour": # hand is from a tournament
270 mat_obj = tourny_finder.search(table_name)
271 if mat_obj:
272 (tour_number, tab_number) = mat_obj.group(1, 2)
273 temp_key = tour_number
274 else: # tourney, but can't get number and table
275 #print _("could not find tournament: skipping")
276 sys.stderr.write(_("Could not find tournament %d in hand %d. Skipping.") % (int(tour_number), int(new_hand_id)) + "\n")
277 continue
279 else:
280 temp_key = table_name
282 # Update an existing HUD
283 if temp_key in self.hud_dict:
284 self.hud_dict[temp_key].stat_dict = stat_dict
285 self.hud_dict[temp_key].cards = cards
286 [aw.update_data(new_hand_id, self.db_connection) for aw in self.hud_dict[temp_key].aux_windows]
287 self.update_HUD(new_hand_id, temp_key, self.config)
289 # Or create a new HUD
290 else:
291 if type == "tour":
292 tablewindow = Tables.discover_tournament_table(self.config, tour_number, tab_number)
293 else:
294 tablewindow = Tables.discover_table_by_name(self.config, table_name)
295 if tablewindow is None:
296 # If no client window is found on the screen, complain and continue
297 if type == "tour":
298 table_name = "%s %s" % (tour_number, tab_number)
299 sys.stderr.write(_("Table name %s not found, skipping.")% table_name)
300 else:
301 self.create_HUD(new_hand_id, tablewindow, temp_key, max, poker_game, stat_dict, cards)
302 self.db_connection.connection.rollback()
304 if __name__== "__main__":
306 sys.stderr.write(_("Tournament tracker starting"))
307 sys.stderr.write(_("Using db name = %s") % (options.dbname))
309 Configuration.set_logfile("fpdb-log.txt")
310 # start the HUD_main object
311 hm = ttracker_main(db_name = options.dbname)
313 # start the event loop
314 gtk.main()