OSDN Git Service

Do not raise in the case of invalid arguments
[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-2008 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/floodgate'
21 #require 'shogi_server/league/persistent'
22
23 module ShogiServer # for a namespace
24
25 ######################################################
26 # League
27 #
28 class League
29
30   def initialize
31     @mutex = Monitor.new # guard @players
32     @games = Hash::new
33     @players = Hash::new
34     @event = nil
35     @dir = File.dirname(__FILE__)
36     @floodgate = Floodgate.new(self)
37     #@floodgate.run
38   end
39   attr_accessor :players, :games, :event, :dir
40
41   def shutdown
42     @mutex.synchronize do
43       @players.each do |name, player| 
44         @persistent.save(player)
45       end
46     end
47     @floodgate.shutdown
48   end
49
50   def restart
51     @floodgate = Floodgate.new(self)
52     #@floodgate.run
53   end
54
55   # this should be called just after instanciating a League object.
56   def setup_players_database
57     filename = File.join(@dir, "players.yaml")
58     @persistent = Persistent.new(filename)
59   end
60
61   def add(player)
62     @persistent.load_player(player)
63     @mutex.synchronize do
64       @players[player.name] = player
65     end
66   end
67   
68   def delete(player)
69     @persistent.save(player)
70     @mutex.synchronize do
71       @players.delete(player.name)
72     end
73   end
74
75   def save(player)
76     @persistent.save(player)
77   end
78
79   def reload
80     @mutex.synchronize do
81       @players.each do |name, player| 
82         @persistent.load_player(player)
83       end
84     end
85   end
86
87   def find_all_players
88     found = nil
89     @mutex.synchronize do
90       found = @players.find_all do |name, player|
91         yield player
92       end
93     end
94     return found.map {|a| a.last}
95   end
96   
97   def find(player_name)
98     found = nil
99     @mutex.synchronize do
100       found = @players[player_name]
101     end
102     return found
103   end
104
105   def get_player(status, game_name, sente, searcher)
106     found = nil
107     @mutex.synchronize do
108       found = @players.find do |name, player|
109         (player.status == status) &&
110         (player.game_name == game_name) &&
111         ( (sente == nil) || 
112           (player.sente == nil) || 
113           (player.sente == sente) ) &&
114         (player.name != searcher.name)
115       end
116     end
117     return found ? found.last : nil
118   end
119   
120   def rated_players
121     return @persistent.get_players
122   end
123 end # class League
124
125 end # ShogiServer
126