Coverage for GuiTourneyPlayerStats.py: 0%

169 statements  

« 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 

4from time import time 

5from PyQt5.QtCore import Qt 

6from PyQt5.QtGui import QStandardItem, QStandardItemModel 

7from PyQt5.QtWidgets import ( 

8 QApplication, 

9 QFrame, 

10 QMainWindow, 

11 QScrollArea, 

12 QSplitter, 

13 QTableView, 

14 QVBoxLayout, 

15) 

16 

17# import Charset 

18import Filters 

19 

20colalias, colshow, colheading, colxalign, colformat, coltype = 0, 1, 2, 3, 4, 5 

21 

22 

23class GuiTourneyPlayerStats(QSplitter): 

24 def __init__(self, config, db, sql, mainwin, debug=True): 

25 super().__init__(mainwin) 

26 self.conf = config 

27 self.db = db 

28 self.cursor = self.db.cursor 

29 self.sql = sql 

30 self.main_window = mainwin 

31 self.debug = debug 

32 

33 self.liststore = [] 

34 self.listcols = [] 

35 

36 filters_display = { 

37 "Heroes": True, 

38 "Sites": True, 

39 "Seats": True, 

40 "Dates": True, 

41 "Button2": True, 

42 } 

43 

44 self.stats_frame = None 

45 self.stats_vbox = None 

46 self.detailFilters = [] 

47 

48 self.filters = Filters.Filters(self.db, display=filters_display) 

49 self.filters.registerButton2Name("_Refresh Stats") 

50 self.filters.registerButton2Callback(self.refreshStats) 

51 

52 scroll = QScrollArea() 

53 scroll.setWidget(self.filters) 

54 scroll.setWidgetResizable(True) 

55 

56 self.columns = [ 

57 ["siteName", True, "Site", 0.0, "%s", "str"], 

58 ["category", True, "Cat.", 0.0, "%s", "str"], 

59 ["limitType", True, "Limit", 0.0, "%s", "str"], 

60 ["currency", True, "Curr.", 0.0, "%s", "str"], 

61 ["buyIn", True, "BuyIn", 1.0, "%3.2f", "str"], 

62 ["fee", True, "Fee", 1.0, "%3.2f", "str"], 

63 ["maxSeats", True, "Seats", 0.0, "%s", "str"], 

64 ["knockout", True, "KO", 0.0, "%s", "str"], 

65 ["reEntry", True, "ReEntry", 0.0, "%s", "str"], 

66 ["playerName", False, "Name", 0.0, "%s", "str"], 

67 ["tourneyCount", True, "#", 1.0, "%1.0f", "str"], 

68 ["itm", True, "ITM%", 1.0, "%3.2f", "str"], 

69 ["_1st", False, "1st", 1.0, "%1.0f", "str"], 

70 ["_2nd", True, "2nd", 1.0, "%1.0f", "str"], 

71 ["_3rd", True, "3rd", 1.0, "%1.0f", "str"], 

72 ["unknownRank", True, "Rank?", 1.0, "%1.0f", "str"], 

73 ["spent", True, "Spent", 1.0, "%3.2f", "str"], 

74 ["won", True, "Won", 1.0, "%3.2f", "str"], 

75 ["net", True, "Net", 1.0, "%3.2f", "str"], 

76 ["roi", True, "ROI%", 1.0, "%3.2f", "str"], 

77 ["profitPerTourney", True, "$/Tour", 1.0, "%3.2f", "str"], 

78 ] 

79 

80 self.stats_frame = QFrame() 

81 self.stats_frame.setLayout(QVBoxLayout()) 

82 

83 self.stats_vbox = QSplitter(Qt.Vertical) 

84 self.stats_frame.layout().addWidget(self.stats_vbox) 

85 

86 self.addWidget(scroll) 

87 self.addWidget(self.stats_frame) 

88 self.setStretchFactor(0, 0) 

89 self.setStretchFactor(1, 1) 

90 

91 def addGrid(self, vbox, query_name, numTourneys, tourneyTypes, playerids, sitenos, seats): 

92 grid = 0 

93 

94 query = self.sql.query[query_name] 

95 query = self.refineQuery(query, numTourneys, tourneyTypes, playerids, sitenos, seats) 

96 self.cursor.execute(query) 

97 result = self.cursor.fetchall() 

98 colnames = [desc[0] for desc in self.cursor.description] 

99 

100 view = QTableView() 

101 model = QStandardItemModel(0, len(self.columns)) 

102 view.setModel(model) 

103 view.verticalHeader().hide() 

104 vbox.addWidget(view) 

105 self.liststore.append(model) 

106 self.listcols.append([]) 

107 

108 # Create Header 

109 for col, column in enumerate(self.columns): 

110 s = column[colheading] 

111 self.listcols[grid].append(s) 

112 model.setHorizontalHeaderLabels(self.listcols[grid]) 

113 

114 # Fullfill 

115 for row_data in result: 

116 treerow = [] 

117 for col, column in enumerate(self.columns): 

118 if column[colalias] in colnames: 

119 value = row_data[colnames.index(column[colalias])] 

120 else: 

121 value = None 

122 if column[colalias] == "siteName": 

123 if row_data[colnames.index("speed")] != "Normal": 

124 if ( 

125 row_data[colnames.index("speed")] == "Hyper" 

126 and row_data[colnames.index("siteName")] == "Full Tilt Poker" 

127 ): 

128 value = value + " " + "Super Turbo" 

129 else: 

130 value = value + " " + row_data[colnames.index("speed")] 

131 if column[colalias] in ["knockout", "reEntry"]: 

132 value = "Yes" if row_data[colnames.index(column[colalias])] == 1 else "No" 

133 item = QStandardItem("") 

134 if value is not None and value != -999: 

135 item = QStandardItem(column[colformat] % value) 

136 item.setEditable(False) 

137 item.setTextAlignment(Qt.AlignRight) 

138 treerow.append(item) 

139 model.appendRow(treerow) 

140 

141 view.resizeColumnsToContents() 

142 view.setSortingEnabled(True) 

143 

144 def createStatsTable(self, vbox, tourneyTypes, playerids, sitenos, seats): 

145 startTime = time() 

146 

147 numTourneys = self.filters.getNumTourneys() 

148 self.addGrid( 

149 vbox, 

150 "tourneyPlayerDetailedStats", 

151 numTourneys, 

152 tourneyTypes, 

153 playerids, 

154 sitenos, 

155 seats, 

156 ) 

157 

158 print(("Stats page displayed in %4.2f seconds") % (time() - startTime)) 

159 

160 def fillStatsFrame(self, vbox): 

161 tourneyTypes = self.filters.getTourneyTypes() 

162 sites = self.filters.getSites() 

163 heroes = self.filters.getHeroes() 

164 siteids = self.filters.getSiteIds() 

165 seats = self.filters.getSeats() 

166 # dates = self.filters.getDates() 

167 sitenos = [] 

168 playerids = [] 

169 

170 # Selected site 

171 for site in sites: 

172 sitenos.append(siteids[site]) 

173 _hname = heroes.get(site, "") 

174 if not _hname: 

175 raise ValueError(f"Hero name not found for site {site}") 

176 result = self.db.get_player_id(self.conf, site, _hname) 

177 if result is not None: 

178 playerids.append(int(result)) 

179 

180 if not sitenos: 

181 print("No sites selected - defaulting to PokerStars") 

182 sitenos = [2] 

183 if not playerids: 

184 print("No player ids found") 

185 return 

186 

187 self.createStatsTable(vbox, tourneyTypes, playerids, sitenos, seats) 

188 

189 def refineQuery(self, query, numTourneys, tourneyTypes, playerids, sitenos, seats): 

190 having = "" 

191 

192 if playerids: 

193 nametest = str(tuple(playerids)) 

194 nametest = nametest.replace(",)", ")") 

195 else: 

196 nametest = "1 = 2" 

197 pname = "p.name" 

198 

199 query = query.replace("<nametest>", nametest) 

200 query = query.replace("<playerName>", pname) 

201 query = query.replace("<havingclause>", having) 

202 

203 if sitenos: 

204 sitetest = str(tuple(sitenos)) 

205 sitetest = sitetest.replace(",)", ")") 

206 sitetest = "and tt.siteId in %s" % sitetest 

207 else: 

208 sitetest = "and tt.siteId IS NULL" 

209 query = query.replace("<sitetest>", sitetest) 

210 

211 if seats: 

212 query = query.replace("<seats_test>", "between " + str(seats["from"]) + " and " + str(seats["to"])) 

213 query = query.replace("<groupbyseats>", ",h.seats") 

214 query = query.replace("<orderbyseats>", ",h.seats") 

215 else: 

216 query = query.replace("<seats_test>", "between 0 and 100") 

217 query = query.replace("<groupbyseats>", "") 

218 query = query.replace("<orderbyseats>", "") 

219 

220 flagtest = "" 

221 if self.detailFilters: 

222 for f in self.detailFilters: 

223 if len(f) == 3: 

224 flagtest += " and %s between %s and %s " % (f[0], str(f[1]), str(f[2])) 

225 query = query.replace("<flagtest>", flagtest) 

226 

227 if self.db.backend == self.db.MYSQL_INNODB: 

228 query = query.replace("<signed>", "signed ") 

229 else: 

230 query = query.replace("<signed>", "") 

231 

232 start_date, end_date = self.filters.getDates() 

233 query = query.replace("<startdate_test>", start_date) 

234 query = query.replace("<enddate_test>", end_date) 

235 

236 return query 

237 

238 def refreshStats(self): 

239 for i in reversed(range(self.stats_frame.layout().count())): 

240 widgetToRemove = self.stats_frame.layout().itemAt(i).widget() 

241 self.stats_frame.layout().removeWidget(widgetToRemove) 

242 widgetToRemove.setParent(None) 

243 

244 self.liststore = [] 

245 self.listcols = [] 

246 self.stats_vbox = QSplitter(Qt.Vertical) 

247 self.stats_frame.layout().addWidget(self.stats_vbox) 

248 self.fillStatsFrame(self.stats_vbox) 

249 

250 

251if __name__ == "__main__": 

252 import Configuration 

253 

254 config = Configuration.Config() 

255 

256 settings = {} 

257 settings.update(config.get_db_parameters()) 

258 settings.update(config.get_import_parameters()) 

259 settings.update(config.get_default_paths()) 

260 

261 from PyQt5.QtWidgets import QApplication, QMainWindow 

262 import SQL 

263 import Database 

264 

265 app = QApplication([]) 

266 sql = SQL.Sql(db_server=settings["db-server"]) 

267 db = Database.Database(config, sql) 

268 main_window = QMainWindow() 

269 i = GuiTourneyPlayerStats(config, db, sql, main_window) 

270 main_window.setCentralWidget(i) 

271 main_window.show() 

272 main_window.resize(1400, 800) 

273 app.exec_()