OSDN Git Service

Start up shogi-server in foreground
[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-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 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       set_player_values(player, hash)
62     end
63
64     def get_players
65       players = []
66       each_group(true) do |group, players_hash|
67         players_hash.each do |key, value|
68           bp = BasicPlayer.new
69           bp.player_id = key
70           set_player_values(bp, value)
71           players << bp
72         end
73       end
74       return players
75     end
76
77     private
78     def set_player_values(player, hash)
79       return if player.nil? || hash.nil?
80
81       player.name          = hash['name']
82       player.rate          = hash['rate'] || 0
83       player.modified_at   = hash['last_modified']
84       player.rating_group  = hash['rating_group']
85       player.win           = hash['win']  || 0
86       player.loss          = hash['loss'] || 0
87     end
88   end # class Persistent
89
90 end # class League
91 end # module ShogiServer