3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 require 'shogi_server/league/persistent'
22 module ShogiServer # for a namespace
24 ######################################################
29 def initialize(dir=File.dirname(__FILE__))
30 @mutex = Monitor.new # guard @players
36 attr_accessor :players, :games, :event, :dir
38 # this should be called just after instanciating a League object.
39 def setup_players_database
40 filename = File.join(@dir, "players.yaml")
41 @persistent = Persistent.new(filename)
45 @persistent.load_player(player)
47 @players[player.name] = player
53 @players.delete(player.name)
59 @players.each do |name, player|
60 @persistent.load_player(player)
68 found = @players.find_all do |name, player|
72 return found.map {|a| a.last}
78 found = @players[player_name]
83 def get_player(status, game_name, sente, searcher)
86 found = @players.find do |name, player|
87 (player.status == status) &&
88 (player.game_name == game_name) &&
90 (player.sente == nil) ||
91 (player.sente == sente) ) &&
92 (player.name != searcher.name)
95 return found ? found.last : nil
99 return @persistent.get_players
102 # Find a rival for a player.
104 # 1. symbol :continue for an error case
105 # 2. a rival player instance found
106 # 3. nil if rival not found
108 def find_rival(player, game_name)
110 when nil # no preference
111 if Login.handicapped_game_name?(game_name)
112 player.write_safe("##[ERROR] Random turn preference is not allowed for handicapped games\n")
115 return get_player("game_waiting", game_name, nil, player)
116 when true # rival must be gote
117 return get_player("game_waiting", game_name, false, player)
118 when false # rival must be sente
119 return get_player("game_waiting", game_name, true, player)
121 write_safe("##[ERROR] bad game option: %s\n" % [my_sente_string])