Guest User

Untitled

a guest
Jul 17th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. pragma solidity ^0.4.23;
  2.  
  3. contract Game {
  4. mapping (address => uint) playerFees;
  5. mapping (address => bytes1) playersResults;
  6. uint public feeAmount = 0 ether;
  7. address[] players;
  8. uint8 public amount = 0;
  9. uint8 played = 0;
  10. address gameInitiator;
  11. uint public maxGameDuration = 10 minutes;
  12. uint public joinPrice = 0.5 ether;
  13. uint timeStarted = 0 seconds;
  14. //todo: add round duration
  15. struct roundStruct {
  16. uint8 number;
  17. uint rockPeople;
  18. uint scissorsPeople;
  19. uint paperPeople;
  20. }
  21. struct resultStruct {
  22. bytes8 value;
  23. uint amount;
  24. }
  25. roundStruct round;
  26. resultStruct finalResults;
  27.  
  28. event gameStarted();
  29. event allPlayersVotedOrGameExpired(address receiver, bool isWinner); //verify if event sent to the right person
  30. event nextRound(address receiver);
  31. event log(uint msg);
  32.  
  33. function setFee(uint fee) public {
  34. require(gameInitiator == msg.sender, "Fee can be set by game initiator");
  35. require(fee > joinPrice, "Fee might be only bigger than 0.5 eth");
  36. joinPrice = fee;
  37. }
  38.  
  39. function join() public payable {
  40. require(msg.sender != gameInitiator, "There is already such player");
  41. require(msg.value > joinPrice, "Join price is invalid ");
  42. if(gameInitiator == address(0)) {
  43. gameInitiator = msg.sender;
  44. }
  45. playerFees[msg.sender] = msg.value;
  46. feeAmount += msg.value;
  47. players.push(msg.sender);
  48. amount++;
  49. }
  50.  
  51. function makeAChoice(bytes1 result) public {
  52. require(contains(msg.sender), "Sender doesnot exist in list of players");
  53. bool gameExpired = block.timestamp - timeStarted >= maxGameDuration;
  54. if (gameExpired) {
  55. calculateResult(gameExpired);
  56. }
  57. require(!gameExpired, "Game expired");
  58. require(playersResults[msg.sender] == 0, "Your vote was already received");
  59. //encrypt the code
  60. playersResults[msg.sender] = result;
  61. roundStruct memory tempRound = round;
  62. if (result == 1) {//rock
  63. round = roundStruct(tempRound.number, tempRound.rockPeople++, tempRound.scissorsPeople, tempRound.paperPeople);
  64. }
  65. if (result == 2) {//scissors
  66. round = roundStruct(tempRound.number, tempRound.rockPeople, tempRound.scissorsPeople++, tempRound.paperPeople);
  67. }
  68. if (result == 3) {//paper
  69. round = roundStruct(tempRound.number, tempRound.rockPeople, tempRound.scissorsPeople, tempRound.paperPeople++);
  70. }
  71. played += 1;
  72. if (played == amount) {
  73. calculateResult(false);
  74. }
  75. }
  76.  
  77. function startGame() public {
  78. require(amount > 1, "Players has to be more than one");
  79. require(timeStarted == 0x0, "Game was enabled already");
  80. timeStarted = block.timestamp;
  81. round = roundStruct(1,0,0,0);
  82. emit gameStarted();
  83. }
  84. //rules :
  85. //--rock crushes scissors
  86. //--paper covers rock
  87. //--scissors cuts paper
  88. function calculateResult(bool gameExpired) private{
  89. bool hasWinner = (round.rockPeople == 0 || round.scissorsPeople == 0 || round.paperPeople == 0) &&
  90. (played > round.rockPeople || played > round.scissorsPeople || played > round.paperPeople);
  91. bytes8 winner = 0;
  92. uint price = 0 ether;
  93. if (hasWinner) {
  94. finalResults = wins();
  95. winner = finalResults.value;
  96. uint resultAmount = finalResults.amount;
  97. if (resultAmount == 0) {
  98. resultAmount = 1;
  99. }
  100. price = (feeAmount - feeAmount / 20) / resultAmount;
  101. }
  102. for (uint i = 0; i < amount; i++) {
  103. if (hasWinner && winner > 0) {
  104. emit log(2);
  105. bool isWinner = playersResults[players[i]] == winner;
  106. emit allPlayersVotedOrGameExpired(players[i], isWinner);
  107. if (isWinner) {
  108. players[i].transfer(price);
  109. }
  110. } else if (!gameExpired) {
  111. emit nextRound(players[i]);
  112. round.number++;
  113. uint8 numb = round.number;
  114. round = roundStruct(numb, 0,0,0);
  115. }
  116. else {
  117. emit log(4);
  118. clearAllTheData();
  119. }
  120. }
  121. }
  122. function wins() private view returns (resultStruct) {
  123. if (round.rockPeople == 0 && round.scissorsPeople > 0 && round.paperPeople > 0) {
  124. return resultStruct(2, round.scissorsPeople);//scissors
  125. }
  126. if (round.rockPeople > 0 && round.scissorsPeople == 0 && round.paperPeople > 0) {
  127. return resultStruct(3, round.paperPeople);//paper
  128. }
  129. if (round.rockPeople > 0 && round.scissorsPeople > 0 && round.paperPeople == 0) {
  130. return resultStruct(1, round.rockPeople);//rock
  131. }
  132. return resultStruct(0,0);
  133. }
  134. function contains(address player) private view returns (bool){
  135. return playerFees[player] > 0;
  136. }
  137.  
  138. function clearAllTheData() private {
  139. timeStarted = 0x0;
  140. amount = 0;
  141. played = 0;
  142. joinPrice = 0.5 ether;
  143. feeAmount = 0;
  144. for (uint i = 0; i < amount; i++) {
  145. delete playersResults[players[i]];
  146. delete playerFees[players[i]];
  147. delete players[i];
  148. }
  149. }
  150. }
Add Comment
Please, Sign In to add comment