Coverage for Hud.py: 20%
87 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-28 16:41 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-28 16:41 +0000
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""Hud.py
5Create and manage the hud overlays.
6"""
7# Copyright 2008-2012 Ray E. Barker
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24########################################################################
25# todo
30#import L10n
31#_ = L10n.get_translation()
33# Standard Library modules
34import logging
35import copy
37# logging has been set up in fpdb.py or HUD_main.py, use their settings:
38log = logging.getLogger("hud")
40# FreePokerTools modules
41import Configuration
42import Database
43import Hand
46def importName(module_name, name):
47 """Import a named object 'name' from module 'module_name'."""
48# Recipe 16.3 in the Python Cookbook, 2nd ed. Thanks!!!!
50 try:
51 module = __import__(module_name, globals(), locals(), [name])
52 except Exception as e:
53 log.error("Could not load hud module %s: %s" % (module_name, e))
54 return None
55 return(getattr(module, name))
58class Hud(object):
59 def __init__(self, parent, table, max, poker_game, game_type, config):
60# __init__ is (now) intended to be called from the stdin thread, so it
61# must not touch the gui
62 #if parent is None: # running from cli .. # fixme dont think this is working as expected
63 # self.parent = self
64 #else:
65 # self.parent = parent
66 #print "parent", parent
67 self.parent = parent
68 self.table = table
69 self.config = config
70 self.db_hud_connection = None
71 self.poker_game = poker_game
72 self.game_type = game_type # (ring|tour)
73 self.max = max
74 self.type = game_type
75 self.cards = None
76 self.site = table.site
77 self.hud_params = dict.copy(parent.hud_params) # we must dict.copy a fresh hud_params dict
78 # because each aux hud can control local hud param
79 # settings. Simply assigning the dictionary does not
80 # create a local/discrete version of the dictionary,
81 # so the different hud-windows get cross-contaminated
82 self.aux_windows = []
84 self.site_parameters = config.get_site_parameters(self.table.site)
85 self.supported_games_parameters = config.get_supported_games_parameters(self.poker_game, self.game_type)
86 self.layout_set = config.get_layout(self.table.site, self.game_type)
88 # Just throw error and die if any serious config issues are discovered
89 if self.supported_games_parameters == None:
90 log.error(("No <game_stat_set> found for %s games for type %s.\n") % (self.poker_game, self.game_type))
91 return
93 if self.layout_set == None:
94 log.error(("No layout found for %s games for site %s.\n") % (self.game_type, self.table.site))
95 return
97 if self.max not in self.layout_set.layout:
98 log.error(("No layout found for %d-max %s games for site %s.\n") % (self.max, self.game_type, self.table.site))
99 return
100 else:
101 self.layout = copy.deepcopy(self.layout_set.layout[self.max]) # deepcopy required here, because self.layout is used
102 # to propagate block moves from hud to mucked display
103 # (needed because there is only 1 layout for all aux)
104 #
105 # if we didn't deepcopy, self.layout would be shared
106 # amongst all open huds - this is fine until one of the
107 # huds does a resize, and then we have a total mess to
108 # understand how a single block move on a resized screen
109 # should be propagated to other tables of different sizes
111 # if there are AUX windows configured, set them up
112 if not self.supported_games_parameters['aux'] == [""]:
113 for aux in self.supported_games_parameters['aux'].split(","):
114 aux=aux.strip() # remove leading/trailing spaces
115 aux_params = config.get_aux_parameters(aux)
116 my_import = importName(aux_params['module'], aux_params['class'])
117 if my_import == None:
118 continue
119 #The main action happening below !!!
120 # the module/class is instantiated and is fed the config
121 # and aux_params. Normally this is ultimately inherited
122 # at Mucked.Aux_seats() for a hud aux
123 #
124 #The instatiated aux object is recorded in the
125 # self.aux_windows list in this module
126 #
127 #Subsequent updates to the aux's are controlled by
128 # hud_main.pyw
129 #
130 self.aux_windows.append(my_import(self, config, aux_params))
133 self.creation_attrs = None
136 def move_table_position(self): pass
138 def kill(self, *args):
139# kill all stat_windows, popups and aux_windows in this HUD
140# heap dead, burnt bodies, blood 'n guts, veins between my teeth
141# kill all aux windows
142 for aux in self.aux_windows:
143 aux.destroy()
144 self.aux_windows = []
146 def resize_windows(self):
147 # resize self.layout object; this will then be picked-up
148 # by all attached aux's when called by hud_main.idle_update
150 x_scale = 1.0 * self.table.width / self.layout.width
151 y_scale = 1.0 * self.table.height / self.layout.height
153 for i in (list(range(1, self.max + 1))):
154 if self.layout.location[i]:
155 self.layout.location[i] = (
156 (int(self.layout.location[i][0] * x_scale)),
157 (int(self.layout.location[i][1] * y_scale)) )
159 self.layout.common = (
160 int(self.layout.common[0] * x_scale),
161 int(self.layout.common[1] * y_scale) )
163 self.layout.width = self.table.width
164 self.layout.height = self.table.height
166 def reposition_windows(self, *args): pass
168 def save_layout(self, *args):
169# ask each aux to save its layout back to the config object
170 [aux.save_layout() for aux in self.aux_windows]
171# write the layouts back to the HUD_config
172 self.config.save()
175 def create(self, hand, config, stat_dict):
176 # update this hud, to the stats and players as of "hand"
177 # hand is the hand id of the most recent hand played at this table
179 self.stat_dict = stat_dict # stat_dict from HUD_main.read_stdin is mapped here
180 # the db_connection created in HUD_Main is NOT available to the
181 # hud.py and aux handlers, so create a fresh connection in this class
182 # if the db connection is made in __init__, then the sqlite db threading will fail
183 # so the db connection is made here instead.
184 if self.db_hud_connection is None:
185 self.db_hud_connection = Database.Database(self.config)
186 self.cards = self.get_cards(hand)
187 self.db_hud_connection = Database.Database(self.config)
188 # Load a hand instance (factory will load correct type for this hand)
189 self.hand_instance = Hand.hand_factory(hand, config, self.db_hud_connection)
190 self.db_hud_connection.connection.rollback()
191 log.info(('Creating hud from hand ')+str(hand))
192 print((('Creating hud from hand ')+str(hand)))
195 def update(self, hand, config):
196 # re-load a hand instance (factory will load correct type for this hand)
197 self.hand_instance = Hand.hand_factory(hand, config, self.db_hud_connection)
198 log.debug(f"hud update after hand_factory")
199 self.db_hud_connection.connection.rollback()
201 def get_cards(self, hand):
202 cards = self.db_hud_connection.get_cards(hand)
203 if self.poker_game in ['holdem', 'omahahi', 'omahahilo']:
204 comm_cards = self.db_hud_connection.get_common_cards(hand)
205 cards['common'] = comm_cards['common']
206 return cards