OSDN Git Service

Added test cases.
[shogi-server/shogi-server.git] / shogi_server / buoy.rb
1 require 'shogi_server/config'
2 require 'shogi_server/game'
3 require 'yaml'
4 require 'yaml/store'
5
6 module ShogiServer
7
8   class BuoyGame
9     attr_reader :game_name
10     attr_reader :moves
11     attr_reader :owner
12     attr_reader :count
13
14     def initialize(game_name, moves, owner, count)
15       raise "owner name is required" if owner && !owner.instance_of?(String)
16       @game_name, @moves, @owner, @count = game_name, moves, owner, count
17     end
18
19     def decrement_count
20       @count -= 1
21     end
22
23     def ==(rhs)
24       return (@game_name == rhs.game_name && 
25               @moves == rhs.moves &&
26               @owner == rhs.owner &&
27               @count == rhs.count)
28     end
29   end
30
31   class NilBuoyGame < BuoyGame
32     def initialize
33       super(nil, nil, nil, 0)
34     end
35   end
36
37   class Buoy
38
39     # "buoy_hoge-900-0"
40     #
41     def Buoy.game_name?(str)
42       return /^buoy_.*\-\d+\-\d+$/.match(str) ? true : false
43     end
44
45     def initialize(conf = {})
46       @conf = $config || Config.new
47       @conf.merge!(conf, true)
48       filename = @conf[:buoy, :filename] || File.join(@conf[:topdir], "buoy.yaml")
49       @db = YAML::Store.new(filename)
50       @db.transaction do
51       end
52     end
53
54     def is_new_game?(game_name)
55       @db.transaction(true) do
56         return !@db.root?(game_name)
57       end
58     end
59
60     def add_game(buoy_game)
61       @db.transaction do
62         if @db.root?(buoy_game.game_name)
63           # error
64         else
65           hash = {'moves' => buoy_game.moves,
66                   'owner' => buoy_game.owner,
67                   'count' => buoy_game.count}
68           @db[buoy_game.game_name] = hash
69         end
70       end
71     end
72
73     def update_game(buoy_game)
74       @db.transaction do
75         if @db.root?(buoy_game.game_name)
76           hash = {'moves' => buoy_game.moves,
77                   'owner' => buoy_game.owner,
78                   'count' => buoy_game.count}
79           @db[buoy_game.game_name] = hash
80         else
81           # error
82         end
83       end
84     end
85
86     def delete_game(buoy_game)
87       @db.transaction do
88         @db.delete(buoy_game.game_name)
89       end
90     end
91
92     def get_game(game_name)
93       @db.transaction(true) do
94         hash = @db[game_name]
95         if hash
96           moves = hash['moves']
97           owner = hash['owner']
98           count = hash['count'].to_i
99           return BuoyGame.new(game_name, moves, owner, count)
100         else
101           return NilBuoyGame.new
102         end
103       end
104     end
105
106     def decrement_count(buoy_game)
107       return if buoy_game.instance_of?(NilBuoyGame)
108
109       buoy_game.decrement_count
110       if buoy_game.count > 0
111         update_game buoy_game
112         log_message "Buoy #{buoy_game.game_name} remains #{buoy_game.count} slots."
113       else                
114         delete_game buoy_game
115         log_message "Buoy #{buoy_game.game_name} finished."
116       end
117     end
118   end
119
120 end # module ShogiServer