-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGameScoreBet.sol
138 lines (129 loc) · 3.98 KB
/
GameScoreBet.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
contract GameScoreBet {
address initiator;
uint totalStake;
mapping ( address => uint ) stakeOption1;
mapping ( address => uint ) stakeOption2;
mapping ( address => uint ) stakeOption3;
mapping ( address => uint ) stakeOption4;
uint sumStake1;
uint sumStake2;
uint sumStake3;
uint sumStake4;
address[] listOption1; //list of participants of Option1
address[] listOption2;
address[] listOption3;
address[] listOption4; //list of participants of Option4
bool isOpen;
function GameScoreBet() {
initiator = msg.sender;
totalStake = 0;
sumStake1 = 0;
sumStake2 = 0;
sumStake3 = 0;
sumStake4 = 0;
isOpen = true;
}
function betOption1() {
if (!isOpen) throw;
else {
if(stakeOption1[msg.sender] > 0) {
stakeOption1[msg.sender] += msg.value;
sumStake1 += msg.value;
totalStake += msg.value;
}
else {
listOption1.push(msg.sender);
stakeOption1[msg.sender] = msg.value;
sumStake1 += msg.value;
totalStake += msg.value;
}
}
}
function betOption2() {
if (!isOpen) throw;
else {
if(stakeOption2[msg.sender] > 0) {
stakeOption2[msg.sender] += msg.value;
sumStake2 += msg.value;
totalStake += msg.value;
}
else {
listOption2.push(msg.sender);
stakeOption2[msg.sender] = msg.value;
sumStake2 += msg.value;
totalStake += msg.value;
}
}
}
function betOption3() {
if (!isOpen) throw;
else {
if(stakeOption3[msg.sender] > 0) {
stakeOption3[msg.sender] += msg.value;
sumStake3 += msg.value;
totalStake += msg.value;
}
else {
listOption3.push(msg.sender);
stakeOption3[msg.sender] = msg.value;
sumStake3 += msg.value;
totalStake += msg.value;
}
}
}
function betOption4() {
if (!isOpen) throw;
else {
if(stakeOption4[msg.sender] > 0) {
stakeOption4[msg.sender] += msg.value;
sumStake4 += msg.value;
totalStake += msg.value;
}
else {
listOption4.push(msg.sender);
stakeOption4[msg.sender] = msg.value;
sumStake4 += msg.value;
totalStake += msg.value;
}
}
}
function betStop() {
if (msg.sender != initiator) throw;
else {
isOpen = false;
}
}
function settle(uint opt) internal {
if(opt == 1) {
for(uint i = 0;i < listOption1.length; i++) {
listOption1[i].send(totalStake * (stakeOption1[listOption1[i]] / sumStake1) );
}
}
else if(opt == 2) {
for(uint j = 0;j < listOption2.length; j++) {
listOption2[j].send(totalStake * (stakeOption2[listOption2[j]] / sumStake2) );
}
}
else if(opt == 3) {
for(uint k = 0;k < listOption3.length; k++) {
listOption3[k].send(totalStake * (stakeOption3[listOption3[k]] / sumStake3) );
}
}
else if(opt == 4) {
for(uint p = 0;p < listOption4.length; p++) {
listOption4[p].send(totalStake * (stakeOption4[listOption4[p]] / sumStake4) );
}
}
else throw;
}
function getScore(uint answer) {
if(msg.sender != initiator) throw; //initiator could be replaced with score supplier
else {
isOpen = false;
settle(answer);
}
}
function getTotalStake() constant returns (uint) {
return totalStake;
}
}