4 # Author:: Daigo Moriwaki
5 # Homepage:: http://sourceforge.jp/projects/shogi-server/
8 # Copyright (C) 2009-2012 Daigo Moriwaki <daigo at debian dot org>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 # mk_game_results reads CSA files, then outputs a list of game results that
28 # is used by mk_rate to calcurate players ratings.
36 # Sample Command lines that isntall prerequires will work on Debian.
38 # * Ruby 2.0.0 or later
40 # $ sudo aptitude install ruby
44 # $ ./mk_game_results . > game_results.txt
49 # Parse a CSA file. A tab-delimited line format is
50 # time state black_mark black_id white_id white_mark filepath moves
51 # time:: YYYY/MM/DD hh:mm:ss
52 # black_mark:: win lose draw
53 # black_id:: black player's id
54 # white_mark:: win lose draw
55 # white_id:: white player's id
56 # filepath:: absolute file path
57 # moves:: number of moves
59 # @parameter file an absolute path of a csa file
62 if RUBY_VERSION >= "1.9.1"
63 str = File.open(file, "r:EUC-JP").read
65 str = File.open(file, "r").read
77 str.each_line do |line|
80 when /^[\+\-]\d{4}\w{2}.*$/
86 when /^'summary:(.*)$/
87 state, p1, p2 = $1.split(":").map {|a| a.strip}
88 p1_name, p1_mark = p1.split(" ")
89 p2_name, p2_mark = p2.split(" ")
90 if p1_name == black_name
91 black_name, black_mark = p1_name, p1_mark
92 white_name, white_mark = p2_name, p2_mark
93 elsif p2_name == black_name
94 black_name, black_mark = p2_name, p2_mark
95 white_name, white_mark = p1_name, p1_mark
97 raise "Never reach!: #{black_name}, #{p1}, #{p2} in #{file}"
99 when /^'\$END_TIME:(.*)$/
101 when /^'rating:(.*)$/
102 black_id, white_id = $1.split(":").map {|a| a.strip}
106 if black_id && white_id && (black_id != white_id) &&
107 black_mark && white_mark && state && time
108 puts [time, state, black_mark, black_id, white_id, white_mark, file, moves].join("\t")
117 USAGE: #{$0} [options] DIR...
118 DIR where CSA files are looked up recursively
120 --help, -h show this message
128 parser = GetoptLong.new(
129 ["--help", "-h", GetoptLong::NO_ARGUMENT])
132 parser.each_option do |name, arg|
134 $options[name] = arg.dup
138 raise parser.error_message
146 while line = $stdin.gets do
147 next unless %r!.*\.csa$! =~ line
148 grep File.expand_path(line.strip)
151 while dir = ARGV.shift do
152 Dir.glob( File.join(dir, "**", "*.csa") ) {|f| grep(File.expand_path(f))}
161 # vim: tabstop=4 shiftwidth=4 expandtab