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-2012 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, "EUC-JP")
117 # Want to see completed games only
118 $states.add csa.state
119 return unless csa.state == "toryo"
124 # 2. Process gametime
125 duration = (csa.end_time - csa.start_time).to_i
126 $gametime.add duration.to_i
128 # 3. Process movetime
129 values = csa.movetimes
133 $moves.add values.size
136 $stderr.puts "ERROR: %s" % [file]
142 puts "Usage: #{$0} [OPTIONS] dir [...]"
149 parser = GetoptLong.new(
150 ['--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT]
153 parser.each_option do |name, arg|
154 eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_').upcase} = '#{arg}'"
160 $OPT_REPEAT = $OPT_REPEAT.to_i
165 while (cmd = ARGV.shift)
167 if FileTest.directory?(cmd)
168 Dir.glob(File.join(cmd, "**", "*.csa")).each do |file|
169 break if $OPT_REPEAT == 0
172 elsif FileTest.file?(cmd)
173 break if $OPT_REPEAT == 0
176 throw "Unknown file or directory: #{cmd}"
183 puts "=== Toryo Games ==="
191 $gametime.print("gametime.dat")
195 $movetime.print("movetime.dat")
199 $moves.print("moves.dat")