OSDN Git Service

[shogi-server] Addressed Ruby incompatibility on ShogiServer::Usi::alphabetToDan.
[shogi-server/shogi-server.git] / shogi_server / league.rb
1 ## $Id$
2
3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2012 Daigo Moriwaki (daigo at debian dot org)
5 ##
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.
10 ##
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.
15 ##
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
19
20 require 'shogi_server/league/persistent'
21
22 module ShogiServer # for a namespace
23
24 ######################################################
25 # League
26 #
27 class League
28
29   def initialize(dir=File.dirname(__FILE__))
30     @mutex = Monitor.new # guard @players
31     @games = Hash::new
32     @players = Hash::new
33     @event = nil
34     @dir = dir
35   end
36   attr_accessor :players, :games, :event, :dir
37
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)
42   end
43
44   def add(player)
45     @persistent.load_player(player)
46     @mutex.synchronize do
47       @players[player.name] = player
48     end
49   end
50   
51   def delete(player)
52     @mutex.synchronize do
53       @players.delete(player.name)
54     end
55   end
56
57   def reload
58     @mutex.synchronize do
59       @players.each do |name, player| 
60         @persistent.load_player(player)
61       end
62     end
63   end
64
65   def find_all_players
66     found = nil
67     @mutex.synchronize do
68       found = @players.find_all do |name, player|
69         yield player
70       end
71     end
72     return found.map {|a| a.last}
73   end
74   
75   def find(player_name)
76     found = nil
77     @mutex.synchronize do
78       found = @players[player_name]
79     end
80     return found
81   end
82
83   def get_player(status, game_name, sente, searcher)
84     found = nil
85     @mutex.synchronize do
86       found = @players.find do |name, player|
87         (player.status == status) &&
88         (player.game_name == game_name) &&
89         ( (sente == nil) || 
90           (player.sente == nil) || 
91           (player.sente == sente) ) &&
92         (player.name != searcher.name)
93       end
94     end
95     return found ? found.last : nil
96   end
97   
98   def rated_players
99     return @persistent.get_players
100   end
101
102   # Find a rival for a player.
103   # Return,
104   #   1. symbol :continue for an error case
105   #   2. a rival player instance found
106   #   3. nil if rival not found 
107   #
108   def find_rival(player, game_name)
109     case player.sente
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")
113         return :continue
114       end
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) 
120     else
121       write_safe("##[ERROR] bad game option: %s\n" % [my_sente_string])
122       return :continue
123     end
124   end
125
126 end # class League
127
128 end # ShogiServer
129