-
Notifications
You must be signed in to change notification settings - Fork 0
/
BetGameContract.sol
338 lines (274 loc) · 10.4 KB
/
BetGameContract.sol
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
pragma experimental ABIEncoderV2;
/**
* SafeMath
*/
library SafeMath {
function mul(uint256 a, uint256 b) internal constant returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal constant returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function sub(uint256 a, uint256 b) internal constant returns (uint256) {
assert(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal constant returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function Ownable() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) onlyOwner public {
require(newOwner != address(0));
OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
// 竞猜
contract GuessingGame is Ownable{
using SafeMath for uint256;
//event
event betEvent(address _betAddress, uint _betNum, uint _betValue, bytes _betData, uint timestamp);
event betBeginEvent(uint _blockNum);
event betEndEvent(uint _blockNum);
event drawEvent(uint _blockNum, uint _drawNum);
event distributeEvent(uint _blockNum, address _betAddress, uint _betGet);
struct DrawHistory{
uint drawNum;
uint blockNum;
uint totalBet;
}
//struct define
struct BetInfo{
address betAddress;
uint betValue;
uint bbnum;
}
struct GuessingGameInfo{
uint gameBegin;
uint gameEnd;
uint betBegin;
uint betEnd;
uint drawBlock;
uint distributeBlock;
uint drawNum;
uint totalBet;
uint number;
BetInfo[] betInfos;
mapping(uint => uint) betPoolInfo;
}
DrawHistory[] drawHistory; //
GuessingGameInfo[] public gameHistory;
GuessingGameInfo public lastGameInfo; // last gameinfo
GuessingGameInfo public currentGameInfo; // current gameinfo
//BetInfo[] public betInfos;
uint public fee;
address public feeAddress;
uint public gameBeginInterval;
uint public betInterval;
uint public drawInterval;
uint public distributeInterval;
uint public gameEndInterval;
uint public currentPhase;
bool public enable;
uint public gameStatus; // 0: game begin 1:betting begin 2:betting end 3:drawing/distribute beging 4:game end
function GuessingGame(address _feeAddress){
gameBeginInterval = 10;
betInterval = 1000;
drawInterval = 10;
gameEndInterval = 10;
distributeInterval = 0;
enable = true;
fee = 100; // 1%
feeAddress = _feeAddress;
currentGameInfo.gameBegin = block.number;
currentGameInfo.betBegin = currentGameInfo.gameBegin + gameBeginInterval;
currentGameInfo.betEnd = currentGameInfo.betBegin + betInterval;
currentGameInfo.drawBlock = currentGameInfo.betEnd + drawInterval;
currentGameInfo.number = 0;
currentGameInfo.totalBet += this.balance;
gameStatus = 0;
currentPhase++;
}
function DestroyGame(address _newContract) onlyOwner{
require(_newContract != address(0));
selfdestruct(_newContract);
}
function initNextGame() public onlyOwner{
require(gameStatus == 3 && block.number >= currentGameInfo.gameEnd);
lastGameInfo = currentGameInfo;
if (gameHistory.length > 100) {
delete gameHistory[0];
}
gameHistory.push(currentGameInfo);
delete currentGameInfo;
clearBetInfo();
currentGameInfo.gameBegin = block.number;
currentGameInfo.betBegin = currentGameInfo.gameBegin + gameBeginInterval;
currentGameInfo.betEnd = currentGameInfo.betBegin + betInterval;
currentGameInfo.drawBlock = currentGameInfo.betEnd + drawInterval;
currentGameInfo.totalBet += this.balance;
currentGameInfo.number = 0;
gameStatus = 0;
currentPhase++;
}
function setGamePama(uint _gameBeginInterval, uint _betInterval, uint _drawInterval, uint _distributeInterval,uint _gameEndInterval, bool _enable) onlyOwner{
require(block.number > currentGameInfo.gameEnd && _gameBeginInterval >= 0 && _gameEndInterval >= 0 && _betInterval >= 0 && _drawInterval >= 0 && _distributeInterval >= 0);
gameBeginInterval = _gameBeginInterval;
betInterval = _betInterval;
drawInterval = _drawInterval;
distributeInterval = _distributeInterval;
gameEndInterval = _gameEndInterval;
enable = _enable;
}
function setFee(uint _fee, address _feeAddress) onlyOwner{
require(_feeAddress != address(0) && _fee >= 0 && _fee < fee);
feeAddress = _feeAddress;
fee = _fee;
}
function random(uint offSet) internal constant returns (uint){
require(offSet >= 0);
bytes32 blockhash = block.blockhash(lastGameInfo.drawBlock - offSet);
uint random = uint(blockhash) + block.timestamp;
return random;
}
//touzhu
function () payable{
address betAddress = msg.sender;
uint betValue = msg.value;
uint num = bytesToUint(msg.data);
betEvent(betAddress, num, betValue, msg.data, block.timestamp);
require(betAddress != address(0) && betValue > 0 && betNumValid(num) && canBet());
currentGameInfo.betInfos.push(BetInfo(betAddress, betValue, num));
currentGameInfo.betPoolInfo[num] += betValue;
currentGameInfo.totalBet += betValue;
}
function drawing() onlyOwner{
require(gameStatus == 0 && block.number >= currentGameInfo.drawBlock);
uint randomNum = random(0);
uint offSet = randomNum % 200 + 1;
randomNum = random(offSet);
uint randomDrawNum = randomNum % 7 + 1;
currentGameInfo.drawNum = randomDrawNum;
DrawHistory memory history = DrawHistory(currentGameInfo.drawNum, currentGameInfo.drawBlock, currentGameInfo.totalBet);
drawHistory.push(history);
drawEvent(randomDrawNum, block.number);
uint betNumPool = currentGameInfo.betPoolInfo[randomDrawNum];
currentGameInfo.number = betNumPool;
if(betNumPool > 0){
for(uint i = 0; i < currentGameInfo.betInfos.length; ++i){
BetInfo info = currentGameInfo.betInfos[i];
if(info.bbnum == randomDrawNum){
uint betGet = currentGameInfo.totalBet * info.betValue / betNumPool;
uint betFee = betGet * fee / 10000;
betGet -= betFee;
info.betAddress.transfer(betGet);
feeAddress.transfer(betFee);
distributeEvent(block.number, info.betAddress, betGet);
// currentGameInfo.totalBet = currentGameInfo.totalBet - betGet - betFee;
}
}
}
gameStatus = 3;
currentGameInfo.distributeBlock = block.number;
currentGameInfo.gameEnd = currentGameInfo.distributeBlock + gameEndInterval;
}
function getDrawHistory() public constant returns (uint[3][]){
uint256 length = drawHistory.length;
uint[3][] memory historys = new uint[3][](length);
for(uint i = 0; i < drawHistory.length; ++i){
historys[i][0] = drawHistory[i].blockNum;
historys[i][1] = drawHistory[i].drawNum;
historys[i][2] = drawHistory[i].totalBet;
}
return historys;
}
function getBetHistoryByAddress(address _betAddress) public constant returns(uint[11][]){
uint[11][] memory addressBetInfos = new uint[11][](101);
uint k = 0;
for (uint i = 0; i < gameHistory.length; i ++) {
GuessingGameInfo game = gameHistory[i];
uint drawBlock = game.drawBlock;
bool hasRecord = false;
for (uint j = 0; j < game.betInfos.length; j ++) {
BetInfo info = game.betInfos[j];
if (info.betAddress == _betAddress) {
addressBetInfos[k][info.bbnum] += info.betValue;
hasRecord = true;
}
}
if (hasRecord) {
addressBetInfos[k][0] = drawBlock;
addressBetInfos[k][8] = game.drawNum;
addressBetInfos[k][9] = game.number;
addressBetInfos[k][10] = game.totalBet;
k ++;
}
}
hasRecord = false;
for (i = 0; i < currentGameInfo.betInfos.length; i ++) {
info = currentGameInfo.betInfos[i];
if (info.betAddress == _betAddress) {
addressBetInfos[k][info.bbnum] += info.betValue;
hasRecord = true;
}
}
drawBlock = currentGameInfo.drawBlock;
uint drawNum = currentGameInfo.drawNum;
if (hasRecord) {
addressBetInfos[k][0] = drawBlock;
addressBetInfos[k][8] = drawNum;
addressBetInfos[k][9] = game.number;
addressBetInfos[k][10] = game.totalBet;
k ++;
}
return (addressBetInfos);
}
function getGameBetInfo() public constant returns(uint[]) {
uint256 length = 7;
uint256[] memory betInfo = new uint256[](length);
for (uint i = 0; i < length; i++) {
betInfo[i] = currentGameInfo.betPoolInfo[i+1];
}
return betInfo;
}
//
function canBet() internal constant returns(bool){
if(enable && block.number >= currentGameInfo.betBegin && block.number <= currentGameInfo.betEnd){
return true;
}
return false;
}
function betNumValid(uint betNum) internal constant returns(bool){
if(betNum >= 1 && betNum <=7) return true;
return false;
}
function bytesToUint(bytes _bytes) internal constant returns (uint){
uint b = uint(_bytes[3]);
return b;
}
function clearBetInfo() internal constant {
for (uint i = 0; i < 7; i++) {
currentGameInfo.betPoolInfo[i+1] = 0;
}
}
}