13 return /^floodgate\-\d+\-\d+$/.match(str) ? true : false
17 attr_reader :next_time, :league
19 def initialize(league, next_time=nil)
21 @next_time = next_time
30 @next_time = Time.mktime(now.year, now.month, now.day, now.hour, 30)
32 @next_time = Time.mktime(now.year, now.month, now.day, now.hour) + 3600
35 # for test, each 30 seconds
37 @next_time = Time.mktime(now.year, now.month, now.day, now.hour, now.min, 30)
39 @next_time = Time.mktime(now.year, now.month, now.day, now.hour, now.min) + 60
45 players = @league.find_all_players do |pl|
46 pl.status == "game_waiting" &&
47 Floodgate.game_name?(pl.game_name) &&
50 Pairing.match(players)
61 file = Pathname.new $options["floodgate-history"]
62 history = History.new file
70 # Initialize this instance.
71 # @param file_path_name a Pathname object for this storage
73 def initialize(file_path_name)
76 @file = file_path_name
79 # Return a hash describing the game_result
81 # :black: Black's player id
82 # :white: White's player id
83 # :winner: Winner's player id or nil for the game without a winner
84 # :loser: Loser's player id or nil for the game without a loser
86 def make_record(game_result)
88 hash[:game_id] = game_result.game.game_id
89 hash[:black] = game_result.black.player_id
90 hash[:white] = game_result.white.player_id
93 hash[:winner] = game_result.winner.player_id
94 hash[:loser] = game_result.loser.player_id
103 return unless @file.exist?
105 @records = YAML.load_file(@file)
106 unless @records && @records.instance_of?(Array)
107 $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
114 @file.open("w") do |f|
115 f << YAML.dump(@records)
122 def update(game_result)
123 record = make_record(game_result)
124 @@mutex.synchronize do
127 while @records.size > @max_records
134 def last_win?(player_id)
135 rc = last_valid_game(player_id)
136 return false unless rc
137 return rc[:winner] == player_id
140 def last_lose?(player_id)
141 rc = last_valid_game(player_id)
142 return false unless rc
143 return rc[:loser] == player_id
146 def last_valid_game(player_id)
148 @@mutex.synchronize do
149 records = @records.reverse
151 rc = records.find do |rc|
154 (rc[:black] == player_id || rc[:white] == player_id)
161 end # class Floodgate
165 end # module ShogiServer