OSDN Git Service

Refactored methods: League#find_rival and Game::decide_turns.
[shogi-server/shogi-server.git] / shogi_server / league.rb
index 29d7bf2..5936e79 100644 (file)
@@ -26,12 +26,12 @@ 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__)
+    @dir = dir
   end
   attr_accessor :players, :games, :event, :dir
 
@@ -98,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, my_sente_string, game_name)
+    case my_sente_string
+    when "*" # 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 "+" # rival must be gote
+      return get_player("game_waiting", game_name, false, player) 
+    when "-" # 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