OSDN Git Service

Enhanced the CSA Login mode to accept a turn preference.
[shogi-server/shogi-server.git] / shogi_server / league.rb
index 19f7db2..1207472 100644 (file)
@@ -17,8 +17,7 @@
 ## along with this program; if not, write to the Free Software
 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-#require 'shogi_server/league/floodgate'
-#require 'shogi_server/league/persistent'
+require 'shogi_server/league/persistent'
 
 module ShogiServer # for a namespace
 
@@ -27,31 +26,15 @@ module ShogiServer # for a namespace
 #
 class League
 
-  def initialize
+  def initialize(dir=File.dirname(__FILE__))
     @mutex = Monitor.new # guard @players
     @games = Hash::new
     @players = Hash::new
     @event = nil
-    @dir = File.dirname(__FILE__)
-    @floodgate = Floodgate.new(self)
-    #@floodgate.run
+    @dir = dir
   end
   attr_accessor :players, :games, :event, :dir
 
-  def shutdown
-    @mutex.synchronize do
-      @players.each do |name, player| 
-        @persistent.save(player)
-      end
-    end
-    @floodgate.shutdown
-  end
-
-  def restart
-    @floodgate = Floodgate.new(self)
-    #@floodgate.run
-  end
-
   # this should be called just after instanciating a League object.
   def setup_players_database
     filename = File.join(@dir, "players.yaml")
@@ -66,16 +49,11 @@ class League
   end
   
   def delete(player)
-    @persistent.save(player)
     @mutex.synchronize do
       @players.delete(player.name)
     end
   end
 
-  def save(player)
-    @persistent.save(player)
-  end
-
   def reload
     @mutex.synchronize do
       @players.each do |name, player| 
@@ -120,6 +98,31 @@ class League
   def rated_players
     return @persistent.get_players
   end
+
+  # Find a rival for a player.
+  # Return,
+  #   1. symbol :continue for an error case
+  #   2. a rival player instance found
+  #   3. nil if rival not found 
+  #
+  def find_rival(player, game_name)
+    case player.sente
+    when nil # no preference
+      if Login.handicapped_game_name?(game_name)
+        player.write_safe("##[ERROR] Random turn preference is not allowed for handicapped games\n")
+        return :continue
+      end
+      return get_player("game_waiting", game_name, nil, player)
+    when true # rival must be gote
+      return get_player("game_waiting", game_name, false, player) 
+    when false # rival must be sente 
+      return get_player("game_waiting", game_name, true, player) 
+    else
+      write_safe("##[ERROR] bad game option: %s\n" % [my_sente_string])
+      return :continue
+    end
+  end
+
 end # class League
 
 end # ShogiServer