OSDN Git Service

[shogi-server] Bump up the revision to 20201206
[shogi-server/shogi-server.git] / utils / statistics.rb
index f9ab9d8..a722049 100755 (executable)
@@ -1,11 +1,16 @@
-#!/usr/bin/ruby1.9.1
-# This program filters CSA files. For example, if you want only CSA files
-# played by GPS vs Bonanza,
-#   $ ./csa-filter.rb --players gps-l,bonanza some_dir
-# you will see such files under the some_dir directory.
+#!/usr/bin/ruby
+# This program shows statistics of CSA kifu files like following: 
+#   - Monthly #games and #players
+#   - Game results
+#   - Time of each move
+#   - Time of each game
+#   - Moves of each game
+#
+# Sample command line:
+#   $ ./statistics.rb /dev/shm/floodgate
 #
 # Author::    Daigo Moriwaki <daigo at debian dot org>
-# Copyright:: Copyright (C) 2006-2008  Daigo Moriwaki <daigo at debian dot org>
+# Copyright:: Copyright (C) 2009-2012 Daigo Moriwaki <daigo at debian dot org>
 #
 # $Id$
 #
@@ -107,32 +112,24 @@ $states   = State.new
 
 def do_file(file)
   $OPT_REPEAT -= 1 if $OPT_REPEAT > 0
-  csa = CsaFileReader.new(file)
+  csa = CsaFileReader.new(file, "EUC-JP")
 
-  # Want to see complete games
+  # Want to see completed games only
   $states.add csa.state
   return unless csa.state == "toryo"
 
-  # See games between 2008/03 to 2009/07
-  return if csa.start_time <  Time.parse("2008/03/01") ||
-            csa.start_time >= Time.parse("2009/08/01")
-
-  # Process monthly
+  # 1. Process monthly
   $monthly.add(csa)
 
-  # Process gametime
+  # 2. Process gametime
   duration = (csa.end_time - csa.start_time).to_i
-  if duration > 2200
-    $stderr.puts "Too long game: #{file}"
-    return
-  end
   $gametime.add duration.to_i
 
-  # Process movetime
+  # 3. Process movetime
   values = csa.movetimes
   $movetime.add values
 
-  #Process moves
+  # 4. Process moves
   $moves.add values.size
 
 rescue => ex
@@ -142,15 +139,30 @@ end
 
 if $0 == __FILE__
   def usage
-    puts "Usage: #{$0} [OPTIONS] dir [...]"
-    puts "Options:"
+puts <<-EOF
+SYNOPSIS:
+    #{$0} [OPTION]... file or dir [...]
+
+DESCRIPTION:
+    It calculates game statistics, reading CSA record files.
+    If an argument is a directory, it attempts to find files in its
+    subdirectories recursively.
+
+    --filter regexp
+        only files that are matched with a regexp are processed (default: no filter)
+
+    --repeat n
+        at most n files are processed (default: unlimitted)
+EOF
     exit 1
   end
 
   usage if ARGV.empty?
 
   parser = GetoptLong.new(
-             ['--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT]
+             ['--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT],
+             ['--filter', GetoptLong::REQUIRED_ARGUMENT],
+             ['--h', '-h', GetoptLong::NO_ARGUMENT]
            )
   begin
     parser.each_option do |name, arg|
@@ -160,6 +172,10 @@ if $0 == __FILE__
     usage
   end
 
+  if $OPT_H
+    usage
+  end
+
   $OPT_REPEAT = $OPT_REPEAT.to_i
   if $OPT_REPEAT == 0
     $OPT_REPEAT = -1
@@ -170,11 +186,15 @@ if $0 == __FILE__
     if FileTest.directory?(cmd)
       Dir.glob(File.join(cmd, "**", "*.csa")).each do |file|
         break if $OPT_REPEAT == 0
-        do_file(file)
+        if $OPT_FILTER.nil? || /#{$OPT_FILTER}/ =~ file
+          do_file(file)
+        end
       end
     elsif FileTest.file?(cmd)
       break if $OPT_REPEAT == 0
-      do_file(cmd)
+      if $OPT_FILTER.nil? || /#{$OPT_FILTER}/ =~ cmd
+        do_file(cmd)
+      end
     else
       throw "Unknown file or directory: #{cmd}"
     end