-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontractGamblingGame
More file actions
95 lines (74 loc) · 1.85 KB
/
contractGamblingGame
File metadata and controls
95 lines (74 loc) · 1.85 KB
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
contract GamblingGame{
struct Game{
uint numNumbers;
address master;
uint numBets;
uint pot;
bool open;
mapping(uint => address) betList; /// id -> gambler
mapping(uint => uint) numbersBet; /// id -> number
mapping(uint => uint) betAmount; /// id -> amount
mapping(uint => uint) numberAmount; /// number -> totalAmount
uint expirationDate;
}
/// List of games
mapping(uint => Game) gameList;
uint numGames;
function GamblingGame(){
numGames = 0;
}
function createGame(uint timeToBet){
numGames += 1;
Game newGame = gameList[numGames];
newGame.numNumbers = 10;
newGame.master = msg.sender;
newGame.numBets = 0;
newGame.pot = 0;
newGame.expirationDate = block.timestamp + timeToBet;
newGame.open = true;
}
function bet(uint id, uint numberBet){
Game g = gameList[id];
if(g.open){
if(numberBet <= g.numNumbers && numberBet > 0){
g.numBets += 1;
g.pot += msg.value;
g.betList[g.numBets] = msg.sender;
g.numbersBet[g.numBets] = numberBet;
g.betAmount[g.numBets] = msg.value;
g.numberAmount[numberBet] += msg.value;
}
checkExpirationDate(id);
}
}
function checkExpirationDate(uint id){
Game g = gameList[id];
if(block.timestamp > g.expirationDate){
g.open = false;
solveBets(id);
}
}
function solveBets(uint id){
Game g = gameList[id];
uint random = (block.timestamp % g.numNumbers) + 1;
uint betAmount = g.numberAmount[id];
uint i = 1;
address gambler;
if(betAmount == 0){
while(i <= g.numBets){
gambler = g.betList[i];
gambler.send(g.betAmount[i]);
i += 1;
}
}else{
while(i <= g.numBets){
gambler = g.betList[i];
if(random == g.numbersBet[i]){
uint userPrize = ((g.betAmount[i]*g.pot*1000)/betAmount)/1000;
gambler.send(userPrize);
}
i += 1;
}
}
}
}