3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 module ShogiServer # for a namespace
22 # Abstract class to caclulate thinking time.
26 def TimeClock.factory(least_time_per_move, game_name)
29 if (game_name =~ /-(\d+)-(\d+)$/)
33 total_time = total_time_str.to_i
34 byoyomi = byoyomi_str.to_i
36 if (byoyomi_str == "060")
37 @time_clock = StopWatchClock.new(least_time_per_move, total_time, byoyomi)
39 if least_time_per_move == 0
40 @time_clock = ChessClockWithLeastZero.new(least_time_per_move, total_time, byoyomi)
42 @time_clock = ChessClock.new(least_time_per_move, total_time, byoyomi)
47 def initialize(least_time_per_move, total_time, byoyomi)
48 @least_time_per_move = least_time_per_move
49 @total_time = total_time
53 # Returns thinking time duration
55 def time_duration(mytime, start_time, end_time)
60 # Returns what "Time_Unit:" in CSA protocol should provide.
66 # If thinking time runs out, returns true; false otherwise.
68 def timeout?(player, start_time, end_time)
73 # Updates a player's remaining time and returns thinking time.
75 def process_time(player, start_time, end_time)
76 t = time_duration(player.mytime, start_time, end_time)
79 if (player.mytime < 0)
87 # Calculates thinking time with chess clock.
89 class ChessClock < TimeClock
90 def initialize(least_time_per_move, total_time, byoyomi)
94 def time_duration(mytime, start_time, end_time)
95 return [(end_time - start_time).floor, @least_time_per_move].max
98 def timeout?(player, start_time, end_time)
99 t = time_duration(player.mytime, start_time, end_time)
101 if ((player.mytime - t <= -@byoyomi) &&
102 ((@total_time > 0) || (@byoyomi > 0)))
110 return "ChessClock: LeastTimePerMove %d; TotalTime %d; Byoyomi %d" % [@least_time_per_move, @total_time, @byoyomi]
114 # Calculates thinking time with chess clock, truncating decimal seconds for
115 # thinking time. This is a new rule that CSA introduced in November 2014.
117 # least_time_per_move should be 0.
118 # byoyomi should be more than 0.
120 class ChessClockWithLeastZero < ChessClock
121 def initialize(least_time_per_move, total_time, byoyomi)
122 if least_time_per_move != 0
123 raise ArgumentError, "least_time_per_move #{least_time_per_move} should be 0."
128 def time_duration(mytime, start_time, end_time)
129 t = end_time - start_time
134 return "ChessClockWithLeastZero: LeastTimePerMove %d; TotalTime %d; Byoyomi %d" % [@least_time_per_move, @total_time, @byoyomi]
138 class StopWatchClock < TimeClock
139 def initialize(least_time_per_move, total_time, byoyomi)
147 def time_duration(mytime, start_time, end_time)
148 t = [(end_time - start_time).floor, @least_time_per_move].max
149 return (t / @byoyomi) * @byoyomi
152 def timeout?(player, start_time, end_time)
153 t = time_duration(player.mytime, start_time, end_time)
155 if (player.mytime <= t)
163 return "StopWatchClock: LeastTimePerMove %d; TotalTime %d; Byoyomi %d" % [@least_time_per_move, @total_time, @byoyomi]