OSDN Git Service

[shogi-server] - Implemented conversion of move representation between CSA format...
[shogi-server/shogi-server.git] / shogi_server / game.rb
index afb5d08..ec9b702 100644 (file)
@@ -1,7 +1,7 @@
 ## $Id$
 
 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
-## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
+## Copyright (C) 2007-2012 Daigo Moriwaki (daigo at debian dot org)
 ##
 ## This program is free software; you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@
 
 require 'shogi_server/league/floodgate'
 require 'shogi_server/game_result'
+require 'shogi_server/time_clock'
 require 'shogi_server/util'
 
 module ShogiServer # for a namespace
@@ -69,6 +70,8 @@ class Game
     if (@game_name =~ /-(\d+)-(\d+)$/)
       @total_time = $1.to_i
       @byoyomi = $2.to_i
+
+      @time_clock = TimeClock::factory(Least_Time_Per_Move, @game_name)
     end
 
     if (player0.sente)
@@ -119,6 +122,7 @@ class Game
     $league.games[@game_id] = self
 
     log_message(sprintf("game created %s", @game_id))
+    log_message("    " + @time_clock.to_s)
 
     @start_time = nil
     @fh = open(@logfile, "w")
@@ -127,7 +131,7 @@ class Game
 
     propose
   end
-  attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :game_id, :board, :current_player, :next_player, :fh, :monitors
+  attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :game_id, :board, :current_player, :next_player, :fh, :monitors, :time_clock
   attr_accessor :last_move, :current_turn
   attr_reader   :result, :prepared_time
 
@@ -227,22 +231,16 @@ class Game
       return nil
     end
 
-    finish_flag = true
     @end_time = end_time
-    t = [(@end_time - @start_time).floor, Least_Time_Per_Move].max
-    
+    finish_flag = true
     move_status = nil
-    if ((@current_player.mytime - t <= -@byoyomi) && 
-        ((@total_time > 0) || (@byoyomi > 0)))
+
+    if (@time_clock.timeout?(@current_player, @start_time, @end_time))
       status = :timeout
     elsif (str == :timeout)
       return false            # time isn't expired. players aren't swapped. continue game
     else
-      @current_player.mytime -= t
-      if (@current_player.mytime < 0)
-        @current_player.mytime = 0
-      end
-
+      t = @time_clock.process_time(@current_player, @start_time, @end_time)
       move_status = @board.handle_one_move(str, @sente == @current_player)
       # log_debug("move_status: %s for %s's %s" % [move_status, @sente == @current_player ? "BLACK" : "WHITE", str])
 
@@ -366,7 +364,7 @@ Name-:#{@gote.name}
 Rematch_On_Draw:NO
 To_Move:+
 BEGIN Time
-Time_Unit:1sec
+Time_Unit:#{@time_clock.time_unit}
 Total_Time:#{@total_time}
 Byoyomi:#{@byoyomi}
 Least_Time_Per_Move:#{Least_Time_Per_Move}
@@ -400,7 +398,7 @@ Your_Turn:#{sg_flag}
 Rematch_On_Draw:NO
 To_Move:#{@board.teban ? "+" : "-"}
 BEGIN Time
-Time_Unit:1sec
+Time_Unit:#{@time_clock.time_unit}
 Total_Time:#{@total_time}
 Byoyomi:#{@byoyomi}
 Least_Time_Per_Move:#{Least_Time_Per_Move}