OSDN Git Service

Added a test case for Floodgate#game_name?
[shogi-server/shogi-server.git] / shogi_server / league / persistent.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
21
22 class League
23
24   #
25   # This manages those players who have their player_id.
26   # Since mk_rate mainly updates the yaml file, basically,
27   # this only reads data. But this writes some properties.
28   # TODO Such data should be facoted out to another file
29   #
30   class Persistent
31     def initialize(filename)
32       @db = YAML::Store.new(filename)
33       @db.transaction do |pstore|
34         @db['players'] ||= Hash.new
35       end
36     end
37
38     #
39     # trancaction=true means read only
40     #
41     def each_group(transaction=false)
42       @db.transaction(transaction) do
43         groups = @db["players"] || Hash.new
44         groups.each do |group, players|
45           yield group,players
46         end
47       end
48     end
49
50     def load_player(player)
51       return unless player.player_id
52
53       hash = nil
54       each_group(true) do |group, players|
55         hash = players[player.player_id]
56         break if hash
57       end
58       return unless hash
59
60       # a current user
61       player.name          = hash['name']
62       player.rate          = hash['rate'] || 0
63       player.modified_at   = hash['last_modified']
64       player.rating_group  = hash['rating_group']
65       player.win           = hash['win']  || 0
66       player.loss          = hash['loss'] || 0
67     end
68
69     def get_players
70       players = []
71       each_group(true) do |group, players_hash|
72         players << players_hash.keys
73       end
74       return players.flatten.collect do |player_id|
75         p = BasicPlayer.new
76         p.player_id = player_id
77         load_player(p)
78         p
79       end
80     end
81   end # class Persistent
82
83 end # class League
84 end # module ShogiServer