2008-08-19 00:53:25 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""Hud_main.py
|
|
|
|
|
|
|
|
Main for FreePokerTools HUD.
|
|
|
|
"""
|
|
|
|
# Copyright 2008, Ray E. Barker
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# to do font and size
|
|
|
|
|
|
|
|
# Standard Library modules
|
|
|
|
import sys
|
|
|
|
import os
|
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
|
|
|
|
|
|
|
|
# global dict for keeping the huds
|
|
|
|
hud_dict = {}
|
2008-12-24 13:56:32 +01:00
|
|
|
eb = 0 # our former event-box
|
2008-09-15 22:31:55 +02:00
|
|
|
|
2008-08-19 00:53:25 +02:00
|
|
|
db_connection = 0;
|
|
|
|
config = 0;
|
|
|
|
|
|
|
|
def destroy(*args): # call back for terminating the main eventloop
|
|
|
|
gtk.main_quit()
|
2008-09-15 22:31:55 +02:00
|
|
|
|
2008-10-14 16:33:32 +02:00
|
|
|
def create_HUD(new_hand_id, table, db_name, table_name, max, poker_game, db_connection, config, stat_dict):
|
2009-01-06 11:18:45 +01:00
|
|
|
global hud_dict, eb
|
|
|
|
|
2008-10-14 16:33:32 +02:00
|
|
|
def idle_func():
|
2009-01-06 11:18:45 +01:00
|
|
|
global hud_dict, eb
|
|
|
|
|
2008-10-18 03:28:33 +02:00
|
|
|
gtk.gdk.threads_enter()
|
2008-10-14 16:33:32 +02:00
|
|
|
try:
|
2009-01-15 05:05:08 +01:00
|
|
|
newlabel = gtk.Label(table.site + " - " + table_name)
|
2008-12-24 13:56:32 +01:00
|
|
|
eb.add(newlabel)
|
|
|
|
newlabel.show()
|
|
|
|
|
2008-12-17 19:24:37 +01:00
|
|
|
hud_dict[table_name] = Hud.Hud(table, max, poker_game, config, db_connection)
|
2008-12-24 13:56:32 +01:00
|
|
|
hud_dict[table_name].tablehudlabel = newlabel
|
2008-10-14 16:33:32 +02:00
|
|
|
hud_dict[table_name].create(new_hand_id, config)
|
2008-12-17 19:24:37 +01:00
|
|
|
for m in hud_dict[table_name].aux_windows:
|
|
|
|
m.update_data(new_hand_id, db_connection)
|
|
|
|
m.update_gui(new_hand_id)
|
2008-10-18 17:48:24 +02:00
|
|
|
hud_dict[table_name].update(new_hand_id, config, stat_dict)
|
2008-11-07 18:22:37 +01:00
|
|
|
hud_dict[table_name].reposition_windows()
|
2008-10-14 16:33:32 +02:00
|
|
|
return False
|
|
|
|
finally:
|
2008-11-07 18:22:37 +01:00
|
|
|
gtk.gdk.threads_leave()
|
2008-10-14 16:33:32 +02:00
|
|
|
gobject.idle_add(idle_func)
|
|
|
|
|
2008-10-18 18:00:29 +02:00
|
|
|
def update_HUD(new_hand_id, table_name, config, stat_dict):
|
2008-10-14 16:33:32 +02:00
|
|
|
global hud_dict
|
|
|
|
def idle_func():
|
2008-10-18 20:05:38 +02:00
|
|
|
gtk.gdk.threads_enter()
|
2008-10-14 16:33:32 +02:00
|
|
|
try:
|
2008-10-18 17:48:24 +02:00
|
|
|
hud_dict[table_name].update(new_hand_id, config, stat_dict)
|
2008-11-16 23:53:31 +01:00
|
|
|
for m in hud_dict[table_name].aux_windows:
|
|
|
|
m.update_gui(new_hand_id)
|
2008-10-14 16:33:32 +02:00
|
|
|
return False
|
|
|
|
finally:
|
2008-11-07 18:22:37 +01:00
|
|
|
gtk.gdk.threads_leave()
|
2008-10-14 16:33:32 +02:00
|
|
|
gobject.idle_add(idle_func)
|
2009-01-06 11:18:45 +01:00
|
|
|
|
|
|
|
def HUD_removed(tablename):
|
|
|
|
global hud_dict, eb
|
|
|
|
|
2009-01-15 05:05:08 +01:00
|
|
|
tablename = Tables.clean_title(tablename)
|
|
|
|
# TODO: There's a potential problem here somewhere, that this hacks around .. the table_name as being passed to HUD_create is cleaned,
|
|
|
|
# but the table.name as being passed here is not cleaned. I don't know why. -eric
|
2009-01-06 11:18:45 +01:00
|
|
|
if tablename in hud_dict and hud_dict[tablename].deleted:
|
|
|
|
eb.remove(hud_dict[tablename].tablehudlabel)
|
2009-01-15 05:05:08 +01:00
|
|
|
del hud_dict[tablename]
|
2009-01-06 11:18:45 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2008-09-15 22:31:55 +02:00
|
|
|
|
2008-10-18 17:48:24 +02:00
|
|
|
def read_stdin(): # This is the thread function
|
2009-01-06 11:18:45 +01:00
|
|
|
global hud_dict, eb
|
2008-09-15 22:31:55 +02:00
|
|
|
|
2008-11-06 04:44:29 +01:00
|
|
|
db_connection = Database.Database(config, db_name, 'temp')
|
2008-11-11 15:50:20 +01:00
|
|
|
tourny_finder = re.compile('(\d+) (\d+)')
|
2008-11-07 18:22:37 +01:00
|
|
|
|
2008-10-14 16:33:32 +02:00
|
|
|
while True: # wait for a new hand number on stdin
|
|
|
|
new_hand_id = sys.stdin.readline()
|
2008-10-18 03:28:33 +02:00
|
|
|
new_hand_id = string.rstrip(new_hand_id)
|
2008-10-14 16:33:32 +02:00
|
|
|
if new_hand_id == "": # blank line means quit
|
2008-09-30 02:40:42 +02:00
|
|
|
destroy()
|
2009-01-07 00:13:12 +01:00
|
|
|
break # this thread is not always killed immediately with gtk.main_quit()
|
2008-10-14 16:33:32 +02:00
|
|
|
|
|
|
|
# delete hud_dict entries for any HUD destroyed since last iteration
|
2009-01-06 11:18:45 +01:00
|
|
|
# for h in hud_dict:
|
|
|
|
# HUD_removed(h)
|
|
|
|
# removing this function, we shouldn't need it anymore, since the hud should notify us anyway, right?
|
2008-09-15 22:31:55 +02:00
|
|
|
|
2008-11-07 18:22:37 +01:00
|
|
|
# get basic info about the new hand from the db
|
2008-10-14 16:33:32 +02:00
|
|
|
(table_name, max, poker_game) = db_connection.get_table_name(new_hand_id)
|
2008-11-07 18:22:37 +01:00
|
|
|
|
|
|
|
# find out if this hand is from a tournament
|
|
|
|
is_tournament = False
|
2008-11-11 15:50:20 +01:00
|
|
|
(tour_number, tab_number) = (0, 0)
|
|
|
|
mat_obj = tourny_finder.search(table_name)
|
|
|
|
if mat_obj:
|
|
|
|
is_tournament = True
|
|
|
|
(tour_number, tab_number) = mat_obj.group(1, 2)
|
2008-11-07 18:22:37 +01:00
|
|
|
|
2008-10-14 16:33:32 +02:00
|
|
|
stat_dict = db_connection.get_stats_from_hand(new_hand_id)
|
2008-08-19 00:53:25 +02:00
|
|
|
|
2008-11-07 18:22:37 +01:00
|
|
|
# if a hud for this CASH table exists, just update it
|
2009-01-06 11:18:45 +01:00
|
|
|
if table_name in hud_dict:
|
2008-11-16 23:53:31 +01:00
|
|
|
# update the data for the aux_windows
|
|
|
|
for aw in hud_dict[table_name].aux_windows:
|
2008-12-17 19:24:37 +01:00
|
|
|
aw.update_data(new_hand_id, db_connection)
|
2008-10-18 18:00:29 +02:00
|
|
|
update_HUD(new_hand_id, table_name, config, stat_dict)
|
2008-12-17 19:24:37 +01:00
|
|
|
|
2008-11-07 18:22:37 +01:00
|
|
|
# if a hud for this TOURNAMENT table exists, just update it
|
2009-01-06 11:18:45 +01:00
|
|
|
elif tour_number in hud_dict:
|
2008-11-11 15:50:20 +01:00
|
|
|
update_HUD(new_hand_id, tour_number, config, stat_dict)
|
2008-12-17 19:24:37 +01:00
|
|
|
|
2008-11-11 15:50:20 +01:00
|
|
|
# otherwise create a new hud
|
2008-10-24 21:44:53 +02:00
|
|
|
else:
|
2008-11-07 18:22:37 +01:00
|
|
|
if is_tournament:
|
2008-11-11 15:50:20 +01:00
|
|
|
tablewindow = Tables.discover_tournament_table(config, tour_number, tab_number)
|
2008-11-07 18:22:37 +01:00
|
|
|
if tablewindow == None:
|
2008-11-11 15:50:20 +01:00
|
|
|
sys.stderr.write("tournament %s, table %s not found\n" % (tour_number, tab_number))
|
2008-11-07 18:22:37 +01:00
|
|
|
else:
|
2008-11-11 15:50:20 +01:00
|
|
|
create_HUD(new_hand_id, tablewindow, db_name, tour_number, max, poker_game, db_connection, config, stat_dict)
|
2008-10-25 11:36:18 +02:00
|
|
|
else:
|
2008-11-07 18:22:37 +01:00
|
|
|
tablewindow = Tables.discover_table_by_name(config, table_name)
|
|
|
|
if tablewindow == None:
|
|
|
|
sys.stderr.write("table name "+table_name+" not found\n")
|
|
|
|
else:
|
|
|
|
create_HUD(new_hand_id, tablewindow, db_name, table_name, max, poker_game, db_connection, config, stat_dict)
|
2008-08-19 00:53:25 +02:00
|
|
|
|
|
|
|
if __name__== "__main__":
|
2008-09-26 15:18:47 +02:00
|
|
|
sys.stderr.write("HUD_main starting\n")
|
2008-09-15 22:31:55 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
db_name = sys.argv[1]
|
|
|
|
except:
|
2008-09-30 02:42:27 +02:00
|
|
|
db_name = 'fpdb'
|
2008-09-26 15:18:47 +02:00
|
|
|
sys.stderr.write("Using db name = %s\n" % (db_name))
|
2008-09-15 22:31:55 +02:00
|
|
|
|
|
|
|
config = Configuration.Config()
|
2008-10-14 16:33:32 +02:00
|
|
|
|
|
|
|
gobject.threads_init() # this is required
|
2008-10-18 17:48:24 +02:00
|
|
|
thread.start_new_thread(read_stdin, ()) # starts the thread
|
2008-08-19 01:18:17 +02:00
|
|
|
|
2008-08-19 00:53:25 +02:00
|
|
|
main_window = gtk.Window()
|
|
|
|
main_window.connect("destroy", destroy)
|
2008-12-24 13:56:32 +01:00
|
|
|
eb = gtk.VBox()
|
2008-09-15 22:31:55 +02:00
|
|
|
label = gtk.Label('Closing this window will exit from the HUD.')
|
2008-10-22 03:46:30 +02:00
|
|
|
eb.add(label)
|
|
|
|
main_window.add(eb)
|
2008-12-24 13:56:32 +01:00
|
|
|
|
2008-09-15 22:31:55 +02:00
|
|
|
main_window.set_title("HUD Main Window")
|
2008-08-19 00:53:25 +02:00
|
|
|
main_window.show_all()
|
|
|
|
|
|
|
|
gtk.main()
|