-
Notifications
You must be signed in to change notification settings - Fork 0
/
Janken.sol
261 lines (216 loc) · 8.81 KB
/
Janken.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
pragma solidity >=0.5.0 <0.6.0;
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
contract Janken {
using SafeMath for uint256;
struct Game {
GameStatus status;
address host;
address opponent;
bytes32 hostEncryptedHand;
bytes32 opponentEncryptedHand;
Hand hostDecryptedHand;
Hand opponentDecryptedHand;
bytes32 hostSecret;
bytes32 opponentSecret;
uint256 deadlineToJoin;
uint256 deadlineToReveal;
mapping (address => uint256) deposits;
mapping (address => uint256) allowedWithdrawal;
}
enum Hand { Null, Rock, Paper, Scissors }
enum Result { Null, Draw, Win, Loss }
enum GameStatus {
DoesNotExist,
Created,
Started,
AcceptingWithdrawal,
Finished
}
uint256 public gameId = 0;
uint256 public defaultWaitingWindow = 1 days;
uint256 public minDeposit = 1 finney;
mapping (uint256 => Game) public games;
constructor() public payable {}
function () external payable {}
event Created(
uint256 gameId,
address host,
uint256 deposit,
uint256 deadlineToJoin
);
event Started(
uint256 gameId,
address host,
address opponent,
uint256 deposit,
uint256 deadlineToReveal
);
event AcceptingWithdrawal(
uint256 gameId,
address host,
address opponent,
uint256 hostAllowedAmountToWithdrawal,
uint256 opponentAllowedAmountToWithdrawal
);
function createGame(bytes32 encryptedHand) public payable {
require(msg.value >= minDeposit, "deposit must be greater than minDeposit");
gameId += 1;
Game storage game = games[gameId];
game.host = msg.sender;
game.deposits[msg.sender] = msg.value;
game.hostEncryptedHand = encryptedHand;
// solium-disable-next-line security/no-block-members
game.deadlineToJoin = block.timestamp.add(defaultWaitingWindow);
game.status = GameStatus.Created;
emit Created(
gameId,
game.host,
game.deposits[game.host],
game.deadlineToJoin
);
}
function joinGame(uint256 id, bytes32 encryptedHand) public payable {
Game storage game = games[id];
require(game.status != GameStatus.DoesNotExist, "the game does not exist");
gameStatusShouldBe(game, GameStatus.Created);
require(msg.value == game.deposits[game.host], "deposit amount must be equal the game host's amount");
require(hasNotOver(game.deadlineToJoin), "the game was closed for participation");
game.opponent = msg.sender;
game.deposits[msg.sender] = msg.value;
game.opponentEncryptedHand = encryptedHand;
// solium-disable-next-line security/no-block-members
game.deadlineToReveal = block.timestamp.add(defaultWaitingWindow);
game.status = GameStatus.Started;
emit Started(
gameId,
game.host,
game.opponent,
game.deposits[game.opponent],
game.deadlineToReveal
);
}
function revealHand(uint256 id, uint256 handInt, bytes32 secret) public {
Game storage game = games[id];
gameStatusShouldBe(game, GameStatus.Started);
restrictAccessOnlyParticipants(game, msg.sender);
require(hasNotOver(game.deadlineToReveal), "the deadline to reveal your hand of this game has passed");
Hand hand = convertIntToHand(handInt);
bytes32 eHand = encryptedHand(handInt, secret);
if (msg.sender == game.host) {
require(game.hostEncryptedHand == eHand, "commit verification is failed");
game.hostDecryptedHand = hand;
game.hostSecret = secret;
} else if (msg.sender == game.opponent) {
require(game.opponentEncryptedHand == eHand, "commit verification is failed");
game.opponentDecryptedHand = hand;
game.opponentSecret = secret;
}
if (game.hostDecryptedHand != Hand.Null && game.opponentDecryptedHand != Hand.Null) {
Result result = judge(game.hostDecryptedHand, game.opponentDecryptedHand);
if (result == Result.Win) {
game.allowedWithdrawal[game.host] = game.deposits[game.host].mul(2);
} else if (result == Result.Loss) {
game.allowedWithdrawal[game.opponent] = game.deposits[game.opponent].mul(2);
} else if (result == Result.Draw) {
game.allowedWithdrawal[game.host] = game.deposits[game.host];
game.allowedWithdrawal[game.opponent] = game.deposits[game.opponent];
}
game.status = GameStatus.AcceptingWithdrawal;
emit AcceptingWithdrawal(
gameId,
game.host,
game.opponent,
game.allowedWithdrawal[game.host],
game.allowedWithdrawal[game.opponent]
);
}
}
function withdraw(uint256 id) public {
Game storage game = games[id];
gameStatusShouldBe(game, GameStatus.AcceptingWithdrawal);
restrictAccessOnlyParticipants(game, msg.sender);
uint256 allowedAmount = game.allowedWithdrawal[msg.sender];
require(allowedAmount != 0, "you aren't eligible to withdraw");
game.allowedWithdrawal[msg.sender] = 0;
msg.sender.transfer(allowedAmount);
}
function rescue(uint256 id) public {
Game storage game = games[id];
restrictAccessOnlyParticipants(game, msg.sender);
require(isAllowedToRescueAtCreated(game) || isAllowedToRescueAtStarted(game), "invalid rescue");
uint256 hostDeposit = game.deposits[game.host];
uint256 opponentDeposit = game.deposits[game.opponent];
game.deposits[game.host] = 0;
game.deposits[game.opponent] = 0;
msg.sender.transfer(hostDeposit.add(opponentDeposit));
}
function getAllowedWithdrawalAmount(uint256 id, address addr) public view returns (uint256) {
Game storage game = games[id];
return game.allowedWithdrawal[addr];
}
function depositOf(uint id, address addr) public view returns (uint256) {
Game storage game = games[id];
return game.deposits[addr];
}
function judge(Hand hand1, Hand hand2) public pure returns (Result) {
if (hand1 == hand2) {
return Result.Draw;
} else if (
(hand1 == Hand.Rock && hand2 == Hand.Scissors) ||
(hand1 == Hand.Paper && hand2 == Hand.Rock) ||
(hand1 == Hand.Scissors && hand2 == Hand.Paper)
) {
return Result.Win;
} else if (
(hand1 == Hand.Rock && hand2 == Hand.Paper) ||
(hand1 == Hand.Paper && hand2 == Hand.Scissors) ||
(hand1 == Hand.Scissors && hand2 == Hand.Rock)
) {
return Result.Loss;
} else {
assert(false);
}
}
function isAllowedToRescueAtCreated(Game storage game) private view returns(bool) {
return game.status == GameStatus.Created &&
msg.sender == game.host &&
game.deposits[game.host] != 0 &&
hasOver(game.deadlineToJoin);
}
function isAllowedToRescueAtStarted(Game storage game) private view returns(bool) {
return game.status == GameStatus.Started &&
isRevealed(game, msg.sender) &&
game.deposits[game.host] != 0 &&
game.deposits[game.opponent] != 0 &&
hasOver(game.deadlineToReveal);
}
function hasOver(uint256 time) private view returns(bool) {
// solium-disable-next-line security/no-block-members
return block.timestamp > time;
}
function hasNotOver(uint256 time) private view returns(bool) {
return !hasOver(time);
}
function isRevealed(Game memory game, address addr) private pure returns(bool) {
require(addr == game.host || addr == game.opponent, "Unknown player");
if (addr == game.host) {
return game.hostDecryptedHand != Hand.Null;
} else if (addr == game.opponent) {
return game.opponentDecryptedHand != Hand.Null;
}
}
function restrictAccessOnlyParticipants(Game memory game, address sender) private pure {
require(sender == game.host || sender == game.opponent, "forbidden");
}
function gameStatusShouldBe(Game memory game, GameStatus status) private pure {
require(game.status == status, "status is invalid");
}
function convertIntToHand(uint256 handInt) private pure returns (Hand) {
Hand hand = Hand(handInt);
require(hand == Hand.Rock || hand == Hand.Paper || hand == Hand.Scissors, "Invalid value");
return hand;
}
function encryptedHand(uint256 handInt, bytes32 secret) private pure returns (bytes32) {
return keccak256(abi.encodePacked(handInt, secret));
}
}