4 # Author:: Daigo Moriwaki
5 # Homepage:: http://sourceforge.jp/projects/shogi-server/
8 # Copyright (C) 2008-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_rate reads CSA files, calculates rating scores of each player, and then
28 # outputs a yaml file (players.yaml) that Shogi-server can recognize.
32 # (1) csa-file-filter.rb [options] DIR...
35 # where CSA files are looked for recursively
37 # (2) csa-file-filter.rb [options]
39 # CSA file names are put into STDIN.
43 # [<tt>--within</tt> <i>n</i> [days]]
44 # find records that were played last n days
51 # Sample Command lines that isntall prerequires will work on Debian.
53 # * Ruby 1.9.3 or 1.8.7 including RDoc
55 # $ sudo aptitude install ruby ruby1.9.1
59 # If you want kifu files that were played last 14 days to be rated,
61 # $ find /path/to/dir -name "wdoor+floodgate*.csa" | \
62 # ./csa-file-filter --within 14 | \
63 # ./mk_rate --half-life-ignore 14
70 # Filter the filename. If the file passes the criteria, it goes to STDOUT;
71 # otherwise, it is ignored.
76 return unless /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\.csa$/ =~ filename
78 file_date = Date.new($1.to_i, $2.to_i, $3.to_i)
80 return if $now_date - file_date > $options["within"]
87 # Show an usage of this command
92 #{$0} [options] DIR...
93 DIR where CSA files are looked for recursively
95 CSA file names are put into STDIN.
98 --within n [days] find records that were played last n days
99 --help show this message
107 # Parse command options
109 parser = GetoptLong.new(
110 ["--within", GetoptLong::REQUIRED_ARGUMENT],
111 ["--help", "-h", GetoptLong::NO_ARGUMENT]
115 parser.each_option do |name, arg|
117 $options[name] = arg.dup
119 if $options["within"]
120 $options["within"] = $options["within"].to_i
124 raise parser.error_message
133 while line = $stdin.gets do
135 next unless %r!.*\.csa$! =~ line
139 while dir = ARGV.shift do
140 Dir.glob( File.join(dir, "**", "*.csa") ) {|f| filter(f)}
147 $now_date = Date.new(now.year, now.month, now.day)
151 # vim: ts=2 sw=2 sts=0