The test demonstrates that previous kings should receive compensation when dethroned:
uint256 public constant PREVIOUS_KING_PAYOUT_PERCENTAGE = 10;
function testClaimThrone_ShouldPayPreviousKing() public {
uint256 player1BalanceBefore = player1.balance;
vm.prank(player1);
game.claimThrone{value: INITIAL_CLAIM_FEE}();
uint256 claimFee = game.claimFee();
vm.prank(player2);
game.claimThrone{value: claimFee}();
uint256 expectedPayout = (claimFee * PREVIOUS_KING_PAYOUT_PERCENTAGE) / 100;
assertEq(player1.balance, player1BalanceBefore - INITIAL_CLAIM_FEE + expectedPayout);
}
// Add new state variable for previous king payout percentage
contract Game is Ownable {
// ... existing variables ...
+ uint256 public previousKingPayoutPercentage; // Percentage of claim fee for previous king
constructor(
uint256 _initialClaimFee,
uint256 _gracePeriod,
uint256 _feeIncreasePercentage,
- uint256 _platformFeePercentage
+ uint256 _platformFeePercentage,
+ uint256 _previousKingPayoutPercentage
) Ownable(msg.sender) {
// ... existing validation ...
+ require(_previousKingPayoutPercentage <= 50, "Game: Previous king payout percentage must be 0-50.");
// ... existing assignments ...
+ previousKingPayoutPercentage = _previousKingPayoutPercentage;
}
+ event PreviousKingPayout(
+ address indexed previousKing,
+ uint256 payoutAmount,
+ address indexed newKing
+ );
function claimThrone() external payable gameNotEnded nonReentrant {
require(msg.value >= claimFee, "Game: Insufficient ETH sent to claim the throne.");
require(msg.sender != currentKing, "Game: You are already the king. No need to re-claim.");
uint256 sentAmount = msg.value;
uint256 previousKingPayout = 0;
+ // Pay previous king if one exists
+ if (currentKing != address(0)) {
+ previousKingPayout = (sentAmount * previousKingPayoutPercentage) / 100;
+ (bool success, ) = payable(currentKing).call{value: previousKingPayout}("");
+ require(success, "Game: Failed to pay previous king.");
+
+ emit PreviousKingPayout(currentKing, previousKingPayout, msg.sender);
+ }
uint256 currentPlatformFee = (sentAmount * platformFeePercentage) / 100;
- amountToPot = sentAmount - currentPlatformFee;
+ amountToPot = sentAmount - currentPlatformFee - previousKingPayout;
pot = pot + amountToPot;
// ... rest of function
}
+
+ event PreviousKingPayoutPercentageUpdated(uint256 newPreviousKingPayoutPercentage);
+
+ function updatePreviousKingPayoutPercentage(uint256 _newPreviousKingPayoutPercentage)
+ external
+ onlyOwner
+ gameEndedOnly
+ {
+ require(_newPreviousKingPayoutPercentage <= 50, "Game: Previous king payout percentage must be 0-50.");
+ previousKingPayoutPercentage = _newPreviousKingPayoutPercentage;
+ emit PreviousKingPayoutPercentageUpdated(_newPreviousKingPayoutPercentage);
+ }