2 # This program filters CSA files. For example, if you want only CSA files
3 # played by GPS vs Bonanza,
4 # $ ./csa-filter.rb --players gps-l,bonanza some_dir
5 # you will see such files under the some_dir directory.
7 # Author:: Daigo Moriwaki <daigo at debian dot org>
8 # Copyright:: Copyright (C) 2006-2008 Daigo Moriwaki <daigo at debian dot org>
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 $:.unshift File.dirname(__FILE__)
34 @games = Hash.new {|hash,key| hash[key] = 0}
35 @players = Hash.new {|hash,key| hash[key] = Set.new}
40 month = st.strftime("%Y%m")
44 [csa.black_id, csa.white_id].each do |player|
45 @players[month].add(player)
50 puts "YYYYMM\t#games\t#players"
51 @games.sort {|a,b| a[0] <=> b[0]}.each do |key,value|
52 puts "%s\t% 6d\t% 2d" % [key, value, @players[key].size]
72 total = @v.inject(0){|sum, item| sum+item}
73 avg = 1.0*total/@v.size
74 puts "avg: %f sec (size: %d)" % [avg, @v.size]
76 File.open(file, "w") do |f|
77 @v.each {|v| f.puts v}
84 @hash = Hash.new {|hash,key| hash[key] = 0}
88 if value.nil? || value.empty?
96 @hash.sort {|a,b| b[1] <=> a[1]}.each do |key, value|
97 puts "%s\t% 6d" % [key, value]
102 $monthly = Monthly.new
103 $gametime = Values.new
104 $movetime = Values.new
109 $OPT_REPEAT -= 1 if $OPT_REPEAT > 0
110 csa = CsaFileReader.new(file)
112 # Want to see complete games
113 $states.add csa.state
114 return unless csa.state == "toryo"
116 # See games between 2008/03 to 2009/07
117 return if csa.start_time < Time.parse("2008/03/01") ||
118 csa.start_time >= Time.parse("2009/08/01")
124 duration = (csa.end_time - csa.start_time).to_i
126 $stderr.puts "Too long game: #{file}"
129 $gametime.add duration.to_i
132 values = csa.movetimes
136 $moves.add values.size
139 $stderr.puts "ERROR: %s" % [file]
145 puts "Usage: #{$0} [OPTIONS] dir [...]"
152 parser = GetoptLong.new(
153 ['--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT]
156 parser.each_option do |name, arg|
157 eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_').upcase} = '#{arg}'"
163 $OPT_REPEAT = $OPT_REPEAT.to_i
168 while (cmd = ARGV.shift)
170 if FileTest.directory?(cmd)
171 Dir.glob(File.join(cmd, "**", "*.csa")).each do |file|
172 break if $OPT_REPEAT == 0
175 elsif FileTest.file?(cmd)
176 break if $OPT_REPEAT == 0
179 throw "Unknown file or directory: #{cmd}"
186 puts "=== Toryo Games ==="
194 $gametime.print("gametime.dat")
198 $movetime.print("movetime.dat")
202 $moves.print("moves.dat")