Class: Z4BC::Game
- Inherits:
-
Object
- Object
- Z4BC::Game
- Defined in:
- lib/z4blockchain.rb
Instance Attribute Summary collapse
-
#game ⇒ Object
readonly
Returns the value of attribute game.
-
#id ⇒ Object
Returns the value of attribute id.
-
#lvls ⇒ Object
readonly
Returns the value of attribute lvls.
-
#wins ⇒ Object
readonly
Returns the value of attribute wins.
Instance Method Summary collapse
-
#initialize(i) ⇒ Game
constructor
A new instance of Game.
- #key(k) ⇒ Object
- #play(h = {}) ⇒ Object
Constructor Details
#initialize(i) ⇒ Game
Returns a new instance of Game.
162 163 164 165 166 167 |
# File 'lib/z4blockchain.rb', line 162 def initialize i @id = i @game = PStore.new("db/game-game-#{i}.pstore") @wins = PStore.new("db/game-wins-#{i}.pstore") @lvls = PStore.new("db/game-lvls-#{i}.pstore") end |
Instance Attribute Details
#game ⇒ Object (readonly)
Returns the value of attribute game.
160 161 162 |
# File 'lib/z4blockchain.rb', line 160 def game @game end |
#id ⇒ Object
Returns the value of attribute id.
158 159 160 |
# File 'lib/z4blockchain.rb', line 158 def id @id end |
#lvls ⇒ Object (readonly)
Returns the value of attribute lvls.
160 161 162 |
# File 'lib/z4blockchain.rb', line 160 def lvls @lvls end |
#wins ⇒ Object (readonly)
Returns the value of attribute wins.
160 161 162 |
# File 'lib/z4blockchain.rb', line 160 def wins @wins end |
Instance Method Details
#key(k) ⇒ Object
169 170 171 172 |
# File 'lib/z4blockchain.rb', line 169 def key k #Z4blockchain.digest(%[#{@id}:#{k}]) %[#{@id}:#{k}] end |
#play(h = {}) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/z4blockchain.rb', line 174 def play h={} GAMES.transaction { |db| x = db[@id].to_i; db[@id] = x + 1 } @wins.transaction { |db| x = db[h[:winner]].to_i; db[h[:winner]] = x + 1; u = USER[h[:winner]] u.wins.transaction { |ddb| x = ddb[@id].to_i; ddb[@id] = x + 1; } } @game.transaction { |db| [h[:players]].flatten.each { |e| x = db[e].to_i; db[e] = x + 1; u = USER[e] u.game.transaction { |ddb| x = ddb[@id].to_i; ddb[@id] = x + 1; } } } if h.has_key? :award @lvls.transaction { |db| db[h[:award]] = h[:winner] u = User.new(h[:winner]) u.lvls.transaction { |ddb| LVL[h[:award]].incr h[:winner] } } end end |