OSDN Git Service

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