2 # -*- coding: utf-8 -*-
4 Based on HUD_main .. who knows if we want to actually use this or not
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.
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
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
54 # FreePokerTools modules
57 import SummaryEverleaf
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"
68 self
.starttime
= time
.time()
75 self
.numrebuys
= 0 # this should probably be attached to the players list...
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
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
)
114 def addrebuy(self
, widget
=None):
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):
122 def destroy(self
, widget
, data
=None):
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
141 self
.main_window
= gtk
.Window()
142 self
.main_window
.connect("destroy", self
.destroy
)
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)
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..
170 self
.main_window
.resize_children()
171 self
.main_window
.show()
176 t
.editbutton
= editbutton
177 t
.rebuybutton
= rebuybutton
179 print self
.tourney_list
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
190 def create_HUD(self
, new_hand_id
, table
, table_name
, max, poker_game
, stat_dict
, cards
):
194 gtk
.gdk
.threads_enter()
196 newlabel
= gtk
.Label("%s - %s" % (table
.site
, table_name
))
197 self
.vb
.add(newlabel
)
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
:
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()
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.
225 gtk
.gdk
.threads_enter()
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
]
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
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
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
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")
269 if type == "tour": # hand is from a tournament
270 mat_obj
= tourny_finder
.search(table_name
)
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")
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
292 tablewindow
= Tables
.discover_tournament_table(self
.config
, tour_number
, tab_number
)
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
298 table_name
= "%s %s" % (tour_number
, tab_number
)
299 sys
.stderr
.write(_("Table name %s not found, skipping.")% table_name
)
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