-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameMain.sol
217 lines (154 loc) · 5.94 KB
/
gameMain.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
pragma solidity ^0.4.18;
import "./commitReveal.sol";
import "./helper.sol";
contract gameMain is commitReveal, helper {
//Owner of contract
address owner;
//System variables (only to be changed by owner)
uint256 public wager = 10; //wei
uint256 public fee = 0; //wei
uint256 public deposit = 10 * wager;
//Players
address[3] public players;
//Seed, Guess per player
mapping (address => uint8) /**/public seeds; //sum of seeds % 100 gives winningNumber
mapping (address => uint8) /**/public guesses; //players' guesses
//Other variables
uint8 public numRevelations;
uint8 public winningNumber;
mapping (address => bool) /**/public hasDeposit;
mapping (address => uint8) /**/public difference;
//Flags
bool public acceptRevelations;
//Constructor
function gameMain () public {
owner = msg.sender;
newRound();
}
//Wrapper for commit
function doCommit (bytes32 _in) payable public returns (bool success) {
success = true;
if (msg.value == deposit+wager) {
//Add new player to free slot and accept his commit
if (players[0] == 0x0000000000000000000000000000000000000000) {
commit(_in);
players[0] = msg.sender;
notifyNewPlayer(msg.sender, "Player 1 entered.");
} else if (players[1] == 0x0000000000000000000000000000000000000000) {
commit(_in);
players[1] = msg.sender;
notifyNewPlayer(msg.sender, "Player 2 entered.");
} else if (players[2] == 0x0000000000000000000000000000000000000000) {
commit(_in);
players[2] = msg.sender;
notifyNewPlayer(msg.sender, "Player 3 entered.");
acceptRevelations = true;
} else {
success = false;
}
} else {
success = false;
}
if(success) {
hasDeposit[msg.sender] = true;
}
require(success);
}
//Wrapper for reveal
function doReveal (string _secret, uint8 _seed, uint8 _guess) public returns (bool success) {
success = false;
if (reveal (_secret, _seed, _guess) && acceptRevelations) {
if (players[0] == msg.sender) {
success = true;
notifyRevelation(msg.sender, "Player 1 revealed his commitment.");
} else if (players[1] == msg.sender) {
success = true;
notifyRevelation(msg.sender, "Player 2 revealed his commitment.");
} else if (players[2] == msg.sender) {
success = true;
notifyRevelation(msg.sender, "Player 3 revealed his commitment.");
}
}
if (success) {
numRevelations++;
seeds[msg.sender] = _seed % 100;
guesses[msg.sender] = _guess % 100;
hasDeposit[msg.sender] = false; //Prevent "Multiple Entry Attacks"
msg.sender.transfer(deposit);
if (numRevelations == 3) {
determine_winner();
}
}
}
//Function to calculate winningNumber
function calculate_winningNumber () internal {
uint256 sumOfSeeds = 0;
for (uint8 i=0; i<3; i++) {
sumOfSeeds = sumOfSeeds + seeds[players[i]];
}
winningNumber = uint8(sumOfSeeds % 100);
}
//Function to determine winner
function determine_winner () internal {
uint8 i;
uint8 multiWinners = 0;
address winner = players[0];
address[2] winner_candidates;
calculate_winningNumber();
//Calculate difference to winningNumber for each player's guess
for (i=0; i<3; i++) {
difference[players[i]] = diff(winningNumber, guesses[players[i]]);
}
//Check for smallest difference => winner
//If multiple players have the same difference a lot will be drawn
for (i=1; i<3; i++) {
if (difference[players[i]] < difference[winner]) {
//Smallest distance => single winner
winner = players[i];
multiWinners = 0;
} else if (difference[players[i]] == difference[winner]) {
//Same distance => multiple winner candidates
winner_candidates[multiWinners] = players[i];
multiWinners++;
}
}
//Draw a lot if there are multiple winner candidates
if (multiWinners > 0) {
uint8 r = winningNumber % (multiWinners+1);
if (r > 0) {
winner = winner_candidates[r-1];
}
}
winner.transfer(3 * wager - fee);
anounceWinner(winner, "We have a winner!");
newRound();
}
//Function to initialize a new round
//TODO: pay back deposits if owner resets the game
function newRound () internal {
//Clear Players
players = [0x0000000000000000000000000000000000000000,
0x0000000000000000000000000000000000000000,
0x0000000000000000000000000000000000000000];
//Clear other variables
numRevelations = 0;
//Clear flags
acceptRevelations = false;
}
//Function for owner to change system variables
function changeVariables (uint256 _wager, uint256 _fee) public onlyOwner {
wager = _wager;
fee = _fee;
deposit = 10 * _wager;
}
//Events
//TODO: Simplify to single event?
event notifyNewPlayer (address _player, string _msg);
event notifyRevelation (address _player, string _msg);
event anounceWinner (address _winner, string _msg);
//Modifiers
modifier onlyOwner {
require(msg.sender == owner);
_;
}
}