OSDN Git Service

Corrected a merge miss in changelog caused by the previous merge
[shogi-server/shogi-server.git] / shogi_server / time_clock.rb
1 ## $Id$
2
3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
5 ##
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.
10 ##
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.
15 ##
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
19
20 module ShogiServer # for a namespace
21
22 # Abstract class to caclulate thinking time.
23 #
24 class TimeClock
25
26   def TimeClock.factory(least_time_per_move, game_name)
27     total_time_str = nil
28     byoyomi_str = nil
29     if (game_name =~ /-(\d+)-(\d+)$/)
30       total_time_str = $1
31       byoyomi_str    = $2
32     end
33     total_time = total_time_str.to_i
34     byoyomi    = byoyomi_str.to_i
35  
36     if (byoyomi_str == "060")
37       @time_clock = StopWatchClock.new(least_time_per_move, total_time, byoyomi)
38     else
39       @time_clock = ChessClock.new(least_time_per_move, total_time, byoyomi)
40     end
41   end
42
43   def initialize(least_time_per_move, total_time, byoyomi)
44     @least_time_per_move = least_time_per_move
45     @total_time = total_time
46     @byoyomi     = byoyomi
47   end
48
49   # Returns thinking time duration
50   #
51   def time_duration(start_time, end_time)
52     # implement this
53     return 9999999
54   end
55
56   # Returns what "Time_Unit:" in CSA protocol should provide.
57   #
58   def time_unit
59     return "1sec"
60   end
61
62   # If thinking time runs out, returns true; false otherwise.
63   #
64   def timeout?(player, start_time, end_time)
65     # implement this
66     return true
67   end
68
69   # Updates a player's remaining time and returns thinking time.
70   #
71   def process_time(player, start_time, end_time)
72     t = time_duration(start_time, end_time)
73     
74     player.mytime -= t
75     if (player.mytime < 0)
76       player.mytime = 0
77     end
78
79     return t
80   end
81 end
82
83 # Calculates thinking time with chess clock.
84 #
85 class ChessClock < TimeClock
86   def initialize(least_time_per_move, total_time, byoyomi)
87     super
88   end
89
90   def time_duration(start_time, end_time)
91     return [(end_time - start_time).floor, @least_time_per_move].max
92   end
93
94   def timeout?(player, start_time, end_time)
95     t = time_duration(start_time, end_time)
96
97     if ((player.mytime - t <= -@byoyomi) && 
98         ((@total_time > 0) || (@byoyomi > 0)))
99       return true
100     else
101       return false
102     end
103   end
104
105   def to_s
106     return "ChessClock: LeastTimePerMove %d; TotalTime %d; Byoyomi %d" % [@least_time_per_move, @total_time, @byoyomi]
107   end
108 end
109
110 class StopWatchClock < TimeClock
111   def initialize(least_time_per_move, total_time, byoyomi)
112     super
113   end
114
115   def time_unit
116     return "1min"
117   end
118
119   def time_duration(start_time, end_time)
120     t = [(end_time - start_time).floor, @least_time_per_move].max
121     return (t / @byoyomi) * @byoyomi
122   end
123
124   def timeout?(player, start_time, end_time)
125     t = time_duration(start_time, end_time)
126
127     if (player.mytime <= t)
128       return true
129     else
130       return false
131     end
132   end
133
134   def to_s
135     return "StopWatchClock: LeastTimePerMove %d; TotalTime %d; Byoyomi %d" % [@least_time_per_move, @total_time, @byoyomi]
136   end
137 end
138
139 end