-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsps
More file actions
68 lines (56 loc) · 2.47 KB
/
sps
File metadata and controls
68 lines (56 loc) · 2.47 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
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.3/contracts/security/ReentrancyGuard.sol";
contract rpsv1 is ReentrancyGuard{
mapping (address => uint) public playerBalances;
event Received(address, uint);
//give the contract something to bet with
function fundContract() external payable {
emit Received(msg.sender, msg.value);
}
//deposit a player's funds
function deposit() external payable {
playerBalances[msg.sender] += msg.value;
}
//withdraw a player's funds
function withdraw() external nonReentrant {
uint playerBalance = playerBalances[msg.sender];
require(playerBalance > 0);
playerBalances[msg.sender] = 0;
(bool success, ) = address(msg.sender).call{ value: playerBalance }("");
require(success, "withdraw failed to send");
}
function getContractBalance() external view returns(uint contractBalance) {
return address(this).balance;
}
function playGame(string calldata _playerOneChoice, string calldata _playerTwoChoice, uint _gameStake) external returns(uint gameOutcome) {
require(playerBalances[msg.sender] >= _gameStake * (1 ether), "Not enough funds to place bet - please deposit more Ether.");
bytes memory b = bytes.concat(bytes(_playerOneChoice), bytes(_playerTwoChoice));
uint rslt;
if(keccak256(b) == keccak256(bytes("rockrock"))
|| keccak256(b) == keccak256(bytes("paperpaper"))
|| keccak256(b) == keccak256(bytes("scissorsscissors")))
{
//this is a draw
rslt = 0;
} else if(keccak256(b) == keccak256(bytes("scissorspaper"))
|| keccak256(b) == keccak256(bytes("rockscissors"))
|| keccak256(b) == keccak256(bytes("paperrock")))
{
//player 1 wins
playerBalances[msg.sender] += _gameStake * (1 ether);
rslt = 1;
} else if(keccak256(b) == keccak256(bytes("paperscissors"))
|| keccak256(b) == keccak256(bytes("scissorsrock"))
|| keccak256(b) == keccak256(bytes("rockpaper")))
{
//player 2 wins (the contract wins)
playerBalances[msg.sender] -= _gameStake * (1 ether);
rslt = 2;
}
else {
//there was a problem with this game...
rslt = 3;
}
return rslt;
}
}