z4blockchain 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/z4blockchain/version.rb +1 -1
- data/lib/z4blockchain.rb +115 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba2b348422bb80bbf15cda06a7e954f9a157a639af4046fa8f98a59e4c34b41e
|
4
|
+
data.tar.gz: 01a700eedcd7bc9f7563889ff5613aaded34d8999e803b424e6ecc0274d22a10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b67adef24419014deb07a683a179ae104964c12509dcf91cf0e09286a3b7d334a6feead248f1fb718334d38c1077d20a554d687281d03ed4b538754f405cfc9b
|
7
|
+
data.tar.gz: e991ac8b22dfaff8dff5f71b7528b3c6be806e8194c3ef20726ad4779093828291692492f6d20a7fc73e7bbda2b1d05a1850ef0a204e965c346b43e0fcdef7b8
|
data/lib/z4blockchain/version.rb
CHANGED
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
|
|
@@ -76,6 +86,12 @@ module Z4BC
|
|
76
86
|
return h
|
77
87
|
end
|
78
88
|
|
89
|
+
def self.balance u
|
90
|
+
x = 0
|
91
|
+
BANK.each_pair { |k,v| x += v[u] }
|
92
|
+
return x
|
93
|
+
end
|
94
|
+
|
79
95
|
def self.bank b
|
80
96
|
BANK[b]
|
81
97
|
end
|
@@ -98,6 +114,105 @@ module Z4BC
|
|
98
114
|
BLOCKCHAIN << JSON.generate(h)
|
99
115
|
return nil
|
100
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
|
101
216
|
|
102
217
|
class Bank
|
103
218
|
|