-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRollsRoyce.sol
107 lines (89 loc) · 2.98 KB
/
RollsRoyce.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
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract RollsRoyce {
enum CoinFlipOption {
HEAD,
TAIL
}
address private bettingHouseOwner;
address public currentPlayer;
CoinFlipOption userGuess;
mapping(address => uint) playerConsecutiveWins;
mapping(address => bool) claimedPrizeMoney;
mapping(address => uint) playerPool;
constructor() payable {
bettingHouseOwner = msg.sender;
}
receive() external payable {}
function guess(CoinFlipOption _guess) external payable {
require(currentPlayer == address(0), "There is already a player");
require(msg.value == 1 ether, "To play it needs to be 1 ether");
currentPlayer = msg.sender;
depositFunds(msg.sender);
userGuess = _guess;
}
function revealResults() external {
require(
currentPlayer == msg.sender,
"Only the player can reveal the results"
);
CoinFlipOption winningOption = flipCoin();
if (userGuess == winningOption) {
playerConsecutiveWins[currentPlayer] =
playerConsecutiveWins[currentPlayer] +
1;
} else {
playerConsecutiveWins[currentPlayer] = 0;
}
currentPlayer = address(0);
}
function flipCoin() private view returns (CoinFlipOption) {
return
CoinFlipOption(
uint(
keccak256(abi.encodePacked(block.timestamp ^ 0x1F2DF76A6))
) % 2
);
}
function viewWins(address _addr) public view returns (uint) {
return playerConsecutiveWins[_addr];
}
function depositFunds(address _to) internal {
playerPool[_to] += msg.value;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(
address(this).balance >= amount,
"Address: insufficient balance"
);
(bool success, ) = recipient.call{value: amount}("");
}
function withdrawPrizeMoney(address _to) public payable {
require(
msg.sender == _to,
"Only the player can withdraw the prize money"
);
require(
playerConsecutiveWins[_to] >= 3,
"You need to win 3 or more consecutive games to claim the prize money"
);
if (playerConsecutiveWins[_to] >= 3) {
uint prizeMoney = playerPool[_to];
playerPool[_to] = 0;
sendValue(payable(_to), prizeMoney);
}
}
function withdrawFirstWinPrizeMoneyBonus() external {
require(
!claimedPrizeMoney[msg.sender],
"You have already claimed the first win bonus"
);
playerPool[msg.sender] += 1 ether;
withdrawPrizeMoney(msg.sender);
claimedPrizeMoney[msg.sender] = true;
}
function isSolved() public view returns (bool) {
// Return true if the game is solved
return address(this).balance == 0;
}
}