10 # ex. "floodgate-900-0"
13 return /^floodgate\-\d+\-\d+$/.match(str) ? true : false
16 def history_file_path(gamename)
17 return nil unless game_name?(gamename)
18 filename = "floodgate_history_%s.yaml" % [gamename.gsub("floodgate-", "").gsub("-","_")]
19 file = File.join($topdir, filename)
20 return Pathname.new(file)
24 attr_reader :next_time, :league, :game_name
26 def initialize(league, hash={})
28 @next_time = hash[:next_time] || nil
29 @game_name = hash[:game_name] || "floodgate-900-0"
34 return Regexp.new(@game_name).match(str) ? true : false
38 ntg = NextTimeGenerator.factory(@game_name)
40 @next_time = ntg.call(Time.now)
47 players = @league.find_all_players do |pl|
48 pl.status == "game_waiting" &&
49 game_name?(pl.game_name) &&
52 Pairing.match(players)
57 class NextTimeGenerator
59 def factory(game_name)
62 ret = NextTimeGenerator_Debug.new
63 elsif game_name == "floodgate-900-0"
64 ret = NextTimeGenerator_Floodgate_900_0.new
65 elsif game_name == "floodgate-3600-0"
66 ret = NextTimeGenerator_Floodgate_3600_0.new
73 class NextTimeGenerator_Floodgate_900_0
77 return Time.mktime(now.year, now.month, now.day, now.hour, 30)
79 return Time.mktime(now.year, now.month, now.day, now.hour) + 3600
84 class NextTimeGenerator_Floodgate_3600_0
86 # each 2 hours (odd hour)
87 return Time.mktime(now.year, now.month, now.day, now.hour) + ((now.hour%2)+1)*3600
91 class NextTimeGenerator_Debug
93 # for test, each 30 seconds
95 return Time.mktime(now.year, now.month, now.day, now.hour, now.min, 30)
97 return Time.mktime(now.year, now.month, now.day, now.hour, now.min) + 60
108 def factory(pathname)
109 unless ShogiServer::is_writable_file?(pathname.to_s)
110 log_error("Failed to write a history file: %s" % [pathname])
113 history = History.new pathname
121 # Initialize this instance.
122 # @param file_path_name a Pathname object for this storage
124 def initialize(file_path_name)
127 @file = file_path_name
130 # Return a hash describing the game_result
132 # :black: Black's player id
133 # :white: White's player id
134 # :winner: Winner's player id or nil for the game without a winner
135 # :loser: Loser's player id or nil for the game without a loser
137 def make_record(game_result)
139 hash[:game_id] = game_result.game.game_id
140 hash[:black] = game_result.black.player_id
141 hash[:white] = game_result.white.player_id
144 hash[:winner] = game_result.winner.player_id
145 hash[:loser] = game_result.loser.player_id
154 return unless @file.exist?
156 @records = YAML.load_file(@file)
157 unless @records && @records.instance_of?(Array)
158 $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
165 @file.open("w") do |f|
166 f << YAML.dump(@records)
173 def update(game_result)
174 record = make_record(game_result)
175 @@mutex.synchronize do
178 while @records.size > @max_records
185 def last_win?(player_id)
186 rc = last_valid_game(player_id)
187 return false unless rc
188 return rc[:winner] == player_id
191 def last_lose?(player_id)
192 rc = last_valid_game(player_id)
193 return false unless rc
194 return rc[:loser] == player_id
197 def last_valid_game(player_id)
199 @@mutex.synchronize do
200 records = @records.reverse
202 rc = records.find do |rc|
205 (rc[:black] == player_id || rc[:white] == player_id)
212 end # class Floodgate
216 end # module ShogiServer