Coverage for Aux_Classic_Hud.py: 0%

150 statements  

« 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# 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. 

18 

19######################################################################## 

20 

21""" 

22Aux_Classic_Hud.py 

23 

24FPDB classic hud, implemented using new-hud framework. 

25 

26This module structure must be based upon simple HUD in the module Aux_Hud. 

27 

28Aux_Hud is minimal frozen functionality and is not changed,so 

29HUD customisation is done in modules which extend/subclass/override aux_hud. 

30 

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""" 

37 

38 

39import contextlib 

40# import L10n 

41# _ = L10n.get_translation() 

42 

43# to do 

44# ======= 

45# check that the parameters stored at AW level make sense for players 

46# - when playing more than one site 

47# sort out the wierd focus issues in flop-mucked. 

48 

49# Standard Library modules 

50import logging 

51# logging has been set up in fpdb.py or HUD_main.py, use their settings: 

52log = logging.getLogger("hud") 

53 

54# FreePokerTools modules 

55import Aux_Hud 

56import Stats 

57from PyQt5.QtWidgets import QInputDialog, QMessageBox 

58import SQL 

59import Database 

60import Configuration 

61import os 

62 

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 """ 

69 

70 def __init__(self, hud, config, params): 

71 super(Classic_HUD, self).__init__(hud, config, params) 

72 

73 # the following attributes ensure that the correct 

74 # classes are invoked by the calling modules (aux_hud+mucked) 

75 

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 

80 

81 

82class Classic_Stat_Window(Aux_Hud.Simple_Stat_Window): 

83 """Stat windows are the blocks shown continually, 1 for each player.""" 

84 

85 def update_contents(self, i): 

86 super(Classic_Stat_Window, self).update_contents(i) 

87 if i == "common": 

88 return 

89 

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, 

99 self.aw.positions[i][1]+self.aw.hud.table.y) 

100 self.setWindowOpacity(float(self.aw.params['opacity'])) 

101 # show item, just in case it was hidden by the user 

102 self.show() 

103 

104 def button_press_middle(self, event): 

105 self.hide() 

106 

107 

108class Classic_stat(Aux_Hud.Simple_stat): 

109 """A class to display each individual statistic on the Stat_Window""" 

110 

111 def __init__(self, stat, seat, popup, game_stat_config, aw): 

112 

113 super(Classic_stat, self).__init__(stat, seat, popup, game_stat_config, aw) 

114 # game_stat_config is the instance of this stat in the supported games stat configuration 

115 # use this prefix to directly extract the attributes 

116 

117 # debug 

118 # print(f"Initializing Classic_stat for {stat}") 

119 # print(f"stat_locolor: {game_stat_config.stat_locolor}") 

120 # print(f"stat_loth: {game_stat_config.stat_loth}") 

121 # print(f"stat_midcolor: {game_stat_config.stat_midcolor}") 

122 # print(f"stat_hicolor: {game_stat_config.stat_hicolor}") 

123 # print(f"stat_hith: {game_stat_config.stat_hith}") 

124 

125 self.aw = aw 

126 self.popup = game_stat_config.popup 

127 self.click = game_stat_config.click 

128 self.incolor = "rgba(0, 0, 0, 0)" 

129 if self.click == "open_comment_dialog": 

130 self.lab.mouseDoubleClickEvent = self.open_comment_dialog 

131 #print(f"value of self.click in the constructor : {self.click}") # debug 

132 self.tip = game_stat_config.tip # not implemented yet 

133 self.hudprefix = game_stat_config.hudprefix 

134 self.hudsuffix = game_stat_config.hudsuffix 

135 

136 try: 

137 self.stat_locolor = game_stat_config.stat_locolor 

138 self.stat_loth = game_stat_config.stat_loth 

139 except Exception: 

140 self.stat_locolor = self.stat_loth = "" 

141 try: 

142 self.stat_hicolor = game_stat_config.stat_hicolor 

143 self.stat_hith = game_stat_config.stat_hith 

144 except Exception: 

145 self.stat_hicolor = self.stat_hith = "" 

146 

147 try: 

148 self.stat_midcolor = game_stat_config.stat_midcolor 

149 except Exception: 

150 self.stat_midcolor = "" 

151 try: 

152 self.hudcolor = game_stat_config.hudcolor 

153 except Exception: 

154 self.hudcolor = aw.params['fgcolor'] 

155 

156 

157 def open_comment_dialog(self, event): 

158 if self.stat != "playershort": 

159 return 

160 

161 player_id = self.get_player_id() 

162 if player_id is None: 

163 return 

164 

165 player_name = self.get_player_name(player_id) 

166 current_comment = self.get_current_comment(player_id) 

167 

168 new_comment, ok = QInputDialog.getMultiLineText( 

169 None, 

170 f"Add comment to player: {player_name}", 

171 f"Add your comment for {player_name}:", 

172 current_comment 

173 ) 

174 if ok: 

175 self.save_comment(player_id, new_comment) 

176 

177 def get_player_id(self): 

178 for id, data in self.stat_dict.items(): 

179 if data['seat'] == self.lab.aw_seat: 

180 return id 

181 return None 

182 

183 def get_player_name(self, player_id): 

184 db = Database.Database(self.aw.hud.config) 

185 try: 

186 q = db.sql.query['get_player_name'] 

187 db.cursor.execute(q, (player_id,)) 

188 result = db.cursor.fetchone() 

189 return result[0] if result else "Unknown Player" 

190 except Exception as e: 

191 print(f"Error fetching player name: {e}") 

192 return "Unknown Player" 

193 finally: 

194 db.close_connection() 

195 

196 def get_current_comment(self, player_id): 

197 db = Database.Database(self.aw.hud.config) 

198 try: 

199 q = db.sql.query['get_player_comment'] 

200 db.cursor.execute(q, (player_id,)) 

201 result = db.cursor.fetchone() 

202 return result[0] if result else "" 

203 except Exception as e: 

204 print(f"Error fetching comment: {e}") 

205 return "" 

206 finally: 

207 db.close_connection() 

208 

209 def save_comment(self, player_id, comment): 

210 db = Database.Database(self.aw.hud.config) 

211 try: 

212 q = db.sql.query['update_player_comment'] 

213 db.cursor.execute(q, (comment, player_id)) 

214 db.commit() 

215 QMessageBox.information(None, "Comment saved", "The comment has been successfully saved.") 

216 except Exception as e: 

217 print(f"Error saving comment: {e}") 

218 QMessageBox.warning(None, "Error", f"An error occurred while saving the comment: {e}") 

219 finally: 

220 db.close_connection() 

221 

222 def has_comment(self, player_id): 

223 db = Database.Database(self.aw.hud.config) 

224 try: 

225 q = db.sql.query['get_player_comment'] 

226 db.cursor.execute(q, (player_id,)) 

227 result = db.cursor.fetchone() 

228 return bool(result and result[0]) 

229 except Exception as e: 

230 print(f"Error checking comment: {e}") 

231 return False 

232 finally: 

233 db.close_connection() 

234 

235 

236 def update(self, player_id, stat_dict): 

237 super(Classic_stat, self).update(player_id, stat_dict) 

238 

239 if not self.number: # stat did not create, so exit now 

240 return False 

241 

242 fg = self.hudcolor 

243 #print(f"Updating stat for {self.stat}: value={self.number[1]}, loth={self.stat_loth}, hith={self.stat_hith}") 

244 

245 if self.stat_loth != "" and self.stat_hith != "": 

246 try: 

247 value_str = self.number[1] 

248 if value_str == "NA": 

249 fg = self.incolor # default color for NA 

250 else: 

251 value = float(value_str) 

252 if value < float(self.stat_loth): 

253 fg = self.stat_locolor 

254 #print(f"Using locolor: {fg}") 

255 elif value < float(self.stat_hith): 

256 fg = self.stat_midcolor 

257 #print(f"Using midcolor: {fg}") 

258 else: 

259 fg = self.stat_hicolor 

260 #print(f"Using hicolor: {fg}") 

261 except Exception as e: 

262 print(f"Error in color selection: {e}") 

263 

264 

265 statstring = f"{self.hudprefix}{str(self.number[1])}{self.hudsuffix}" 

266 

267 # Check if the player has a comment and adjust color or add symbol if it's playershort 

268 if self.stat == "playershort" and self.has_comment(player_id): 

269 #fg = "#FF0000" # Red color for players with comments 

270 icon_path = os.path.join(Configuration.GRAPHICS_PATH, 'pencil.png') # Chemin vers l'image de l'icône 

271 icon_img = f'<img src="{icon_path}" width="24" height="24">' # Ajuster la taille de l'icône 

272 statstring = f"{icon_img} {self.hudprefix}{str(self.number[1])}{self.hudsuffix} " # Add star symbol 

273 

274 self.set_color(fg=fg, bg=None) 

275 self.lab.setText(statstring) 

276 

277 tip = f"{stat_dict[player_id]['screen_name']}\n{self.number[5]}\n{self.number[3]}, {self.number[4]}" 

278 Stats.do_tip(self.lab, tip) 

279 

280 

281class Classic_label(Aux_Hud.Simple_label): 

282 pass 

283 

284 

285class Classic_table_mw(Aux_Hud.Simple_table_mw): 

286 """ 

287 A class normally controlling the table menu for that table 

288 Normally a 1:1 relationship with the Classic_HUD class 

289  

290 This is invoked by the create_common method of the Classic/Simple_HUD class 

291 (although note that the positions of this block are controlled by shiftx/y 

292 and NOT by the common position in the layout) 

293 Movements of the menu block are handled through Classic_HUD/common methods 

294 """ 

295 pass