#!/usr/bin/ruby # $Id$ # # Author:: Daigo Moriwaki # Homepage:: http://sourceforge.jp/projects/shogi-server/ # #-- # Copyright (C) 2009-2012 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 2.0.0 or later # # $ sudo aptitude install ruby # # == 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 moves # 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 # moves:: number of moves # # @parameter file an absolute path of a csa file # def grep(file) if RUBY_VERSION >= "1.9.1" str = File.open(file, "r:EUC-JP").read else str = File.open(file, "r").read end black_name = "" black_mark = "" black_id = "" white_name = "" white_mark = "" white_id = "" state = "" time = "" moves = 0 str.each_line do |line| line.strip! case line when /^[\+\-]\d{4}\w{2}.*$/ moves += 1 when /^N\+(.*)$/ black_name = $1.strip when /^N\-(.*)$/ white_name = $1.strip when /^'summary:(.*)$/ 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_name}, #{p1}, #{p2} in #{file}" end when /^'\$END_TIME:(.*)$/ time = $1.strip when /^'rating:(.*)$/ black_id, white_id = $1.split(":").map {|a| a.strip} end # case end # do line 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, moves].join("\t") $stdout.flush end end # Show Usage # def usage(io) io.puts <