2 # This program shows statistics of CSA kifu files like following:
3 # - Monthly #games and #players
10 # $ ./statistics.rb /dev/shm/floodgate
12 # Author:: Daigo Moriwaki <daigo at debian dot org>
13 # Copyright:: Copyright (C) 2009 Daigo Moriwaki <daigo at debian dot org>
18 # This program is free software; you can redistribute it and/or modify
19 # it under the terms of the GNU General Public License as published by
20 # the Free Software Foundation; either version 2 of the License, or
21 # (at your option) any later version.
23 # This program is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software
30 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 $:.unshift File.dirname(__FILE__)
39 @games = Hash.new {|hash,key| hash[key] = 0}
40 @players = Hash.new {|hash,key| hash[key] = Set.new}
45 month = st.strftime("%Y%m")
49 [csa.black_id, csa.white_id].each do |player|
50 @players[month].add(player)
55 puts "YYYYMM\t#games\t#players"
56 @games.sort {|a,b| a[0] <=> b[0]}.each do |key,value|
57 puts "%s\t% 6d\t% 2d" % [key, value, @players[key].size]
77 total = @v.inject(0){|sum, item| sum+item}
78 avg = 1.0*total/@v.size
79 puts "avg: %f sec (size: %d)" % [avg, @v.size]
81 File.open(file, "w") do |f|
82 @v.each {|v| f.puts v}
89 @hash = Hash.new {|hash,key| hash[key] = 0}
93 if value.nil? || value.empty?
100 puts "status\t#games"
101 @hash.sort {|a,b| b[1] <=> a[1]}.each do |key, value|
102 puts "%s\t% 6d" % [key, value]
107 $monthly = Monthly.new
108 $gametime = Values.new
109 $movetime = Values.new
114 $OPT_REPEAT -= 1 if $OPT_REPEAT > 0
115 csa = CsaFileReader.new(file)
117 # See games between 2008/03 to 2009/07
118 return if csa.start_time.nil? ||
119 csa.start_time < Time.parse("2008/03/01") ||
120 csa.start_time >= Time.parse("2009/08/01")
122 # Want to see complete games
123 $states.add csa.state
124 return unless csa.state == "toryo"
130 duration = (csa.end_time - csa.start_time).to_i
132 $stderr.puts "Too long game: #{file}"
135 $gametime.add duration.to_i
138 values = csa.movetimes
142 $moves.add values.size
145 $stderr.puts "ERROR: %s" % [file]
151 puts "Usage: #{$0} [OPTIONS] dir [...]"
158 parser = GetoptLong.new(
159 ['--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT]
162 parser.each_option do |name, arg|
163 eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_').upcase} = '#{arg}'"
169 $OPT_REPEAT = $OPT_REPEAT.to_i
174 while (cmd = ARGV.shift)
176 if FileTest.directory?(cmd)
177 Dir.glob(File.join(cmd, "**", "*.csa")).each do |file|
178 break if $OPT_REPEAT == 0
181 elsif FileTest.file?(cmd)
182 break if $OPT_REPEAT == 0
185 throw "Unknown file or directory: #{cmd}"
192 puts "=== Toryo Games ==="
200 $gametime.print("gametime.dat")
204 $movetime.print("movetime.dat")
208 $moves.print("moves.dat")