function testPreviousKingPayout() public {
vm.prank(player1);
game.claimThrone{value: 1 ether}();
uint256 player1BalanceBefore = player1.balance;
console2.log("Player 1 balance before:", player1BalanceBefore);
assertEq(game.currentKing(), player1);
assertEq(game.pot(), 0.95 ether);
assertEq(game.platformFeesBalance(), 0.05 ether);
vm.prank(player2);
game.claimThrone{value: 1.5 ether}();
assertEq(game.currentKing(), player2);
assertEq(game.pot(), 2.2325 ether);
assertEq(game.platformFeesBalance(), 0.1175 ether);
uint256 player1BalanceAfter = player1.balance;
console2.log("Player 1 balance after:", player1BalanceAfter);
}
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.");
+ require(msg.sender != currentKing, "Game: You are already the king. No need to re-claim.");
uint256 sentAmount = msg.value;
+ address previousKing = currentKing;
+ if (previousKing != address(0)) {
+ previousKingPayout = (sentAmount * 10) / 100; // 10% to previous king
+ }
- currentPlatformFee = (sentAmount * platformFeePercentage) / 100;
+ uint256 remainingAmount = sentAmount - previousKingPayout;
+ currentPlatformFee = (remainingAmount * platformFeePercentage) / 100;
- if (currentPlatformFee > (sentAmount - previousKingPayout)) {
- currentPlatformFee = sentAmount - previousKingPayout;
- }
platformFeesBalance = platformFeesBalance + currentPlatformFee;
- amountToPot = sentAmount - currentPlatformFee;
+ amountToPot = remainingAmount - currentPlatformFee;
pot = pot + amountToPot;
+ // Pay previous king immediately
+ if (previousKingPayout > 0) {
+ (bool success, ) = payable(previousKing).call{value: previousKingPayout}("");
+ require(success, "Game: Failed to pay previous king");
+ }
// ... rest of function remains the same