ファイル情報

Rev. 2d5cda5e9dde331e37d58133b688a8b2c6295345
サイズ 4,115 バイト
日時 2017-09-03 14:40:39
作者 Daigo Moriwaki
ログメッセージ

Merge remote-tracking branch 'origin/master' into wdoor-stable

内容

  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. require 'getoptlong'
  47. # Parse a CSA file. A tab-delimited line format is
  48. # time state black_mark black_id white_id white_mark filepath moves
  49. # time:: YYYY/MM/DD hh:mm:ss
  50. # black_mark:: win lose draw
  51. # black_id:: black player's id
  52. # white_mark:: win lose draw
  53. # white_id:: white player's id
  54. # filepath:: absolute file path
  55. # moves:: number of moves
  56. #
  57. # @parameter file an absolute path of a csa file
  58. #
  59. def grep(file)
  60. if RUBY_VERSION >= "1.9.1"
  61. str = File.open(file, "r:EUC-JP").read
  62. else
  63. str = File.open(file, "r").read
  64. end
  65. black_name = nil
  66. black_mark = nil
  67. black_id = nil
  68. white_name = nil
  69. white_mark = nil
  70. white_id = nil
  71. state = nil
  72. time = nil
  73. moves = 0
  74. str.each_line do |line|
  75. line.strip!
  76. case line
  77. when /^[\+\-]\d{4}\w{2}.*$/
  78. moves += 1
  79. when /^N\+(.*)$/
  80. black_name = $1.strip
  81. when /^N\-(.*)$/
  82. white_name = $1.strip
  83. when /^'summary:(.*)$/
  84. state, p1, p2 = $1.split(":").map {|a| a.strip}
  85. p1_name, p1_mark = p1.split(" ")
  86. p2_name, p2_mark = p2.split(" ")
  87. if p1_name == black_name
  88. black_name, black_mark = p1_name, p1_mark
  89. white_name, white_mark = p2_name, p2_mark
  90. elsif p2_name == black_name
  91. black_name, black_mark = p2_name, p2_mark
  92. white_name, white_mark = p1_name, p1_mark
  93. else
  94. raise "Never reach!: #{black_name}, #{p1}, #{p2} in #{file}"
  95. end
  96. when /^'\$END_TIME:(.*)$/
  97. time = $1.strip
  98. when /^'rating:(.*)$/
  99. black_id, white_id = $1.split(":").map {|a| a.strip}
  100. end # case
  101. end # do line
  102. if black_id && white_id && (black_id != white_id) &&
  103. black_mark && white_mark && state && time
  104. puts [time, state, black_mark, black_id, white_id, white_mark, file, moves].join("\t")
  105. $stdout.flush
  106. end
  107. end
  108. # Show Usage
  109. #
  110. def usage(io)
  111. io.puts <<EOF
  112. USAGE: #{$0} [options] DIR...
  113. DIR where CSA files are looked up recursively
  114. OPTOINS:
  115. --help, -h show this message
  116. EOF
  117. end
  118. # MAIN
  119. #
  120. def main
  121. $options = Hash::new
  122. parser = GetoptLong.new(
  123. ["--help", "-h", GetoptLong::NO_ARGUMENT])
  124. parser.quiet = true
  125. begin
  126. parser.each_option do |name, arg|
  127. name.sub!(/^--/, '')
  128. $options[name] = arg.dup
  129. end
  130. rescue
  131. usage($stderr)
  132. raise parser.error_message
  133. end
  134. if $options["help"]
  135. usage($stdout)
  136. exit 0
  137. end
  138. if ARGV.empty?
  139. while line = $stdin.gets do
  140. next unless %r!.*\.csa$! =~ line
  141. grep File.expand_path(line.strip)
  142. end
  143. else
  144. while dir = ARGV.shift do
  145. Dir.glob( File.join(dir, "**", "*.csa") ) {|f| grep(File.expand_path(f))}
  146. end
  147. end
  148. end
  149. if __FILE__ == $0
  150. main
  151. end
  152. # vim: tabstop=4 shiftwidth=4 expandtab
旧リポジトリブラウザで表示