shogi-server source
Revisión | d7dc48d302a0b0ffaeba2f7a05099912e148a1c6 (tree) |
---|---|
Tiempo | 2013-12-29 16:33:19 |
Autor | Daigo Moriwaki <daigo@debi...> |
Commiter | Daigo Moriwaki |
Merge branch '201312-fix-sacrifice'
@@ -1,3 +1,9 @@ | ||
1 | +2013-12-29 Daigo Moriwaki <daigo at debian dot org> | |
2 | + | |
3 | + * [shogi-server] | |
4 | + - Previously, parameters in Floodgate time configuration file were | |
5 | + not applied to actual instances. This issue has been fixed. | |
6 | + | |
1 | 7 | 2013-12-21 Daigo Moriwaki <daigo at debian dot org> |
2 | 8 | |
3 | 9 | * [shogi-server] |
@@ -27,16 +27,17 @@ class League | ||
27 | 27 | # |
28 | 28 | attr_reader :next_time |
29 | 29 | attr_reader :league, :game_name |
30 | - attr_reader :pairing_factory | |
31 | 30 | attr_reader :options |
32 | 31 | |
33 | 32 | def initialize(league, hash={}) |
34 | 33 | @league = league |
35 | - @next_time = hash[:next_time] || nil | |
36 | - @game_name = hash[:game_name] || "floodgate-900-0" | |
37 | - @pairing_factory = "default_factory" # will be updated by NextTimeGenerator | |
38 | - # Options will be updated by NextTimeGenerator | |
39 | - @options = {:sacrifice => "gps500+e293220e3f8a3e59f79f6b0efffaa931"} | |
34 | + @next_time = hash[:next_time] || nil | |
35 | + @game_name = hash[:game_name] || "floodgate-900-0" | |
36 | + # Options will be updated by NextTimeGenerator and then passed to a | |
37 | + # pairing factory. | |
38 | + @options = {} | |
39 | + @options[:pairing_factory] = hash[:pairing_factory] || "default_factory" | |
40 | + @options[:sacrifice] = hash[:sacrifice] || "gps500+e293220e3f8a3e59f79f6b0efffaa931" | |
40 | 41 | charge if @next_time.nil? |
41 | 42 | end |
42 | 43 |
@@ -44,25 +45,33 @@ class League | ||
44 | 45 | return Regexp.new(@game_name).match(str) ? true : false |
45 | 46 | end |
46 | 47 | |
48 | + def pairing_factory | |
49 | + return @options[:pairing_factory] | |
50 | + end | |
51 | + | |
52 | + def sacrifice | |
53 | + return @options[:sacrifice] | |
54 | + end | |
55 | + | |
47 | 56 | def charge |
48 | 57 | ntg = NextTimeGenerator.factory(@game_name) |
49 | - @pairing_factory = ntg.pairing_factory | |
50 | - @options[:sacrifice] = ntg.sacrifice | |
51 | 58 | if ntg |
52 | 59 | @next_time = ntg.call(Time.now) |
60 | + @options[:pairing_factory] = ntg.pairing_factory | |
61 | + @options[:sacrifice] = ntg.sacrifice | |
53 | 62 | else |
54 | 63 | @next_time = nil |
55 | 64 | end |
56 | 65 | end |
57 | 66 | |
58 | 67 | def match_game |
59 | - log_message("Starting Floodgate games...: %s, %s" % [@game_name, @pairing_factory]) | |
68 | + log_message("Starting Floodgate games...: %s, %s" % [@game_name, @options]) | |
60 | 69 | players = @league.find_all_players do |pl| |
61 | 70 | pl.status == "game_waiting" && |
62 | 71 | game_name?(pl.game_name) && |
63 | 72 | pl.sente == nil |
64 | 73 | end |
65 | - logics = Pairing.send(@pairing_factory, @options) | |
74 | + logics = Pairing.send(@options[:pairing_factory], @options) | |
66 | 75 | Pairing.match(players, logics) |
67 | 76 | end |
68 | 77 |
@@ -150,9 +159,9 @@ class League | ||
150 | 159 | @lines.each do |line| |
151 | 160 | case line |
152 | 161 | when %r!^\s*set\s+pairing_factory\s+(\w+)! |
153 | - @pairing_factory = $1 | |
162 | + @pairing_factory = $1.chomp | |
154 | 163 | when %r!^\s*set\s+sacrifice\s+(.*)! |
155 | - @sacrifice = $1 | |
164 | + @sacrifice = $1.chomp | |
156 | 165 | when %r!^\s*(\w+)\s+(\d{1,2}):(\d{1,2})! |
157 | 166 | dow, hour, minute = $1, $2.to_i, $3.to_i |
158 | 167 | dow_index = ::ShogiServer::parse_dow(dow) |
@@ -68,15 +68,16 @@ module ShogiServer | ||
68 | 68 | floodgate.match_game |
69 | 69 | end |
70 | 70 | |
71 | - # Regenerate floodgate instances from next_array for the next matches. | |
72 | - # @param next_array array of [game_name, next_time] | |
71 | + # Regenerate floodgate instances from next_instances for the next matches. | |
72 | + # @param next_instances array of [game_name, next_time] | |
73 | 73 | # |
74 | - def regenerate_leagues(next_array) | |
75 | - leagues = next_array.collect do |game_name, next_time| | |
76 | - log_message("Regenerating a floodgate league...: %s %s" % [game_name, next_time]) | |
74 | + def regenerate_leagues(next_instances) | |
75 | + leagues = next_instances.collect do |prev| | |
76 | + log_message("Regenerating a floodgate league...: %s %s %s %s" % | |
77 | + [prev.game_name, prev.next_time, prev.pairing_factory, prev.sacrifice]) | |
77 | 78 | floodgate = ShogiServer::League::Floodgate.new($league, |
78 | - {:game_name => game_name, | |
79 | - :next_time => next_time}) | |
79 | + {:game_name => prev.game_name, :next_time => prev.next_time, | |
80 | + :pairing_factory => prev.pairing_factory, :sacrifice => prev.sacrifice}) | |
80 | 81 | end |
81 | 82 | floodgate_reload_log(leagues) |
82 | 83 | return leagues |
@@ -98,20 +99,18 @@ module ShogiServer | ||
98 | 99 | floodgate = next_league(leagues) |
99 | 100 | next if wait_next_floodgate(floodgate) |
100 | 101 | |
101 | - next_array = leagues.collect do |floodgate| | |
102 | - if (floodgate.next_time - Time.now) > 0 | |
103 | - [floodgate.game_name, floodgate.next_time] | |
104 | - else | |
102 | + next_instances = leagues.collect do |floodgate| | |
103 | + unless (floodgate.next_time - Time.now) > 0 | |
105 | 104 | start_games(floodgate) |
106 | 105 | floodgate.charge # updates next_time |
107 | - [floodgate.game_name, floodgate.next_time] | |
108 | 106 | end |
107 | + floodgate | |
109 | 108 | end |
110 | 109 | |
111 | 110 | reload_shogi_server |
112 | 111 | |
113 | 112 | # Regenerate floodgate instances after ShogiServer.realod |
114 | - leagues = regenerate_leagues(next_array) | |
113 | + leagues = regenerate_leagues(next_instances) | |
115 | 114 | rescue Exception => ex |
116 | 115 | # ignore errors |
117 | 116 | log_error("[in Floodgate's thread] #{ex} #{ex.backtrace}") |
@@ -65,7 +65,13 @@ class TestSetupFloodgate < Test::Unit::TestCase | ||
65 | 65 | def test_regenerate_leagues |
66 | 66 | game_names = %w(floodgate-900-0 floodgate-3600-0) |
67 | 67 | now = Time.now |
68 | - next_array = [["floodgate-900-0", now+100], ["floodgate-3600-0", now+200]] | |
68 | + next_array = [] | |
69 | + next_array << ShogiServer::League::Floodgate.new($league, | |
70 | + {:game_name => "floodgate-900-0", | |
71 | + :next_time => (now+100)}) | |
72 | + next_array << ShogiServer::League::Floodgate.new($league, | |
73 | + {:game_name => "floodgate-3600-0", | |
74 | + :next_time => (now+200)}) | |
69 | 75 | objs = @sf.regenerate_leagues(next_array) |
70 | 76 | assert_equal 2, objs.size |
71 | 77 | assert_instance_of ShogiServer::League::Floodgate, objs[0] |
@@ -1,3 +1,4 @@ | ||
1 | +$:.unshift File.dirname(__FILE__) | |
1 | 2 | $:.unshift File.join(File.dirname(__FILE__), "..") |
2 | 3 | $topdir = File.expand_path File.dirname(__FILE__) |
3 | 4 | require "baseclient" |