OSDN Git Service

Release 20140107
[shogi-server/shogi-server.git] / utils / csa-filter.rb
1 #!/usr/bin/ruby
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.
6 #
7 # Author::    Daigo Moriwaki <daigo at debian dot org>
8 # Copyright:: Copyright (C) 2006-2012 Daigo Moriwaki <daigo at debian dot org>
9 #
10 # $Id$
11 #
12 #--
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.
17 #
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.
22 #
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
26 #++
27
28 require 'time'
29 require 'pathname'
30 require 'getoptlong'
31 require 'nkf'
32
33 class CsaFileReader
34   WIN_MARK  = "win"
35   LOSS_MARK = "lose"
36   DRAW_MARK = "draw"
37
38   attr_reader :file_name
39   attr_reader :str
40   attr_reader :black_name, :white_name
41   attr_reader :black_id, :white_id
42   attr_reader :winner, :loser
43   attr_reader :state
44   attr_reader :start_time, :end_time
45
46   def initialize(file_name)
47     @file_name = file_name
48     grep
49   end
50
51   def grep
52     @str = File.open(@file_name, "r:Shift_JIS:EUC-JP").read
53
54
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
68       else
69         raise "Never reach!: #{black} #{white} #{p3} #{p2}"
70       end
71     end
72     if /^\$START_TIME:(.*)$/ =~ @str
73       @start_time = Time.parse($1.strip)
74     end
75     if /^'\$END_TIME:(.*)$/ =~ @str
76       @end_time = Time.parse($1.strip)
77     end
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
90         else
91           raise "Never reached!"
92         end
93       end
94     end
95   end
96
97   def movetimes
98     ret = []
99     @str.gsub(%r!^T(\d+)!) do |match|
100       ret << $1.to_i
101     end
102     return ret
103   end
104
105   def to_s
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"
111   end
112
113   def identify_id(id)
114     if /@NORATE\+/ =~ id # the player having @NORATE in the name should not be rated
115       return nil
116     end
117     id.gsub(/@.*?\+/,"+")
118   end
119 end
120
121
122 if $0 == __FILE__
123   def usage
124     puts "Usage: #{$0} [OPTIONS] dir [...]"
125     puts "Options:"
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"
129     exit 1
130   end
131
132   usage if ARGV.empty?
133
134   parser = GetoptLong.new(
135              ['--black',   GetoptLong::REQUIRED_ARGUMENT],
136              ['--white',   GetoptLong::REQUIRED_ARGUMENT],
137              ['--players', GetoptLong::REQUIRED_ARGUMENT]
138            )
139   begin
140     parser.each_option do |name, arg|
141       eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_').upcase} = '#{arg}'"
142     end
143   rescue
144     usage
145   end
146   
147   while dir = ARGV.shift
148     Dir.glob(File.join(dir, "**", "*.csa")).each do |file|
149       csa = CsaFileReader.new(file)
150
151       next unless csa.black_id && csa.white_id
152
153       if $OPT_PLAYERS
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)
159           next
160         end
161       end
162       
163       if $OPT_BLACK
164         next unless csa.black_id.downcase.index($OPT_BLACK.downcase) == 0
165       end
166       if $OPT_WHITE
167         next unless csa.white_id.downcase.index($OPT_WHITE.downcase) == 0
168       end
169       puts csa.file_name
170     end
171   end
172 end