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
30 attr_reader :pairing_factory
32 def initialize(league, hash={})
34 @next_time = hash[:next_time] || nil
35 @game_name = hash[:game_name] || "floodgate-900-0"
36 @pairing_factory = "default_factory" # will be updated by NextTimeGenerator
37 charge if @next_time.nil?
41 return Regexp.new(@game_name).match(str) ? true : false
45 ntg = NextTimeGenerator.factory(@game_name)
46 @pairing_factory = ntg.pairing_factory
48 @next_time = ntg.call(Time.now)
55 log_message("Starting Floodgate games...: %s, %s" % [@game_name, @pairing_factory])
56 players = @league.find_all_players do |pl|
57 pl.status == "game_waiting" &&
58 game_name?(pl.game_name) &&
61 logics = Pairing.send(@pairing_factory)
62 Pairing.match(players, logics)
67 class NextTimeGenerator
69 def factory(game_name)
71 conf_file_name = File.join($topdir, "#{game_name}.conf")
74 ret = NextTimeGenerator_Debug.new
75 elsif File.exists?(conf_file_name)
76 lines = IO.readlines(conf_file_name)
77 ret = NextTimeGeneratorConfig.new(lines)
78 elsif game_name == "floodgate-900-0"
79 ret = NextTimeGenerator_Floodgate_900_0.new
80 elsif game_name == "floodgate-3600-0"
81 ret = NextTimeGenerator_Floodgate_3600_0.new
88 class AbstructNextTimeGenerator
90 attr_reader :pairing_factory
95 @pairing_factory = "default_factory"
99 # Schedule the next time from configuration files.
102 # # This is a comment line
103 # set <parameter_name> <value>
107 # DoW := "Sun" | "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" |
108 # "Sunday" | "Monday" | "Tuesday" | "Wednesday" | "Thursday" |
109 # "Friday" | "Saturday"
120 # Specifies a factory function name generating a pairing
121 # method which will be used in a specific Floodgate game.
122 # ex. floodgate_zyunisen
124 class NextTimeGeneratorConfig < AbstructNextTimeGenerator
127 # Read configuration contents.
129 def initialize(lines)
134 def call(now=Time.now)
135 if now.kind_of?(Time)
136 now = ::ShogiServer::time2datetime(now)
140 # now.cwday 1(Monday)-7
141 @lines.each do |line|
143 when %r!^\s*set\s+pairing_factory\s+(\w+)!
144 @pairing_factory = $1
145 when %r!^\s*(\w+)\s+(\d{1,2}):(\d{1,2})!
146 dow, hour, minute = $1, $2.to_i, $3.to_i
147 dow_index = ::ShogiServer::parse_dow(dow)
148 next if dow_index.nil?
149 next unless (0..23).include?(hour)
150 next unless (0..59).include?(minute)
151 time = DateTime::commercial(now.cwyear, now.cweek, dow_index, hour, minute) rescue next
152 time += 7 if time <= now
156 candidates.map! {|dt| ::ShogiServer::datetime2time(dt)}
157 return candidates.empty? ? nil : candidates.min
161 # Schedule the next time for floodgate-900-0: each 30 minutes
163 class NextTimeGenerator_Floodgate_900_0 < AbstructNextTimeGenerator
173 return Time.mktime(now.year, now.month, now.day, now.hour, 30)
175 return Time.mktime(now.year, now.month, now.day, now.hour) + 3600
180 # Schedule the next time for floodgate-3600-0: each 2 hours (odd hour)
182 class NextTimeGenerator_Floodgate_3600_0 < AbstructNextTimeGenerator
191 return Time.mktime(now.year, now.month, now.day, now.hour) + ((now.hour%2)+1)*3600
195 # Schedule the next time for debug: each 30 seconds.
197 class NextTimeGenerator_Debug < AbstructNextTimeGenerator
207 return Time.mktime(now.year, now.month, now.day, now.hour, now.min, 30)
209 return Time.mktime(now.year, now.month, now.day, now.hour, now.min) + 60
220 def factory(pathname)
221 unless ShogiServer::is_writable_file?(pathname.to_s)
222 log_error("Failed to write a history file: %s" % [pathname])
225 history = History.new pathname
233 # Initialize this instance.
234 # @param file_path_name a Pathname object for this storage
236 def initialize(file_path_name)
239 @file = file_path_name
242 # Return a hash describing the game_result
244 # :black: Black's player id
245 # :white: White's player id
246 # :winner: Winner's player id or nil for the game without a winner
247 # :loser: Loser's player id or nil for the game without a loser
249 def make_record(game_result)
251 hash[:game_id] = game_result.game.game_id
252 hash[:black] = game_result.black.player_id
253 hash[:white] = game_result.white.player_id
256 hash[:winner] = game_result.winner.player_id
257 hash[:loser] = game_result.loser.player_id
266 return unless @file.exist?
269 @records = YAML.load_file(@file)
270 unless @records && @records.instance_of?(Array)
271 $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
275 $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
282 @file.open("w") do |f|
283 f << YAML.dump(@records)
290 def update(game_result)
291 record = make_record(game_result)
292 @@mutex.synchronize do
295 while @records.size > @max_records
302 def last_win?(player_id)
303 rc = last_valid_game(player_id)
304 return false unless rc
305 return rc[:winner] == player_id
308 def last_lose?(player_id)
309 rc = last_valid_game(player_id)
310 return false unless rc
311 return rc[:loser] == player_id
314 def last_opponent(player_id)
315 rc = last_valid_game(player_id)
317 if rc[:black] == player_id
319 elsif rc[:white] == player_id
326 def last_valid_game(player_id)
328 @@mutex.synchronize do
329 records = @records.reverse
331 rc = records.find do |rc|
334 (rc[:black] == player_id || rc[:white] == player_id)
339 def win_games(player_id)
341 @@mutex.synchronize do
342 records = @records.reverse
344 rc = records.find_all do |rc|
345 rc[:winner] == player_id && rc[:loser]
350 def loss_games(player_id)
352 @@mutex.synchronize do
353 records = @records.reverse
355 rc = records.find_all do |rc|
356 rc[:winner] && rc[:loser] == player_id
363 end # class Floodgate
367 end # module ShogiServer