-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPS.sol
199 lines (151 loc) · 6.33 KB
/
RPS.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
pragma solidity >=0.4.22 <0.6.0;
pragma experimental ABIEncoderV2;
//import "https://github.com/pipermerriam/ethereum-string-utils/blob/master/contracts/StringLib.sol";
contract RPS {
address public manager; //only one that can make the contract happen
address payable public player1;
address payable public player2;
address payable public gameWages; //stores the total waged
uint public winningPlayer; //might not be needeed
address public winnerAddress; //might not be needed
//in case there is an excess
mapping(address => uint) public excess;
//the state machine
enum State{
START,
P1_PLAYED,
BOTH_PLAYED
}
State public gameState;
constructor() public {
manager = msg.sender;
gameState = State.START; //this is a state variable
}
//createa random numbers
uint nonce = 100;
function random() public view returns (uint) {
uint rand = uint(keccak256(abi.encodePacked(now, msg.sender, nonce))) % 900;
// rand = rand + 100;
// nonce++;
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$ IDK WHY THIS LIBRARY IMPORT ISNT WORKING
//rand = stringUtils.uintToBytes(rand); //using a call to an external library
return rand; //this is now in string format
}
//get the players' input
function encode_commitment_p1(string memory choice_p1, string memory rand) public pure returns (bytes32){
//concatonate the strings
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$ IDK if this imported the rand from function random ^^above
bytes memory _ch_p1 = bytes(choice_p1);
bytes memory _rand = bytes(rand);
string memory p1_string = new string(_ch_p1.length + _rand.length);
bytes memory choice_p1_rand = bytes(p1_string); //the concatonated string
return keccak256(bytes("choice_p1_rand")); //this is now in bytes
}
function encode_commitment_p2(string memory choice_p2, string memory rand) public pure returns (bytes32){
//concatonate the strings
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$ IDK if this imported the rand from function random ^^above
bytes memory _ch_p2 = bytes(choice_p2);
bytes memory _rand = bytes(rand);
string memory p2_string = new string(_ch_p2.length + _rand.length);
bytes memory choice_p2_rand = bytes(p2_string); //the concatonated string
return keccak256(bytes("choice_p2_rand")); //this is now in bytes
}
//STRUCT FOR BLINDING PLAYER1 RESPONSES
struct blinded_p1 {
bytes32 player1_choice;
uint p1_waged;
}
mapping(address => blinded_p1) public p1_blinded_choice;
//helper function
function choicePlayer1() pure public returns(blinded_p1 memory) {
}
//STRUCT FOR BLINDING PLAYER2 RESPONSES
struct blinded_p2 {
bytes32 player2_choice;
uint p1_waged;
}
mapping(address => blinded_p2) public p2_blinded_choice;
//helper function
function choicePlayer2() pure public returns(blinded_p2 memory) {
}
/*
Accepts a commitment (generated via encode_commitment) and a wager of ethereum
*/
function play(bytes32 commitment) public payable {
}
////$$$$$$$$$$$$$$$$$$$$$$$$$$$$ WAS WORKING ON THIS FUNCTION BELOW ///////
function playStateStart() public payable notManager returns(bool start){
require(gameState == State.START);
require(msg.value > 0.00001 ether); //requires players to place a minimum bid
uint currentWageP1 = player1.balance;
uint currentWageP2 = player2.balance;
//return money if bids are not equal
if(currentWageP2 > currentWageP1){
uint excessWager = player2.balance - player1.balance;
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$i dont know how to send back this money to player 2
require(msg.sender.send(excessWager));
//require(msg.sender.send(player2));
}else{
if(currentWageP2 < currentWageP1){
gameState = State.START;
}
}
return true;
}
function payWinner() public {
// uint index = random() % players.length;
// address winnerAddress = players[index];
require(msg.sender.send(manager.balance));
require(msg.sender.send(address(this).balance));
//only the manager pays the player out the balance
require(msg.sender == manager);
require(msg.sender.send(player1.balance));
//resets the game to play again, put this at the end of paying out
//players = new address(0);
}
/* After both players reveal, this allows the winner
to claim their reward (both wagers).
In the event of a tie, this function should let
each player withdraw their initial wager.
*/
function withdraw() public {
require(gameState == State.BOTH_PLAYED);
address winner;
uint winnings;
if(gameState == State.BOTH_PLAYED){
require(msg.sender == winner);
require(msg.sender.send(winner.balance));
//winner = msg.sender;
//winnings = gameWages(msg.sender);
}
}
// ////////MODIFIERS-- IDK IF TO BOTHER WITH THESE
//modifer so only manager can do these things
modifier restrictedManagerOnly() {
require(msg.sender == manager);
_;
}
//modifer so only player1 can do these things, player2 can't change player1's bid
modifier restrictedP1Only() {
require(msg.sender == player1);
_;
}
//modifer so only player2 can do these things, player2 can't change player1's bid
modifier restrictedP2Only() {
require(msg.sender == player2);
_;
}
// makes sure they both wager same amount
// $$$$$$$$$i I DON TTHINK THIS IS WORKING BUT DOESNT MATTER
modifier wageSame {
require(msg.sender.send(player1.balance) == msg.sender.send(player2.balance));
_;
//require(player1.)
}
//modifier so contract manager can't change other people's inputs
modifier notManager() {
require(msg.sender != manager);
_;
}
}