OSDN Git Service

[shogi-server] Bump up the revision to 20201206
[shogi-server/shogi-server.git] / utils / statistics.rb
index b931317..a722049 100755 (executable)
@@ -112,33 +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")
 
-  # See games between 2008/03 to 2009/07
-  return if csa.start_time.nil? ||
-            csa.start_time <  Time.parse("2008/03/01") ||
-            csa.start_time >= Time.parse("2009/08/01")
-
-  # Want to see complete games
+  # Want to see completed games only
   $states.add csa.state
   return unless csa.state == "toryo"
 
-  # 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
@@ -148,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|
@@ -166,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
@@ -176,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