-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGuiTourneyGraphViewer.py
326 lines (270 loc) · 13.1 KB
/
GuiTourneyGraphViewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Copyright 2008-2011 Carl Gherardi
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as published by
#the Free Software Foundation, version 3 of the License.
#
#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 Affero General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
#In the "official" distribution you can find the license in agpl-3.0.txt.
import L10n
_ = L10n.get_translation()
import pygtk
pygtk.require('2.0')
import gtk
import os
import sys
import traceback
from time import *
from datetime import datetime
#import pokereval
import Database
import Filters
import Charset
try:
calluse = not 'matplotlib' in sys.modules
import matplotlib
if calluse:
matplotlib.use('GTKAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
from matplotlib.backends.backend_gtkagg import NavigationToolbar2GTKAgg as NavigationToolbar
from matplotlib.font_manager import FontProperties
from numpy import arange, cumsum
from pylab import *
except ImportError, inst:
print _("""Failed to load libs for graphing, graphing will not function. Please install numpy and matplotlib if you want to use graphs.""")
print _("""This is of no consequence for other parts of the program, e.g. import and HUD are NOT affected by this problem.""")
print "ImportError: %s" % inst.args
class GuiTourneyGraphViewer:
def __init__(self, querylist, config, parent, debug=True):
"""Constructor for GraphViewer"""
self.sql = querylist
self.conf = config
self.debug = debug
self.parent = parent
#print "start of GraphViewer constructor"
self.db = Database.Database(self.conf, sql=self.sql)
filters_display = { "Heroes" : True,
"Sites" : True,
"Games" : False,
"Limits" : False,
"LimitSep" : False,
"LimitType" : False,
"Type" : False,
"UseType" : 'tour',
"Seats" : False,
"SeatSep" : False,
"Dates" : True,
"Groups" : False,
"Button1" : True,
"Button2" : True
}
self.filters = Filters.Filters(self.db, self.conf, self.sql, display = filters_display)
self.filters.registerButton1Name(_("Refresh _Graph"))
self.filters.registerButton1Callback(self.generateGraph)
self.filters.registerButton2Name(_("_Export to File"))
self.filters.registerButton2Callback(self.exportGraph)
self.mainHBox = gtk.HBox(False, 0)
self.mainHBox.show()
self.leftPanelBox = self.filters.get_vbox()
self.hpane = gtk.HPaned()
self.hpane.pack1(self.leftPanelBox)
self.mainHBox.add(self.hpane)
# hierarchy: self.mainHBox / self.hpane / self.graphBox / self.canvas / self.fig / self.ax
self.graphBox = gtk.VBox(False, 0)
self.graphBox.show()
self.hpane.pack2(self.graphBox)
self.hpane.show()
self.fig = None
#self.exportButton.set_sensitive(False)
self.canvas = None
self.db.rollback()
def get_vbox(self):
"""returns the vbox of this thread"""
return self.mainHBox
#end def get_vbox
def clearGraphData(self):
try:
if self.canvas:
self.graphBox.remove(self.canvas)
except:
pass
if self.fig != None:
self.fig.clear()
self.fig = Figure(figsize=(5,4), dpi=100)
if self.canvas is not None:
self.canvas.destroy()
self.canvas = FigureCanvas(self.fig) # a gtk.DrawingArea
def generateGraph(self, widget, data):
self.clearGraphData()
sitenos = []
playerids = []
sites = self.filters.getSites()
heroes = self.filters.getHeroes()
siteids = self.filters.getSiteIds()
# Which sites are selected?
for site in sites:
if sites[site] == True:
sitenos.append(siteids[site])
_hname = Charset.to_utf8(heroes[site])
result = self.db.get_player_id(self.conf, site, _hname)
if result is not None:
playerids.append(int(result))
if not sitenos:
#Should probably pop up here.
print _("No sites selected - defaulting to PokerStars")
self.db.rollback()
return
if not playerids:
print _("No player ids found")
self.db.rollback()
return
#Set graph properties
self.ax = self.fig.add_subplot(111)
#Get graph data from DB
green = self.getData(playerids, sitenos)
starttime = time()
print _("Graph generated in: %s") %(time() - starttime)
#Set axis labels and grid overlay properites
currencies = [c for c in self.filters.cbCurrencies if self.filters.cbCurrencies[c].get_active()]
str_currencies = currencies.__str__().replace('play', 'free').replace('USD', u'$').replace('EUR', u'€').replace('u', '').replace("'", '').replace('[', '').replace(']', '')
self.ax.set_xlabel(_("Tournaments"), fontsize = 12)
self.ax.set_ylabel(str_currencies, fontsize = 12)
self.ax.grid(color='g', linestyle=':', linewidth=0.2)
if green.all() == None or green.all() == []:
self.ax.set_title(_("No Data for Player(s) Found"))
green = ([ 0., 0., 0., 0., 500., 1000., 900., 800.,
700., 600., 500., 400., 300., 200., 100., 0.,
500., 1000., 1000., 1000., 1000., 1000., 1000., 1000.,
1000., 1000., 1000., 1000., 1000., 1000., 875., 750.,
625., 500., 375., 250., 125., 0., 0., 0.,
0., 500., 1000., 900., 800., 700., 600., 500.,
400., 300., 200., 100., 0., 500., 1000., 1000.])
red = ([ 0., 0., 0., 0., 500., 1000., 900., 800.,
700., 600., 500., 400., 300., 200., 100., 0.,
0., 0., 0., 0., 0., 0., 125., 250.,
375., 500., 500., 500., 500., 500., 500., 500.,
500., 500., 375., 250., 125., 0., 0., 0.,
0., 500., 1000., 900., 800., 700., 600., 500.,
400., 300., 200., 100., 0., 500., 1000., 1000.])
blue = ([ 0., 0., 0., 0., 500., 1000., 900., 800.,
700., 600., 500., 400., 300., 200., 100., 0.,
0., 0., 0., 0., 0., 0., 125., 250.,
375., 500., 625., 750., 875., 1000., 875., 750.,
625., 500., 375., 250., 125., 0., 0., 0.,
0., 500., 1000., 900., 800., 700., 600., 500.,
400., 300., 200., 100., 0., 500., 1000., 1000.])
self.ax.plot(green, color='green', label=_('Tournaments') + ': %d\n' % len(green) + _('Profit') + ': $%.2f' % green[-1])
self.graphBox.add(self.canvas)
self.canvas.show()
self.canvas.draw()
#TODO: Do something useful like alert user
else:
self.ax.set_title(_("Tournament Results ("+str_currencies+")"))
#Draw plot
self.ax.plot(green, color='green', label=_('Tournaments') + ': %d\n' % len(green) + _('Profit') + ': (' + str_currencies + ')%.2f' % green[-1])
legend = self.ax.legend(loc='upper left', fancybox=True, shadow=True, prop=FontProperties(size='smaller'))
legend.draggable(True)
self.graphBox.add(self.canvas)
self.canvas.show()
self.canvas.draw()
#self.exportButton.set_sensitive(True)
#end of def showClicked
def getData(self, names, sites):
tmp = self.sql.query['tourneyGraph']
#print "DEBUG: getData"
start_date, end_date = self.filters.getDates()
#Buggered if I can find a way to do this 'nicely' take a list of integers and longs
# and turn it into a tuple readale by sql.
# [5L] into (5) not (5,) and [5L, 2829L] into (5, 2829)
nametest = str(tuple(names))
sitetest = str(tuple(sites))
filters_display = { "Heroes" : True,
"Sites" : True,
"Games" : True,
"Currencies": True,
"Limits" : True,
"LimitSep" : True,
"LimitType" : True,
"Type" : False,
"UseType" : 'ring',
"Seats" : False,
"SeatSep" : False,
"Dates" : True,
"GraphOps" : True,
"Groups" : False,
"Button1" : True,
"Button2" : True
}
self.filters = Filters.Filters(self.db, self.conf, self.sql, display = filters_display)
currencies = self.filters.getCurrencies()
q = []
for n in currencies:
if currencies[n]:
q.append(n)
currencytest = str(tuple(q))
currencytest = currencytest.replace(",)",")")
currencytest = currencytest.replace("u'","'")
currencytest = "AND tp.winningsCurrency in %s" % currencytest
#Must be a nicer way to deal with tuples of size 1 ie. (2,) - which makes sql barf
tmp = tmp.replace("<player_test>", nametest)
tmp = tmp.replace("<site_test>", sitetest)
tmp = tmp.replace("<startdate_test>", start_date)
tmp = tmp.replace("<enddate_test>", end_date)
tmp = tmp.replace("<currency_test>", currencytest)
tmp = tmp.replace(",)", ")")
#print "DEBUG: sql query:", tmp
self.db.cursor.execute(tmp)
#returns (HandId,Winnings,Costs,Profit)
winnings = self.db.cursor.fetchall()
self.db.rollback()
if len(winnings) == 0:
return None
green = map(lambda x:float(x[1]), winnings)
#blue = map(lambda x: float(x[1]) if x[2] == True else 0.0, winnings)
#red = map(lambda x: float(x[1]) if x[2] == False else 0.0, winnings)
greenline = cumsum(green)
#blueline = cumsum(blue)
#redline = cumsum(red)
return (greenline/100)
def exportGraph (self, widget, data):
if self.fig is None:
return # Might want to disable export button until something has been generated.
dia_chooser = gtk.FileChooserDialog(title=_("Please choose the directory you wish to export to:"),
action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OK,gtk.RESPONSE_OK))
dia_chooser.set_destroy_with_parent(True)
dia_chooser.set_transient_for(self.parent)
try:
dia_chooser.set_filename(self.exportFile) # use previously chosen export path as default
except:
pass
response = dia_chooser.run()
if response <> gtk.RESPONSE_OK:
print _('Closed, no graph exported')
dia_chooser.destroy()
return
# generate a unique filename for export
now = datetime.now()
now_formatted = now.strftime("%Y%m%d%H%M%S")
self.exportFile = dia_chooser.get_filename() + "/fpdb" + now_formatted + ".png"
dia_chooser.destroy()
#print "DEBUG: self.exportFile = %s" %(self.exportFile)
self.fig.savefig(self.exportFile, format="png")
#display info box to confirm graph created
diainfo = gtk.MessageDialog(parent=self.parent,
flags=gtk.DIALOG_DESTROY_WITH_PARENT,
type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_OK,
message_format=_("Graph created"))
diainfo.format_secondary_text(self.exportFile)
diainfo.run()
diainfo.destroy()
#end of def exportGraph