From: beatles Date: Sun, 8 Nov 2009 13:48:48 +0000 (+0000) Subject: * [mk_rate] X-Git-Tag: 20170902~154 X-Git-Url: http://git.sourceforge.jp/view?p=shogi-server%2Fshogi-server.git;a=commitdiff_plain;h=3c4fcc1a48d9ae6134ee41fd9a84c465a0c3a912 * [mk_rate] - Split a pre-process collecting game results from csa files into a new command, mk_game_results. Now, Generating players.html requires two steps as follows: % ./mk_game_results dir_of_csa_files > 00list % ./mk_rate 00list > players.html or % ./mk_game_results dir_of_csa_files | ./mk_rate > players.html (Closes: #19454) --- diff --git a/changelog b/changelog index 45f1052..f2ae844 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,15 @@ +2009-11-08 Daigo Moriwaki + + * [mk_rate] + - Split a pre-process collecting game results from csa files into + a new command, mk_game_results. Now, Generating players.html + requires two steps as follows: + % ./mk_game_results dir_of_csa_files > 00list + % ./mk_rate 00list > players.html + or + % ./mk_game_results dir_of_csa_files | ./mk_rate > players.html + (Closes: #19454) + 2009-08-06 Daigo Moriwaki * [shogi-server] diff --git a/mk_game_results b/mk_game_results new file mode 100755 index 0000000..caaf7c7 --- /dev/null +++ b/mk_game_results @@ -0,0 +1,142 @@ +#!/usr/bin/ruby +# $Id$ +# +# Author:: Daigo Moriwaki +# Homepage:: http://sourceforge.jp/projects/shogi-server/ +# +#-- +# Copyright (C) 2009 Daigo Moriwaki +# +# 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 < +# Copyright (C) 2006-2009 Daigo Moriwaki # # 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 @@ -623,38 +623,24 @@ def identify_id(id) id.gsub(/@.*?\+/,"+") end -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} - return if state == "abnormal" - 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 = Time.parse($1.strip) +# Parse a game result line +# +def parse(line) + time, state, black_mark, black_id, white_id, white_mark, file = line.split("\t") + unless time && state && black_mark && black_id && + white_id && white_mark && file + $stderr.puts "Failed to parse the line : #{line}" + return end - if /^'rating:(.*)$/ =~ str - black_id, white_id = $1.split(":").map {|a| a.strip} - black_id = identify_id(black_id) - white_id = identify_id(white_id) - if black_id && white_id && (black_id != white_id) && - black_mark && white_mark - add(black_mark, black_id, white_id, white_mark, time) - end + + return if state == "abnormal" + time = Time.parse(time) + black_id = identify_id(black_id) + white_id = identify_id(white_id) + + if black_id && white_id && (black_id != white_id) && + black_mark && white_mark + add(black_mark, black_id, white_id, white_mark, time) end end @@ -721,12 +707,15 @@ def main if ARGV.empty? while line = $stdin.gets do - next unless %r!.*\.csa$! =~ line - grep line.strip + parse line.strip end else - while dir = ARGV.shift do - Dir.glob( File.join(dir, "**", "*.csa") ) {|f| grep(f)} + while file = ARGV.shift do + File.open(file) do |f| + f.each_line do |line| + parse line.strip + end + end end end