Scid  4.7.0
stats.tcl
Go to the documentation of this file.
1 ###
2 ### windows/stats.tcl: Filter Statistics window for Scid
3 ###
4 
5 set ::windows::stats::isOpen 0
6 set ::windows::stats::like_graphyear 1
7 set ::windows::stats::like_graphelo 1
8 set ::windows::stats::old_year 0
9 set ::windows::stats::old_elo 0
10 set ::windows::stats::stat_year 0
11 set ::windows::stats::statelo 0
12 
13 proc ::windows::stats::Open {} {
14  set w .statsWin
15  if {[winfo exists $w]} {
16  focus .
17  destroy $w
18  set ::windows::stats::isOpen 0
19  return
20  }
22 # toplevel $w
24  wm title $w "Scid: $::tr(FilterStatistic)"
26  bind $w <Configure> "recordWinSize $w"
27 
28 # use autoscrollframe to display large infos
29  autoscrollframe $w.statsasb text $w.stats -width 64 -height 40\
30  -foreground black -background white -font font_Fixed\
31  -wrap none
32 
33  ttk::frame $w.fbuttons
34  ttk::button $w.fbuttons.print -image tb_save -command {
35  set ftype {
36  { "Text files" {".txt"} }
37  { "All files" {"*"} }
38  }
39  set fname [tk_getSaveFile -initialdir [pwd] -filetypes $ftype -title "Save text file"]
40  if {$fname != ""} {
41  if {[catch {set tempfile [open $fname w]}]} {
42  tk_messageBox -title "Scid: Error saving file" -type ok -icon warning \
43  -message "Unable to save the file: $fname\n\n"
44  } else {
45  puts $tempfile [.statsWin.stats get 1.0 end-1c]
46  close $tempfile
47  }
48  }
49  }
50  ttk::checkbutton $w.fbuttons.graphyear -text $::tr(Year) \
51  -variable ::windows::stats::like_graphyear -command ::windows::stats::refresh_wnd
52  ttk::checkbutton $w.fbuttons.graphelo -text $::tr(Rating) \
53  -variable ::windows::stats::like_graphelo -command ::windows::stats::refresh_wnd
54  ttk::checkbutton $w.fbuttons.statelo -text "StatElo" \
55  -variable ::windows::stats::statelo -command ::windows::stats::refresh_wnd
56  ttk::checkbutton $w.fbuttons.statyear -text "StatYear" \
57  -variable ::windows::stats::stat_year -command ::windows::stats::refresh_wnd
58  ttk::checkbutton $w.fbuttons.old_elo -text "OldElo" \
59  -variable ::windows::stats::old_elo -command ::windows::stats::refresh_wnd
60  ttk::checkbutton $w.fbuttons.oldyear -text "OldYear" \
61  -variable ::windows::stats::old_year -command ::windows::stats::refresh_wnd
62 
63  ttk::button $w.fbuttons.setup -image tb_config -command configureFilterGraph
64  pack $w.fbuttons.graphyear $w.fbuttons.graphelo $w.fbuttons.statyear $w.fbuttons.statelo $w.fbuttons.oldyear $w.fbuttons.old_elo -side left
65  pack $w.fbuttons.setup $w.fbuttons.print -side right -padx "5 0" -pady 2
66  pack $w.fbuttons -side top -fill x
67  pack $w.statsasb -side top -fill both -expand yes
68  set ::windows::stats::isOpen 1
69  bind $w <Control-q> "destroy $w"
70  bind $w <Escape> "destroy $w"
71  bind $w <F1> { helpWindow Index }
72  bind $w <Destroy> {
73  set ::windows::stats::isOpen 0
74  }
75 
76  wm resizable $w 0 1
77 
79 }
80 
81 # TODO: remove this function
82 proc ::windows::stats::Refresh {} {
83  ::notify::DatabaseModified $::curr_db dbfilter
84 }
85 
86 proc ::windows::stats::refresh_wnd {} {
87  global FilterMaxMoves FilterMinMoves FilterStepMoves FilterMaxElo FilterMinElo FilterStepElo FilterMaxYear FilterMinYear FilterStepYear FilterGuessELO
88  variable display
89 
90  if {! [winfo exists .statsWin]} { return}
91 
92  # Set up variables for translated phrases:
93  set all [::utils::string::Capital $::tr(allGames)]
94  set both $::tr(OprepStatBoth)
95  set since $::tr(OprepStatSince)
96  set games [::utils::string::Capital $::tr(games)]
97  set score [::utils::string::Capital $::tr(score)]
98 
99  # Find length of longest left-hand column:
100  set alen [expr {[string length $all] + 1}]
101  set blen [expr {[string length $both] + 7}]
102  set slen [expr {[string length $since] + 12}]
103  set len $alen
104  if {$len < $blen} { set len $blen}
105  if {$len < $slen} { set len $slen}
106 
107  set height 4
108  set ratings 0
109  set years 0
110  set rlist [lsort -decreasing [array names display r*]]
111  set ylist [lsort [array names display y*]]
112 
113  foreach i $rlist {
114  if {$display($i)} { set ratings 1}
115  }
116  foreach i $ylist {
117  if {$display($i)} { set years 1}
118  }
119 
120  if {$ratings} { incr height}
121  if {$years} { incr height}
122 
123  set s ""
124  set sm "\n"
125  set stat ""
126  append s " [::utils::string::Pad $stat [expr $len - 4]] [::utils::string::PadRight $games 10]"
127  append s " 1-0 =-= 0-1 [::utils::string::PadRight $score 8]\n"
128  append s "--------------------------------------------------------------"
129  append s "\n [::utils::string::Pad $all $len]" [sc_filter stats all]
130 # New Statistic: Count the games in intervalls "start elo - end elo"
131 # if elo is deselected in option menu, then enlarge the intervall to next selectet elo.
132 # New Statistic: Count the games in intervalls
133  if { $::windows::stats::like_graphelo } {
134  set endElo $FilterMaxElo
135  set startElo $FilterMinElo
136  for {set i $startElo} {$i <= $endElo} {set i [expr {$i + $FilterStepElo}]} {
137  incr height
138  incr height
139  set nelo [expr {$i+$FilterStepElo}]
140  # shorten gap between 0 and "useful" ratings 1800
141  set j $i
142  if { $i < 100 } { set i [expr { 1800 - $FilterStepElo}]}
143  set stat "min. Elo $i-$nelo"
144  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $i $nelo]
145  set stat "max. Elo $i-$nelo"
146  # +10000 workaround to trigger max elo in filter function
147  append sm "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $i [expr {$nelo + 10000}]]
148  }
149  }
150  if {$::windows::stats::statelo && $ratings} {
151  append s "\n"
152  set j 0
153  set k [lindex $rlist $j]
154  while { $k!= "" && ! $display($k) } {
155  incr j
156  set k [lindex $ylist $j]
157  if { $k == "" } { break}
158  }
159  set nelo [string range [lindex $rlist $j] 1 end]
160  incr height
161  #first line searches all games greater 2600 Elo
162  set stat "min. Elo $nelo-3500"
163  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $nelo 9999]
164  set stat "max. Elo $nelo-3500"
165  append sm "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $nelo -9999]
166  set j 0
167  foreach i $rlist {
168  incr j
169  if {$display($i)} {
170  incr height
171  set elo [string range $i 1 end]
172  set l $j
173  set k [lindex $rlist $l]
174  while { $k!= "" && ! $display($k) } {
175  incr l
176  set k [lindex $rlist $l]
177  if { $k == "" } { break}
178  }
179  set nelo [string range [lindex $rlist $l] 1 end]
180  if { $nelo == "" } { set nelo 0}
181  #count all games where player whith lowest Elo is in the specific range
182  set stat "min. Elo $nelo-$elo"
183  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $nelo $elo]
184  set stat "max. Elo $nelo-$elo"
185  #count all games where player whith highest Elo is in the specific range
186  # +10000 workaround to trigger max elo in filter function
187  append sm "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $nelo [expr {$elo + 10000}]]
188  }
189  }
190  }
191  append s $sm
192  append s "\n"
193 # New Statistic: Count the games in intervalls "from year - to year"
194 # if year is deselected in option menu, then enlarge the intervall to next selectet year.
195  if { $::windows::stats::like_graphyear } {
196  set endYear $FilterMaxYear
197  set startYear $FilterMinYear
198  for {set i $startYear} {$i <= $endYear} {set i [expr {$i + $FilterStepYear}]} {
199  incr height
200  set ie [expr {$i+$FilterStepYear}]
201  set stat "$i - $ie"
202  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats year $i $ie]
203  }
204  }
205  if {$::windows::stats::stat_year && $years} {
206 # this statistic ignores the intervall and uses 1 Year as intervall
207  set j 0
208  foreach i $ylist {
209  incr j
210  if {$display($i)} {
211  incr height
212  set year [string range $i 1 end]
213  set l $j
214  set k [lindex $ylist $l]
215  while { $k!= "" && ! $display($k) } {
216  incr l
217  set k [lindex $ylist $l]
218  if { $k == "" } { break}
219  }
220  set nyear [string range $k 1 end]
221  if { $nyear == "" } { set nyear 2099}
222  set stat "$year - $nyear"
223  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats year $year $nyear]
224  }
225  }
226  }
227 #Old statistic: count the games from specific value to maximum value
228  set stat ""
229  if { $::windows::stats::old_elo || $::windows::stats::old_year} {
230  append s "\n\n"
231  append s "OldYear/OldElo[::utils::string::Pad $stat [expr $len - 17]] [::utils::string::PadRight $games 10]"
232  append s " 1-0 =-= 0-1 [::utils::string::PadRight $score 8]\n"
233  append s "--------------------------------------------------------------"
234  append s "\n [::utils::string::Pad $all $len]" [sc_filter stats all]
235  }
236  if {$ratings && $::windows::stats::old_elo} {
237  append s "\n"
238  foreach i $rlist {
239  if {$display($i)} {
240  incr height
241  set elo [string range $i 1 end]
242  set stat "$both $elo+"
243  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats elo $elo 0]
244  }
245  }
246  }
247 
248  if {$years && $::windows::stats::old_year} {
249  append s "\n"
250  foreach i $ylist {
251  if {$display($i)} {
252  incr height
253  set year [string range $i 1 end]
254  set stat "$since $year.01.01"
255  append s "\n [::utils::string::Pad $stat $len]" [sc_filter stats year $year 0]
256  }
257  }
258  }
259  append s "\n"
260 
261  set w .statsWin.stats
262  if { $height > 50 } { set height 50}
263  $w configure -state normal
264  $w delete 1.0 end
265  $w insert end $s
266  $w tag configure bgGray -background gray95
267  # Shade every second line to help readability:
268  set lastLineNum [expr {int([$w index end])}]
269  for {set i 2} {$i <= $lastLineNum} {incr i 2} {
270  $w tag add bgGray $i.0 "$i.0 lineend +1c"
271  }
272  $w tag configure blue -foreground darkBlue
273  $w tag configure red -foreground red
274  $w tag add blue 1.0 2.0
275  $w tag add red 2.0 3.0
276  $w configure -height $height
277  $w configure -state disabled
278 }