start changes to allow different hud choices for hero and opponents

This commit is contained in:
sqlcoder 2009-09-26 11:30:12 +01:00
parent 3d301718ae
commit ea74862a5a
6 changed files with 188 additions and 90 deletions

View File

@ -406,10 +406,19 @@ class Database:
err = traceback.extract_tb(sys.exc_info()[2])[-1]
print "***Error: "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
def get_stats_from_hand(self, hand, aggregate = False, hud_style = 'A', agg_bb_mult = 100):
def get_stats_from_hand( self, hand
, hud_params = {'aggregate_tour':False, 'aggregate_ring':False, 'hud_style':'A', 'agg_bb_mult':100}
, hero_ids = {}
):
aggregate = hud_params['aggregate_tour'] if type == "tour" else hud_params['aggregate_ring']
hud_style = hud_params['hud_style']
agg_bb_mult = hud_params['agg_bb_mult']
stat_dict = {}
if hud_style == 'S':
return( self.get_stats_from_hand_session(hand) )
self.get_stats_from_hand_session(hand, stat_dict)
return stat_dict
else: # hud_style == A
@ -433,7 +442,6 @@ class Database:
# now get the stats
c.execute(self.sql.query[query], subs)
colnames = [desc[0] for desc in c.description]
stat_dict = {}
for row in c.fetchall():
t_dict = {}
for name, val in zip(colnames, row):
@ -444,7 +452,7 @@ class Database:
return stat_dict
# uses query on handsplayers instead of hudcache to get stats on just this session
def get_stats_from_hand_session(self, hand):
def get_stats_from_hand_session(self, hand, stat_dict):
query = self.sql.query['get_stats_from_hand_session']
if self.db_server == 'mysql':
@ -459,31 +467,32 @@ class Database:
#print "sess_stats: subs =", subs, "subs[0] =", subs[0]
c.execute(query, subs)
colnames = [desc[0] for desc in c.description]
n,stat_dict = 0,{}
row = c.fetchone()
while row:
if colnames[0].lower() == 'player_id':
playerid = row[0]
else:
log.error("ERROR: query %s result does not have player_id as first column" % (query,))
break
n = 0
row = c.fetchone()
if colnames[0].lower() == 'player_id':
playerid = row[0]
while row:
for name, val in zip(colnames, row):
if not playerid in stat_dict:
stat_dict[playerid] = {}
stat_dict[playerid][name.lower()] = val
elif not name.lower() in stat_dict[playerid]:
stat_dict[playerid][name.lower()] = val
elif name.lower() not in ('hand_id', 'player_id', 'seat', 'screen_name', 'seats'):
stat_dict[playerid][name.lower()] += val
n += 1
if n >= 10000: break # todo: don't think this is needed so set nice and high
# for now - comment out or remove?
row = c.fetchone()
else:
log.error("ERROR: query %s result does not have player_id as first column" % (query,))
for name, val in zip(colnames, row):
if not playerid in stat_dict:
stat_dict[playerid] = {}
stat_dict[playerid][name.lower()] = val
elif not name.lower() in stat_dict[playerid]:
stat_dict[playerid][name.lower()] = val
elif name.lower() not in ('hand_id', 'player_id', 'seat', 'screen_name', 'seats'):
stat_dict[playerid][name.lower()] += val
n += 1
if n >= 4000: break # todo: don't think this is needed so set nice and high
# for now - comment out or remove?
row = c.fetchone()
#print " %d rows fetched, len(stat_dict) = %d" % (n, len(stat_dict))
#print "session stat_dict =", stat_dict
return stat_dict
#return stat_dict
def get_player_id(self, config, site, player_name):
c = self.connection.cursor()

View File

@ -74,6 +74,22 @@ hud_session_gap = 30 # Gap (minutes) between hands that indicates a change of s
# a new session)
#hud_hands = 0 # Max number of hands from each player to use for hud stats (not used)
def_hud_params = { 'aggregate_ring' : False
, 'aggregate_tour' : False
, 'hud_style' : 'A'
, 'hud_days ' : 90
, 'agg_bb_mult' : 100
, 'hud_session_gap' : 30
# second set of variables for hero
, 'h_aggregate_ring' : False
, 'h_aggreagte_tour' : False
, 'h_hud_style' : 'A'
, 'h_hud_days ' : 90
, 'h_agg_bb_mult' : 100
, 'h_hud_session_gap' : 30
}
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.
@ -82,6 +98,7 @@ class HUD_main(object):
self.db_name = db_name
self.config = Configuration.Config(file=options.config, dbname=options.dbname)
self.hud_dict = {}
self.hud_params = def_hud_params
# a thread to read stdin
gobject.threads_init() # this is required
@ -164,68 +181,84 @@ class HUD_main(object):
# 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.
self.db_connection = Database.Database(self.config, self.db_name, 'temp')
self.db_connection.init_hud_stat_vars(hud_days)
tourny_finder = re.compile('(\d+) (\d+)')
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()
# get basic info about the new hand from the db
# if there is a db error, complain, skip hand, and proceed
try:
(table_name, max, poker_game, type) = self.db_connection.get_table_name(new_hand_id)
stat_dict = self.db_connection.get_stats_from_hand(new_hand_id, aggregate_stats[type]
,hud_style, agg_bb_mult)
try:
self.db_connection = Database.Database(self.config, self.db_name, 'temp')
self.db_connection.init_hud_stat_vars(hud_days)
tourny_finder = re.compile('(\d+) (\d+)')
# 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()
# get basic info about the new hand from the db
# if there is a db error, complain, skip hand, and proceed
try:
(table_name, max, poker_game, type, site_id) = self.db_connection.get_table_name(new_hand_id)
stat_dict = self.db_connection.get_stats_from_hand(new_hand_id, self.hud_params, self.hero_ids)
#self.hud_params['aggregate_tour'] if type = "tour"
# else self.hud_params['aggregate_ring'],
#hud_style,
#agg_bb_mult)
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
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)))
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
else:
temp_key = table_name
# Update an existing HUD
if temp_key in self.hud_dict:
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)
# Or create a new HUD
else:
if type == "tour":
tablewindow = Tables.discover_tournament_table(self.config, tour_number, tab_number)
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
# Update an existing HUD
if temp_key in self.hud_dict:
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)
# Or create a new HUD
else:
tablewindow = Tables.discover_table_by_name(self.config, table_name)
if tablewindow == None:
# If no client window is found on the screen, complain and continue
if type == "tour":
table_name = "%s %s" % (tour_number, tab_number)
sys.stderr.write("table name "+table_name+" not found, skipping.\n")
else:
self.create_HUD(new_hand_id, tablewindow, temp_key, max, poker_game, stat_dict, cards)
self.db_connection.connection.rollback()
tablewindow = Tables.discover_tournament_table(self.config, tour_number, tab_number)
else:
tablewindow = Tables.discover_table_by_name(self.config, table_name)
if tablewindow == None:
# If no client window is found on the screen, complain and continue
if type == "tour":
table_name = "%s %s" % (tour_number, tab_number)
sys.stderr.write("table name "+table_name+" not found, skipping.\n")
else:
self.create_HUD(new_hand_id, tablewindow, temp_key, max, poker_game, stat_dict, cards)
self.db_connection.connection.rollback()
except:
err = traceback.extract_tb(sys.exc_info()[2])[-1]
print "***Error: "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
if __name__== "__main__":

View File

@ -73,6 +73,7 @@ class Hud:
self.stacked = True
self.site = table.site
self.mw_created = False
self.hud_params = parent.hud_params
self.stat_windows = {}
self.popup_windows = {}
@ -218,7 +219,11 @@ class Hud:
# heap dead, burnt bodies, blood 'n guts, veins between my teeth
for s in self.stat_windows.itervalues():
s.kill_popups()
s.window.destroy()
try:
# throws "invalid window handle" in WinXP (sometimes?)
s.window.destroy()
except:
pass
self.stat_windows = {}
# also kill any aux windows
for aux in self.aux_windows:
@ -621,7 +626,57 @@ class Popup_window:
# window.window.reparent(self.table.gdkhandle, 0, 0)
window.window.set_transient_for(self.table.gdkhandle)
# window.present()
class HUD_Params:
def __init__(self, hud):
self.aggregate_stats = hud.def_aggregate_stats
self.hud_style = hud.def_hud_style
self.hud_days = hud.def_hud_days
self.agg_bb_mult = hud.def_agg_bb_mult
self.hud_session_gap = hud.def_hud_session_gap
self.h_aggregate_stats = hud.def_h_aggregate_stats
self.h_hud_style = hud.def_h_hud_style
self.h_hud_days = hud.def_h_hud_days
self.h_agg_bb_mult = hud.def_h_agg_bb_mult
self.h_hud_session_gap = hud.def_h_hud_session_gap
def set_aggregate_stats(self, agg):
self.aggregate_stats = agg
def set_hud_style(self, style):
self.hud_style = style
def set_hud_days(self, days):
self.hud_days = days
def set_agg_bb_mult(self, mult):
self.agg_bb_mult = mult
def set_hud_session_gap(self, gap):
self.hud_session_gap = gap
def set_aggregate_stats(self, agg):
self.aggregate_stats = agg
def set_hud_style(self, style):
self.hud_style = style
def set_hud_days(self, days):
self.hud_days = days
def set_agg_bb_mult(self, mult):
self.agg_bb_mult = mult
def set_hud_session_gap(self, gap):
self.hud_session_gap = gap
def get_aggregate_stats(self):
return self.aggregate_stats
def get_hud_style(self):
return self.hud_style
def get_hud_days(self):
return self.hud_days
def get_agg_bb_mult(self):
return self.agg_bb_mult
def get_hud_session_gap(self):
return self.hud_session_gap
if __name__== "__main__":
main_window = gtk.Window()

View File

@ -1581,10 +1581,11 @@ class Sql:
"""
self.query['get_table_name'] = """
select tableName, maxSeats, category, type
from Hands,Gametypes
where Hands.id = %s
and Gametypes.id = Hands.gametypeId
select h.tableName, h.maxSeats, gt.category, gt.type, gt.siteId
from Hands h
,Gametypes gt
where h.id = %s
and gt.id = h.gametypeId
"""
self.query['get_actual_seat'] = """

View File

@ -248,7 +248,7 @@ class ttracker_main(object):
# get basic info about the new hand from the db
# if there is a db error, complain, skip hand, and proceed
try:
(table_name, max, poker_game, type) = self.db_connection.get_table_name(new_hand_id)
(table_name, max, poker_game, type, site_id) = self.db_connection.get_table_name(new_hand_id)
stat_dict = self.db_connection.get_stats_from_hand(new_hand_id, aggregate_stats[type]
,hud_style, agg_bb_mult)

View File

@ -549,7 +549,7 @@ class Importer:
if self.callHud:
#print "call to HUD here. handsId:",handsId
#pipe the Hands.id out to the HUD
#print "sending hand to hud", handsId, "pipe =", self.caller.pipe_to_hud
print "sending hand to hud", handsId, "pipe =", self.caller.pipe_to_hud
self.caller.pipe_to_hud.stdin.write("%s" % (handsId) + os.linesep)
except Exceptions.DuplicateError:
duplicates += 1