OSDN Git Service

[shogi-server]
[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     def initialize(league)
12       @league = league
13       @next_time = nil
14       charge
15     end
16
17     def run
18       @thread = Thread.new do
19         Thread.pass
20         while (true)
21           begin
22             sleep(10)
23             next if Time.now < @next_time
24             @league.reload
25             match_game
26             charge
27           rescue Exception => ex 
28             # ignore errors
29             log_error("[in Floodgate's thread] #{ex} #{ex.backtrace}")
30           end
31         end
32       end
33     end
34
35     def shutdown
36       @thread.kill if @thread
37     end
38
39     # private
40
41     def charge
42       now = Time.now
43       # if now.min < 30
44       #   @next_time = Time.mktime(now.year, now.month, now.day, now.hour, 30)
45       # else
46       #   @next_time = Time.mktime(now.year, now.month, now.day, now.hour) + 3600
47       # end
48       # for test
49       if now.sec < 30
50         @next_time = Time.mktime(now.year, now.month, now.day, now.hour, now.min, 30)
51       else
52         @next_time = Time.mktime(now.year, now.month, now.day, now.hour, now.min) + 60
53       end
54     end
55
56     def match_game
57       players = @league.find_all_players do |pl|
58         pl.status == "game_waiting" &&
59         Floodgate.game_name?(pl.game_name) &&
60         pl.sente == nil
61       end
62       #log_warning("DEBUG: %s" % [File.join(File.dirname(__FILE__), "pairing.rb")])
63       #load File.join(File.dirname(__FILE__), "pairing.rb")
64       Pairing.default_pairing.match(players)
65     end
66   end # class Floodgate
67
68 end # class League
69 end # module ShogiServer