OSDN Git Service

- shogi-server, shogi_server/league/floodgate.rb: Changed the argument of Floodgate...
[shogi-server/shogi-server.git] / shogi_server / league / floodgate.rb
index 484ffd3..4f6ac2b 100644 (file)
@@ -16,12 +16,17 @@ class League
 
     attr_reader :next_time, :league
 
-    def initialize(league, next_time=nil)
+    def initialize(league, hash={})
       @league = league
-      @next_time = next_time
+      @next_time = hash[:next_time] || nil
+      @game_name = hash[:game_name] || "floodgate-900-0"
       charge
     end
 
+    def game_name?(str)
+      return Regexp.new(@game_name).match(str) ? true : false
+    end
+
     def charge
       now = Time.now
       unless $DEBUG
@@ -44,7 +49,7 @@ class League
     def match_game
       players = @league.find_all_players do |pl|
         pl.status == "game_waiting" &&
-        Floodgate.game_name?(pl.game_name) &&
+        game_name?(pl.game_name) &&
         pl.sente == nil
       end
       Pairing.match(players)
@@ -67,7 +72,8 @@ class League
 
       attr_reader :records
 
-      # file_path_name is a Pathname object for this storage
+      # Initialize this instance.
+      # @param file_path_name a Pathname object for this storage
       #
       def initialize(file_path_name)
         @records = []
@@ -101,13 +107,16 @@ class League
       def load
         return unless @file.exist?
 
-        yaml = @file.open("r") {|f| f.read}
-        @records = YAML.load(yaml)
+        @records = YAML.load_file(@file)
+        unless @records && @records.instance_of?(Array)
+          $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
+          @records = []
+        end
       end
 
       def save
         begin
-          @file.open("w+") do |f| 
+          @file.open("w") do |f| 
             f << YAML.dump(@records)
           end
         rescue Errno::ENOSPC