OSDN Git Service

Merge branch '201410-maxmoves'
[shogi-server/shogi-server.git] / mk_game_results
1 #!/usr/bin/ruby
2 # $Id$
3 #
4 # Author:: Daigo Moriwaki
5 # Homepage:: http://sourceforge.jp/projects/shogi-server/
6 #
7 #--
8 # Copyright (C) 2009-2012 Daigo Moriwaki <daigo at debian dot org>
9 #
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.
14 #
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.
19 #
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
23 #++
24 #
25 # == Synopsis
26 #
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.
29 #
30 # == Usage
31 #
32 # ./mk_rate DIR..
33
34 # == PREREQUIRE
35 #
36 # Sample Command lines that isntall prerequires will work on Debian.
37 #
38 # * Ruby 2.0.0 or later
39 #
40 #   $ sudo aptitude install ruby
41 #
42 # == Run
43 #
44 #   $ ./mk_game_results . > game_results.txt
45 #
46
47 require 'getoptlong'
48
49 # Parse a CSA file. A tab-delimited line format is
50 #   time  state  black_mark  black_id  white_id  white_mark  filepath 
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 #
58 # @parameter file an absolute path of a csa file
59 #
60 def grep(file)
61   if RUBY_VERSION >= "1.9.1"
62     str = File.open(file, "r:EUC-JP").read
63   else
64     str = File.open(file, "r").read
65   end
66
67   if /^N\+(.*)$/ =~ str then black_name = $1.strip end
68   if /^N\-(.*)$/ =~ str then white_name = $1.strip end
69
70   if /^'summary:(.*)$/ =~ str
71     state, p1, p2 = $1.split(":").map {|a| a.strip}    
72     p1_name, p1_mark = p1.split(" ")
73     p2_name, p2_mark = p2.split(" ")
74     if p1_name == black_name
75       black_name, black_mark = p1_name, p1_mark
76       white_name, white_mark = p2_name, p2_mark
77     elsif p2_name == black_name
78       black_name, black_mark = p2_name, p2_mark
79       white_name, white_mark = p1_name, p1_mark
80     else
81       raise "Never reach!: #{black} #{white} #{p3} #{p2}"
82     end
83   end
84
85   if /^'\$END_TIME:(.*)$/ =~ str
86     time = $1.strip
87   end
88
89   if /^'rating:(.*)$/ =~ str
90     black_id, white_id = $1.split(":").map {|a| a.strip}
91     if black_id && white_id && (black_id != white_id) &&
92        black_mark && white_mark && state && time
93       puts [time, state, black_mark, black_id, white_id, white_mark, file].join("\t")
94     end
95   end
96   $stdout.flush
97 end
98
99 # Show Usage
100 #
101 def usage(io)
102     io.puts <<EOF
103 USAGE: #{$0} [options] DIR...
104   DIR                where CSA files are looked up recursively
105 OPTOINS:
106   --help, -h         show this message
107 EOF
108 end
109
110 # MAIN 
111 #
112 def main
113   $options = Hash::new
114   parser = GetoptLong.new(
115     ["--help", "-h",        GetoptLong::NO_ARGUMENT])
116   parser.quiet = true
117   begin
118     parser.each_option do |name, arg|
119       name.sub!(/^--/, '')
120       $options[name] = arg.dup
121     end
122   rescue
123     usage($stderr)
124     raise parser.error_message
125   end
126   if $options["help"]
127     usage($stdout) 
128     exit 0
129   end
130
131   if ARGV.empty?
132     while line = $stdin.gets do
133       next unless %r!.*\.csa$! =~ line
134       grep File.expand_path(line.strip)
135     end
136   else
137     while dir = ARGV.shift do
138       Dir.glob( File.join(dir, "**", "*.csa") ) {|f| grep(File.expand_path(f))}
139     end
140   end
141 end
142
143 if __FILE__ == $0
144   main
145 end
146
147 # vim: ts=2 sw=2 sts=0