Coverage for Aux_Classic_Hud.py: 0%
148 statements
« prev ^ index » next coverage.py v7.6.3, created at 2024-10-15 19:33 +0000
« prev ^ index » next coverage.py v7.6.3, created at 2024-10-15 19:33 +0000
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright 2011-2012, "Gimick" of the FPDB project fpdb.sourceforge.net
4# - bbtgaf@googlemail.com
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU Affero General Public License as published by
8# the Free Software Foundation, version 3 of the License.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU Affero General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17# In the "official" distribution you can find the license in agpl-3.0.txt.
19########################################################################
21"""
22Aux_Classic_Hud.py
24FPDB classic hud, implemented using new-hud framework.
26This module structure must be based upon simple HUD in the module Aux_Hud.
28Aux_Hud is minimal frozen functionality and is not changed,so
29HUD customisation is done in modules which extend/subclass/override aux_hud.
31***HERE BE DRAGONS***
32Please take extra care making changes to this code - there is
33multiple-level inheritence in much of this code, class heirarchies
34are not immediately obvious, and there is very close linkage with most of
35the Hud modules.
36"""
38# import L10n
39# _ = L10n.get_translation()
41# to do
42# =======
43# check that the parameters stored at AW level make sense for players
44# - when playing more than one site
45# sort out the wierd focus issues in flop-mucked.
47# Standard Library modules
48import logging
51# FreePokerTools modules
52import Aux_Hud
53import Stats
54from PyQt5.QtWidgets import QInputDialog, QMessageBox
55import Database
56import Configuration
57import os
59# logging has been set up in fpdb.py or HUD_main.py, use their settings:
60log = logging.getLogger("hud")
63class Classic_HUD(Aux_Hud.Simple_HUD):
64 """
65 There is one instance of this class per poker table
66 the stat_windows for each player are controlled
67 from this class.
68 """
70 def __init__(self, hud, config, params):
71 super(Classic_HUD, self).__init__(hud, config, params)
73 # the following attributes ensure that the correct
74 # classes are invoked by the calling modules (aux_hud+mucked)
76 self.aw_class_window = Classic_Stat_Window
77 self.aw_class_stat = Classic_stat
78 self.aw_class_table_mw = Classic_table_mw
79 self.aw_class_label = Classic_label
82class Classic_Stat_Window(Aux_Hud.Simple_Stat_Window):
83 """Stat windows are the blocks shown continually, 1 for each player."""
85 def update_contents(self, i):
86 super(Classic_Stat_Window, self).update_contents(i)
87 if i == "common":
88 return
90 # control kill/display of active/inactive player stat blocks
91 if self.aw.get_id_from_seat(i) is None:
92 # no player dealt in this seat for this hand
93 # hide the display
94 self.hide()
95 else:
96 # player dealt-in, force display of stat block
97 # need to call move() to re-establish window position
98 self.move(self.aw.positions[i][0] + self.aw.hud.table.x, self.aw.positions[i][1] + self.aw.hud.table.y)
99 self.setWindowOpacity(float(self.aw.params["opacity"]))
100 # show item, just in case it was hidden by the user
101 self.show()
103 def button_press_middle(self, event):
104 self.hide()
107class Classic_stat(Aux_Hud.Simple_stat):
108 """A class to display each individual statistic on the Stat_Window"""
110 def __init__(self, stat, seat, popup, game_stat_config, aw):
111 super(Classic_stat, self).__init__(stat, seat, popup, game_stat_config, aw)
112 # game_stat_config is the instance of this stat in the supported games stat configuration
113 # use this prefix to directly extract the attributes
115 # debug
116 # print(f"Initializing Classic_stat for {stat}")
117 # print(f"stat_locolor: {game_stat_config.stat_locolor}")
118 # print(f"stat_loth: {game_stat_config.stat_loth}")
119 # print(f"stat_midcolor: {game_stat_config.stat_midcolor}")
120 # print(f"stat_hicolor: {game_stat_config.stat_hicolor}")
121 # print(f"stat_hith: {game_stat_config.stat_hith}")
123 self.aw = aw
124 self.popup = game_stat_config.popup
125 self.click = game_stat_config.click
126 self.incolor = "rgba(0, 0, 0, 0)"
127 if self.click == "open_comment_dialog":
128 self.lab.mouseDoubleClickEvent = self.open_comment_dialog
129 # print(f"value of self.click in the constructor : {self.click}") # debug
130 self.tip = game_stat_config.tip # not implemented yet
131 self.hudprefix = game_stat_config.hudprefix
132 self.hudsuffix = game_stat_config.hudsuffix
134 try:
135 self.stat_locolor = game_stat_config.stat_locolor
136 self.stat_loth = game_stat_config.stat_loth
137 except Exception:
138 self.stat_locolor = self.stat_loth = ""
139 try:
140 self.stat_hicolor = game_stat_config.stat_hicolor
141 self.stat_hith = game_stat_config.stat_hith
142 except Exception:
143 self.stat_hicolor = self.stat_hith = ""
145 try:
146 self.stat_midcolor = game_stat_config.stat_midcolor
147 except Exception:
148 self.stat_midcolor = ""
149 try:
150 self.hudcolor = game_stat_config.hudcolor
151 except Exception:
152 self.hudcolor = aw.params["fgcolor"]
154 def open_comment_dialog(self, event):
155 if self.stat != "playershort":
156 return
158 player_id = self.get_player_id()
159 if player_id is None:
160 return
162 player_name = self.get_player_name(player_id)
163 current_comment = self.get_current_comment(player_id)
165 new_comment, ok = QInputDialog.getMultiLineText(
166 None, f"Add comment to player: {player_name}", f"Add your comment for {player_name}:", current_comment
167 )
168 if ok:
169 self.save_comment(player_id, new_comment)
171 def get_player_id(self):
172 for id, data in self.stat_dict.items():
173 if data["seat"] == self.lab.aw_seat:
174 return id
175 return None
177 def get_player_name(self, player_id):
178 db = Database.Database(self.aw.hud.config)
179 try:
180 q = db.sql.query["get_player_name"]
181 db.cursor.execute(q, (player_id,))
182 result = db.cursor.fetchone()
183 return result[0] if result else "Unknown Player"
184 except Exception as e:
185 print(f"Error fetching player name: {e}")
186 return "Unknown Player"
187 finally:
188 db.close_connection()
190 def get_current_comment(self, player_id):
191 db = Database.Database(self.aw.hud.config)
192 try:
193 q = db.sql.query["get_player_comment"]
194 db.cursor.execute(q, (player_id,))
195 result = db.cursor.fetchone()
196 return result[0] if result else ""
197 except Exception as e:
198 print(f"Error fetching comment: {e}")
199 return ""
200 finally:
201 db.close_connection()
203 def save_comment(self, player_id, comment):
204 db = Database.Database(self.aw.hud.config)
205 try:
206 q = db.sql.query["update_player_comment"]
207 db.cursor.execute(q, (comment, player_id))
208 db.commit()
209 QMessageBox.information(None, "Comment saved", "The comment has been successfully saved.")
210 except Exception as e:
211 print(f"Error saving comment: {e}")
212 QMessageBox.warning(None, "Error", f"An error occurred while saving the comment: {e}")
213 finally:
214 db.close_connection()
216 def has_comment(self, player_id):
217 db = Database.Database(self.aw.hud.config)
218 try:
219 q = db.sql.query["get_player_comment"]
220 db.cursor.execute(q, (player_id,))
221 result = db.cursor.fetchone()
222 return bool(result and result[0])
223 except Exception as e:
224 print(f"Error checking comment: {e}")
225 return False
226 finally:
227 db.close_connection()
229 def update(self, player_id, stat_dict):
230 super(Classic_stat, self).update(player_id, stat_dict)
232 if not self.number: # stat did not create, so exit now
233 return False
235 fg = self.hudcolor
236 # print(f"Updating stat for {self.stat}: value={self.number[1]}, loth={self.stat_loth}, hith={self.stat_hith}")
238 if self.stat_loth != "" and self.stat_hith != "":
239 try:
240 value_str = self.number[1]
241 if value_str == "NA":
242 fg = self.incolor # default color for NA
243 else:
244 value = float(value_str)
245 if value < float(self.stat_loth):
246 fg = self.stat_locolor
247 # print(f"Using locolor: {fg}")
248 elif value < float(self.stat_hith):
249 fg = self.stat_midcolor
250 # print(f"Using midcolor: {fg}")
251 else:
252 fg = self.stat_hicolor
253 # print(f"Using hicolor: {fg}")
254 except Exception as e:
255 print(f"Error in color selection: {e}")
257 statstring = f"{self.hudprefix}{str(self.number[1])}{self.hudsuffix}"
259 # Check if the player has a comment and adjust color or add symbol if it's playershort
260 if self.stat == "playershort" and self.has_comment(player_id):
261 # fg = "#FF0000" # Red color for players with comments
262 icon_path = os.path.join(Configuration.GRAPHICS_PATH, "pencil.png") # Chemin vers l'image de l'icône
263 icon_img = f'<img src="{icon_path}" width="24" height="24">' # Ajuster la taille de l'icône
264 statstring = f"{icon_img} {self.hudprefix}{str(self.number[1])}{self.hudsuffix} " # Add star symbol
266 self.set_color(fg=fg, bg=None)
267 self.lab.setText(statstring)
269 tip = f"{stat_dict[player_id]['screen_name']}\n{self.number[5]}\n{self.number[3]}, {self.number[4]}"
270 Stats.do_tip(self.lab, tip)
273class Classic_label(Aux_Hud.Simple_label):
274 pass
277class Classic_table_mw(Aux_Hud.Simple_table_mw):
278 """
279 A class normally controlling the table menu for that table
280 Normally a 1:1 relationship with the Classic_HUD class
282 This is invoked by the create_common method of the Classic/Simple_HUD class
283 (although note that the positions of this block are controlled by shiftx/y
284 and NOT by the common position in the layout)
285 Movements of the menu block are handled through Classic_HUD/common methods
286 """
288 pass