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
38 attr_reader :file_name
40 attr_reader :black_name, :white_name
41 attr_reader :black_id, :white_id
42 attr_reader :winner, :loser
44 attr_reader :start_time, :end_time
46 def initialize(file_name)
47 @file_name = file_name
52 @str = File.open(@file_name, "r:Shift_JIS:EUC-JP").read
55 if /^N\+(.*)$/ =~ @str then @black_name = $1.strip end
56 if /^N\-(.*)$/ =~ @str then @white_name = $1.strip end
57 if /^'summary:(.*)$/ =~ @str
58 @state, p1, p2 = $1.split(":").map {|a| a.strip}
59 return if @state == "abnormal"
60 p1_name, p1_mark = p1.split(" ")
61 p2_name, p2_mark = p2.split(" ")
62 if p1_name == @black_name
63 @black_name, black_mark = p1_name, p1_mark
64 @white_name, white_mark = p2_name, p2_mark
65 elsif p2_name == @black_name
66 @black_name, black_mark = p2_name, p2_mark
67 @white_name, white_mark = p1_name, p1_mark
69 raise "Never reach!: #{black} #{white} #{p3} #{p2}"
72 if /^\$START_TIME:(.*)$/ =~ @str
73 @start_time = Time.parse($1.strip)
75 if /^'\$END_TIME:(.*)$/ =~ @str
76 @end_time = Time.parse($1.strip)
78 if /^'rating:(.*)$/ =~ @str
79 black_id, white_id = $1.split(":").map {|a| a.strip}
80 @black_id = identify_id(black_id)
81 @white_id = identify_id(white_id)
82 if @black_id && @white_id && (@black_id != @white_id) &&
83 @black_mark && @white_mark
84 if black_mark == WIN_MARK && white_mark == LOSS_MARK
85 @winner, @loser = @black_id, @white_id
86 elsif black_mark == LOSS_MARK && white_mark == WIN_MARK
87 @winner, @loser = @white_id, @black_id
88 elsif black_mark == DRAW_MARK && white_mark == DRAW_MARK
89 @winner, @loser = nil, nil
91 raise "Never reached!"
99 @str.gsub(%r!^T(\d+)!) do |match|
106 return "Summary: #{@file_name}\n" +
107 "BlackName #{@black_name}, WhiteName #{@white_name}\n" +
108 "BlackId #{@black_id}, WhiteId #{@white_id}\n" +
109 "Winner #{@winner}, Loser #{@loser}\n" +
110 "Start #{@start_time}, End #{@end_time}\n"
114 if /@NORATE\+/ =~ id # the player having @NORATE in the name should not be rated
117 id.gsub(/@.*?\+/,"+")
124 puts "Usage: #{$0} [OPTIONS] dir [...]"
126 puts " --players player_a,player_b select games of the player_a vs the player_b"
127 puts " --black player select games of which the player is Black"
128 puts " --white player select games of which the player is White"
134 parser = GetoptLong.new(
135 ['--black', GetoptLong::REQUIRED_ARGUMENT],
136 ['--white', GetoptLong::REQUIRED_ARGUMENT],
137 ['--players', GetoptLong::REQUIRED_ARGUMENT]
140 parser.each_option do |name, arg|
141 eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_').upcase} = '#{arg}'"
147 while dir = ARGV.shift
148 Dir.glob(File.join(dir, "**", "*.csa")).each do |file|
149 csa = CsaFileReader.new(file)
151 next unless csa.black_id && csa.white_id
154 players = $OPT_PLAYERS.split(",")
155 unless (csa.black_id.downcase.index(players[0].downcase) == 0 &&
156 csa.white_id.downcase.index(players[1].downcase) == 0) ||
157 (csa.black_id.downcase.index(players[1].downcase) == 0 &&
158 csa.white_id.downcase.index(players[0].downcase) == 0)
164 next unless csa.black_id.downcase.index($OPT_BLACK.downcase) == 0
167 next unless csa.white_id.downcase.index($OPT_WHITE.downcase) == 0