|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.24; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; |
| 5 | +import "@openzeppelin/contracts/access/Ownable.sol"; |
| 6 | + |
| 7 | +contract AgentTrumpGame is ReentrancyGuard, Ownable { |
| 8 | + // State variables remain the same |
| 9 | + uint256 public gameEndTime; |
| 10 | + uint256 public constant INITIAL_GAME_DURATION = 30 minutes; |
| 11 | + uint256 public constant ESCALATION_PERIOD = 5 minutes; |
| 12 | + uint256 public constant BASE_MULTIPLIER = 200; |
| 13 | + uint256 public constant GAME_FEE = 0.0009 ether; |
| 14 | + uint256 public escalationStartTime; |
| 15 | + uint256 public currentMultiplier; |
| 16 | + uint256 public lastGuessTime; |
| 17 | + uint256 public totalCollected; |
| 18 | + uint256 public currentRequiredAmount; |
| 19 | + address public lastPlayer; |
| 20 | + bool public gameWon; |
| 21 | + bool public escalationActive; |
| 22 | + |
| 23 | + struct PlayerResponse { |
| 24 | + string response; |
| 25 | + uint256 timestamp; |
| 26 | + bool exists; |
| 27 | + } |
| 28 | + |
| 29 | + mapping(address => PlayerResponse[]) public playerResponses; |
| 30 | + |
| 31 | + // Events remain the same |
| 32 | + event GuessSubmitted( |
| 33 | + address indexed player, |
| 34 | + uint256 amount, |
| 35 | + uint256 multiplier, |
| 36 | + string response, |
| 37 | + uint256 timestamp, |
| 38 | + uint256 responseIndex |
| 39 | + ); |
| 40 | + event GameWon(address indexed winner, uint256 reward); |
| 41 | + event GameEnded(address indexed lastPlayer, uint256 lastPlayerReward, uint256 ownerReward); |
| 42 | + event EscalationStarted(uint256 startTime); |
| 43 | + event GameExtended(uint256 newEndTime, uint256 newMultiplier); |
| 44 | + |
| 45 | + // Constructor and other functions remain the same until endGame |
| 46 | + constructor() Ownable(msg.sender) { |
| 47 | + gameEndTime = block.timestamp + INITIAL_GAME_DURATION; |
| 48 | + currentMultiplier = 100; |
| 49 | + lastGuessTime = block.timestamp; |
| 50 | + currentRequiredAmount = GAME_FEE; |
| 51 | + } |
| 52 | + |
| 53 | + // Modified endGame function |
| 54 | + function endGame() external onlyOwner nonReentrant { |
| 55 | + require(totalCollected > 0, "No funds to distribute"); |
| 56 | + require(lastPlayer != address(0), "No players participated"); |
| 57 | + |
| 58 | + uint256 lastPlayerReward = (totalCollected * 10) / 100; |
| 59 | + uint256 ownerReward = totalCollected - lastPlayerReward; |
| 60 | + |
| 61 | + totalCollected = 0; |
| 62 | + |
| 63 | + (bool success1, ) = payable(lastPlayer).call{value: lastPlayerReward}(""); |
| 64 | + require(success1, "Last player reward transfer failed"); |
| 65 | + |
| 66 | + (bool success2, ) = payable(owner()).call{value: ownerReward}(""); |
| 67 | + require(success2, "Owner reward transfer failed"); |
| 68 | + |
| 69 | + // Update game state |
| 70 | + gameEndTime = block.timestamp; |
| 71 | + gameWon = false; |
| 72 | + |
| 73 | + emit GameEnded(lastPlayer, lastPlayerReward, ownerReward); |
| 74 | + } |
| 75 | + |
| 76 | + // Rest of the contract functions remain unchanged |
| 77 | + function deposit() external payable onlyOwner { |
| 78 | + require(msg.value > 0, "Must deposit some ETH"); |
| 79 | + } |
| 80 | + |
| 81 | + function withdraw() external onlyOwner { |
| 82 | + require(block.timestamp > gameEndTime || gameWon, "Game still in progress"); |
| 83 | + require(address(this).balance > 0, "No balance to withdraw"); |
| 84 | + |
| 85 | + (bool success, ) = payable(owner()).call{value: address(this).balance}(""); |
| 86 | + require(success, "Withdraw failed"); |
| 87 | + } |
| 88 | + |
| 89 | + function getCurrentRequiredAmount() public view returns (uint256) { |
| 90 | + if (!escalationActive) return GAME_FEE; |
| 91 | + return currentRequiredAmount; |
| 92 | + } |
| 93 | + |
| 94 | + function shouldStartEscalation() public view returns (bool) { |
| 95 | + return !escalationActive && |
| 96 | + block.timestamp >= (gameEndTime - ESCALATION_PERIOD); |
| 97 | + } |
| 98 | + |
| 99 | + function shouldExtendGame() public view returns (bool) { |
| 100 | + return escalationActive && |
| 101 | + block.timestamp <= gameEndTime && |
| 102 | + (block.timestamp - lastGuessTime) <= ESCALATION_PERIOD; |
| 103 | + } |
| 104 | + |
| 105 | + function submitGuess(string calldata response) external payable nonReentrant { |
| 106 | + require(!gameWon, "Game already won"); |
| 107 | + require(bytes(response).length > 0, "Response cannot be empty"); |
| 108 | + require(bytes(response).length <= 1000, "Response too long"); |
| 109 | + |
| 110 | + if (shouldStartEscalation()) { |
| 111 | + escalationActive = true; |
| 112 | + escalationStartTime = block.timestamp; |
| 113 | + currentRequiredAmount = GAME_FEE * BASE_MULTIPLIER / 100; |
| 114 | + emit EscalationStarted(escalationStartTime); |
| 115 | + } |
| 116 | + |
| 117 | + require(msg.value >= currentRequiredAmount, "Insufficient payment"); |
| 118 | + |
| 119 | + if (shouldExtendGame()) { |
| 120 | + gameEndTime = block.timestamp + ESCALATION_PERIOD; |
| 121 | + currentRequiredAmount = currentRequiredAmount * BASE_MULTIPLIER / 100; |
| 122 | + emit GameExtended(gameEndTime, currentRequiredAmount); |
| 123 | + } |
| 124 | + |
| 125 | + require(block.timestamp <= gameEndTime, "Game has ended"); |
| 126 | + |
| 127 | + if (msg.value > currentRequiredAmount) { |
| 128 | + uint256 excess = msg.value - currentRequiredAmount; |
| 129 | + (bool success, ) = payable(msg.sender).call{value: excess}(""); |
| 130 | + require(success, "Refund failed"); |
| 131 | + } |
| 132 | + |
| 133 | + uint256 ownerShare = (currentRequiredAmount * 30) / 100; |
| 134 | + (bool success2, ) = payable(owner()).call{value: ownerShare}(""); |
| 135 | + require(success2, "Owner payment failed"); |
| 136 | + |
| 137 | + playerResponses[msg.sender].push(PlayerResponse({ |
| 138 | + response: response, |
| 139 | + timestamp: block.timestamp, |
| 140 | + exists: true |
| 141 | + })); |
| 142 | + |
| 143 | + uint256 responseIndex = playerResponses[msg.sender].length - 1; |
| 144 | + |
| 145 | + totalCollected += (currentRequiredAmount - ownerShare); |
| 146 | + lastPlayer = msg.sender; |
| 147 | + lastGuessTime = block.timestamp; |
| 148 | + |
| 149 | + emit GuessSubmitted( |
| 150 | + msg.sender, |
| 151 | + currentRequiredAmount, |
| 152 | + currentMultiplier, |
| 153 | + response, |
| 154 | + block.timestamp, |
| 155 | + responseIndex |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + function getPlayerResponseCount(address player) external view returns (uint256) { |
| 160 | + return playerResponses[player].length; |
| 161 | + } |
| 162 | + |
| 163 | + function getPlayerResponseByIndex(address player, uint256 index) external view returns ( |
| 164 | + string memory response, |
| 165 | + uint256 timestamp, |
| 166 | + bool exists |
| 167 | + ) { |
| 168 | + require(index < playerResponses[player].length, "Response index out of bounds"); |
| 169 | + PlayerResponse memory playerResponse = playerResponses[player][index]; |
| 170 | + return (playerResponse.response, playerResponse.timestamp, playerResponse.exists); |
| 171 | + } |
| 172 | + |
| 173 | + function getAllPlayerResponses(address player) external view returns ( |
| 174 | + string[] memory responses, |
| 175 | + uint256[] memory timestamps, |
| 176 | + bool[] memory exists |
| 177 | + ) { |
| 178 | + uint256 responseCount = playerResponses[player].length; |
| 179 | + |
| 180 | + responses = new string[](responseCount); |
| 181 | + timestamps = new uint256[](responseCount); |
| 182 | + exists = new bool[](responseCount); |
| 183 | + |
| 184 | + for (uint256 i = 0; i < responseCount; i++) { |
| 185 | + PlayerResponse memory response = playerResponses[player][i]; |
| 186 | + responses[i] = response.response; |
| 187 | + timestamps[i] = response.timestamp; |
| 188 | + exists[i] = response.exists; |
| 189 | + } |
| 190 | + |
| 191 | + return (responses, timestamps, exists); |
| 192 | + } |
| 193 | + |
| 194 | + function buttonPushed(address winner) external onlyOwner nonReentrant { |
| 195 | + require(block.timestamp <= gameEndTime, "Game has ended"); |
| 196 | + require(!gameWon, "Game already won"); |
| 197 | + require(winner != address(0), "Invalid winner address"); |
| 198 | + require(playerResponses[winner].length > 0, "Winner must have submitted at least one response"); |
| 199 | + |
| 200 | + gameWon = true; |
| 201 | + uint256 reward = totalCollected; |
| 202 | + totalCollected = 0; |
| 203 | + |
| 204 | + (bool success, ) = payable(winner).call{value: reward}(""); |
| 205 | + require(success, "Reward transfer failed"); |
| 206 | + |
| 207 | + emit GameWon(winner, reward); |
| 208 | + } |
| 209 | + |
| 210 | + function getTimeRemaining() external view returns (uint256) { |
| 211 | + if (block.timestamp >= gameEndTime) return 0; |
| 212 | + return gameEndTime - block.timestamp; |
| 213 | + } |
| 214 | + |
| 215 | + function getContractBalance() external view returns (uint256) { |
| 216 | + return address(this).balance; |
| 217 | + } |
| 218 | + |
| 219 | + function getCurrentEscalationPeriod() external view returns (uint256) { |
| 220 | + if (!escalationActive) return 0; |
| 221 | + return (block.timestamp - escalationStartTime) / ESCALATION_PERIOD; |
| 222 | + } |
| 223 | + |
| 224 | + receive() external payable {} |
| 225 | + fallback() external payable {} |
| 226 | +} |
0 commit comments