OSDN Git Service

Added some module functions related to DateTime or Time.
[shogi-server/shogi-server.git] / shogi_server / game.rb
index 4aeaf3a..e920105 100644 (file)
@@ -31,7 +31,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
@@ -100,18 +100,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
 
@@ -138,15 +138,20 @@ class Game
     end
     
     if (@current_player == killer)
-      result = GameResultAbnormalWin.new(self, @next_player, @current_player)
-      result.process
+      @result = GameResultAbnormalWin.new(self, @next_player, @current_player)
+      @result.process
       finish
     end
   end
 
   def finish
     log_message(sprintf("game finished %s", @game_id))
-    @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))    
+
+    # In a case where a player in agree_waiting or start_waiting status is
+    # rejected, a GameResult object is not yet instanciated.
+    # See test/TC_before_agree.rb.
+    end_time = @result ? @result.end_time : Time.now
+    @fh.printf("'$END_TIME:%s\n", end_time.strftime("%Y/%m/%d %H:%M:%S"))    
     @fh.close
 
     @sente.game = nil
@@ -169,7 +174,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
 
@@ -181,7 +186,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
@@ -202,55 +207,55 @@ 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
+    @result = nil
     if (@next_player.status != "game") # rival is logout or disconnected
-      result = GameResultAbnormalWin.new(self, @current_player, @next_player)
+      @result = GameResultAbnormalWin.new(self, @current_player, @next_player)
     elsif (status == :timeout)
       # current_player losed
-      result = GameResultTimeoutWin.new(self, @next_player, @current_player)
+      @result = GameResultTimeoutWin.new(self, @next_player, @current_player)
     elsif (move_status == :illegal)
-      result = GameResultIllegalMoveWin.new(self, @next_player, @current_player)
+      @result = GameResultIllegalMoveWin.new(self, @next_player, @current_player)
     elsif (move_status == :kachi_win)
-      result = GameResultKachiWin.new(self, @current_player, @next_player)
+      @result = GameResultKachiWin.new(self, @current_player, @next_player)
     elsif (move_status == :kachi_lose)
-      result = GameResultIllegalKachiWin.new(self, @next_player, @current_player)
+      @result = GameResultIllegalKachiWin.new(self, @next_player, @current_player)
     elsif (move_status == :toryo)
-      result = GameResultToryoWin.new(self, @next_player, @current_player)
+      @result = GameResultToryoWin.new(self, @next_player, @current_player)
     elsif (move_status == :outori)
       # The current player captures the next player's king
-      result = GameResultOutoriWin.new(self, @current_player, @next_player)
+      @result = GameResultOutoriWin.new(self, @current_player, @next_player)
     elsif (move_status == :oute_sennichite_sente_lose)
-      result = GameResultOuteSennichiteWin.new(self, @gote, @sente) # Sente is checking
+      @result = GameResultOuteSennichiteWin.new(self, @gote, @sente) # Sente is checking
     elsif (move_status == :oute_sennichite_gote_lose)
-      result = GameResultOuteSennichiteWin.new(self, @sente, @gote) # Gote is checking
+      @result = GameResultOuteSennichiteWin.new(self, @sente, @gote) # Gote is checking
     elsif (move_status == :sennichite)
-      result = GameResultSennichiteDraw.new(self, @current_player, @next_player)
+      @result = GameResultSennichiteDraw.new(self, @current_player, @next_player)
     elsif (move_status == :uchifuzume)
       # the current player losed
-      result = GameResultUchifuzumeWin.new(self, @next_player, @current_player)
+      @result = GameResultUchifuzumeWin.new(self, @next_player, @current_player)
     elsif (move_status == :oute_kaihimore)
       # the current player losed
-      result = GameResultOuteKaihiMoreWin.new(self, @next_player, @current_player)
+      @result = GameResultOuteKaihiMoreWin.new(self, @next_player, @current_player)
     else
       finish_flag = false
     end
-    result.process if result
+    @result.process if @result
     finish() if finish_flag
     @current_player, @next_player = @next_player, @current_player
     @start_time = Time::new