1 require 'shogi_server/util'
12 # ex. "floodgate-900-0"
15 return /^floodgate\-\d+\-\d+$/.match(str) ? true : false
18 def history_file_path(gamename)
19 return nil unless game_name?(gamename)
20 filename = "floodgate_history_%s.yaml" % [gamename.gsub("floodgate-", "").gsub("-","_")]
21 file = File.join($topdir, filename)
22 return Pathname.new(file)
26 # @next_time is updated if and only if charge() was called
28 attr_reader :next_time
29 attr_reader :league, :game_name
32 def initialize(league, hash={})
34 @next_time = hash[:next_time] || nil
35 @game_name = hash[:game_name] || "floodgate-900-0"
36 # Options will be updated by NextTimeGenerator and then passed to a
39 @options[:pairing_factory] = hash[:pairing_factory] || "default_factory"
40 @options[:sacrifice] = hash[:sacrifice] || "gps500+e293220e3f8a3e59f79f6b0efffaa931"
41 charge if @next_time.nil?
45 return Regexp.new(@game_name).match(str) ? true : false
49 return @options[:pairing_factory]
53 return @options[:sacrifice]
57 ntg = NextTimeGenerator.factory(@game_name)
59 @next_time = ntg.call(Time.now)
60 @options[:pairing_factory] = ntg.pairing_factory
61 @options[:sacrifice] = ntg.sacrifice
68 log_message("Starting Floodgate games...: %s, %s" % [@game_name, @options])
69 players = @league.find_all_players do |pl|
70 pl.status == "game_waiting" &&
71 game_name?(pl.game_name) &&
74 logics = Pairing.send(@options[:pairing_factory], @options)
75 Pairing.match(players, logics)
80 class NextTimeGenerator
82 def factory(game_name)
84 conf_file_name = File.join($topdir, "#{game_name}.conf")
87 ret = NextTimeGenerator_Debug.new
88 elsif File.exists?(conf_file_name)
89 lines = IO.readlines(conf_file_name)
90 ret = NextTimeGeneratorConfig.new(lines)
91 elsif game_name == "floodgate-900-0"
92 ret = NextTimeGenerator_Floodgate_900_0.new
93 elsif game_name == "floodgate-3600-0"
94 ret = NextTimeGenerator_Floodgate_3600_0.new
101 class AbstructNextTimeGenerator
103 attr_reader :pairing_factory
104 attr_reader :sacrifice
109 @pairing_factory = "default_factory"
110 @sacrifice = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
114 # Schedule the next time from configuration files.
117 # # This is a comment line
118 # set <parameter_name> <value>
122 # DoW := "Sun" | "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" |
123 # "Sunday" | "Monday" | "Tuesday" | "Wednesday" | "Thursday" |
124 # "Friday" | "Saturday"
135 # Specifies a factory function name generating a pairing
136 # method which will be used in a specific Floodgate game.
137 # ex. set pairing_factory floodgate_zyunisen
139 # Specifies a sacrificed player.
140 # ex. set sacrifice gps500+e293220e3f8a3e59f79f6b0efffaa931
142 class NextTimeGeneratorConfig < AbstructNextTimeGenerator
145 # Read configuration contents.
147 def initialize(lines)
152 def call(now=Time.now)
153 if now.kind_of?(Time)
154 now = ::ShogiServer::time2datetime(now)
158 # now.cwday 1(Monday)-7
159 @lines.each do |line|
161 when %r!^\s*set\s+pairing_factory\s+(\w+)!
162 @pairing_factory = $1.chomp
163 when %r!^\s*set\s+sacrifice\s+(.*)!
164 @sacrifice = $1.chomp
165 when %r!^\s*(\w+)\s+(\d{1,2}):(\d{1,2})!
166 dow, hour, minute = $1, $2.to_i, $3.to_i
167 dow_index = ::ShogiServer::parse_dow(dow)
168 next if dow_index.nil?
169 next unless (0..23).include?(hour)
170 next unless (0..59).include?(minute)
171 time = DateTime::commercial(now.cwyear, now.cweek, dow_index, hour, minute) rescue next
172 time += 7 if time <= now
179 log_warning("Floodgate: Unsupported syntax in a next time generator config file: %s" % [line])
182 candidates.map! {|dt| ::ShogiServer::datetime2time(dt)}
183 return candidates.empty? ? nil : candidates.min
187 # Schedule the next time for floodgate-900-0: each 30 minutes
189 class NextTimeGenerator_Floodgate_900_0 < AbstructNextTimeGenerator
199 return Time.mktime(now.year, now.month, now.day, now.hour, 30)
201 return Time.mktime(now.year, now.month, now.day, now.hour) + 3600
206 # Schedule the next time for floodgate-3600-0: each 2 hours (odd hour)
208 class NextTimeGenerator_Floodgate_3600_0 < AbstructNextTimeGenerator
217 return Time.mktime(now.year, now.month, now.day, now.hour) + ((now.hour%2)+1)*3600
221 # Schedule the next time for debug: each 30 seconds.
223 class NextTimeGenerator_Debug < AbstructNextTimeGenerator
233 return Time.mktime(now.year, now.month, now.day, now.hour, now.min, 30)
235 return Time.mktime(now.year, now.month, now.day, now.hour, now.min) + 60
246 def factory(pathname)
247 unless ShogiServer::is_writable_file?(pathname.to_s)
248 log_error("Failed to write a history file: %s" % [pathname])
251 history = History.new pathname
259 # Initialize this instance.
260 # @param file_path_name a Pathname object for this storage
262 def initialize(file_path_name)
265 @file = file_path_name
268 # Return a hash describing the game_result
270 # :black: Black's player id
271 # :white: White's player id
272 # :winner: Winner's player id or nil for the game without a winner
273 # :loser: Loser's player id or nil for the game without a loser
275 def make_record(game_result)
277 hash[:game_id] = game_result.game.game_id
278 hash[:black] = game_result.black.player_id
279 hash[:white] = game_result.white.player_id
282 hash[:winner] = game_result.winner.player_id
283 hash[:loser] = game_result.loser.player_id
292 return unless @file.exist?
295 @records = YAML.load_file(@file)
296 unless @records && @records.instance_of?(Array)
297 $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
301 $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
308 @file.open("w") do |f|
309 f << YAML.dump(@records)
316 def update(game_result)
317 record = make_record(game_result)
318 @@mutex.synchronize do
321 while @records.size > @max_records
328 def last_win?(player_id)
329 rc = last_valid_game(player_id)
330 return false unless rc
331 return rc[:winner] == player_id
334 def last_lose?(player_id)
335 rc = last_valid_game(player_id)
336 return false unless rc
337 return rc[:loser] == player_id
340 def last_opponent(player_id)
341 rc = last_valid_game(player_id)
343 if rc[:black] == player_id
345 elsif rc[:white] == player_id
352 def last_valid_game(player_id)
354 @@mutex.synchronize do
355 records = @records.reverse
357 rc = records.find do |rc|
360 (rc[:black] == player_id || rc[:white] == player_id)
365 def win_games(player_id)
367 @@mutex.synchronize do
368 records = @records.reverse
370 rc = records.find_all do |rc|
371 rc[:winner] == player_id && rc[:loser]
376 def loss_games(player_id)
378 @@mutex.synchronize do
379 records = @records.reverse
381 rc = records.find_all do |rc|
382 rc[:winner] && rc[:loser] == player_id
389 end # class Floodgate
393 end # module ShogiServer