OSDN Git Service

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