-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetgame.cpp
299 lines (214 loc) · 8.12 KB
/
betgame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "betgame.hpp"
namespace gameio {
void betgame::bet(name from, name to, asset quantity, string memo){
if (from == get_self() || to != get_self()){
return;
}
name winner = name(memo);
auto team = teams.find(winner.value);
check(team != teams.end(), "team dosn't exists");
auto game = games.find(team->gameid);
check(game != games.end(), "game does't exists");
check(game->status == 1, "game playing or game over");
check((game->redteam == winner || game->blueteam == winner), "team is not playing");
record_index records(get_self(), game->id);
auto rowid = records.available_primary_key();
rowid = rowid > 0 ? rowid : 1;
auto timestamp = now();
auto temp = zero();
records.emplace(get_self(), [&](auto& row){
row.id = rowid;
row.user = from;
row.winner = winner;
row.quantity = quantity;
row.timestamp = timestamp;
});
// update game info
if (game->redteam == winner){
games.modify(game, get_self(), [&](auto& row){
row.ramount += quantity;
row.rusercount += 1;
});
}else{
games.modify(game, get_self(), [&](auto& row){
row.bamount += quantity;
row.busercount += 1;
});
}
// update user info
auto user = users.find(from.value);
if (user == users.end()){
users.emplace(get_self(), [&](auto& row){
row.name = from;
row.count = 1;
row.amount = quantity;
row.reward = temp;
row.totalreward = temp;
row.reg_time = timestamp;
});
}else{
users.modify(user, get_self(), [&](auto& row){
row.count += 1;
row.amount += quantity;
});
}
}
// start a game (only
void betgame::start(name red, name blue, name admin){
auto ft = teams.find(red.value);
auto st = teams.find(blue.value);
check((ft != teams.end() && st != teams.end()), "team don't exists");
check((ft->playing == false && st->playing == false), "team is playing can't start new");
auto rowid = games.available_primary_key();
rowid = rowid > 0 ? rowid : 1;
auto timestamp = now();
auto temp = zero();
games.emplace(get_self(), [&](auto& row){
row.id = rowid;
row.redteam = red;
row.blueteam = blue;
row.admin = admin;
row.timestamp = timestamp;
row.ramount = temp;
row.bamount = temp;
row.status = 1;
});
teams.modify(ft, get_self(), [&](auto& row){
row.gameid = rowid;
row.playing = true;
});
teams.modify(st, get_self(), [&](auto& row){
row.gameid = rowid;
row.playing = true;
});
}
void betgame::play(uint64_t gameid){
auto game = games.find(gameid);
check(game != games.end(), "Game don't exists");
check(game->status == 1, "game over");
require_auth(game->admin);
auto timestamp = now();
games.modify(game, get_self(), [&](auto& row){
row.status = 2;
row.timestamp = timestamp;
});
}
void betgame::editeam(string action, name title, string description){
require_auth(get_self());
auto team = teams.find(title.value);
if (action == "delete"){
check(team != teams.end(), "Team don't exists");
teams.erase(team);
print("Delete succeed");
}else if(action == "create"){
check(team == teams.end(), "Team already created");
teams.emplace(get_self(), [&](auto& row){
row.title = title;
row.description = description;
row.playing = false;
});
print("Create complete: ", title);
}else{
print("Unsupported action");
}
}
// 发起结果录入,只有指定的管理员能进行操作
void betgame::lottery(uint64_t gameid, name winner){
auto team = teams.find(winner.value);
check(team != teams.end(), "Team don't exists");
auto game = games.find(gameid);
check(game != games.end(), "Game don't exists");
check(game->status == 2, "game over or game not start");
check((game->redteam == winner || game->blueteam == winner), "winner don't playing");
require_auth(game->admin);
auto timestamp = now();
games.modify(game, get_self(), [&](auto& row){
row.status = 3;
row.winner = winner;
row.timestamp = timestamp;
});
auto redteam = teams.find(game->redteam.value);
if (redteam != teams.end()){
teams.modify(redteam, get_self(), [&](auto& row){
row.gameid = 0;
row.playing = false;
});
}
auto blueteam = teams.find(game->blueteam.value);
if (blueteam != teams.end()){
teams.modify(blueteam, get_self(), [&](auto& row){
row.gameid = 0;
row.playing = false;
});
}
}
void betgame::claim(uint64_t gameid, name account){
require_auth(account);
// 查询游戏信息
auto game = games.find(gameid);
check(game != games.end(), "Game don't exists");
check(game->status == 3, "Game does'n end");
// 查询用户信息
auto user = users.find(account.value);
check(user != users.end(), "user don't exists");
// 找出投注记录
record_index records(get_self(), gameid);
auto secondary = records.get_index<name("user")>();
auto bet_records = secondary.find(account.value);
auto total = game->ramount + game->bamount;
auto pool = game->ramount;
if (game->blueteam == game->winner){
pool = game->bamount;
}
// 判断胜利的记录
while (bet_records != secondary.end() && bet_records->user == account){
if (game->winner == bet_records->winner && bet_records->claimed == false){
auto reward_amount = total.amount * (bet_records->quantity.amount * 1.0 / pool.amount);
auto reward = asset(reward_amount, EOS_SYMBOL);
users.modify(user, get_self(), [&](auto& row){
row.reward += reward;
row.totalreward += reward;
});
auto record = records.find(bet_records->id);
records.modify(record, get_self(), [&](auto& row){
row.claimed = true;
});
}
bet_records++;
}
user = users.find(account.value);
if (user->reward.amount > 0){
// send EOS
send(EOS_TOKEN_CONTRACT, account, user->reward, "claim");
}
auto temp = zero();
auto timestamp = now();
users.modify(user, get_self(), [&](auto& row){
row.reward = temp;
row.claim_time = timestamp;
});
}
void betgame::send(name contract, name user, asset quantity, string memo){
auto data = std::make_tuple(get_self(), user, quantity, memo);
action(permission_level{get_self(), name("active")}, contract, name("transfer"), data).send();
}
uint64_t betgame::now(){
return current_time_point().sec_since_epoch();
}
asset betgame::zero(){
return asset(0, EOS_SYMBOL);
}
void betgame::clear(name team, uint64_t gameid){
record_index records(get_self(), gameid);
std::vector <uint64_t> keyForRecords;
for (auto &record : records) {
keyForRecords.push_back(record.id);
}
for (uint64_t id : keyForRecords) {
auto record = records.find(id);
if (record != records.end()) {
records.erase(record);
}
}
}
}