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 1d7d9e1..4f6ac2b 100644 (file)
@@ -7,19 +7,26 @@ module ShogiServer
 class League
   class Floodgate
     class << self
+      # "floodgate-900-0"
+      #
       def game_name?(str)
-        return /^floodgate-\d+-\d+$/.match(str) ? true : false
+        return /^floodgate\-\d+\-\d+$/.match(str) ? true : false
       end
     end
 
     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
@@ -42,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)
@@ -57,11 +64,16 @@ class League
       class << self
         def factory
           file = Pathname.new $options["floodgate-history"]
-          return History.new file
+          history = History.new file
+          history.load
+          return history
         end
       end
 
-      # file_path_name is a Pathname object for this storage
+      attr_reader :records
+
+      # Initialize this instance.
+      # @param file_path_name a Pathname object for this storage
       #
       def initialize(file_path_name)
         @records = []
@@ -95,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
@@ -122,26 +137,26 @@ class League
       end
       
       def last_win?(player_id)
-        rc = last_valid_record(player_id)
+        rc = last_valid_game(player_id)
         return false unless rc
         return rc[:winner] == player_id
       end
       
       def last_lose?(player_id)
-        rc = last_valid_record(player_id)
+        rc = last_valid_game(player_id)
         return false unless rc
         return rc[:loser] == player_id
       end
 
-      def last_valid_record(player_id)
+      def last_valid_game(player_id)
         records = nil
-        @mutex.synchronize do
+        @@mutex.synchronize do
           records = @records.reverse
         end
         rc = records.find do |rc|
           rc[:winner] && 
           rc[:loser]  && 
-          (rc[:black] == player_id || rc[:while] == player_id)
+          (rc[:black] == player_id || rc[:white] == player_id)
         end
         return rc
       end