Add 'Currencies' filter to the ring player stats viewer.
[fpdb-dooglus.git] / pyfpdb / Stats.py
blob614b3b0a90569c170ae8d84fda51b805b2d41b01
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """Manage collecting and formatting of stats and tooltips."""
5 # Copyright 2008-2011, Ray E. Barker
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 ########################################################################
24 # How to write a new stat:
25 # 0 Do not use a name like "xyz_2". Names ending in _ and a single digit are
26 # used to indicate the number of decimal places the user wants to see in the Hud.
27 # 1 You can see a listing of all the raw stats (e.g., from the HudCache table)
28 # by running Database.py as a stand along program. You need to combine
29 # those raw stats to get stats to present to the HUD. If you need more
30 # information than is in the HudCache table, then you have to write SQL.
31 # 2 The raw stats seen when you run Database.py are available in the Stats.py
32 # in the stat_dict dict. For example the number of vpips would be
33 # stat_dict[player]['vpip']. So the % vpip is
34 # float(stat_dict[player]['vpip'])/float(stat_dict[player]['n']). You can see how the
35 # keys of stat_dict relate to the column names in HudCache by inspecting
36 # the proper section of the SQL.py module.
37 # The stat_dict keys should be in lower case, i.e. vpip not VPIP, since
38 # postgres returns the column names in lower case.
39 # 3 You have to write a small function for each stat you want to add. See
40 # the vpip() function for example. This function has to be protected from
41 # exceptions, using something like the try:/except: paragraphs in vpip.
42 # 4 The name of the function has to be the same as the of the stat used
43 # in the config file.
44 # 5 The stat functions have a peculiar return value, which is outlined in
45 # the do_stat function. This format is useful for tool tips and maybe
46 # other stuff.
47 # 6 For each stat you make add a line to the __main__ function to test it.
49 import L10n
50 _ = L10n.get_translation()
52 # Standard Library modules
53 import sys
55 # pyGTK modules
56 import pygtk
57 import gtk
58 import re
60 # FreePokerTools modules
61 import Configuration
62 import Database
63 import Charset
65 import logging
66 # logging has been set up in fpdb.py or HUD_main.py, use their settings:
67 log = logging.getLogger("db")
70 re_Places = re.compile("_[0-9]$")
72 # String manipulation
73 import codecs
74 encoder = codecs.lookup(Configuration.LOCALE_ENCODING)
76 stat_descriptions = {}
78 # Since tuples are immutable, we have to create a new one when
79 # overriding any decimal placements. Copy old ones and recreate the
80 # second value in tuple to specified format-
81 def __stat_override(decimals, stat_vals):
82 s = '%.*f' % (decimals, 100.0*stat_vals[0])
83 res = (stat_vals[0], s, stat_vals[2],
84 stat_vals[3], stat_vals[4], stat_vals[5])
85 return res
88 def do_tip(widget, tip):
89 _tip = Charset.to_utf8(tip)
90 widget.set_tooltip_text(_tip)
93 def do_stat(stat_dict, player = 24, stat = 'vpip'):
94 statname = stat
95 match = re_Places.search(stat)
96 if match: # override if necessary
97 statname = stat[0:-2]
99 result = eval("%(stat)s(stat_dict, %(player)d)" % {'stat': statname, 'player': player})
101 # If decimal places have been defined, override result[1]
102 # NOTE: decimal place override ALWAYS assumes the raw result is a
103 # fraction (x/100); manual decimal places really only make sense for
104 # percentage values. Also, profit/100 hands (bb/BB) already default
105 # to three decimal places anyhow, so they are unlikely override
106 # candidates.
107 if match:
108 places = int(stat[-1:])
109 result = __stat_override(places, result)
110 return result
112 # OK, for reference the tuple returned by the stat is:
113 # 0 - The stat, raw, no formating, eg 0.33333333
114 # 1 - formatted stat with appropriate precision, eg. 33; shown in HUD
115 # 2 - formatted stat with appropriate precision, punctuation and a hint, eg v=33%
116 # 3 - same as #2 except name of stat instead of hint, eg vpip=33%
117 # 4 - the calculation that got the stat, eg 9/27
118 # 5 - the name of the stat, useful for a tooltip, eg vpip
120 ###########################################
121 # functions that return individual stats
123 def totalprofit(stat_dict, player):
124 stat_descriptions["totalprofit"] = _("Total Profit") + " (totalprofit)"
125 if stat_dict[player]['net'] != 0:
126 stat = float(stat_dict[player]['net']) / 100
127 return (stat, '$%.2f' % stat, 'tp=$%.2f' % stat, 'totalprofit=$%.2f' % stat, str(stat), _('Total Profit'))
128 return ('0', '$0.00', 'tp=0', 'totalprofit=0', '0', _('Total Profit'))
130 def playername(stat_dict, player):
131 stat_descriptions["playername"] = _("Player Name") + " (playername)"
132 return (stat_dict[player]['screen_name'],
133 stat_dict[player]['screen_name'],
134 stat_dict[player]['screen_name'],
135 stat_dict[player]['screen_name'],
136 stat_dict[player]['screen_name'],
137 stat_dict[player]['screen_name'])
139 def playershort(stat_dict, player):
140 stat_descriptions["playershort"] = (_("Player Name")+" 1-6") + " (playershort)"
141 r = stat_dict[player]['screen_name']
142 if (len(r) > 7):
143 r = r[:6] + ".."
144 return (r,
148 stat_dict[player]['screen_name'],
149 (_("Player Name")+" 1-6")
152 def vpip(stat_dict, player):
153 stat_descriptions["vpip"] = _("Voluntarily put in preflop/3rd street %") + " (vpip)"
154 stat = 0.0
155 try:
156 stat = float(stat_dict[player]['vpip'])/float(stat_dict[player]['n'])
157 return (stat,
158 '%3.1f' % (100.0*stat),
159 'v=%3.1f%%' % (100.0*stat),
160 'vpip=%3.1f%%' % (100.0*stat),
161 '(%d/%d)' % (stat_dict[player]['vpip'], stat_dict[player]['n']),
162 _('Voluntarily put in preflop/3rd street %')
164 except: return (stat,
165 'NA',
166 'v=NA',
167 'vpip=NA',
168 '(0/0)',
169 _('Voluntarily put in preflop/3rd street %')
172 def pfr(stat_dict, player):
173 stat_descriptions["pfr"] = _("Preflop/3rd street raise %") + " (pfr)"
174 stat = 0.0
175 try:
176 stat = float(stat_dict[player]['pfr'])/float(stat_dict[player]['n'])
177 return (stat,
178 '%3.1f' % (100.0*stat),
179 'p=%3.1f%%' % (100.0*stat),
180 'pfr=%3.1f%%' % (100.0*stat),
181 '(%d/%d)' % (stat_dict[player]['pfr'], stat_dict[player]['n']),
182 _('Preflop/3rd street raise %')
184 except:
185 return (stat,
186 'NA',
187 'p=NA',
188 'pfr=NA',
189 '(0/0)',
190 _('Preflop/3rd street raise %')
193 def wtsd(stat_dict, player):
194 stat_descriptions["wtsd"] = _("% went to showdown when seen flop/4th street") + " (wtsd)"
195 stat = 0.0
196 try:
197 stat = float(stat_dict[player]['sd'])/float(stat_dict[player]['saw_f'])
198 return (stat,
199 '%3.1f' % (100.0*stat),
200 'w=%3.1f%%' % (100.0*stat),
201 'wtsd=%3.1f%%' % (100.0*stat),
202 '(%d/%d)' % (stat_dict[player]['sd'], stat_dict[player]['saw_f']),
203 _('% went to showdown when seen flop/4th street')
205 except:
206 return (stat,
207 'NA',
208 'w=NA',
209 'wtsd=NA',
210 '(0/0)',
211 _('% went to showdown when seen flop/4th street')
214 def wmsd(stat_dict, player):
215 stat_descriptions["wmsd"] = _("% won some money at showdown") + " (wmsd)"
216 stat = 0.0
217 try:
218 stat = float(stat_dict[player]['wmsd'])/float(stat_dict[player]['sd'])
219 return (stat,
220 '%3.1f' % (100.0*stat),
221 'w=%3.1f%%' % (100.0*stat),
222 'wmsd=%3.1f%%' % (100.0*stat),
223 '(%5.1f/%d)' % (float(stat_dict[player]['wmsd']), stat_dict[player]['sd']),
224 _('% won some money at showdown')
226 except:
227 return (stat,
228 'NA',
229 'w=NA',
230 'wmsd=NA',
231 '(0/0)',
232 _('% won some money at showdown')
235 # Money is stored as pennies, so there is an implicit 100-multiplier
236 # already in place
237 def profit100(stat_dict, player):
238 stat_descriptions["profit100"] = _("Profit per 100 hands") + " (profit100)"
239 stat = 0.0
240 try:
241 stat = float(stat_dict[player]['net'])/float(stat_dict[player]['n'])
242 return (stat,
243 '%.2f' % (stat),
244 'p=%.2f' % (stat),
245 'p/100=%.2f' % (stat),
246 '%d/%d' % (stat_dict[player]['net'], stat_dict[player]['n']),
247 _('Profit per 100 hands')
249 except:
250 print _("exception calculating p/100: 100 * %d / %d") % (stat_dict[player]['net'], stat_dict[player]['n'])
251 return (stat,
252 'NA',
253 'p=NA',
254 'p/100=NA',
255 '(0/0)',
256 _('Profit per 100 hands')
259 def bbper100(stat_dict, player):
260 stat_descriptions["bbper100"] = _("Big blinds won per 100 hands") + " (bbper100)"
261 stat = 0.0
262 try:
263 stat = 100.0 * float(stat_dict[player]['net']) / float(stat_dict[player]['bigblind'])
264 return (stat,
265 '%5.3f' % (stat),
266 'bb100=%5.3f' % (stat),
267 'bb100=%5.3f' % (stat),
268 '(%d,%d)' % (100*stat_dict[player]['net'],stat_dict[player]['bigblind']),
269 _('Big blinds won per 100 hands')
271 except:
272 log.info(_("exception calculating bb/100: ")+str(stat_dict[player]))
273 return (stat,
274 'NA',
275 'bb100=NA',
276 'bb100=NA',
277 '(--)',
278 _('Big blinds won per 100 hands')
281 def BBper100(stat_dict, player):
282 stat_descriptions["BBper100"] = _("Big bets won per 100 hands") + " (BBper100)"
283 stat = 0.0
284 try:
285 stat = 50 * float(stat_dict[player]['net']) / float(stat_dict[player]['bigblind'])
286 return (stat,
287 '%5.3f' % (stat),
288 'BB100=%5.3f' % (stat),
289 'BB100=%5.3f' % (stat),
290 '(%d,%d)' % (100*stat_dict[player]['net'],2*stat_dict[player]['bigblind']),
291 _('Big bets won per 100 hands')
293 except:
294 log.info(_("exception calculating BB/100: ")+str(stat_dict[player]))
295 return (stat,
296 'NA',
297 'BB100=NA',
298 'BB100=NA',
299 '(--)',
300 _('Big bets won per 100 hands')
303 def saw_f(stat_dict, player):
304 stat_descriptions["saw_f"] = _("Flop/4th street seen %") + " (saw_f)"
305 try:
306 num = float(stat_dict[player]['saw_f'])
307 den = float(stat_dict[player]['n'])
308 stat = num/den
309 return (stat,
310 '%3.1f' % (100.0*stat),
311 'sf=%3.1f%%' % (100.0*stat),
312 'saw_f=%3.1f%%' % (100.0*stat),
313 '(%d/%d)' % (stat_dict[player]['saw_f'], stat_dict[player]['n']),
314 _('Flop/4th street seen %')
316 except:
317 stat = 0.0
318 return (stat,
319 'NA',
320 'sf=NA',
321 'saw_f=NA',
322 '(0/0)',
323 _('Flop/4th street seen %')
326 def n(stat_dict, player):
327 stat_descriptions["n"] = _("Number of hands seen") + " (n)"
328 try:
329 # If sample is large enough, use X.Yk notation instead
330 _n = stat_dict[player]['n']
331 fmt = '%d' % _n
332 if _n >= 10000:
333 k = _n / 1000
334 c = _n % 1000
335 _c = float(c) / 100.0
336 d = int(round(_c))
337 if d == 10:
338 k += 1
339 d = 0
340 fmt = '%d.%dk' % (k, d)
341 return (stat_dict[player]['n'],
342 '%s' % fmt,
343 'n=%d' % (stat_dict[player]['n']),
344 'n=%d' % (stat_dict[player]['n']),
345 '(%d)' % (stat_dict[player]['n']),
346 _('Number of hands seen')
348 except:
349 # Number of hands shouldn't ever be "NA"; zeroes are better here
350 return (0,
351 '%d' % (0),
352 'n=%d' % (0),
353 'n=%d' % (0),
354 '(%d)' % (0),
355 _('Number of hands seen')
358 def fold_f(stat_dict, player):
359 #TODO: remove
360 stat = 0.0
361 try:
362 stat = float(stat_dict[player]['fold_2'])/float(stat_dict[player]['saw_f'])
363 return (stat,
364 '%3.1f' % (100.0*stat),
365 'ff=%3.1f%%' % (100.0*stat),
366 'fold_f=%3.1f%%' % (100.0*stat),
367 '(%d/%d)' % (stat_dict[player]['fold_2'], stat_dict[player]['saw_f']),
368 ('folded flop/4th')
370 except:
371 return (stat,
372 'NA',
373 'ff=NA',
374 'fold_f=NA',
375 '(0/0)',
376 ('folded flop/4th')
379 def steal(stat_dict, player):
380 stat_descriptions["steal"] = _("% steal attempted") + " (steal)"
381 stat = 0.0
382 try:
383 stat = float(stat_dict[player]['steal'])/float(stat_dict[player]['steal_opp'])
384 return (stat,
385 '%3.1f' % (100.0*stat),
386 'st=%3.1f%%' % (100.0*stat),
387 'steal=%3.1f%%' % (100.0*stat),
388 '(%d/%d)' % (stat_dict[player]['steal'], stat_dict[player]['steal_opp']),
389 _('% steal attempted')
391 except:
392 return (stat, 'NA', 'st=NA', 'steal=NA', '(0/0)', '% steal attempted')
394 def s_steal(stat_dict, player):
395 stat_descriptions["s_steal"] = _("% steal success") + " (s_steal)"
396 stat = 0.0
397 try:
398 stat = float(stat_dict[player]['suc_st'])/float(stat_dict[player]['steal'])
399 return (stat,
400 '%3.1f' % (100.0*stat),
401 's_st=%3.1f%%' % (100.0*stat),
402 's_steal=%3.1f%%' % (100.0*stat),
403 '(%d/%d)' % (stat_dict[player]['suc_st'], stat_dict[player]['steal']),
404 _('% steal success')
406 except:
407 return (stat, 'NA', 'st=NA', 's_steal=NA', '(0/0)', '% steal success')
409 def f_SB_steal(stat_dict, player):
410 stat_descriptions["f_SB_steal"] = _("% folded SB to steal") + " (f_SB_steal)"
411 stat = 0.0
412 try:
413 stat = float(stat_dict[player]['sbnotdef'])/float(stat_dict[player]['sbstolen'])
414 return (stat,
415 '%3.1f' % (100.0*stat),
416 'fSB=%3.1f%%' % (100.0*stat),
417 'fSB_s=%3.1f%%' % (100.0*stat),
418 '(%d/%d)' % (stat_dict[player]['sbnotdef'], stat_dict[player]['sbstolen']),
419 _('% folded SB to steal'))
420 except:
421 return (stat,
422 'NA',
423 'fSB=NA',
424 'fSB_s=NA',
425 '(0/0)',
426 _('% folded SB to steal'))
428 def f_BB_steal(stat_dict, player):
429 stat_descriptions["f_BB_steal"] = _("% folded BB to steal") + " (f_BB_steal)"
430 stat = 0.0
431 try:
432 stat = float(stat_dict[player]['bbnotdef'])/float(stat_dict[player]['bbstolen'])
433 return (stat,
434 '%3.1f' % (100.0*stat),
435 'fBB=%3.1f%%' % (100.0*stat),
436 'fBB_s=%3.1f%%' % (100.0*stat),
437 '(%d/%d)' % (stat_dict[player]['bbnotdef'], stat_dict[player]['bbstolen']),
438 _('% folded BB to steal'))
439 except:
440 return (stat,
441 'NA',
442 'fBB=NA',
443 'fBB_s=NA',
444 '(0/0)',
445 _('% folded BB to steal'))
447 def f_steal(stat_dict, player):
448 stat_descriptions["f_steal"] = _("% folded blind to steal") + " (f_steal)"
449 stat = 0.0
450 try:
451 folded_blind = stat_dict[player]['sbnotdef'] + stat_dict[player]['bbnotdef']
452 blind_stolen = stat_dict[player]['sbstolen'] + stat_dict[player]['bbstolen']
454 stat = float(folded_blind)/float(blind_stolen)
455 return (stat,
456 '%3.1f' % (100.0*stat),
457 'fB=%3.1f%%' % (100.0*stat),
458 'fB_s=%3.1f%%' % (100.0*stat),
459 '(%d/%d)' % (folded_blind, blind_stolen),
460 _('% folded blind to steal'))
461 except:
462 return (stat,
463 'NA',
464 'fB=NA',
465 'fB_s=NA',
466 '(0/0)',
467 _('% folded blind to steal'))
469 def three_B(stat_dict, player):
470 stat_descriptions["three_B"] = _("% 3 bet preflop/3rd street") + " (three_B)"
471 stat = 0.0
472 try:
473 stat = float(stat_dict[player]['tb_0'])/float(stat_dict[player]['tb_opp_0'])
474 return (stat,
475 '%3.1f' % (100.0*stat),
476 '3B=%3.1f%%' % (100.0*stat),
477 '3B_pf=%3.1f%%' % (100.0*stat),
478 '(%d/%d)' % (stat_dict[player]['tb_0'], stat_dict[player]['tb_opp_0']),
479 _('% 3 bet preflop/3rd street'))
480 except:
481 return (stat,
482 'NA',
483 '3B=NA',
484 '3B_pf=NA',
485 '(0/0)',
486 _('% 3 bet preflop/3rd street'))
488 def four_B(stat_dict, player):
489 stat_descriptions["four_B"] = _("% 4 bet preflop/3rd street") + " (four_B)"
490 stat = 0.0
491 try:
492 stat = float(stat_dict[player]['fb_0'])/float(stat_dict[player]['fb_opp_0'])
493 return (stat,
494 '%3.1f' % (100.0*stat),
495 '4B=%3.1f%%' % (100.0*stat),
496 '4B=%3.1f%%' % (100.0*stat),
497 '(%d/%d)' % (stat_dict[player]['fb_0'], stat_dict[player]['fb_opp_0']),
498 _('% 4 bet preflop/3rd street'))
499 except:
500 return (stat,
501 'NA',
502 '4B=NA',
503 '4B=NA',
504 '(0/0)',
505 _('% 4 bet preflop/3rd street'))
507 def cfour_B(stat_dict, player):
508 stat_descriptions["cfour_B"] = _("% cold 4 bet preflop/3rd street") + " (cfour_B)"
509 stat = 0.0
510 try:
511 stat = float(stat_dict[player]['cfb_0'])/float(stat_dict[player]['cfb_opp_0'])
512 return (stat,
513 '%3.1f' % (100.0*stat),
514 'C4B=%3.1f%%' % (100.0*stat),
515 'C4B_pf=%3.1f%%' % (100.0*stat),
516 '(%d/%d)' % (stat_dict[player]['cfb_0'], stat_dict[player]['cfb_opp_0']),
517 _('% cold 4 bet preflop/3rd street'))
518 except:
519 return (stat,
520 'NA',
521 'C4B=NA',
522 'C4B_pf=NA',
523 '(0/0)',
524 _('% cold 4 bet preflop/3rd street'))
526 def squeeze(stat_dict, player):
527 stat_descriptions["squeeze"] = _("% squeeze preflop") + " (squeeze)"
528 stat = 0.0
529 try:
530 stat = float(stat_dict[player]['sqz_0'])/float(stat_dict[player]['sqz_opp_0'])
531 return (stat,
532 '%3.1f' % (100.0*stat),
533 'SQZ=%3.1f%%' % (100.0*stat),
534 'SQZ_pf=%3.1f%%' % (100.0*stat),
535 '(%d/%d)' % (stat_dict[player]['sqz_0'], stat_dict[player]['sqz_opp_0']),
536 _('% squeeze preflop'))
537 except:
538 return (stat,
539 'NA',
540 'SQZ=NA',
541 'SQZ_pf=NA',
542 '(0/0)',
543 _('% squeeze preflop'))
546 def raiseToSteal(stat_dict, player):
547 stat_descriptions["raiseToSteal"] = _("% raise to steal") + " (raiseToSteal)"
548 stat = 0.0
549 try:
550 stat = float(stat_dict[player]['rts'])/float(stat_dict[player]['rts_opp'])
551 return (stat,
552 '%3.1f' % (100.0*stat),
553 'RST=%3.1f%%' % (100.0*stat),
554 'RST_pf=%3.1f%%' % (100.0*stat),
555 '(%d/%d)' % (stat_dict[player]['rts'], stat_dict[player]['rts_opp']),
556 _('% raise to steal'))
557 except:
558 return (stat,
559 'NA',
560 'RST=NA',
561 'RST_pf=NA',
562 '(0/0)',
563 _('% raise to steal'))
566 def f_3bet(stat_dict, player):
567 stat_descriptions["f_3bet"] = _("% fold to 3 bet preflop/3rd street") + " (f_3bet)"
568 stat = 0.0
569 try:
570 stat = float(stat_dict[player]['f3b_0'])/float(stat_dict[player]['f3b_opp_0'])
571 return (stat,
572 '%3.1f' % (100.0*stat),
573 'F3B=%3.1f%%' % (100.0*stat),
574 'F3B_pf=%3.1f%%' % (100.0*stat),
575 '(%d/%d)' % (stat_dict[player]['f3b_0'], stat_dict[player]['f3b_opp_0']),
576 _('% fold to 3 bet preflop/3rd street'))
577 except:
578 return (stat,
579 'NA',
580 'F3B=NA',
581 'F3B_pf=NA',
582 '(0/0)',
583 _('% fold to 3 bet preflop/3rd street'))
585 def f_4bet(stat_dict, player):
586 stat_descriptions["f_4bet"] = _("% fold to 4 bet preflop/3rd street") + " (f_4bet)"
587 stat = 0.0
588 try:
589 stat = float(stat_dict[player]['f4b_0'])/float(stat_dict[player]['f4b_opp_0'])
590 return (stat,
591 '%3.1f' % (100.0*stat),
592 'F4B=%3.1f%%' % (100.0*stat),
593 'F4B_pf=%3.1f%%' % (100.0*stat),
594 '(%d/%d)' % (stat_dict[player]['f4b_0'], stat_dict[player]['f4b_opp_0']),
595 _('% fold to 4 bet preflop/3rd street'))
596 except:
597 return (stat,
598 'NA',
599 'F4B=NA',
600 'F4B_pf=NA',
601 '(0/0)',
602 _('% fold to 4 bet preflop/3rd street'))
604 def WMsF(stat_dict, player):
605 stat_descriptions["WMsF"] = _("% won money when seen flop/4th street") + " (WMsF)"
606 stat = 0.0
607 try:
608 stat = float(stat_dict[player]['w_w_s_1'])/float(stat_dict[player]['saw_1'])
609 return (stat,
610 '%3.1f' % (100.0*stat),
611 'wf=%3.1f%%' % (100.0*stat),
612 'w_w_f=%3.1f%%' % (100.0*stat),
613 '(%d/%d)' % (stat_dict[player]['w_w_s_1'], stat_dict[player]['saw_f']),
614 _('% won money when seen flop/4th street'))
615 except:
616 return (stat,
617 'NA',
618 'wf=NA',
619 'w_w_f=NA',
620 '(0/0)',
621 _('% won money when seen flop/4th street'))
623 def a_freq1(stat_dict, player):
624 stat_descriptions["a_freq1"] = _("Aggression frequency flop/4th street") + " (a_freq1)"
625 stat = 0.0
626 try:
627 stat = float(stat_dict[player]['aggr_1'])/float(stat_dict[player]['saw_f'])
628 return (stat,
629 '%3.1f' % (100.0*stat),
630 'a1=%3.1f%%' % (100.0*stat),
631 'a_fq_1=%3.1f%%' % (100.0*stat),
632 '(%d/%d)' % (stat_dict[player]['aggr_1'], stat_dict[player]['saw_f']),
633 _('Aggression frequency flop/4th street'))
634 except:
635 return (stat,
636 'NA',
637 'a1=NA',
638 'a_fq_1=NA',
639 '(0/0)',
640 _('Aggression frequency flop/4th street'))
642 def a_freq2(stat_dict, player):
643 stat_descriptions["a_freq2"] = _("Aggression frequency turn/5th street") + " (a_freq2)"
644 stat = 0.0
645 try:
646 stat = float(stat_dict[player]['aggr_2'])/float(stat_dict[player]['saw_2'])
647 return (stat,
648 '%3.1f' % (100.0*stat),
649 'a2=%3.1f%%' % (100.0*stat),
650 'a_fq_2=%3.1f%%' % (100.0*stat),
651 '(%d/%d)' % (stat_dict[player]['aggr_2'], stat_dict[player]['saw_2']),
652 _('Aggression frequency turn/5th street'))
653 except:
654 return (stat,
655 'NA',
656 'a2=NA',
657 'a_fq_2=NA',
658 '(0/0)',
659 _('Aggression frequency turn/5th street'))
661 def a_freq3(stat_dict, player):
662 stat_descriptions["a_freq3"] = _("Aggression frequency river/6th street") + " (a_freq3)"
663 stat = 0.0
664 try:
665 stat = float(stat_dict[player]['aggr_3'])/float(stat_dict[player]['saw_3'])
666 return (stat,
667 '%3.1f' % (100.0*stat),
668 'a3=%3.1f%%' % (100.0*stat),
669 'a_fq_3=%3.1f%%' % (100.0*stat),
670 '(%d/%d)' % (stat_dict[player]['aggr_3'], stat_dict[player]['saw_3']),
671 _('Aggression frequency river/6th street'))
672 except:
673 return (stat,
674 'NA',
675 'a3=NA',
676 'a_fq_3=NA',
677 '(0/0)',
678 _('Aggression frequency river/6th street'))
680 def a_freq4(stat_dict, player):
681 stat_descriptions["a_freq4"] = _("Aggression frequency 7th street") + " (a_freq4)"
682 stat = 0.0
683 try:
684 stat = float(stat_dict[player]['aggr_4'])/float(stat_dict[player]['saw_4'])
685 return (stat,
686 '%3.1f' % (100.0*stat),
687 'a4=%3.1f%%' % (100.0*stat),
688 'a_fq_4=%3.1f%%' % (100.0*stat),
689 '(%d/%d)' % (stat_dict[player]['aggr_4'], stat_dict[player]['saw_4']),
690 _('Aggression frequency 7th street'))
691 except:
692 return (stat,
693 'NA',
694 'a4=NA',
695 'a_fq_4=NA',
696 '(0/0)',
697 _('Aggression frequency 7th street'))
699 def a_freq_123(stat_dict, player):
700 stat_descriptions["a_freq_123"] = _("Post-flop aggression frequency") + " (a_freq_123)"
701 stat = 0.0
702 try:
703 stat = float( stat_dict[player]['aggr_1'] + stat_dict[player]['aggr_2'] + stat_dict[player]['aggr_3']
704 ) / float( stat_dict[player]['saw_1'] + stat_dict[player]['saw_2'] + stat_dict[player]['saw_3']);
705 return (stat,
706 '%3.1f' % (100.0*stat),
707 'afq=%3.1f%%' % (100.0*stat),
708 'postf_aggfq=%3.1f%%' % (100.0*stat),
709 '(%d/%d)' % ( stat_dict[player]['aggr_1']
710 + stat_dict[player]['aggr_2']
711 + stat_dict[player]['aggr_3']
712 , stat_dict[player]['saw_1']
713 + stat_dict[player]['saw_2']
714 + stat_dict[player]['saw_3']
716 _('Post-flop aggression frequency'))
717 except:
718 return (stat,
719 'NA',
720 'a3=NA',
721 'a_fq_3=NA',
722 '(0/0)',
723 _('Post-flop aggression frequency'))
725 def agg_freq(stat_dict, player):
726 #TODO: remove, dupe of a_freq_123
727 stat = 0.0
728 try:
729 #Agression on the flop and all streets
730 bet_raise = stat_dict[player]['aggr_1'] + stat_dict[player]['aggr_2'] + stat_dict[player]['aggr_3'] + stat_dict[player]['aggr_4']
731 #number post flop streets seen, this must be number of post-flop calls !!
732 post_call = stat_dict[player]['call_1'] + stat_dict[player]['call_2'] + stat_dict[player]['call_3'] + stat_dict[player]['call_4']
733 #Number of post flop folds this info is not yet in the database
734 post_fold = stat_dict[player]['f_freq_1'] + stat_dict[player]['f_freq_2'] + stat_dict[player]['f_freq_3'] + stat_dict[player]['f_freq_4']
736 stat = float (bet_raise) / float(post_call + post_fold + bet_raise)
738 return (stat,
739 '%3.1f' % (100.0*stat),
740 'afr=%3.1f%%' % (100.0*stat),
741 'agg_fr=%3.1f%%' % (100.0*stat),
742 '(%d/%d)' % (bet_raise, (post_call + post_fold + bet_raise)),
743 ('Aggression Freq'))
744 except:
745 return (stat,
746 'NA',
747 'af=NA',
748 'agg_f=NA',
749 '(0/0)',
750 ('Aggression Freq'))
752 def agg_fact(stat_dict, player):
753 stat_descriptions["agg_fact"] = _("Aggression factor") + " (agg_fact)"
754 stat = 0.0
755 try:
756 bet_raise = stat_dict[player]['aggr_1'] + stat_dict[player]['aggr_2'] + stat_dict[player]['aggr_3'] + stat_dict[player]['aggr_4']
757 post_call = stat_dict[player]['call_1'] + stat_dict[player]['call_2'] + stat_dict[player]['call_3'] + stat_dict[player]['call_4']
759 if post_call > 0:
760 stat = float (bet_raise) / float(post_call)
761 else:
762 stat = float (bet_raise)
763 return (stat,
764 '%2.2f' % (stat) ,
765 'afa=%2.2f' % (stat) ,
766 'agg_fa=%2.2f' % (stat) ,
767 '(%d/%d)' % (bet_raise, post_call),
768 _('Aggression factor'))
769 except:
770 return (stat,
771 'NA',
772 'afa=NA',
773 'agg_fa=NA',
774 '(0/0)',
775 _('Aggression factor'))
777 def cbet(stat_dict, player):
778 stat_descriptions["cbet"] = _("% continuation bet") + " (cbet)"
779 stat = 0.0
780 try:
781 cbets = stat_dict[player]['cb_1']+stat_dict[player]['cb_2']+stat_dict[player]['cb_3']+stat_dict[player]['cb_4']
782 oppt = stat_dict[player]['cb_opp_1']+stat_dict[player]['cb_opp_2']+stat_dict[player]['cb_opp_3']+stat_dict[player]['cb_opp_4']
783 stat = float(cbets)/float(oppt)
784 return (stat,
785 '%3.1f' % (100.0*stat),
786 'cbet=%3.1f%%' % (100.0*stat),
787 'cbet=%3.1f%%' % (100.0*stat),
788 '(%d/%d)' % (cbets, oppt),
789 _('% continuation bet'))
790 except:
791 return (stat,
792 'NA',
793 'cbet=NA',
794 'cbet=NA',
795 '(0/0)',
796 _('% continuation bet'))
798 def cb1(stat_dict, player):
799 stat_descriptions["cb1"] = _("% continuation bet flop/4th street") + " (cb1)"
800 stat = 0.0
801 try:
802 stat = float(stat_dict[player]['cb_1'])/float(stat_dict[player]['cb_opp_1'])
803 return (stat,
804 '%3.1f' % (100.0*stat),
805 'cb1=%3.1f%%' % (100.0*stat),
806 'cb_1=%3.1f%%' % (100.0*stat),
807 '(%d/%d)' % (stat_dict[player]['cb_1'], stat_dict[player]['cb_opp_1']),
808 _('% continuation bet flop/4th street'))
809 except:
810 return (stat,
811 'NA',
812 'cb1=NA',
813 'cb_1=NA',
814 '(0/0)',
815 _('% continuation bet flop/4th street'))
817 def cb2(stat_dict, player):
818 stat_descriptions["cb2"] = _("% continuation bet turn/5th street") + " (cb2)"
819 stat = 0.0
820 try:
821 stat = float(stat_dict[player]['cb_2'])/float(stat_dict[player]['cb_opp_2'])
822 return (stat,
823 '%3.1f' % (100.0*stat),
824 'cb2=%3.1f%%' % (100.0*stat),
825 'cb_2=%3.1f%%' % (100.0*stat),
826 '(%d/%d)' % (stat_dict[player]['cb_2'], stat_dict[player]['cb_opp_2']),
827 _('% continuation bet turn/5th street'))
828 except:
829 return (stat,
830 'NA',
831 'cb2=NA',
832 'cb_2=NA',
833 '(0/0)',
834 _('% continuation bet turn/5th street'))
836 def cb3(stat_dict, player):
837 stat_descriptions["cb3"] = _("% continuation bet river/6th street") + " (cb3)"
838 stat = 0.0
839 try:
840 stat = float(stat_dict[player]['cb_3'])/float(stat_dict[player]['cb_opp_3'])
841 return (stat,
842 '%3.1f' % (100.0*stat),
843 'cb3=%3.1f%%' % (100.0*stat),
844 'cb_3=%3.1f%%' % (100.0*stat),
845 '(%d/%d)' % (stat_dict[player]['cb_3'], stat_dict[player]['cb_opp_3']),
846 _('% continuation bet river/6th street'))
847 except:
848 return (stat,
849 'NA',
850 'cb3=NA',
851 'cb_3=NA',
852 '(0/0)',
853 _('% continuation bet river/6th street'))
855 def cb4(stat_dict, player):
856 stat_descriptions["cb4"] = _("% continuation bet 7th street") + " (cb4)"
857 stat = 0.0
858 try:
859 stat = float(stat_dict[player]['cb_4'])/float(stat_dict[player]['cb_opp_4'])
860 return (stat,
861 '%3.1f' % (100.0*stat),
862 'cb4=%3.1f%%' % (100.0*stat),
863 'cb_4=%3.1f%%' % (100.0*stat),
864 '(%d/%d)' % (stat_dict[player]['cb_4'], stat_dict[player]['cb_opp_4']),
865 _('% continuation bet 7th street'))
866 except:
867 return (stat,
868 'NA',
869 'cb4=NA',
870 'cb_4=NA',
871 '(0/0)',
872 _('% continuation bet 7th street'))
874 def ffreq1(stat_dict, player):
875 stat_descriptions["ffreq1"] = _("% fold frequency flop/4th street") + " (ffreq1)"
876 stat = 0.0
877 try:
878 stat = float(stat_dict[player]['f_freq_1'])/float(stat_dict[player]['was_raised_1'])
879 return (stat,
880 '%3.1f' % (100.0*stat),
881 'ff1=%3.1f%%' % (100.0*stat),
882 'ff_1=%3.1f%%' % (100.0*stat),
883 '(%d/%d)' % (stat_dict[player]['f_freq_1'], stat_dict[player]['was_raised_1']),
884 _('% fold frequency flop/4th street'))
885 except:
886 return (stat,
887 'NA',
888 'ff1=NA',
889 'ff_1=NA',
890 '(0/0)',
891 _('% fold frequency flop/4th street'))
893 def ffreq2(stat_dict, player):
894 stat_descriptions["ffreq2"] = _("% fold frequency turn/5th street") + " (ffreq2)"
895 stat = 0.0
896 try:
897 stat = float(stat_dict[player]['f_freq_2'])/float(stat_dict[player]['was_raised_2'])
898 return (stat,
899 '%3.1f' % (100.0*stat),
900 'ff2=%3.1f%%' % (100.0*stat),
901 'ff_2=%3.1f%%' % (100.0*stat),
902 '(%d/%d)' % (stat_dict[player]['f_freq_2'], stat_dict[player]['was_raised_2']),
903 _('% fold frequency turn/5th street'))
904 except:
905 return (stat,
906 'NA',
907 'ff2=NA',
908 'ff_2=NA',
909 '(0/0)',
910 _('% fold frequency turn/5th street'))
912 def ffreq3(stat_dict, player):
913 stat_descriptions["ffreq3"] = _("% fold frequency river/6th street") + " (ffreq3)"
914 stat = 0.0
915 try:
916 stat = float(stat_dict[player]['f_freq_3'])/float(stat_dict[player]['was_raised_3'])
917 return (stat,
918 '%3.1f' % (100.0*stat),
919 'ff3=%3.1f%%' % (100.0*stat),
920 'ff_3=%3.1f%%' % (100.0*stat),
921 '(%d/%d)' % (stat_dict[player]['f_freq_3'], stat_dict[player]['was_raised_3']),
922 _('% fold frequency river/6th street'))
923 except:
924 return (stat,
925 'NA',
926 'ff3=NA',
927 'ff_3=NA',
928 '(0/0)',
929 _('% fold frequency river/6th street'))
931 def ffreq4(stat_dict, player):
932 stat_descriptions["ffreq4"] = _("% fold frequency 7th street") + " (ffreq4)"
933 stat = 0.0
934 try:
935 stat = float(stat_dict[player]['f_freq_4'])/float(stat_dict[player]['was_raised_4'])
936 return (stat,
937 '%3.1f' % (100.0*stat),
938 'ff4=%3.1f%%' % (100.0*stat),
939 'ff_4=%3.1f%%' % (100.0*stat),
940 '(%d/%d)' % (stat_dict[player]['f_freq_4'], stat_dict[player]['was_raised_4']),
941 _('% fold frequency 7th street'))
942 except:
943 return (stat,
944 'NA',
945 'ff4=NA',
946 'ff_4=NA',
947 '(0/0)',
948 _('% fold frequency 7th street'))
951 def build_stat_descriptions(stats_file):
952 for method in dir(stats_file):
953 if method in ("Charset", "Configuration", "Database", "GInitiallyUnowned", "gtk", "pygtk",
954 "player", "c", "db_connection", "do_stat", "do_tip", "stat_dict", "h", "re",
955 "re_Percent", "re_Places", "L10n", "sys", "_", "log", "encoder", "codecs",
956 "logging"):
957 continue
958 if method.startswith('__'):
959 continue
960 try:
961 eval(method+"(None, None)")
962 except:
963 pass
965 return stat_descriptions
967 if __name__== "__main__":
968 #TODO: fix
969 statlist = dir()
970 misslist = [ "Configuration", "Database", "Charset", "codecs", "encoder"
971 , "do_stat", "do_tip", "GInitiallyUnowned", "gtk", "pygtk"
972 , "re", "re_Places"
974 statlist = [ x for x in statlist if x not in dir(sys) ]
975 statlist = [ x for x in statlist if x not in dir(codecs) ]
976 statlist = [ x for x in statlist if x not in misslist ]
977 #print "statlist is", statlist
979 c = Configuration.Config()
980 #TODO: restore the below code. somehow it creates a version 119 DB but commenting this out makes it print a stat list
981 db_connection = Database.Database(c)
982 h = db_connection.get_last_hand()
983 stat_dict = db_connection.get_stats_from_hand(h, "ring")
985 for player in stat_dict.keys():
986 print (_("Example stats, player = %s hand = %s:") % (player, h))
987 for attr in statlist:
988 print " ", do_stat(stat_dict, player=player, stat=attr)
989 break
990 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'vpip')
991 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'pfr')
992 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'wtsd')
993 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'profit100')
994 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'saw_f')
995 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'n')
996 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'fold_f')
997 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'wmsd')
998 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'steal')
999 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'f_SB_steal')
1000 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'f_BB_steal')
1001 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'f_steal')
1002 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'three_B')
1003 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'WMsF')
1004 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'a_freq1')
1005 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'a_freq2')
1006 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'a_freq3')
1007 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'a_freq4')
1008 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'a_freq_123')
1009 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'cb1')
1010 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'cb2')
1011 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'cb3')
1012 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'cb4')
1013 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'ffreq1')
1014 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'ffreq2')
1015 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'ffreq3')
1016 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'ffreq4')
1017 #print "player = ", player, do_stat(stat_dict, player = player, stat = 'playershort')
1018 #print "\n"
1020 print _("\n\nLegal stats:")
1021 print _("(add _0 to name to display with 0 decimal places, _1 to display with 1, etc)\n")
1022 for attr in statlist:
1023 print "%-14s %s" % (attr, eval("%s.__doc__" % (attr)))
1024 # print " <pu_stat pu_stat_name = \"%s\"> </pu_stat>" % (attr)
1025 print
1027 #db_connection.close_connection