z4blockchain 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 441cf17361208db50b47e83e503d3a925b4e2f787db8be38cd3257aeb287cd26
4
- data.tar.gz: 37f5a8856a56719c639a77dbd2bc5e483c36d56071036edf7f46a2ecd3cb3ebc
3
+ metadata.gz: ba2b348422bb80bbf15cda06a7e954f9a157a639af4046fa8f98a59e4c34b41e
4
+ data.tar.gz: 01a700eedcd7bc9f7563889ff5613aaded34d8999e803b424e6ecc0274d22a10
5
5
  SHA512:
6
- metadata.gz: ac66a6c1d06177116fff2b01af67cef2864114a5272b91dab717383273e12758fdc84743ebb9621751aff5ac2f4aab1569b112dac7443b3d2fbde969cd20de70
7
- data.tar.gz: 6dae7ff51aaf2a5d8cf6babf24eb927f0ea5369e5d7bf04d6c8dd42432456939b30f6348d5af067b66131f992e2e29f4079ca54e2b234c14c40ff515760ea790
6
+ metadata.gz: b67adef24419014deb07a683a179ae104964c12509dcf91cf0e09286a3b7d334a6feead248f1fb718334d38c1077d20a554d687281d03ed4b538754f405cfc9b
7
+ data.tar.gz: e991ac8b22dfaff8dff5f71b7528b3c6be806e8194c3ef20726ad4779093828291692492f6d20a7fc73e7bbda2b1d05a1850ef0a204e965c346b43e0fcdef7b8
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Z4blockchain
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  COMMIT = `git log --format="%H" -n 1`.strip
6
6
  end
data/lib/z4blockchain.rb CHANGED
@@ -25,6 +25,16 @@ module Z4BC
25
25
  BANKS = PStore.new("db/banks.pstore")
26
26
 
27
27
  BANK = Hash.new { |h,k| h[k] = Bank.new(k) }
28
+
29
+ GAMES = PStore.new("db/games.pstore")
30
+
31
+ GAME = Hash.new { |h,k| h[k] = Game.new(k) }
32
+
33
+ LVLS = PStore.new("db/lvls.pstore")
34
+
35
+ LVL = Hash.new { |h,k| h[k] = Lvls.new(k) }
36
+
37
+ USER = Hash.new { |h,k| h[k] = User.new(k) }
28
38
 
29
39
  class Error < StandardError; end
30
40
 
@@ -104,6 +114,105 @@ module Z4BC
104
114
  BLOCKCHAIN << JSON.generate(h)
105
115
  return nil
106
116
  end
117
+
118
+ def self.game
119
+ GAME
120
+ end
121
+
122
+ def self.user
123
+ USER
124
+ end
125
+
126
+ def self.level
127
+ LVLS
128
+ end
129
+
130
+ def self.lvls
131
+ LVL
132
+ end
133
+
134
+ class User
135
+
136
+ attr_accessor :id
137
+
138
+ attr_reader :game, :wins, :lvls
139
+
140
+ def initialize i
141
+ @id = i
142
+ @game = PStore.new("db/player-game-#{i}.pstore")
143
+ @wins = PStore.new("db/player-wins-#{i}.pstore")
144
+ @lvls = PStore.new("db/player-lvls-#{i}.pstore")
145
+ end
146
+
147
+ def to_h
148
+ h = { game: {}, wins: {}, lvls: {} }
149
+ @game.transaction { |db| db.keys.each { |e| h[:game][e] = db[e] } }
150
+ @wins.transaction { |db| db.keys.each { |e| h[:wins][e] = db[e] } }
151
+ @lvls.transaction { |db| db.keys.each { |e| h[:lvls][e] = db[e] } }
152
+ return h
153
+ end
154
+ end
155
+
156
+ class Game
157
+
158
+ attr_accessor :id
159
+
160
+ attr_reader :game, :wins, :lvls
161
+
162
+ def initialize i
163
+ @id = i
164
+ @game = PStore.new("db/game-game-#{i}.pstore")
165
+ @wins = PStore.new("db/game-wins-#{i}.pstore")
166
+ @lvls = PStore.new("db/game-lvls-#{i}.pstore")
167
+ end
168
+
169
+ def key k
170
+ #Z4blockchain.digest(%[#{@id}:#{k}])
171
+ %[#{@id}:#{k}]
172
+ end
173
+
174
+ def play h={}
175
+ GAMES.transaction { |db| x = db[@id].to_i; db[@id] = x + 1 }
176
+ @wins.transaction { |db|
177
+ x = db[h[:winner]].to_i;
178
+ db[h[:winner]] = x + 1;
179
+ u = USER[h[:winner]]
180
+ u.wins.transaction { |ddb| x = ddb[@id].to_i; ddb[@id] = x + 1; }
181
+ }
182
+ @game.transaction { |db|
183
+ [h[:players]].flatten.each { |e|
184
+ x = db[e].to_i;
185
+ db[e] = x + 1;
186
+ u = USER[e]
187
+ u.game.transaction { |ddb| x = ddb[@id].to_i; ddb[@id] = x + 1; }
188
+ }
189
+ }
190
+ if h.has_key? :award
191
+ @lvls.transaction { |db|
192
+ db[h[:award]] = h[:winner]
193
+ u = User.new(h[:winner])
194
+ u.lvls.transaction { |ddb|
195
+ LVL[h[:award]].incr h[:winner]
196
+ }
197
+ }
198
+ end
199
+ end
200
+ end
201
+
202
+ class Lvls
203
+ attr_accessor :id
204
+
205
+ attr_reader :user
206
+
207
+ def initialize i
208
+ @id = i
209
+ @user = PStore.new("lvls-user-#{i}")
210
+ end
211
+ def incr u
212
+ @user.transaction { |db| x = db[u].to_i; db[u] = x + 1; }
213
+ LVLS.transaction { |db| x = db[@id].to_i; db[@id] = x + 1 }
214
+ end
215
+ end
107
216
 
108
217
  class Bank
109
218
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: z4blockchain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Olson