Skip to content

Instantly share code, notes, and snippets.

@z0r0z
Created October 9, 2021 21:38
Show Gist options
  • Save z0r0z/dfc0dcb5804617b2069dbcce963aa780 to your computer and use it in GitHub Desktop.
Save z0r0z/dfc0dcb5804617b2069dbcce963aa780 to your computer and use it in GitHub Desktop.
SushiToken Battle Royale on Polygon
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.9;
/// @notice Minimal ERC-20 token interface.
interface IERC20Minimal {
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
/// @notice SushiToken Battle Royale on Polygon.
contract SquishiGame {
event Join(address indexed player);
event Hit(address indexed hitter, address indexed victim);
event Heal(address indexed healer, address indexed friend);
event Death(address indexed player);
/// @dev SushiToken on Polygon:
IERC20Minimal public constant sushi = IERC20Minimal(0x0b3F868E0BE5597D5DB7fEB59E1CADBb0fdDa50a);
/// @dev Game variables:
uint256 public immutable gameEnds = block.timestamp + 9 days;
uint256 public players;
uint256 public pot = sushi.balanceOf(address(this));
mapping(address => bool) public claimed;
mapping(address => uint256) public health;
mapping(address => uint256) public lastActionTimestamp;
modifier rested() {
require(block.timestamp - lastActionTimestamp[msg.sender] > 1 hours);
_;
lastActionTimestamp[msg.sender] = block.timestamp;
}
// **** JOIN ****
/// @dev Deposit sushi and join game.
function join() public {
require(block.timestamp < gameEnds, "GAME_OVER");
require(!isAlive(msg.sender), "PLAYING");
require(
/// @dev Take 3 sushi to give life to new player.
sushi.transferFrom(msg.sender, address(this), 3 ether)
&&
/// @dev Burn 1 sushi to squishi gods.
sushi.transfer(address(0xdead), 1 ether)
, "SUSHI_TX_FAILED"
);
health[msg.sender] = 9;
players++;
emit Join(msg.sender);
}
// **** PLAY ****
/// @dev Check if player is still alive.
function isAlive(address player) public view returns (bool alive) {
alive = health[player] > 0;
}
/// @dev Attack another player.
function hit(address victim) public rested {
require(isAlive(msg.sender), "DEAD");
require(isAlive(victim), "DEAD");
health[victim] = health[victim] - 1;
emit Hit(msg.sender, victim);
if (health[victim] == 0) {
players--;
emit Death(victim);
}
}
/// @dev Heal another player.
function heal(address friend) public rested {
require(isAlive(msg.sender), "DEAD");
require(isAlive(friend), "DEAD");
require(health[friend] < 9, "HEALED");
health[friend] = health[friend] + 1;
lastActionTimestamp[msg.sender] = block.timestamp;
}
// **** WIN ****
/// @dev Remaining players can claim fair share of sushi `pot`.
function claimWinnings() public {
require(block.timestamp >= gameEnds, "GAME_NOT_OVER");
require(isAlive(msg.sender), "DEAD");
require(!claimed[msg.sender], "CLAIMED");
uint256 winnings = pot / players;
sushi.transfer(msg.sender, winnings);
claimed[msg.sender] = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment