OSDN Git Service

The Buoy behaivor is changed.
[shogi-server/shogi-server.git] / shogi_server / game.rb
index 532c552..5f7cd0e 100644 (file)
@@ -19,6 +19,7 @@
 
 require 'shogi_server/league/floodgate'
 require 'shogi_server/game_result'
+require 'shogi_server/util'
 
 module ShogiServer # for a namespace
 
@@ -31,7 +32,7 @@ class Game
   @@mutex = Mutex.new
   @@time  = 0
   def initialize(game_name, player0, player1, board)
-    @monitors = Array::new
+    @monitors = Array::new # array of MonitorHandler*
     @game_name = game_name
     if (@game_name =~ /-(\d+)-(\d+)$/)
       @total_time = $1.to_i
@@ -71,8 +72,8 @@ class Game
                              @prepared_time.strftime("%Y"),
                              @prepared_time.strftime("%m"),
                              @prepared_time.strftime("%d"))
-    FileUtils.mkdir_p(log_dir_name) unless File.exist?(log_dir_name)
     @logfile = File.join(log_dir_name, @game_id + ".csa")
+    Mkdir.mkdir_for(@logfile)
 
     $league.games[@game_id] = self
 
@@ -100,18 +101,18 @@ class Game
     return player.status == "game" && @current_player == player
   end
 
-  def monitoron(monitor)
-    @monitors.delete(monitor)
-    @monitors.push(monitor)
+  def monitoron(monitor_handler)
+    monitoroff(monitor_handler)
+    @monitors.push(monitor_handler)
   end
 
-  def monitoroff(monitor)
-    @monitors.delete(monitor)
+  def monitoroff(monitor_handler)
+    @monitors.delete_if {|mon| mon == monitor_handler}
   end
 
   def each_monitor
-    @monitors.each do |monitor|
-      yield monitor
+    @monitors.each do |monitor_handler|
+      yield monitor_handler
     end
   end
 
@@ -174,7 +175,7 @@ class Game
   end
 
   # class Game
-  def handle_one_move(str, player)
+  def handle_one_move(str, player, end_time)
     unless turn?(player)
       return false if str == :timeout
 
@@ -186,7 +187,7 @@ class Game
     end
 
     finish_flag = true
-    @end_time = Time::new
+    @end_time = end_time
     t = [(@end_time - @start_time).floor, Least_Time_Per_Move].max
     
     move_status = nil
@@ -207,20 +208,20 @@ class Game
       if [:illegal, :uchifuzume, :oute_kaihimore].include?(move_status)
         @fh.printf("'ILLEGAL_MOVE(%s)\n", str)
       else
-        if [:normal, :outori, :sennichite, :oute_sennichite_sente_lose, :oute_sennichite_gote_lose].include?(move_status)
+        if :toryo != move_status
           # Thinking time includes network traffic
           @sente.write_safe(sprintf("%s,T%d\n", str, t))
           @gote.write_safe(sprintf("%s,T%d\n", str, t))
           @fh.printf("%s\nT%d\n", str, t)
           @last_move = sprintf("%s,T%d", str, t)
           @current_turn += 1
-        end
 
-        @monitors.each do |monitor|
-          monitor.write_safe(show.gsub(/^/, "##[MONITOR][#{@game_id}] "))
-          monitor.write_safe(sprintf("##[MONITOR][%s] +OK\n", @game_id))
-        end
-      end
+          @monitors.each do |monitor_handler|
+            monitor_handler.write_one_move(@game_id, self)
+          end
+        end # if
+        # if move_status is :toryo then a GameResult message will be sent to monitors   
+      end # if
     end
 
     @result = nil
@@ -258,7 +259,7 @@ class Game
     @result.process if @result
     finish() if finish_flag
     @current_player, @next_player = @next_player, @current_player
-    @start_time = Time::new
+    @start_time = Time.now
     return finish_flag
   end
 
@@ -276,7 +277,7 @@ class Game
     @gote.write_safe(sprintf("START:%s\n", @game_id))
     @sente.mytime = @total_time
     @gote.mytime = @total_time
-    @start_time = Time::new
+    @start_time = Time.now
   end
 
   def propose
@@ -288,7 +289,7 @@ class Game
     @sente.write_safe(propose_message("+"))
     @gote.write_safe(propose_message("-"))
 
-    now = Time::new.strftime("%Y/%m/%d %H:%M:%S")
+    now = Time.now.strftime("%Y/%m/%d %H:%M:%S")
     @fh.puts("$START_TIME:#{now}")
     @fh.print <<EOM
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
@@ -369,11 +370,14 @@ Byoyomi:#{@byoyomi}
 Least_Time_Per_Move:#{Least_Time_Per_Move}
 END Time
 BEGIN Position
-#{@board.to_s.chomp}
+#{Board::INITIAL_POSITION}
+#{@board.initial_moves.collect {|m| m + ",T1"}.join("\n")}
 END Position
 END Game_Summary
 EOM
-    return str
+    # An empty @board.initial_moves causes an empty line, which should be
+    # eliminated.
+    return str.gsub("\n\n", "\n")
   end
 
   def prepared_expire?
@@ -387,7 +391,7 @@ EOM
   private
   
   def issue_current_time
-    time = Time::new.strftime("%Y%m%d%H%M%S").to_i
+    time = Time.now.strftime("%Y%m%d%H%M%S").to_i
     @@mutex.synchronize do
       while time <= @@time do
         time += 1