OSDN Git Service

Record Floodgate games' history
[shogi-server/shogi-server.git] / shogi_server / league / floodgate.rb
index 317dfe1..1d7d9e1 100644 (file)
@@ -1,3 +1,7 @@
+require 'thread'
+require 'ostruct'
+require 'pathname'
+
 module ShogiServer
 
 class League
@@ -43,7 +47,109 @@ class League
       end
       Pairing.match(players)
     end
+
+
+    #
+    #
+    class History
+      @@mutex = Mutex.new
+
+      class << self
+        def factory
+          file = Pathname.new $options["floodgate-history"]
+          return History.new file
+        end
+      end
+
+      # file_path_name is a Pathname object for this storage
+      #
+      def initialize(file_path_name)
+        @records = []
+        @max_records = 100
+        @file = file_path_name
+      end
+
+      # Return a hash describing the game_result
+      # :game_id: game id
+      # :black:   Black's player id
+      # :white:   White's player id
+      # :winner:  Winner's player id or nil for the game without a winner
+      # :loser:   Loser's player id or nil for the game without a loser
+      #
+      def make_record(game_result)
+        hash = Hash.new
+        hash[:game_id] = game_result.game.game_id
+        hash[:black]   = game_result.black.player_id
+        hash[:white]   = game_result.white.player_id
+        case game_result
+        when GameResultWin
+          hash[:winner] = game_result.winner.player_id
+          hash[:loser]  = game_result.loser.player_id
+        else
+          hash[:winner] = nil
+          hash[:loser]  = nil
+        end
+        return hash
+      end
+
+      def load
+        return unless @file.exist?
+
+        yaml = @file.open("r") {|f| f.read}
+        @records = YAML.load(yaml)
+      end
+
+      def save
+        begin
+          @file.open("w+") do |f| 
+            f << YAML.dump(@records)
+          end
+        rescue Errno::ENOSPC
+          # ignore
+        end
+      end
+
+      def update(game_result)
+        record = make_record(game_result)
+        @@mutex.synchronize do 
+          load
+          @records << record
+          while @records.size > @max_records
+            @records.shift
+          end
+          save
+        end
+      end
+      
+      def last_win?(player_id)
+        rc = last_valid_record(player_id)
+        return false unless rc
+        return rc[:winner] == player_id
+      end
+      
+      def last_lose?(player_id)
+        rc = last_valid_record(player_id)
+        return false unless rc
+        return rc[:loser] == player_id
+      end
+
+      def last_valid_record(player_id)
+        records = nil
+        @mutex.synchronize do
+          records = @records.reverse
+        end
+        rc = records.find do |rc|
+          rc[:winner] && 
+          rc[:loser]  && 
+          (rc[:black] == player_id || rc[:while] == player_id)
+        end
+        return rc
+      end
+    end # class History
+
+
   end # class Floodgate
 
+
 end # class League
 end # module ShogiServer