2008-08-19 00:53:25 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""Hud_main.py
|
|
|
|
|
|
|
|
Main for FreePokerTools HUD.
|
|
|
|
"""
|
2009-03-05 02:04:23 +01:00
|
|
|
# Copyright 2008, 2009, Ray E. Barker
|
2008-08-19 00:53:25 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
# to do allow window resizing
|
|
|
|
# to do hud to echo, but ignore non numbers
|
2008-12-17 19:24:37 +01:00
|
|
|
# to do no stat window for hero
|
2008-08-19 00:53:25 +02:00
|
|
|
# to do things to add to config.xml
|
|
|
|
|
|
|
|
# Standard Library modules
|
|
|
|
import sys
|
2009-06-15 05:14:53 +02:00
|
|
|
import os
|
|
|
|
import Options
|
2009-08-02 00:15:04 +02:00
|
|
|
import traceback
|
2009-02-27 16:36:45 +01:00
|
|
|
|
2009-06-15 05:14:53 +02:00
|
|
|
(options, sys.argv) = Options.fpdb_options()
|
|
|
|
|
|
|
|
if not options.errorsToConsole:
|
|
|
|
print "Note: error output is being diverted to fpdb-error-log.txt and HUD-error.txt. Any major error will be reported there _only_."
|
2009-08-06 13:23:57 +02:00
|
|
|
errorFile = open('HUD-error.txt', 'w', 0)
|
2009-06-15 05:14:53 +02:00
|
|
|
sys.stderr = errorFile
|
2009-02-27 16:36:45 +01:00
|
|
|
|
2008-09-15 22:31:55 +02:00
|
|
|
import thread
|
2008-10-16 21:15:28 +02:00
|
|
|
import time
|
2008-10-18 03:28:33 +02:00
|
|
|
import string
|
2008-11-07 18:22:37 +01:00
|
|
|
import re
|
2008-08-19 00:53:25 +02:00
|
|
|
|
|
|
|
# pyGTK modules
|
|
|
|
import pygtk
|
|
|
|
import gtk
|
|
|
|
import gobject
|
|
|
|
|
|
|
|
# FreePokerTools modules
|
|
|
|
import Configuration
|
|
|
|
import Database
|
|
|
|
import Tables
|
|
|
|
import Hud
|
|
|
|
|
2009-04-08 22:43:40 +02:00
|
|
|
|
2009-09-29 00:59:17 +02:00
|
|
|
# HUD params:
|
2009-09-27 22:23:00 +02:00
|
|
|
# - Set aggregate_ring and/or aggregate_tour to True is you want to include stats from other blind levels in the HUD display
|
2009-10-16 00:02:50 +02:00
|
|
|
# - If aggregation is used, the value of agg_bb_mult determines what levels are included. If
|
|
|
|
# agg_bb_mult is M and current blind level is L, blinds between L/M and L*M are included. e.g.
|
2009-09-27 22:23:00 +02:00
|
|
|
# if agg_bb_mult is 100, almost all levels are included in all HUD displays
|
2009-10-16 00:02:50 +02:00
|
|
|
# if agg_bb_mult is 2, levels from half to double the current blind level are included in the HUD
|
|
|
|
# if agg_bb_mult is 1 only the current level is included
|
2009-09-27 22:23:00 +02:00
|
|
|
# - Set hud_style to A to see stats for all-time
|
|
|
|
# Set hud_style to S to only see stats for current session (currently this shows stats for the last 24 hours)
|
|
|
|
# Set hud_style to T to only see stats for the last N days (uses value in hud_days)
|
|
|
|
# - Set hud_days to N to see stats for the last N days in the HUD (only applies if hud_style is T)
|
|
|
|
def_hud_params = { # Settings for all players apart from program owner ('hero')
|
|
|
|
'aggregate_ring' : False
|
|
|
|
, 'aggregate_tour' : True
|
|
|
|
, 'hud_style' : 'A'
|
|
|
|
, 'hud_days' : 90
|
2009-10-16 00:02:50 +02:00
|
|
|
, 'agg_bb_mult' : 10000 # 1 means no aggregation
|
2009-09-27 22:23:00 +02:00
|
|
|
# , 'hud_session_gap' : 30 not currently used
|
|
|
|
# Second set of variables for hero - these settings only apply to the program owner
|
2009-09-26 12:30:12 +02:00
|
|
|
, 'h_aggregate_ring' : False
|
2009-09-28 01:51:09 +02:00
|
|
|
, 'h_aggregate_tour' : True
|
2009-09-29 00:59:17 +02:00
|
|
|
, 'h_hud_style' : 'S' # A(ll) / S(ession) / T(ime in days)
|
2009-10-16 00:02:50 +02:00
|
|
|
, 'h_hud_days' : 60
|
|
|
|
, 'h_agg_bb_mult' : 10000 # 1 means no aggregation
|
2009-09-27 22:23:00 +02:00
|
|
|
# , 'h_hud_session_gap' : 30 not currently used
|
2009-09-26 12:30:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-22 00:19:49 +01:00
|
|
|
class HUD_main(object):
|
|
|
|
"""A main() object to own both the read_stdin thread and the gui."""
|
|
|
|
# This class mainly provides state for controlling the multiple HUDs.
|
|
|
|
|
|
|
|
def __init__(self, db_name = 'fpdb'):
|
|
|
|
self.db_name = db_name
|
2009-06-15 05:14:53 +02:00
|
|
|
self.config = Configuration.Config(file=options.config, dbname=options.dbname)
|
2009-02-22 00:19:49 +01:00
|
|
|
self.hud_dict = {}
|
2009-10-14 15:04:09 +02:00
|
|
|
self.hud_params = self.config.get_hud_ui_parameters()
|
2009-02-22 00:19:49 +01:00
|
|
|
|
|
|
|
# a thread to read stdin
|
|
|
|
gobject.threads_init() # this is required
|
|
|
|
thread.start_new_thread(self.read_stdin, ()) # starts the thread
|
|
|
|
|
|
|
|
# a main window
|
|
|
|
self.main_window = gtk.Window()
|
|
|
|
self.main_window.connect("destroy", self.destroy)
|
|
|
|
self.vb = gtk.VBox()
|
|
|
|
self.label = gtk.Label('Closing this window will exit from the HUD.')
|
|
|
|
self.vb.add(self.label)
|
|
|
|
self.main_window.add(self.vb)
|
|
|
|
self.main_window.set_title("HUD Main Window")
|
|
|
|
self.main_window.show_all()
|
|
|
|
|
|
|
|
def destroy(*args): # call back for terminating the main eventloop
|
|
|
|
gtk.main_quit()
|
2009-02-28 01:47:52 +01:00
|
|
|
|
|
|
|
def kill_hud(self, event, table):
|
|
|
|
# called by an event in the HUD, to kill this specific HUD
|
2009-05-21 21:30:18 +02:00
|
|
|
if table in self.hud_dict:
|
|
|
|
self.hud_dict[table].kill()
|
2009-10-25 12:34:07 +01:00
|
|
|
self.hud_dict[table].main_window.destroy()
|
2009-05-21 21:30:18 +02:00
|
|
|
self.vb.remove(self.hud_dict[table].tablehudlabel)
|
|
|
|
del(self.hud_dict[table])
|
2009-03-04 14:17:23 +01:00
|
|
|
self.main_window.resize(1,1)
|
2009-02-28 01:47:52 +01:00
|
|
|
|
2009-09-30 00:34:52 +02:00
|
|
|
def create_HUD(self, new_hand_id, table, table_name, max, poker_game, type, stat_dict, cards):
|
|
|
|
"""type is "ring" or "tour" used to set hud_params"""
|
2009-01-06 11:18:45 +01:00
|
|
|
|
2009-02-22 00:19:49 +01:00
|
|
|
def idle_func():
|
|
|
|
|
|
|
|
gtk.gdk.threads_enter()
|
2009-10-25 13:27:55 +01:00
|
|
|
try: # TODO: seriously need to decrease the scope of this block.. what are we expecting to error?
|
2009-10-26 21:50:17 +01:00
|
|
|
# TODO: The purpose of this try/finally block is to make darn sure that threads_leave()
|
|
|
|
# TODO: gets called. If there is an exception and threads_leave() doesn't get called we
|
|
|
|
# TODO: lock up. REB
|
2009-03-27 00:17:00 +01:00
|
|
|
newlabel = gtk.Label("%s - %s" % (table.site, table_name))
|
2009-02-22 00:19:49 +01:00
|
|
|
self.vb.add(newlabel)
|
|
|
|
newlabel.show()
|
2009-03-04 14:17:23 +01:00
|
|
|
self.main_window.resize_children()
|
2009-02-22 00:19:49 +01:00
|
|
|
|
|
|
|
self.hud_dict[table_name].tablehudlabel = newlabel
|
2009-02-26 05:35:15 +01:00
|
|
|
self.hud_dict[table_name].create(new_hand_id, self.config, stat_dict, cards)
|
2009-02-22 00:19:49 +01:00
|
|
|
for m in self.hud_dict[table_name].aux_windows:
|
2009-03-05 02:04:23 +01:00
|
|
|
m.create()
|
2009-02-22 00:19:49 +01:00
|
|
|
m.update_gui(new_hand_id)
|
2009-02-26 05:35:15 +01:00
|
|
|
self.hud_dict[table_name].update(new_hand_id, self.config)
|
2009-02-22 00:19:49 +01:00
|
|
|
self.hud_dict[table_name].reposition_windows()
|
|
|
|
return False
|
|
|
|
finally:
|
|
|
|
gtk.gdk.threads_leave()
|
2009-02-27 18:42:49 +01:00
|
|
|
|
|
|
|
self.hud_dict[table_name] = Hud.Hud(self, table, max, poker_game, self.config, self.db_connection)
|
2009-04-07 05:42:36 +02:00
|
|
|
self.hud_dict[table_name].table_name = table_name
|
2009-03-05 02:04:23 +01:00
|
|
|
self.hud_dict[table_name].stat_dict = stat_dict
|
|
|
|
self.hud_dict[table_name].cards = cards
|
2009-09-30 00:34:52 +02:00
|
|
|
|
|
|
|
if type == "tour" and self.hud_params['aggregate_tour'] == False:
|
|
|
|
self.hud_dict[table_name].hud_params['agg_bb_mult'] = 1
|
|
|
|
elif type == "ring" and self.hud_params['aggregate_ring'] == False:
|
|
|
|
self.hud_dict[table_name].hud_params['agg_bb_mult'] = 1
|
|
|
|
if type == "tour" and self.hud_params['h_aggregate_tour'] == False:
|
|
|
|
self.hud_dict[table_name].hud_params['h_agg_bb_mult'] = 1
|
|
|
|
elif type == "ring" and self.hud_params['h_aggregate_ring'] == False:
|
|
|
|
self.hud_dict[table_name].hud_params['h_agg_bb_mult'] = 1
|
|
|
|
self.hud_params['aggregate_ring'] == True
|
|
|
|
self.hud_params['h_aggregate_ring'] == True
|
|
|
|
|
2009-03-27 00:17:00 +01:00
|
|
|
[aw.update_data(new_hand_id, self.db_connection) for aw in self.hud_dict[table_name].aux_windows]
|
2009-02-22 00:19:49 +01:00
|
|
|
gobject.idle_add(idle_func)
|
|
|
|
|
2009-02-26 05:35:15 +01:00
|
|
|
def update_HUD(self, new_hand_id, table_name, config):
|
2009-02-22 00:19:49 +01:00
|
|
|
"""Update a HUD gui from inside the non-gui read_stdin thread."""
|
|
|
|
# This is written so that only 1 thread can touch the gui--mainly
|
|
|
|
# for compatibility with Windows. This method dispatches the
|
|
|
|
# function idle_func() to be run by the gui thread, at its leisure.
|
|
|
|
def idle_func():
|
|
|
|
gtk.gdk.threads_enter()
|
2009-10-25 13:27:55 +01:00
|
|
|
# try:
|
|
|
|
self.hud_dict[table_name].update(new_hand_id, config)
|
|
|
|
[aw.update_gui(new_hand_id) for aw in self.hud_dict[table_name].aux_windows]
|
|
|
|
# finally:
|
|
|
|
gtk.gdk.threads_leave()
|
|
|
|
return False
|
|
|
|
|
2009-02-22 00:19:49 +01:00
|
|
|
gobject.idle_add(idle_func)
|
|
|
|
|
|
|
|
def read_stdin(self): # This is the thread function
|
|
|
|
"""Do all the non-gui heavy lifting for the HUD program."""
|
|
|
|
|
|
|
|
# This db connection is for the read_stdin thread only. It should not
|
|
|
|
# be passed to HUDs for use in the gui thread. HUD objects should not
|
|
|
|
# need their own access to the database, but should open their own
|
|
|
|
# if it is required.
|
2009-10-29 00:53:31 +01:00
|
|
|
self.db_connection = Database.Database(self.config)
|
|
|
|
tourny_finder = re.compile('(\d+) (\d+)')
|
2009-09-26 12:30:12 +02:00
|
|
|
|
2009-10-29 00:53:31 +01:00
|
|
|
# get hero's screen names and player ids
|
|
|
|
self.hero, self.hero_ids = {}, {}
|
|
|
|
for site in self.config.get_supported_sites():
|
|
|
|
result = self.db_connection.get_site_id(site)
|
|
|
|
if result:
|
|
|
|
site_id = result[0][0]
|
|
|
|
self.hero[site_id] = self.config.supported_sites[site].screen_name
|
|
|
|
self.hero_ids[site_id] = self.db_connection.get_player_id(self.config, site, self.hero[site_id])
|
|
|
|
|
|
|
|
while 1: # wait for a new hand number on stdin
|
|
|
|
new_hand_id = sys.stdin.readline()
|
|
|
|
new_hand_id = string.rstrip(new_hand_id)
|
|
|
|
if new_hand_id == "": # blank line means quit
|
|
|
|
self.destroy()
|
|
|
|
break # this thread is not always killed immediately with gtk.main_quit()
|
2009-09-26 12:30:12 +02:00
|
|
|
# get basic info about the new hand from the db
|
|
|
|
# if there is a db error, complain, skip hand, and proceed
|
2009-10-29 00:53:31 +01:00
|
|
|
try:
|
|
|
|
(table_name, max, poker_game, type, site_id) = self.db_connection.get_table_name(new_hand_id)
|
2009-09-26 12:30:12 +02:00
|
|
|
|
2009-10-29 00:53:31 +01:00
|
|
|
cards = self.db_connection.get_cards(new_hand_id)
|
|
|
|
comm_cards = self.db_connection.get_common_cards(new_hand_id)
|
|
|
|
if comm_cards != {}: # stud!
|
|
|
|
cards['common'] = comm_cards['common']
|
|
|
|
except Exception, err:
|
|
|
|
err = traceback.extract_tb(sys.exc_info()[2])[-1]
|
|
|
|
print "db error: skipping "+str(new_hand_id)+" "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
|
|
|
|
if new_hand_id: # new_hand_id is none if we had an error prior to the store
|
|
|
|
sys.stderr.write("Database error %s in hand %d. Skipping.\n" % (err, int(new_hand_id)))
|
|
|
|
continue
|
2009-09-26 12:30:12 +02:00
|
|
|
|
2009-10-29 00:53:31 +01:00
|
|
|
if type == "tour": # hand is from a tournament
|
|
|
|
mat_obj = tourny_finder.search(table_name)
|
|
|
|
if mat_obj:
|
|
|
|
(tour_number, tab_number) = mat_obj.group(1, 2)
|
|
|
|
temp_key = tour_number
|
|
|
|
else: # tourney, but can't get number and table
|
|
|
|
print "could not find tournament: skipping "
|
|
|
|
#sys.stderr.write("Could not find tournament %d in hand %d. Skipping.\n" % (int(tour_number), int(new_hand_id)))
|
|
|
|
continue
|
|
|
|
|
|
|
|
else:
|
|
|
|
temp_key = table_name
|
2009-09-26 12:30:12 +02:00
|
|
|
|
|
|
|
# Update an existing HUD
|
2009-10-29 00:53:31 +01:00
|
|
|
if temp_key in self.hud_dict:
|
|
|
|
try:
|
|
|
|
# get stats using hud's specific params
|
|
|
|
self.db_connection.init_hud_stat_vars( self.hud_dict[temp_key].hud_params['hud_days']
|
|
|
|
, self.hud_dict[temp_key].hud_params['h_hud_days'])
|
|
|
|
stat_dict = self.db_connection.get_stats_from_hand(new_hand_id, type, self.hud_dict[temp_key].hud_params, self.hero_ids[site_id])
|
|
|
|
except:
|
|
|
|
err = traceback.extract_tb(sys.exc_info()[2])[-1]
|
|
|
|
print "db get_stats error: skipping "+str(new_hand_id)+" "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
|
|
|
|
if new_hand_id: # new_hand_id is none if we had an error prior to the store
|
|
|
|
sys.stderr.write("Database get_stats error %s in hand %d. Skipping.\n" % (err, int(new_hand_id)))
|
|
|
|
continue
|
|
|
|
self.hud_dict[temp_key].stat_dict = stat_dict
|
|
|
|
self.hud_dict[temp_key].cards = cards
|
|
|
|
[aw.update_data(new_hand_id, self.db_connection) for aw in self.hud_dict[temp_key].aux_windows]
|
|
|
|
self.update_HUD(new_hand_id, temp_key, self.config)
|
|
|
|
|
2009-09-26 12:30:12 +02:00
|
|
|
# Or create a new HUD
|
2009-10-29 00:53:31 +01:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
# get stats using default params
|
|
|
|
self.db_connection.init_hud_stat_vars( self.hud_params['hud_days'], self.hud_params['h_hud_days'] )
|
|
|
|
stat_dict = self.db_connection.get_stats_from_hand(new_hand_id, type, self.hud_params, self.hero_ids[site_id])
|
|
|
|
except:
|
|
|
|
err = traceback.extract_tb(sys.exc_info()[2])[-1]
|
|
|
|
print "db get_stats error: skipping "+str(new_hand_id)+" "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
|
|
|
|
if new_hand_id: # new_hand_id is none if we had an error prior to the store
|
|
|
|
sys.stderr.write("Database get_stats error %s in hand %d. Skipping.\n" % (err, int(new_hand_id)))
|
|
|
|
continue
|
|
|
|
if type == "tour":
|
|
|
|
tablewindow = Tables.discover_tournament_table(self.config, tour_number, tab_number)
|
2008-11-07 18:22:37 +01:00
|
|
|
else:
|
2009-10-29 00:53:31 +01:00
|
|
|
tablewindow = Tables.discover_table_by_name(self.config, table_name)
|
2009-11-03 20:30:52 +01:00
|
|
|
if tablewindow is None:
|
2009-09-26 12:30:12 +02:00
|
|
|
# If no client window is found on the screen, complain and continue
|
2009-10-29 00:53:31 +01:00
|
|
|
if type == "tour":
|
|
|
|
table_name = "%s %s" % (tour_number, tab_number)
|
2009-11-03 20:30:52 +01:00
|
|
|
sys.stderr.write("HUD create: table name "+table_name+" not found, skipping.\n")
|
2009-10-29 00:53:31 +01:00
|
|
|
else:
|
|
|
|
self.create_HUD(new_hand_id, tablewindow, temp_key, max, poker_game, type, stat_dict, cards)
|
|
|
|
self.db_connection.connection.rollback()
|
2008-08-19 00:53:25 +02:00
|
|
|
|
|
|
|
if __name__== "__main__":
|
2008-09-15 22:31:55 +02:00
|
|
|
|
2009-06-15 05:14:53 +02:00
|
|
|
sys.stderr.write("HUD_main starting\n")
|
|
|
|
sys.stderr.write("Using db name = %s\n" % (options.dbname))
|
2008-09-15 22:31:55 +02:00
|
|
|
|
2009-02-22 00:19:49 +01:00
|
|
|
# start the HUD_main object
|
2009-06-15 05:14:53 +02:00
|
|
|
hm = HUD_main(db_name = options.dbname)
|
2008-10-14 16:33:32 +02:00
|
|
|
|
2009-02-22 00:19:49 +01:00
|
|
|
# start the event loop
|
2008-08-19 00:53:25 +02:00
|
|
|
gtk.main()
|