OSDN Git Service

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