fpdb/pyfpdb/HUD_main.py

144 lines
4.4 KiB
Python
Raw Normal View History

#!/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 kill window on my seat
# to do adjust for preferred seat
# to do allow window resizing
# to do hud to echo, but ignore non numbers
# to do no hud window for hero
# to do things to add to config.xml
# to do font and size
2008-09-15 22:31:55 +02:00
# to do bg and fg color
# to do opacity
# Standard Library modules
import sys
import os
2008-09-15 22:31:55 +02:00
import thread
import Queue
errorfile = open('HUD-error.txt', 'w', 0)
sys.stderr = errorfile
# 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-09-15 22:31:55 +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
def process_new_hand(new_hand_id, db_name):
# there is a new hand_id to be processed
# read the hand_id from stdin and strip whitespace
2008-09-15 22:31:55 +02:00
global hud_dict
for h in hud_dict.keys():
if hud_dict[h].deleted:
del(hud_dict[h])
2008-09-15 22:31:55 +02:00
db_connection = Database.Database(config, db_name, 'temp')
(table_name, max, poker_game) = db_connection.get_table_name(new_hand_id)
# if a hud for this table exists, just update it
if hud_dict.has_key(table_name):
hud_dict[table_name].update(new_hand_id, db_connection, config)
# otherwise create a new hud
else:
table_windows = Tables.discover(config)
for t in table_windows.keys():
if table_windows[t].name == table_name:
2008-09-15 22:31:55 +02:00
hud_dict[table_name] = Hud.Hud(table_windows[t], max, poker_game, config, db_name)
hud_dict[table_name].create(new_hand_id, config)
hud_dict[table_name].update(new_hand_id, db_connection, config)
break
# print "table name \"%s\" not identified, no hud created" % (table_name)
2008-09-15 22:31:55 +02:00
db_connection.close_connection()
return(1)
def check_stdin(db_name):
try:
hand_no = dataQueue.get(block=False)
process_new_hand(hand_no, db_name)
except:
pass
return True
def read_stdin(source, condition, db_name):
new_hand_id = sys.stdin.readline()
2008-09-30 02:40:42 +02:00
if new_hand_id == "":
destroy()
2008-09-15 22:31:55 +02:00
process_new_hand(new_hand_id, db_name)
return True
def producer(): # This is the thread function
while True:
hand_no = sys.stdin.readline() # reads stdin
if hand_no == "":
2008-09-30 02:40:42 +02:00
destroy()
2008-09-15 22:31:55 +02:00
dataQueue.put(hand_no) # and puts result on the queue
if __name__== "__main__":
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'
sys.stderr.write("Using db name = %s\n" % (db_name))
2008-09-15 22:31:55 +02:00
config = Configuration.Config()
# db_connection = Database.Database(config, 'fpdb', 'holdem')
if os.name == 'posix':
s_id = gobject.io_add_watch(sys.stdin, gobject.IO_IN, read_stdin, db_name)
elif os.name == 'nt':
dataQueue = Queue.Queue() # shared global. infinite size
gobject.threads_init() # this is required
thread.start_new_thread(producer, ()) # starts the thread
gobject.timeout_add(1000, check_stdin, db_name)
else:
print "Sorry your operating system is not supported."
2008-08-19 01:18:17 +02:00
sys.exit()
main_window = gtk.Window()
main_window.connect("destroy", destroy)
2008-09-15 22:31:55 +02:00
label = gtk.Label('Closing this window will exit from the HUD.')
main_window.add(label)
2008-09-15 22:31:55 +02:00
main_window.set_title("HUD Main Window")
main_window.show_all()
gtk.main()