-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk_game_results
executable file
·142 lines (131 loc) · 3.48 KB
/
mk_game_results
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/ruby
# $Id$
#
# Author:: Daigo Moriwaki
# Homepage:: http://sourceforge.jp/projects/shogi-server/
#
#--
# Copyright (C) 2009 Daigo Moriwaki <daigo at debian dot org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#++
#
# == Synopsis
#
# mk_game_results reads CSA files, then outputs a list of game results that
# is used by mk_rate to calcurate players ratings.
#
# == Usage
#
# ./mk_rate DIR..
#
# == PREREQUIRE
#
# Sample Command lines that isntall prerequires will work on Debian.
#
# * Ruby 1.8.7
#
# $ sudo aptitude install ruby1.8
#
# == Run
#
# $ ./mk_game_results . > game_results.txt
#
require 'getoptlong'
# Parse a CSA file. A tab-delimited line format is
# time state black_mark black_id white_id white_mark filepath
# time:: YYYY/MM/DD hh:mm:ss
# black_mark:: win lose draw
# black_id:: black player's id
# white_mark:: win lose draw
# white_id:: white player's id
# filepath:: absolute file path
#
# @parameter file an absolute path of a csa file
#
def grep(file)
str = File.open(file).read
if /^N\+(.*)$/ =~ str then black_name = $1.strip end
if /^N\-(.*)$/ =~ str then white_name = $1.strip end
if /^'summary:(.*)$/ =~ str
state, p1, p2 = $1.split(":").map {|a| a.strip}
p1_name, p1_mark = p1.split(" ")
p2_name, p2_mark = p2.split(" ")
if p1_name == black_name
black_name, black_mark = p1_name, p1_mark
white_name, white_mark = p2_name, p2_mark
elsif p2_name == black_name
black_name, black_mark = p2_name, p2_mark
white_name, white_mark = p1_name, p1_mark
else
raise "Never reach!: #{black} #{white} #{p3} #{p2}"
end
end
if /^'\$END_TIME:(.*)$/ =~ str
time = $1.strip
end
if /^'rating:(.*)$/ =~ str
black_id, white_id = $1.split(":").map {|a| a.strip}
if black_id && white_id && (black_id != white_id) &&
black_mark && white_mark && state && time
puts [time, state, black_mark, black_id, white_id, white_mark, file].join("\t")
end
end
end
# Show Usage
#
def usage(io)
io.puts <<EOF
USAGE: #{$0} [options] DIR...
DIR where CSA files are looked up recursively
OPTOINS:
--help, -h show this message
EOF
end
# MAIN
#
def main
$options = Hash::new
parser = GetoptLong.new(
["--help", "-h", GetoptLong::NO_ARGUMENT])
parser.quiet = true
begin
parser.each_option do |name, arg|
name.sub!(/^--/, '')
$options[name] = arg.dup
end
rescue
usage($stderr)
raise parser.error_message
end
if $options["help"]
usage($stdout)
exit 0
end
if ARGV.empty?
while line = $stdin.gets do
next unless %r!.*\.csa$! =~ line
grep File.expand_path(line.strip)
end
else
while dir = ARGV.shift do
Dir.glob( File.join(dir, "**", "*.csa") ) {|f| grep(File.expand_path(f))}
end
end
end
if __FILE__ == $0
main
end
# vim: ts=2 sw=2 sts=0