OSDN Git Service

Debugged Floodgate
[shogi-server/shogi-server.git] / shogi_server / league / floodgate.rb
1 module ShogiServer
2
3 class League
4   class Floodgate
5     class << self
6       def game_name?(str)
7         return /^floodgate-\d+-\d+$/.match(str) ? true : false
8       end
9     end
10
11     attr_reader :next_time, :league
12
13     def initialize(league, next_time=nil)
14       @league = league
15       @next_time = next_time
16       charge
17     end
18
19     def charge
20       now = Time.now
21       unless $DEBUG
22         # each 30 minutes
23         if now.min < 30
24           @next_time = Time.mktime(now.year, now.month, now.day, now.hour, 30)
25         else
26           @next_time = Time.mktime(now.year, now.month, now.day, now.hour) + 3600
27         end
28       else
29         # for test, each 30 seconds
30         if now.sec < 30
31           @next_time = Time.mktime(now.year, now.month, now.day, now.hour, now.min, 30)
32         else
33           @next_time = Time.mktime(now.year, now.month, now.day, now.hour, now.min) + 60
34         end
35       end
36     end
37
38     def match_game
39       players = @league.find_all_players do |pl|
40         pl.status == "game_waiting" &&
41         Floodgate.game_name?(pl.game_name) &&
42         pl.sente == nil
43       end
44       Pairing.match(players)
45     end
46   end # class Floodgate
47
48 end # class League
49 end # module ShogiServer