// File: src/Roshambo.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
//   ██████╗░░█████╗░░██████╗██╗░░██╗░█████╗░███╗░░░███╗██████╗░░█████╗░   //
//   ██╔══██╗██╔══██╗██╔════╝██║░░██║██╔══██╗████╗░████║██╔══██╗██╔══██╗   //
//   ██████╔╝██║░░██║╚█████╗░███████║███████║██╔████╔██║██████╦╝██║░░██║   //
//   ██╔══██╗██║░░██║░╚═══██╗██╔══██║██╔══██║██║╚██╔╝██║██╔══██╗██║░░██║   //
//   ██║░░██║╚█████╔╝██████╔╝██║░░██║██║░░██║██║░╚═╝░██║██████╦╝╚█████╔╝   //
//   ╚═╝░░╚═╝░╚════╝░╚═════╝░╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░░░░╚═╝╚═════╝░░╚════╝░   //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////

import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {Multicall} from "src/utils/Multicall.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Pausable} from "@openzeppelin/contracts/security/Pausable.sol";
import {Recorder} from "src/Recorder.sol";
import {Renderer} from "src/Renderer.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

import "src/interfaces/IERC4906.sol";
import "src/interfaces/IRoshambo.sol";

/// @title Roshambo
/// @author swaHili (swa.eth)
/// @notice Just a friendly onchain game of Block Paper Scissors
contract Roshambo is IRoshambo, IERC4906, ERC721, Multicall, Ownable, Pausable {
    using Strings for uint8;
    using Strings for uint72;
    using Strings for uint160;
    using Strings for uint256;
    /// @notice Address of Recorder contract used for recording player stats
    Recorder public immutable recorder;
    /// @notice Address of Renderer contract used for renderering svg metadata
    Renderer public immutable renderer;
    /// @notice Duration of block time used as time-constraint for commit and reveal actions
    uint256 public constant BLOCK_DURATION = 6969;
    /// @notice Minimum wager amount required to play
    uint256 public constant MIN_WAGER = 0.01 ether;
    /// @notice Address of beneficiary for collecting game fees
    address public beneficiary;
    /// @notice Dynamic rake percentage used to calculate game fee
    uint16 public rake;
    /// @notice Current ID of most recently created game
    uint40 public currentId;
    /// @notice Total supply of game NFTs
    uint40 public totalSupply;
    /// @notice Mapping of game ID to game struct
    mapping(uint40 => Game) public games;
    /// @notice Mapping of player address to balance available for withdrawal
    mapping(address => uint256) public balances;

    /// @dev Deploys new Recorder contract, initializes Renderer contract and sets beneficiary address
    constructor(Renderer _renderer, address _beneficiary) payable ERC721("Roshambo", "ROSHAMBO") {
        recorder = new Recorder();
        renderer = _renderer;
        beneficiary = _beneficiary;
    }

    /// @notice Creates new game with given wager and executes first throw
    /// @dev Player choice will change from None (0) to Hidden (1)
    /// @dev Game remains in Pending (0) stage until another player joins
    /// @param _rounds Maximum number of rounds to be played
    /// @param _commitment Generated hash of player address, choice, and secret
    /// @return currentId ID of the game just now created
    function newGame(
        uint8 _rounds,
        bytes32 _commitment
    ) external payable whenNotPaused returns (uint40) {
        // Reverts if rounds is an even value
        if (_rounds % 2 == 0) revert InvalidRounds();
        // Reverts if wager amount is less than the minimum
        if (msg.value < MIN_WAGER) revert InsufficientWager();

        // Initializes new game info and sets caller as player1
        Game storage game = games[++currentId];
        game.p1.player = msg.sender;
        game.pot = uint72(msg.value);
        game.totalRounds = _rounds;
        game.currentRound++;

        // Emits event for creating new game
        emit NewGame(currentId, _rounds, msg.sender, uint72(msg.value));

        // Commits player's first choice for the current round
        Round storage round = game.rounds[game.currentRound];
        _commit(currentId, game, round, _commitment);

        return currentId;
    }

    /// @notice Joins pending game by matching wager amount and executes first throw
    /// @dev Player choice will change from None (0) to Hidden (1)
    /// @dev Game skips the Commit (1) stage and moves directly into the Reveal (2) stage
    /// @param _gameId ID of the game being joined
    /// @param _commitment Generated hash of player address, choice, and secret
    function joinGame(uint40 _gameId, bytes32 _commitment) external payable whenNotPaused {
        Game storage game = _verify(_gameId);
        // Reverts if game is not in Pending stage
        if (game.stage != Stage.PENDING) revert InvalidStage();
        // Reverts if caller is player1
        if (msg.sender == game.p1.player) revert InvalidPlayer();
        // Reverts if wager amount does not match current game pot
        if (msg.value != game.pot) revert InvalidWager();

        // Updates game pot and sets caller as player2
        game.p2.player = msg.sender;
        game.pot += uint72(msg.value);

        // Emits event for joining game
        emit JoinGame(_gameId, msg.sender, game.pot);

        // Mints new game NFT to contract and increments supply
        _mint(address(this), _gameId);
        totalSupply++;

        // Commits player's first choice for the current round
        Round storage round = game.rounds[game.currentRound];
        _commit(_gameId, game, round, _commitment);
    }

    /// @notice Commits new choice for current round of game
    /// @dev Player choice will change from None (0) to Hidden (1)
    /// @dev Game moves into Reveal stage only once both players have committed their choice
    /// @param _gameId ID of the game being joined
    /// @param _commitment Generated hash of player address, choice, and secret
    function commit(uint40 _gameId, bytes32 _commitment) external {
        Game storage game = _verify(_gameId);
        Round storage round = game.rounds[game.currentRound];
        // Reverts if game is not in Commit stage
        if (game.stage != Stage.COMMIT) revert InvalidStage();
        // Reverts if current block number is passed the block number set for committing choices
        if (block.number > round.commitBlock) revert TimeElapsed();

        // Commits player choice for the current round
        _commit(_gameId, game, round, _commitment);
    }

    /// @notice Reveals previously committed choice for current round of game
    /// @dev Call will revert if either choice or secret are incorrect
    /// @dev Player choice will change from Hidden (1) to one of the three game choices
    /// @dev Game moves into Settle (3) stage only once both players have revealed their choice
    /// @param _gameId ID of the game being joined
    /// @param _choice Enum value of choice: Block (2) Paper (3) Scissors (4)
    /// @param _secret Unique password used to ultimately reproduce commitment hash
    function reveal(uint40 _gameId, Choice _choice, string calldata _secret) external {
        Game storage game = _verify(_gameId);
        Round storage round = game.rounds[game.currentRound];
        // Reverts if game is not in Reveal stage
        if (game.stage != Stage.REVEAL) revert InvalidStage();
        // Reverts if current block number is passed the block number set for revealing choices
        if (block.number > round.revealBlock) revert TimeElapsed();

        Player storage p1 = game.p1;
        Player storage p2 = game.p2;

        // Checks if caller is either player1 or player2
        if (p1.player == msg.sender) {
            // Reverts if player1 has already revealed their choice
            if (round.p1Choice != Choice.HIDDEN) revert AlreadyRevealed();
            // Verifies reveal for player1 by reproducing commitment hash
            _reveal(msg.sender, _choice, _secret, p1.commitment);
            round.p1Choice = _choice;
        } else if (p2.player == msg.sender) {
            // Reverts if player2 has already revealed their choice
            if (round.p2Choice != Choice.HIDDEN) revert AlreadyRevealed();
            // Verifies reveal for player2 by reproducing commitment hash
            _reveal(msg.sender, _choice, _secret, p2.commitment);
            round.p2Choice = _choice;
        } else {
            // Reverts if caller is NOT player1 or player2
            revert InvalidPlayer();
        }

        // Checks if both players have revealed their choices
        if (round.p1Choice != Choice.HIDDEN && round.p2Choice != Choice.HIDDEN) {
            // Moves game into Settle stage
            game.stage = Stage.SETTLE;
            // Emits event for second player to reveal their choice
            emit Reveal(_gameId, game.currentRound, msg.sender, _choice, game.stage);
            // Settles current round
            settle(_gameId);
        } else {
            // Emits event for first player to reveal their choice
            emit Reveal(_gameId, game.currentRound, msg.sender, _choice, game.stage);
            // Emits event for marketplaces to refresh metadata due to state update
            emit MetadataUpdate(_gameId);
        }
    }

    /// @notice Cancels pending game and transfers wager amount back to caller
    /// @param _gameId ID of the game being canceled
    function cancel(uint40 _gameId) external {
        Game storage game = _verify(_gameId);
        // Reverts if game is not in Pending stage
        if (game.stage != Stage.PENDING) revert InvalidStage();
        // Reverts if caller is not player1
        if (msg.sender != game.p1.player) revert InvalidPlayer();

        // Deletes game
        uint72 pot = game.pot;
        delete games[_gameId];

        // Transfers wager amount back to caller
        (bool success, ) = msg.sender.call{value: pot}("");
        if (!success) revert TransferFailed();

        // Emits event for canceling game
        emit Cancel(_gameId, msg.sender, pot);
    }

    /// @notice Withdraws pending balance amount to given account
    /// @param _to Address receiving ether
    function withdraw(address _to) external {
        uint256 balance = balances[_to];
        // Reverts if account balance is zero
        if (balance == 0) revert InsufficientBalance();

        // Resets account balance
        balances[_to] = 0;

        // Transfers balance to given account
        (bool success, ) = _to.call{value: balance}("");
        if (!success) revert TransferFailed();

        // Emits event for withdrawing balance
        emit Withdraw(msg.sender, _to, balance);
    }

    /// @dev Sets contract beneficiary for receiving game fees
    function setBeneficiary(address _beneficiary) external payable onlyOwner {
        beneficiary = _beneficiary;
    }

    /// @dev Sets dynamic rake percentage for calculating game fees
    function setRake(uint16 _rake) external payable onlyOwner {
        rake = _rake;
    }

    /// @dev Pauses executions for all pausable functions
    function pause() external payable onlyOwner {
        _pause();
    }

    /// @dev Unpauses executions for all pausable functions
    function unpause() external payable onlyOwner {
        _unpause();
    }

    /// @notice Gets total choice usage of player from all finished games
    /// @param _player Address of the player
    /// @param _choice Enum value of choice: Block (2) Paper (3) Scissors (4)
    function getUsageRate(
        address _player,
        Choice _choice
    ) external view returns (uint256 usage, uint256 totalGames, uint256 totalRounds) {
        uint40[] memory gameIds = recorder.getGameIds(_player);
        totalGames = gameIds.length;
        unchecked {
            for (uint40 i; i < totalGames; ++i) {
                Game storage game = games[gameIds[i]];
                totalRounds += game.currentRound;
                for (uint8 j = 1; j <= game.currentRound; ++j) {
                    if (
                        (_player == game.p1.player && _choice == game.rounds[j].p1Choice) ||
                        (_player == game.p2.player && _choice == game.rounds[j].p2Choice)
                    ) usage++;
                }
            }
        }
    }

    /// @notice Gets total series and rounds wins of player from all finished games
    /// @param _player Address of the player
    function getWinRate(
        address _player
    )
        external
        view
        returns (uint256 gamesWon, uint256 roundsWon, uint256 totalGames, uint256 totalRounds)
    {
        uint40[] memory gameIds = recorder.getGameIds(_player);
        totalGames = gameIds.length;
        unchecked {
            for (uint40 i; i < totalGames; ++i) {
                Game storage game = games[gameIds[i]];
                totalRounds += game.currentRound;
                if (game.winner == _player) gamesWon++;
                for (uint8 j = 1; j <= game.currentRound; ++j) {
                    if (_player == game.rounds[j].winner) roundsWon++;
                }
            }
        }
    }

    /// @notice Gets total wages and profits of player from all finished games
    /// @param _player Address of the player
    function getProfitMargin(
        address _player
    ) external view returns (uint256 profits, uint256 wages, uint256 totalGames) {
        uint40[] memory gameIds = recorder.getGameIds(_player);
        totalGames = gameIds.length;
        unchecked {
            for (uint40 i; i < totalGames; ++i) {
                Game storage game = games[gameIds[i]];
                uint256 wager = game.pot / 2;
                wages += wager;
                if (_player == game.winner) profits += wager;
            }
        }
    }

    /// @notice Gets current state of game for given round
    /// @param _gameId ID of the game
    /// @param _round Value of the round
    function getRound(
        uint40 _gameId,
        uint8 _round
    )
        external
        view
        returns (
            Choice p1Choice,
            Choice p2Choice,
            uint40 commitBlock,
            uint40 revealBlock,
            address winner
        )
    {
        Game storage game = games[_gameId];
        Round memory round = game.rounds[_round];
        p1Choice = round.p1Choice;
        p2Choice = round.p2Choice;
        commitBlock = round.commitBlock;
        revealBlock = round.revealBlock;
        winner = round.winner;
    }

    /// @notice Generates commitment hash required for committing choice
    /// @param _player Address of the player
    /// @param _choice Enum value of valid choice: Block (2) Paper (3) Scissors (4)
    /// @param _secret Unique password used to ultimately reproduce commitment hash
    function getCommit(
        address _player,
        Choice _choice,
        string calldata _secret
    ) external pure returns (bytes32) {
        if (_choice == Choice.NONE || _choice == Choice.HIDDEN) revert InvalidChoice();
        return keccak256(abi.encodePacked(_player, _choice, _secret));
    }

    /// @notice Settles current round and determines winner for round or game
    /// @dev Round either repeats if there's a tie OR determines winner and moves on to next round
    /// @dev Series ends in a draw if still in first round AND if either player fails to commit OR if both players fail to reveal
    /// @dev Game either moves back to Commit (1) stage or finishes in Draw (4) OR Success (5) stage
    /// @param _gameId ID of the game
    function settle(uint40 _gameId) public {
        Game storage game = _verify(_gameId);
        Round storage round = game.rounds[game.currentRound];
        // Sets game to Settle stage if block number is past commit or reveal block
        if (
            (game.stage == Stage.COMMIT && block.number > round.commitBlock) ||
            (game.stage == Stage.REVEAL && block.number > round.revealBlock)
        ) {
            game.stage = Stage.SETTLE;
        }
        // Reverts if game is not in Settle stage
        if (game.stage != Stage.SETTLE) revert InvalidStage();

        // Initializes game values
        Player storage p1 = game.p1;
        Player storage p2 = game.p2;
        Choice choice1 = round.p1Choice;
        Choice choice2 = round.p2Choice;
        address player1 = p1.player;
        address player2 = p2.player;

        // Determines outcome of current round and possibly the game
        if (
            game.currentRound == 1 &&
            ((choice1 == Choice.NONE || choice2 == Choice.NONE) ||
                (choice1 == Choice.HIDDEN && choice2 == Choice.HIDDEN))
        ) {
            // Finalizes game as a draw if it never moves to the Reveal stage
            _draw(_gameId, game, player1, choice1, player2, choice2);
        } else {
            if (
                (choice1 == Choice.BLOCK && choice2 == Choice.SCISSORS) ||
                (choice1 == Choice.PAPER && choice2 == Choice.BLOCK) ||
                (choice1 == Choice.SCISSORS && choice2 == Choice.PAPER) ||
                (choice1 == Choice.HIDDEN && choice2 == Choice.NONE) ||
                (choice1 != Choice.NONE && choice1 != Choice.HIDDEN && choice2 == Choice.HIDDEN)
            ) {
                // Settles player1 as winner of current round and possibly the game
                _settle(_gameId, game, round, p1, p2);
            } else if (
                (choice2 == Choice.BLOCK && choice1 == Choice.SCISSORS) ||
                (choice2 == Choice.PAPER && choice1 == Choice.BLOCK) ||
                (choice2 == Choice.SCISSORS && choice1 == Choice.PAPER) ||
                (choice2 == Choice.HIDDEN && choice1 == Choice.NONE) ||
                (choice2 != Choice.NONE && choice2 != Choice.HIDDEN && choice1 == Choice.HIDDEN)
            ) {
                // Settles player2 as winner of current round and possibly the game
                _settle(_gameId, game, round, p2, p1);
            } else {
                // Emits event for settling round or the game
                emit Settle(
                    _gameId,
                    game.currentRound,
                    Stage.COMMIT,
                    round.winner,
                    player1,
                    choice1,
                    p1.wins,
                    player2,
                    choice2,
                    p2.wins
                );

                // Resets the current round due to a tie
                _reset(_gameId, game, round, p1, p2);
            }
        }

        // Emits event for marketplaces to refresh metadata due to state update
        emit MetadataUpdate(_gameId);
    }

    /// @notice Generates metadata of game attributes and renders svg of NFT
    /// @param _tokenId ID of the game
    function tokenURI(uint256 _tokenId) public view virtual override returns (string memory) {
        Game storage game = games[uint40(_tokenId)];
        Round memory round = game.rounds[game.currentRound];
        Player memory p1 = game.p1;
        Player memory p2 = game.p2;
        string memory name = string.concat("Block Paper Scissors #", _tokenId.toString());
        string memory description = "Just a friendly onchain game of Block Paper Scissors. Shoot!";
        string memory gameTraits = _generateGameTraits(game, p1, p2);
        string memory playerTraits = _generatePlayerTraits(p1.player, p2.player, round);
        string memory image = Base64.encode(
            abi.encodePacked(
                renderer.generateImage(
                    _tokenId,
                    game.pot,
                    p1.player,
                    round.p1Choice,
                    p2.player,
                    round.p2Choice
                )
            )
        );

        return
            string.concat(
                "data:application/json;base64,",
                Base64.encode(
                    abi.encodePacked(
                        string.concat(
                            '{"name":"',
                            name,
                            '",',
                            '"description":"',
                            description,
                            '",',
                            '"image": "data:image/svg+xml;base64,',
                            image,
                            '",',
                            '"attributes": [',
                            playerTraits,
                            gameTraits,
                            "]}"
                        )
                    )
                )
            );
    }

    /// @dev Executes commitment of player choice for current round
    /// @param _gameId ID of the game
    /// @param _game Storage value of game info
    /// @param _round Storage value of current round info
    /// @param _commitment Generated hash of player address, choice, and secret
    function _commit(
        uint40 _gameId,
        Game storage _game,
        Round storage _round,
        bytes32 _commitment
    ) internal {
        Player storage p1 = _game.p1;
        Player storage p2 = _game.p2;

        // Checks if caller is player1 or player2
        if (p1.player == msg.sender) {
            // Reverts if player1 has already committed their choice
            if (p1.commitment != bytes32(0)) revert AlreadyCommitted();
            // Stores commitment of player1 and sets their choice to Hidden
            p1.commitment = _commitment;
            _round.p1Choice = Choice.HIDDEN;
        } else if (p2.player == msg.sender) {
            // Reverts if player2 has already committed their choice
            if (p2.commitment != bytes32(0)) revert AlreadyCommitted();
            // Stores commitment of player2 and sets their choice to Hidden
            p2.commitment = _commitment;
            _round.p2Choice = Choice.HIDDEN;
        } else {
            // Reverts if caller is not either player1 or player2
            revert InvalidPlayer();
        }

        // Checks if both players have committed their choice
        if (p1.commitment != bytes32(0) && p2.commitment != bytes32(0)) {
            // Moves game into the Reveal stage
            _game.stage = Stage.REVEAL;
            // Emits event for second player to commit their choice
            emit Commit(_gameId, _game.currentRound, msg.sender, _commitment, _game.stage);

            // Initializes reveal block using block duration
            _round.revealBlock = uint40(block.number + BLOCK_DURATION);
            // Emits event for the current round with a new reveal block
            emit CurrentRound(_gameId, _game.currentRound, _round.revealBlock);
        } else {
            // Emits event for first player to commit their choice
            emit Commit(_gameId, _game.currentRound, msg.sender, _commitment, _game.stage);
        }

        // Emits event for marketplaces to refresh metadata due to state update
        emit MetadataUpdate(_gameId);
    }

    /// @dev Resets the current round due to a tie
    /// @param _gameId ID of the game
    /// @param _game Storage value of game info
    /// @param _round Storage value of current round info
    /// @param _p1 Storage value of player1 info
    /// @param _p2 Storage value of player2 info
    function _reset(
        uint40 _gameId,
        Game storage _game,
        Round storage _round,
        Player storage _p1,
        Player storage _p2
    ) internal {
        // Resets the game, round and player info
        _game.stage = Stage.COMMIT;
        _p1.commitment = bytes32(0);
        _p2.commitment = bytes32(0);
        _round.p1Choice = Choice.NONE;
        _round.p2Choice = Choice.NONE;
        // Updates the commit block for the current round using block duration
        _round.commitBlock = uint40(block.number + BLOCK_DURATION);

        // Emits event for resetting the current round with a new commit block
        emit ResetRound(_gameId, _game.currentRound, _round.commitBlock);
    }

    /// @dev Begins the next round of the game
    /// @param _gameId ID of the game
    /// @param _game Storage value of game info
    /// @param _round Storage value of current round info
    /// @param _p1 Storage value of player1 info
    /// @param _p2 Storage value of player2 info
    function _next(
        uint40 _gameId,
        Game storage _game,
        Round storage _round,
        Player storage _p1,
        Player storage _p2
    ) internal {
        // Initializes the game and player info
        _game.stage = Stage.COMMIT;
        _p1.commitment = bytes32(0);
        _p2.commitment = bytes32(0);
        // Initializes the commit block of the new round using block duration
        _round.commitBlock = uint40(block.number + BLOCK_DURATION);

        // Emits event for moving game into the next round with a new commit block
        emit NextRound(_gameId, _game.currentRound, _round.commitBlock);
    }

    /// @dev Finalizes the game as a draw due to inactivity by either or both players
    /// @param _gameId ID of the game
    /// @param _game Storage value of game info
    /// @param _player1 Address of player1
    /// @param _choice1 Choice  of player1
    /// @param _player2 Address of player2
    /// @param _choice2 Choice  of player2
    function _draw(
        uint40 _gameId,
        Game storage _game,
        address _player1,
        Choice _choice1,
        address _player2,
        Choice _choice2
    ) internal {
        // Moves game into the Draw stage
        _game.stage = Stage.DRAW;
        // Calculates wager amount and updates balances of both players
        uint72 wager = _game.pot / 2;
        balances[_player1] += wager;
        balances[_player2] += wager;

        // Emits event for settling the game in a draw
        emit Settle(
            _gameId,
            _game.currentRound,
            _game.stage,
            _game.winner,
            _player1,
            _choice1,
            _game.p1.wins,
            _player2,
            _choice2,
            _game.p2.wins
        );
    }

    /// @dev Settles the round or game with a winner
    /// @param _gameId ID of the game
    /// @param _game Storage value of game info
    /// @param _round Storage value of current round info
    /// @param _winner Storage value of winner info
    /// @param _loser Storage value of loser info
    function _settle(
        uint40 _gameId,
        Game storage _game,
        Round storage _round,
        Player storage _winner,
        Player storage _loser
    ) internal {
        // Increments player wins and sets them as round winner
        _winner.wins++;
        _round.winner = _winner.player;

        // Checks if winner of round has also won more than half the total possible games
        if (_winner.wins > _game.totalRounds / 2) {
            // Finalizes winner of game
            _success(_gameId, _game, _round, _round.winner, _game.pot);
            // Records both player stats on Recorder contract
            recorder.setRecord(
                _gameId,
                _game.pot,
                _game.totalRounds,
                _game.currentRound,
                _winner.player,
                _loser.player
            );
        } else {
            // Emits settle event for round winner
            emit Settle(
                _gameId,
                _game.currentRound,
                Stage.COMMIT,
                _round.winner,
                _game.p1.player,
                _round.p1Choice,
                _game.p1.wins,
                _game.p2.player,
                _round.p2Choice,
                _game.p2.wins
            );

            // Increments round and moves game into the next round
            _round = _game.rounds[++_game.currentRound];
            _next(_gameId, _game, _round, _winner, _loser);
        }
    }

    /// @dev Finalizes game with a winner
    /// @param _gameId ID of the game
    /// @param _game Storage value of game info
    /// @param _round Storage value of current round info
    /// @param _winner Address of game winner
    /// @param _pot Ether amount of game pot
    function _success(
        uint40 _gameId,
        Game storage _game,
        Round storage _round,
        address _winner,
        uint72 _pot
    ) internal {
        // Moves game into the Success stage and sets winner
        _game.stage = Stage.SUCCESS;
        _game.winner = _winner;

        // Emits events for settling game with a winner
        emit Settle(
            _gameId,
            _game.currentRound,
            _game.stage,
            _winner,
            _game.p1.player,
            _round.p1Choice,
            _game.p1.wins,
            _game.p2.player,
            _round.p2Choice,
            _game.p2.wins
        );

        // Checks if rake has been set
        if (rake > 0) {
            // Adjusts rake percentage base on pot size and calculates game fee accordingly
            uint256 adjusted = recorder.adjustRake(_pot, rake);
            uint256 fee = (uint256(_pot) * adjusted) / 10_000;
            // Updates balance of winner minus fee amount
            balances[_winner] += (_pot - fee);
            balances[beneficiary] += fee;
        } else {
            // Updates balance of winner with entire game pot
            balances[_winner] += _pot;
        }

        // Mints NFT to winner
        _burn(_gameId);
        _mint(_winner, _gameId);
    }

    /// @dev Verifies that the game exists
    /// @param _gameId ID of the game
    /// @return game Storage value of game info
    function _verify(uint40 _gameId) internal view returns (Game storage game) {
        game = games[_gameId];
        // Reverts if game does not exist due to invalid ID or canceled game
        if (_gameId == 0 || _gameId > currentId || game.p1.player == address(0))
            revert InvalidGame();
    }

    /// @dev Verifies player reveal matches commitment hash
    /// @param _player Address of player revealing choice
    /// @param _choice Choice provided by player
    /// @param _secret Secret password provided by player
    /// @param _commitment Generated hash of player choice stored during Commit stage
    function _reveal(
        address _player,
        Choice _choice,
        string memory _secret,
        bytes32 _commitment
    ) internal pure {
        if (keccak256(abi.encodePacked(_player, _choice, _secret)) != _commitment)
            revert InvalidReveal();
    }

    /// @dev Generates game attributes of the game
    /// @param _game Storage value of game info
    /// @param _p1 Player1 info
    /// @param _p2 Player2 info
    /// @return JSON value of game traits
    function _generateGameTraits(
        Game storage _game,
        Player memory _p1,
        Player memory _p2
    ) internal view returns (string memory) {
        string memory pot = string.concat(unicode"Ξ ", recorder.weiToEth(_game.pot));
        string memory total = _game.totalRounds.toString();
        string memory current = _game.currentRound.toString();
        string memory round = string.concat(current, " of ", total);
        string memory stage = renderer.getStage(_game.stage);
        string memory winner = uint160(_game.winner).toHexString(20);
        if (_p1.wins > _p2.wins) {
            winner = uint160(_p1.player).toHexString(20);
        } else if (_p2.wins > _p1.wins) {
            winner = uint160(_p2.player).toHexString(20);
        }

        return
            string(
                abi.encodePacked(
                    '{"trait_type":"Pot", "value":"',
                    pot,
                    '"},',
                    '{"trait_type":"Round", "value":"',
                    round,
                    '"},',
                    '{"trait_type":"Stage", "value":"',
                    stage,
                    '"},',
                    '{"trait_type":"Winner", "value":"',
                    winner,
                    '"}'
                )
            );
    }

    /// @dev Generates player attributes of the game
    /// @param _player1 Address of player1
    /// @param _player2 Address of player2
    /// @param _round Round info
    /// @return JSON value of player traits
    function _generatePlayerTraits(
        address _player1,
        address _player2,
        Round memory _round
    ) internal view returns (string memory) {
        string memory player1 = uint160(_player1).toHexString(20);
        string memory player2 = uint160(_player2).toHexString(20);
        string memory choice1 = renderer.getChoice(_round.p1Choice);
        string memory choice2 = renderer.getChoice(_round.p2Choice);

        return
            string(
                abi.encodePacked(
                    '{"trait_type":"',
                    choice1,
                    '", "value":"',
                    player1,
                    '"},',
                    '{"trait_type":"',
                    choice2,
                    '", "value":"',
                    player2,
                    '"},'
                )
            );
    }
}

// File: lib/openzeppelin-contracts/contracts/utils/Base64.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/Base64.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides a set of functions to operate with Base64 strings.
 *
 * _Available since v4.5._
 */
library Base64 {
    /**
     * @dev Base64 Encoding/Decoding Table
     */
    string internal constant _TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    /**
     * @dev Converts a `bytes` to its Bytes64 `string` representation.
     */
    function encode(bytes memory data) internal pure returns (string memory) {
        /**
         * Inspired by Brecht Devos (Brechtpd) implementation - MIT licence
         * https://github.com/Brechtpd/base64/blob/e78d9fd951e7b0977ddca77d92dc85183770daf4/base64.sol
         */
        if (data.length == 0) return "";

        // Loads the table into memory
        string memory table = _TABLE;

        // Encoding takes 3 bytes chunks of binary data from `bytes` data parameter
        // and split into 4 numbers of 6 bits.
        // The final Base64 length should be `bytes` data length multiplied by 4/3 rounded up
        // - `data.length + 2`  -> Round up
        // - `/ 3`              -> Number of 3-bytes chunks
        // - `4 *`              -> 4 characters for each chunk
        string memory result = new string(4 * ((data.length + 2) / 3));

        /// @solidity memory-safe-assembly
        assembly {
            // Prepare the lookup table (skip the first "length" byte)
            let tablePtr := add(table, 1)

            // Prepare result pointer, jump over length
            let resultPtr := add(result, 32)

            // Run over the input, 3 bytes at a time
            for {
                let dataPtr := data
                let endPtr := add(data, mload(data))
            } lt(dataPtr, endPtr) {

            } {
                // Advance 3 bytes
                dataPtr := add(dataPtr, 3)
                let input := mload(dataPtr)

                // To write each character, shift the 3 bytes (18 bits) chunk
                // 4 times in blocks of 6 bits for each character (18, 12, 6, 0)
                // and apply logical AND with 0x3F which is the number of
                // the previous character in the ASCII table prior to the Base64 Table
                // The result is then added to the table to get the character to write,
                // and finally write it in the result pointer but with a left shift
                // of 256 (1 byte) - 8 (1 ASCII char) = 248 bits

                mstore8(resultPtr, mload(add(tablePtr, and(shr(18, input), 0x3F))))
                resultPtr := add(resultPtr, 1) // Advance

                mstore8(resultPtr, mload(add(tablePtr, and(shr(12, input), 0x3F))))
                resultPtr := add(resultPtr, 1) // Advance

                mstore8(resultPtr, mload(add(tablePtr, and(shr(6, input), 0x3F))))
                resultPtr := add(resultPtr, 1) // Advance

                mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
                resultPtr := add(resultPtr, 1) // Advance
            }

            // When data `bytes` is not exactly 3 bytes long
            // it is padded with `=` characters at the end
            switch mod(mload(data), 3)
            case 1 {
                mstore8(sub(resultPtr, 1), 0x3d)
                mstore8(sub(resultPtr, 2), 0x3d)
            }
            case 2 {
                mstore8(sub(resultPtr, 1), 0x3d)
            }
        }

        return result;
    }
}

// File: lib/openzeppelin-contracts/contracts/token/ERC721/ERC721.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC721/ERC721.sol)

pragma solidity ^0.8.0;

import "./IERC721.sol";
import "./IERC721Receiver.sol";
import "./extensions/IERC721Metadata.sol";
import "../../utils/Address.sol";
import "../../utils/Context.sol";
import "../../utils/Strings.sol";
import "../../utils/introspection/ERC165.sol";

/**
 * @dev Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC721] Non-Fungible Token Standard, including
 * the Metadata extension, but not including the Enumerable extension, which is available separately as
 * {ERC721Enumerable}.
 */
contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
    using Address for address;
    using Strings for uint256;

    // Token name
    string private _name;

    // Token symbol
    string private _symbol;

    // Mapping from token ID to owner address
    mapping(uint256 => address) private _owners;

    // Mapping owner address to token count
    mapping(address => uint256) private _balances;

    // Mapping from token ID to approved address
    mapping(uint256 => address) private _tokenApprovals;

    // Mapping from owner to operator approvals
    mapping(address => mapping(address => bool)) private _operatorApprovals;

    /**
     * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
     */
    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
    }

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
        return
            interfaceId == type(IERC721).interfaceId ||
            interfaceId == type(IERC721Metadata).interfaceId ||
            super.supportsInterface(interfaceId);
    }

    /**
     * @dev See {IERC721-balanceOf}.
     */
    function balanceOf(address owner) public view virtual override returns (uint256) {
        require(owner != address(0), "ERC721: address zero is not a valid owner");
        return _balances[owner];
    }

    /**
     * @dev See {IERC721-ownerOf}.
     */
    function ownerOf(uint256 tokenId) public view virtual override returns (address) {
        address owner = _ownerOf(tokenId);
        require(owner != address(0), "ERC721: invalid token ID");
        return owner;
    }

    /**
     * @dev See {IERC721Metadata-name}.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev See {IERC721Metadata-symbol}.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev See {IERC721Metadata-tokenURI}.
     */
    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        _requireMinted(tokenId);

        string memory baseURI = _baseURI();
        return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : "";
    }

    /**
     * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
     * token will be the concatenation of the `baseURI` and the `tokenId`. Empty
     * by default, can be overridden in child contracts.
     */
    function _baseURI() internal view virtual returns (string memory) {
        return "";
    }

    /**
     * @dev See {IERC721-approve}.
     */
    function approve(address to, uint256 tokenId) public virtual override {
        address owner = ERC721.ownerOf(tokenId);
        require(to != owner, "ERC721: approval to current owner");

        require(
            _msgSender() == owner || isApprovedForAll(owner, _msgSender()),
            "ERC721: approve caller is not token owner or approved for all"
        );

        _approve(to, tokenId);
    }

    /**
     * @dev See {IERC721-getApproved}.
     */
    function getApproved(uint256 tokenId) public view virtual override returns (address) {
        _requireMinted(tokenId);

        return _tokenApprovals[tokenId];
    }

    /**
     * @dev See {IERC721-setApprovalForAll}.
     */
    function setApprovalForAll(address operator, bool approved) public virtual override {
        _setApprovalForAll(_msgSender(), operator, approved);
    }

    /**
     * @dev See {IERC721-isApprovedForAll}.
     */
    function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
        return _operatorApprovals[owner][operator];
    }

    /**
     * @dev See {IERC721-transferFrom}.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        //solhint-disable-next-line max-line-length
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: caller is not token owner or approved");

        _transfer(from, to, tokenId);
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        safeTransferFrom(from, to, tokenId, "");
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes memory data
    ) public virtual override {
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: caller is not token owner or approved");
        _safeTransfer(from, to, tokenId, data);
    }

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * `data` is additional data, it has no specified format and it is sent in call to `to`.
     *
     * This internal function is equivalent to {safeTransferFrom}, and can be used to e.g.
     * implement alternative mechanisms to perform token transfer, such as signature-based.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeTransfer(
        address from,
        address to,
        uint256 tokenId,
        bytes memory data
    ) internal virtual {
        _transfer(from, to, tokenId);
        require(_checkOnERC721Received(from, to, tokenId, data), "ERC721: transfer to non ERC721Receiver implementer");
    }

    /**
     * @dev Returns the owner of the `tokenId`. Does NOT revert if token doesn't exist
     */
    function _ownerOf(uint256 tokenId) internal view virtual returns (address) {
        return _owners[tokenId];
    }

    /**
     * @dev Returns whether `tokenId` exists.
     *
     * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
     *
     * Tokens start existing when they are minted (`_mint`),
     * and stop existing when they are burned (`_burn`).
     */
    function _exists(uint256 tokenId) internal view virtual returns (bool) {
        return _ownerOf(tokenId) != address(0);
    }

    /**
     * @dev Returns whether `spender` is allowed to manage `tokenId`.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
        address owner = ERC721.ownerOf(tokenId);
        return (spender == owner || isApprovedForAll(owner, spender) || getApproved(tokenId) == spender);
    }

    /**
     * @dev Safely mints `tokenId` and transfers it to `to`.
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeMint(address to, uint256 tokenId) internal virtual {
        _safeMint(to, tokenId, "");
    }

    /**
     * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
     * forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
     */
    function _safeMint(
        address to,
        uint256 tokenId,
        bytes memory data
    ) internal virtual {
        _mint(to, tokenId);
        require(
            _checkOnERC721Received(address(0), to, tokenId, data),
            "ERC721: transfer to non ERC721Receiver implementer"
        );
    }

    /**
     * @dev Mints `tokenId` and transfers it to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - `to` cannot be the zero address.
     *
     * Emits a {Transfer} event.
     */
    function _mint(address to, uint256 tokenId) internal virtual {
        require(to != address(0), "ERC721: mint to the zero address");
        require(!_exists(tokenId), "ERC721: token already minted");

        _beforeTokenTransfer(address(0), to, tokenId, 1);

        // Check that tokenId was not minted by `_beforeTokenTransfer` hook
        require(!_exists(tokenId), "ERC721: token already minted");

        unchecked {
            // Will not overflow unless all 2**256 token ids are minted to the same owner.
            // Given that tokens are minted one by one, it is impossible in practice that
            // this ever happens. Might change if we allow batch minting.
            // The ERC fails to describe this case.
            _balances[to] += 1;
        }

        _owners[tokenId] = to;

        emit Transfer(address(0), to, tokenId);

        _afterTokenTransfer(address(0), to, tokenId, 1);
    }

    /**
     * @dev Destroys `tokenId`.
     * The approval is cleared when the token is burned.
     * This is an internal function that does not check if the sender is authorized to operate on the token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     *
     * Emits a {Transfer} event.
     */
    function _burn(uint256 tokenId) internal virtual {
        address owner = ERC721.ownerOf(tokenId);

        _beforeTokenTransfer(owner, address(0), tokenId, 1);

        // Update ownership in case tokenId was transferred by `_beforeTokenTransfer` hook
        owner = ERC721.ownerOf(tokenId);

        // Clear approvals
        delete _tokenApprovals[tokenId];

        unchecked {
            // Cannot overflow, as that would require more tokens to be burned/transferred
            // out than the owner initially received through minting and transferring in.
            _balances[owner] -= 1;
        }
        delete _owners[tokenId];

        emit Transfer(owner, address(0), tokenId);

        _afterTokenTransfer(owner, address(0), tokenId, 1);
    }

    /**
     * @dev Transfers `tokenId` from `from` to `to`.
     *  As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     *
     * Emits a {Transfer} event.
     */
    function _transfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {
        require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer from incorrect owner");
        require(to != address(0), "ERC721: transfer to the zero address");

        _beforeTokenTransfer(from, to, tokenId, 1);

        // Check that tokenId was not transferred by `_beforeTokenTransfer` hook
        require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer from incorrect owner");

        // Clear approvals from the previous owner
        delete _tokenApprovals[tokenId];

        unchecked {
            // `_balances[from]` cannot overflow for the same reason as described in `_burn`:
            // `from`'s balance is the number of token held, which is at least one before the current
            // transfer.
            // `_balances[to]` could overflow in the conditions described in `_mint`. That would require
            // all 2**256 token ids to be minted, which in practice is impossible.
            _balances[from] -= 1;
            _balances[to] += 1;
        }
        _owners[tokenId] = to;

        emit Transfer(from, to, tokenId);

        _afterTokenTransfer(from, to, tokenId, 1);
    }

    /**
     * @dev Approve `to` to operate on `tokenId`
     *
     * Emits an {Approval} event.
     */
    function _approve(address to, uint256 tokenId) internal virtual {
        _tokenApprovals[tokenId] = to;
        emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
    }

    /**
     * @dev Approve `operator` to operate on all of `owner` tokens
     *
     * Emits an {ApprovalForAll} event.
     */
    function _setApprovalForAll(
        address owner,
        address operator,
        bool approved
    ) internal virtual {
        require(owner != operator, "ERC721: approve to caller");
        _operatorApprovals[owner][operator] = approved;
        emit ApprovalForAll(owner, operator, approved);
    }

    /**
     * @dev Reverts if the `tokenId` has not been minted yet.
     */
    function _requireMinted(uint256 tokenId) internal view virtual {
        require(_exists(tokenId), "ERC721: invalid token ID");
    }

    /**
     * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
     * The call is not executed if the target address is not a contract.
     *
     * @param from address representing the previous owner of the given token ID
     * @param to target address that will receive the tokens
     * @param tokenId uint256 ID of the token to be transferred
     * @param data bytes optional data to send along with the call
     * @return bool whether the call correctly returned the expected magic value
     */
    function _checkOnERC721Received(
        address from,
        address to,
        uint256 tokenId,
        bytes memory data
    ) private returns (bool) {
        if (to.isContract()) {
            try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, data) returns (bytes4 retval) {
                return retval == IERC721Receiver.onERC721Received.selector;
            } catch (bytes memory reason) {
                if (reason.length == 0) {
                    revert("ERC721: transfer to non ERC721Receiver implementer");
                } else {
                    /// @solidity memory-safe-assembly
                    assembly {
                        revert(add(32, reason), mload(reason))
                    }
                }
            }
        } else {
            return true;
        }
    }

    /**
     * @dev Hook that is called before any token transfer. This includes minting and burning. If {ERC721Consecutive} is
     * used, the hook may be called as part of a consecutive (batch) mint, as indicated by `batchSize` greater than 1.
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, ``from``'s tokens will be transferred to `to`.
     * - When `from` is zero, the tokens will be minted for `to`.
     * - When `to` is zero, ``from``'s tokens will be burned.
     * - `from` and `to` are never both zero.
     * - `batchSize` is non-zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256, /* firstTokenId */
        uint256 batchSize
    ) internal virtual {
        if (batchSize > 1) {
            if (from != address(0)) {
                _balances[from] -= batchSize;
            }
            if (to != address(0)) {
                _balances[to] += batchSize;
            }
        }
    }

    /**
     * @dev Hook that is called after any token transfer. This includes minting and burning. If {ERC721Consecutive} is
     * used, the hook may be called as part of a consecutive (batch) mint, as indicated by `batchSize` greater than 1.
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, ``from``'s tokens were transferred to `to`.
     * - When `from` is zero, the tokens were minted for `to`.
     * - When `to` is zero, ``from``'s tokens were burned.
     * - `from` and `to` are never both zero.
     * - `batchSize` is non-zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _afterTokenTransfer(
        address from,
        address to,
        uint256 firstTokenId,
        uint256 batchSize
    ) internal virtual {}
}

// File: src/utils/Multicall.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

abstract contract Multicall {
    function multicall(bytes[] calldata _data) external returns (bytes[] memory results) {
        uint256 length = _data.length;
        results = new bytes[](length);

        bool success;
        for (uint256 i; i < length; ) {
            bytes memory result;
            (success, result) = address(this).delegatecall(_data[i]);
            if (!success) {
                if (result.length == 0) revert();
                _revertedWithReason(result);
            }

            results[i] = result;
            unchecked {
                ++i;
            }
        }
    }

    function _revertedWithReason(bytes memory _response) internal pure {
        assembly {
            let returndata_size := mload(_response)
            revert(add(32, _response), returndata_size)
        }
    }
}

// File: lib/openzeppelin-contracts/contracts/access/Ownable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// File: lib/openzeppelin-contracts/contracts/security/Pausable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which allows children to implement an emergency stop
 * mechanism that can be triggered by an authorized account.
 *
 * This module is used through inheritance. It will make available the
 * modifiers `whenNotPaused` and `whenPaused`, which can be applied to
 * the functions of your contract. Note that they will not be pausable by
 * simply including this module, only once the modifiers are put in place.
 */
abstract contract Pausable is Context {
    /**
     * @dev Emitted when the pause is triggered by `account`.
     */
    event Paused(address account);

    /**
     * @dev Emitted when the pause is lifted by `account`.
     */
    event Unpaused(address account);

    bool private _paused;

    /**
     * @dev Initializes the contract in unpaused state.
     */
    constructor() {
        _paused = false;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is not paused.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    modifier whenNotPaused() {
        _requireNotPaused();
        _;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is paused.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    modifier whenPaused() {
        _requirePaused();
        _;
    }

    /**
     * @dev Returns true if the contract is paused, and false otherwise.
     */
    function paused() public view virtual returns (bool) {
        return _paused;
    }

    /**
     * @dev Throws if the contract is paused.
     */
    function _requireNotPaused() internal view virtual {
        require(!paused(), "Pausable: paused");
    }

    /**
     * @dev Throws if the contract is not paused.
     */
    function _requirePaused() internal view virtual {
        require(paused(), "Pausable: not paused");
    }

    /**
     * @dev Triggers stopped state.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    function _pause() internal virtual whenNotPaused {
        _paused = true;
        emit Paused(_msgSender());
    }

    /**
     * @dev Returns to normal state.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    function _unpause() internal virtual whenPaused {
        _paused = false;
        emit Unpaused(_msgSender());
    }
}

// File: src/Recorder.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

//////////////////////////////////////////////////////////////////////////
//                                                                      //
//   ██████╗░███████╗░█████╗░░█████╗░██████╗░██████╗░███████╗██████╗░   //
//   ██╔══██╗██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔════╝██╔══██╗   //
//   ██████╔╝█████╗░░██║░░╚═╝██║░░██║██████╔╝██║░░██║█████╗░░██████╔╝   //
//   ██╔══██╗██╔══╝░░██║░░██╗██║░░██║██╔══██╗██║░░██║██╔══╝░░██╔══██╗   //
//   ██║░░██║███████╗╚█████╔╝╚█████╔╝██║░░██║██████╔╝███████╗██║░░██║   //
//   ╚═╝░░╚═╝╚══════╝░╚════╝░░╚════╝░╚═╝░░╚═╝╚═════╝░╚══════╝╚═╝░░╚═╝   //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

import {Owned} from "@solmate/auth/Owned.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

import "src/interfaces/IRecorder.sol";
import "src/interfaces/IRenderer.sol";

/// @title Recorder
/// @author swaHili (swa.eth)
/// @notice Records player stats for Roshambo
contract Recorder is Owned, IRecorder {
    using Strings for uint256;
    string public name;
    string public symbol;
    mapping(address => Record) public records;

    constructor() Owned(msg.sender) {
        name = "Recorder";
        symbol = "RECORDER";
    }

    function setRecord(
        uint40 _gameId,
        uint72 _pot,
        uint8 _totalRounds,
        uint8 _currentRound,
        address _winner,
        address _loser
    ) external onlyOwner {
        Record storage winner = records[_winner];
        Record storage loser = records[_loser];
        winner.games.push(_gameId);
        loser.games.push(_gameId);
        uint128 wager = uint128(_pot) / 2;
        uint8 winnerRoundsWon = (_totalRounds / 2) + 1;
        uint8 loserRoundsWon = _currentRound - winnerRoundsWon;

        winner.seriesWon++;
        winner.roundsWon += winnerRoundsWon;
        winner.roundsLost += loserRoundsWon;
        winner.wagers += wager;
        winner.profits += wager;

        emit SetRecord(
            _winner,
            winner.playerId,
            _gameId,
            winner.roundsWon,
            winner.seriesWon,
            winner.roundsLost,
            winner.seriesLost,
            winner.wagers,
            winner.profits
        );

        loser.seriesLost++;
        loser.roundsWon += loserRoundsWon;
        loser.roundsLost += winnerRoundsWon;
        loser.wagers += wager;

        emit SetRecord(
            _loser,
            loser.playerId,
            _gameId,
            loser.roundsWon,
            loser.seriesWon,
            loser.roundsLost,
            loser.seriesLost,
            loser.wagers,
            loser.profits
        );
    }

    function getGameIds(address _player) external view returns (uint40[] memory) {
        Record memory record = records[_player];
        return record.games;
    }

    function adjustRake(uint256 _pot, uint256 _rake) external pure returns (uint256 adjusted) {
        if (_pot > 10 ether) {
            adjusted = _rake - ((_rake * 50) / 100);
        } else if (_pot >= 5 ether) {
            adjusted = _rake - ((_rake * 40) / 100);
        } else if (_pot >= 2.5 ether) {
            adjusted = _rake - ((_rake * 30) / 100);
        } else if (_pot >= 1 ether) {
            adjusted = _rake - ((_rake * 20) / 100);
        } else if (_pot >= 0.5 ether) {
            adjusted = _rake - ((_rake * 10) / 100);
        } else {
            adjusted = _rake;
        }
    }

    function weiToEth(uint256 _weiAmount) public pure returns (string memory) {
        uint256 ethAmount = (_weiAmount * 1e4) / 1e18;
        string memory base = (ethAmount / 10000).toString();
        string memory decimal = (ethAmount % 10000).toString();

        // Adds leading zeros to the decimal part, if needed
        while (bytes(decimal).length < 4) {
            decimal = string(abi.encodePacked("0", decimal));
        }

        // Removes trailing zeros from the decimal part
        bytes memory decimalBytes = bytes(decimal);
        int256 endIndex = int256(decimalBytes.length);
        for (int256 i = endIndex - 1; i >= 0; i--) {
            if (decimalBytes[uint256(i)] != "0") {
                endIndex = i + 1;
                break;
            }
            // In case we have a string "0000"
            if (i == 0) endIndex = 0;
        }

        bytes memory resultBytes = new bytes(uint256(endIndex));
        for (uint256 i; i < uint256(endIndex); ++i) {
            resultBytes[i] = decimalBytes[i];
        }
        decimal = string(resultBytes);

        // If the decimal part is not empty, append it to the base part
        if (bytes(decimal).length > 0) {
            return string(abi.encodePacked(base, ".", decimal));
        } else {
            return base;
        }
    }
}

// File: src/Renderer.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

///////////////////////////////////////////////////////////////////////////
//                                                                       //
//   ██████╗░███████╗███╗░░██╗██████╗░███████╗██████╗░███████╗██████╗░   //
//   ██╔══██╗██╔════╝████╗░██║██╔══██╗██╔════╝██╔══██╗██╔════╝██╔══██╗   //
//   ██████╔╝█████╗░░██╔██╗██║██║░░██║█████╗░░██████╔╝█████╗░░██████╔╝   //
//   ██╔══██╗██╔══╝░░██║╚████║██║░░██║██╔══╝░░██╔══██╗██╔══╝░░██╔══██╗   //
//   ██║░░██║███████╗██║░╚███║██████╔╝███████╗██║░░██║███████╗██║░░██║   //
//   ╚═╝░░╚═╝╚══════╝╚═╝░░╚══╝╚═════╝░╚══════╝╚═╝░░╚═╝╚══════╝╚═╝░░╚═╝   //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

import "src/interfaces/IRenderer.sol";
import "src/interfaces/IRoshambo.sol";

/// @title Renderer
/// @author swaHili (swa.eth)
/// @notice Renders the svg metadata for Roshambo
contract Renderer is IRenderer {
    using Strings for uint256;

    function generateImage(
        uint256 _tokenId,
        uint256 _pot,
        address _player1,
        Choice _choice1,
        address _player2,
        Choice _choice2
    ) external pure returns (string memory svg) {
        svg = ROOT;
        (string memory _block, string memory _paper, string memory _scissors) = generatePalette(
            _tokenId,
            _pot,
            _player1,
            _choice1,
            _player2,
            _choice2
        );
        if (
            (_choice1 == Choice.NONE || _choice1 == Choice.HIDDEN) &&
            (_choice2 == Choice.NONE || _choice2 == Choice.HIDDEN)
        ) {
            svg = string.concat(svg, _generateGame(_block, _paper, _scissors));
        } else {
            svg = string.concat(
                svg,
                "<g transform='scale (-1, 1)' transform-origin='center'>",
                _generateBlock(_block),
                _generateAnimation(),
                "</path></g>"
            );
            if (_choice1 == Choice.BLOCK)
                svg = string.concat(svg, TRANSFORM, _generateBlock(_block), REVEAL);
            if (_choice1 == Choice.PAPER)
                svg = string.concat(svg, "<g opacity='0'>", _generatePaper(_paper), REVEAL);
            if (_choice1 == Choice.SCISSORS)
                svg = string.concat(svg, TRANSFORM, _generateScissors(_scissors), REVEAL);

            svg = string.concat(
                svg,
                "<g>",
                _generateBlock(_block),
                _generateAnimation(),
                "</path></g>"
            );
            if (_choice2 == Choice.BLOCK)
                svg = string.concat(svg, "<g opacity='0'>", _generateBlock(_block), REVEAL);
            if (_choice2 == Choice.PAPER)
                svg = string.concat(svg, TRANSFORM, _generatePaper(_paper), REVEAL);
            if (_choice2 == Choice.SCISSORS)
                svg = string.concat(svg, "<g opacity='0'>", _generateScissors(_scissors), REVEAL);
        }

        svg = string.concat(svg, "</svg>");
    }

    function generatePalette(
        uint256 _tokenId,
        uint256 _pot,
        address _player1,
        Choice _choice1,
        address _player2,
        Choice _choice2
    ) public pure returns (string memory _block, string memory _paper, string memory _scissors) {
        bytes32 hash = keccak256(
            abi.encodePacked(_tokenId, _pot, _player1, _choice1, _player2, _choice2)
        );

        uint256 index = _tokenId % 31;
        uint256 r = uint8(hash[index]) % 256;
        uint256 g = uint8(hash[index + 1]) % 256;
        uint256 b = uint8(hash[index + 2]) % 256;

        uint256 min = 30;
        uint256 max = 225;
        r = (r % (max - min)) + min;
        g = (g % (max - min)) + min;
        b = (b % (max - min)) + min;

        _block = string.concat("rgb(", r.toString(), ",", g.toString(), ",", b.toString(), ")");
        _paper = string.concat("rgb(", g.toString(), ",", b.toString(), ",", r.toString(), ")");
        _scissors = string.concat("rgb(", b.toString(), ",", r.toString(), ",", g.toString(), ")");
    }

    function getChoice(Choice _choice) external pure returns (string memory choice) {
        if (_choice == Choice.NONE) {
            choice = "None";
        } else if (_choice == Choice.HIDDEN) {
            choice = "Hidden";
        } else if (_choice == Choice.BLOCK) {
            choice = "Block";
        } else if (_choice == Choice.PAPER) {
            choice = "Paper";
        } else if (_choice == Choice.SCISSORS) {
            choice = "Scissors";
        }
    }

    function getStage(Stage _stage) external pure returns (string memory stage) {
        if (_stage == Stage.PENDING) {
            stage = "Pending";
        } else if (_stage == Stage.COMMIT) {
            stage = "Commit";
        } else if (_stage == Stage.REVEAL) {
            stage = "Reveal";
        } else if (_stage == Stage.SETTLE) {
            stage = "Settle";
        } else if (_stage == Stage.DRAW) {
            stage = "Draw";
        } else if (_stage == Stage.SUCCESS) {
            stage = "Success";
        }
    }

    function _generateGame(
        string memory _block,
        string memory _paper,
        string memory _scissors
    ) internal pure returns (string memory) {
        return
            string.concat(
                "<g><path fill='",
                _block,
                "' d='m697.2 548.4c10.801 7.1992 25.199 10.801 39.602 6 19.199-6 32.398-25.199 32.398-45.602v-42c16.801-9.6016 28.801-26.398 31.199-46.801l8.3984-61.199c4.8008-32.398-6-66-27.602-91.199l-54-62.398v-49.199c0-8.3984-6-14.398-14.398-14.398s-14.398 6-14.398 14.398v55.199c0 3.6016 1.1992 7.1992 3.6016 9.6016l57.602 66c16.801 19.199 24 43.199 20.398 68.398l-8.3984 61.199c-2.3984 18-18 31.199-36 30l-74.398-1.1992c-9.6016 0-16.801-7.1992-16.801-16.801 0-4.8008 2.3984-8.3984 4.8008-12 3.6016-3.6016 7.1992-4.8008 12-4.8008l50.398 2.3984c7.1992 0 13.199-4.8008 14.398-12l4.8008-32.398c1.1992-8.3984-4.8008-15.602-12-16.801s-15.602 4.8008-16.801 12l-2.3984 16.801c-50.398-24-46.801-73.199-46.801-75.602 1.1992-8.3984-4.8008-14.398-13.199-15.602h-1.1992c-7.1992 0-13.199 6-14.398 13.199-2.3984 20.398 3.6016 54 28.801 80.398-9.6016 1.1992-19.199 4.8008-26.398 12-3.6016 3.6016-6 7.1992-8.3984 10.801-7.1992-4.8008-15.602-7.1992-25.199-7.1992-14.398 0-27.602 7.1992-36 18-7.1992-6-16.801-8.3984-26.398-8.3984-7.1992 0-13.199 1.1992-19.199 4.8008-7.1992-40.801-8.3984-82.801-8.3984-112.8 0-26.398 14.398-50.398 36-64.801l13.199-8.3984c4.8008-2.3984 7.1992-7.1992 7.1992-12v-57.602c0-8.3984-6-14.398-14.398-14.398s-14.398 6-14.398 14.398v49.199l-2.4141 1.207c-31.199 19.199-49.199 52.801-50.398 88.801 0 37.199 1.1992 91.199 13.199 140.4-1.1992 4.8008-2.3984 9.6016-2.3984 14.398v46.801c0 25.199 20.398 48 45.602 48 10.801 0 19.199-3.6016 27.602-8.3984 8.3984 10.801 21.602 18 36 18 9.6016 0 19.199-3.6016 26.398-8.3984 8.3984 10.801 21.602 19.199 37.199 19.199 14.398-1.2031 27.598-8.4023 35.996-19.203zm8.4023-74.398 28.801 1.1992h4.8008v36c0 9.6016-7.1992 16.801-16.801 16.801-9.6016 0-16.801-7.1992-16.801-16.801zm-153.6 27.602c0 9.6016-7.1992 16.801-16.801 16.801-9.6016 0-16.801-7.1992-16.801-16.801v-49.199c0-9.6016 7.1992-16.801 16.801-16.801 9.6016 0 16.801 7.1992 16.801 16.801zm62.398 8.3984c0 9.6016-7.1992 16.801-16.801 16.801-9.6016 0-16.801-7.1992-16.801-16.801l0.003906-66c0-9.6016 7.1992-16.801 16.801-16.801 9.6016 0 16.801 7.1992 16.801 16.801zm28.801 10.801v-51.602c6 2.3984 12 3.6016 18 4.8008h15.602v46.801c0 9.6016-7.1992 16.801-16.801 16.801s-16.801-7.2031-16.801-16.801z'/><path fill='",
                _scissors,
                "' d='m546 687.6c-10.801-4.8008-24-6-34.801-1.1992l-108 39.602 72-60c19.199-15.602 21.602-45.602 6-64.801-16.801-19.199-45.602-21.602-64.801-6l-128.4 106.8c-16.801-12-39.602-14.398-60-6l-57.602 24c-31.199 13.199-54 38.398-64.801 69.602l-26.398 78-43.199 25.199c-7.1992 3.6016-9.6016 13.199-4.8008 19.199s13.199 9.6016 19.199 4.8008l48-27.602c3.6016-1.1992 4.8008-4.8008 6-8.3984l27.602-82.801c8.3984-24 25.199-43.199 49.199-52.801l57.602-24c16.801-7.1992 34.801 0 44.398 14.398v1.1992l36 64.801c4.8008 8.3984 1.1992 18-6 22.801-3.5977 3.6016-8.3984 3.6016-13.199 2.4023s-8.3984-4.8008-9.6016-8.3984l-24-45.602c-3.6016-6-10.801-9.6016-18-7.1992l-30 12c-7.1992 2.3984-10.801 10.801-8.3984 18 2.3984 7.1992 10.801 10.801 18 8.3984l15.602-6c4.8008 55.199-39.602 76.801-42 78-7.1992 3.6016-10.801 12-7.1992 19.199 0 0 0 1.1992 1.1992 1.1992 3.6016 6 12 9.6016 18 6 18-8.3984 44.398-30 55.199-64.801 6 7.1992 14.398 13.199 24 15.602 4.8008 1.1992 9.6016 2.3984 15.602 1.1992 0 6 0 13.199 2.3984 19.199 1.1992 2.3984 2.3984 4.8008 3.6016 7.1992 6 9.6016 15.602 16.801 25.199 20.398-3.6016 10.805-3.6016 20.406 0 30.004 1.1992 2.3984 2.3984 4.8008 3.6016 7.1992 3.6016 6 8.3984 10.801 13.199 14.398-30 24-62.398 43.199-86.398 57.602-22.801 13.199-50.398 13.199-74.398 1.1992l-14.398-7.1992c-4.8008-2.3984-9.6016-2.3984-14.398 0l-49.207 27.602c-7.1992 3.6016-9.6016 13.199-4.8008 19.199 3.6016 7.1992 13.199 9.6016 19.199 4.8008l43.199-25.199 7.1992 3.6016c32.398 16.801 70.801 16.801 102-1.1992 30-16.801 72-43.199 108-74.398 2.3984 0 3.6016-1.1992 6-1.1992l45.602-16.801c24-8.3984 36-34.801 27.602-58.801-3.6016-9.6016-9.6016-16.801-18-21.602 1.1992-1.1992 2.3984-3.6016 3.6016-4.8008 4.8008-10.801 6-24 1.1992-34.801-2.3984-6-4.8008-10.801-8.3984-14.398l92.398-33.602c12-3.6016 20.398-12 26.398-24 4.8008-10.801 6-24 1.1992-34.801-4.8008-12-13.199-20.398-24-26.398zm-237.6 36s0-1.1992-1.1992-1.1992l126-105.6c7.1992-6 18-4.8008 24 2.3984 1.1992 1.1992 1.1992 1.1992 1.1992 2.3984 3.6016 7.1992 2.3984 16.801-3.6016 21.602l-130.8 109.2zm37.203 151.2c-3.6016-2.3984-7.1992-4.8008-8.3984-9.6016-3.6016-8.3984 1.1992-19.199 9.6016-21.602l62.398-22.801c4.8008-1.1992 8.3984-1.1992 13.199 0 3.6016 1.1992 6 3.6016 7.1992 7.1992 0 1.1992 1.1992 1.1992 1.1992 2.3984 1.1992 4.8008 1.1992 8.3984-1.1992 13.199-2.3984 3.6016-4.8008 7.1992-9.6016 8.3984l-62.398 22.801c-3.6016 2.4062-8.4023 2.4062-12 0.007812zm98.398 31.199c-2.3984 3.6016-4.8008 7.1992-9.6016 8.3984l-45.602 16.801c-4.8008 1.1992-8.3984 1.1992-13.199 0-4.8008-1.1992-7.1992-4.8008-8.3984-9.6016-1.1992-4.8008-1.1992-8.3984 0-13.199 2.3984-3.6016 6-7.1992 9.6016-8.3984l45.602-16.801c8.3984-3.6016 19.199 1.1992 21.602 9.6016 2.3945 4.8008 2.3945 8.3984-0.003906 13.199zm97.199-169.2c-2.3984 3.6016-4.8008 7.1992-9.6016 8.3984l-182.4 66c0-7.1992-1.1992-15.602-4.8008-22.801l-4.8008-9.6016 181.2-66c4.8008-1.1992 8.3984-1.1992 13.199 0 3.6016 2.3984 7.1992 4.8008 8.3984 9.6016 1.207 6 1.207 9.6016-1.1953 14.402z'/><path fill='",
                _paper,
                "' d='m1162.8 882-42-24-21.602-70.801c-9.6016-33.602-32.398-61.199-63.602-78l-90-48c-21.602-12-48-4.8008-61.199 16.801-1.1992 1.1992-1.1992 3.6016-2.3984 4.8008l-97.199-54c-21.602-12-50.398-4.8008-62.398 16.801-3.6016 6-4.8008 13.199-6 20.398-18-2.3984-36 6-45.602 22.801-9.6016 16.801-7.1992 37.199 3.6016 50.398-6 3.6016-10.801 8.3984-14.398 15.602-12 21.602-4.8008 50.398 16.801 62.398l16.801 9.6016c-4.8008 3.6016-8.3984 8.3984-10.801 13.199-12 21.602-4.8008 50.398 16.801 62.398l61.199 34.801c14.398 8.3984 30 18 46.801 32.398l2.3984 1.1992c38.398 30 139.2 108 212.4 61.199l42 24c7.1992 3.6016 15.602 1.1992 19.199-6 3.6016-7.1992 1.1992-15.602-6-19.199l-49.199-27.602c-4.8008-2.3984-10.801-2.3984-15.602 1.1992-56.398 44.398-150-28.801-186-56.398l-2.3984-1.1992c-18-14.398-34.801-25.199-50.398-33.602l-61.199-34.801c-8.3984-4.8008-10.801-14.398-6-22.801 4.8008-8.3984 15.602-10.801 22.801-6l90 50.398c7.1992 3.6016 15.602 1.1992 19.199-6 3.6016-7.1992 1.1992-15.602-6-19.199l-140.41-82.793c-8.3984-4.8008-10.801-15.602-6-22.801 4.8008-8.3984 14.398-10.801 22.801-6l123.6 69.602c7.1992 3.6016 15.602 1.1992 19.199-4.8008 3.6016-7.1992 1.1992-15.602-6-19.199l-144-81.602c-8.3984-4.8008-10.801-15.602-6-22.801 4.8008-8.3984 15.602-10.801 22.801-6l133.2 75.602c7.1992 3.6016 15.602 1.1992 19.199-6 3.6016-7.1992 1.1992-15.602-6-19.199l-112.8-63.602c-8.3984-4.8008-10.801-15.602-6-22.801 4.8008-8.3984 14.398-10.801 22.801-6l108 61.199c3.6016 12 10.801 22.801 21.602 28.801l31.199 18c-25.199 100.8 54 134.4 55.199 134.4 7.1992 2.3984 14.398 0 18-6 0 0 0-1.1992 1.1992-1.1992 2.3984-7.1992-1.1992-15.602-8.3984-18-2.3984-1.1992-54-22.801-40.801-92.398 3.6016 2.3984 8.3984 6 12 9.6016l7.1992 6c6 4.8008 14.398 4.8008 20.398-1.1992 4.8008-6 4.8008-14.398-1.1992-20.398l-7.1992-6c-7.1992-13.203-15.598-20.402-26.398-25.203l-45.602-26.398c-4.8008-2.3984-7.1992-6-8.3984-10.801s0-9.6016 2.3984-13.199c4.8008-8.3984 15.602-10.801 22.801-6l90 48c24 13.199 42 34.801 50.398 61.199l22.801 76.801c1.1992 3.6016 3.6016 6 7.1992 8.3984l46.801 26.398c7.1992 3.6016 15.602 1.1992 19.199-6 4.8008-7.1992 2.4023-16.797-4.7969-20.398z'/></g>"
            );
    }

    function _generateAnimation() internal pure returns (string memory) {
        return
            "<animate attributeName='opacity' from='1' to='0' begin='6s' dur='0.5s' fill='freeze'/><animateTransform attributeName='transform' attributeType='XML' type='scale' keyTimes='0; 0.5; 1' keySplines='0.42 0 1 1; 0.42 0 1 1' values='1; 0.8; 1' begin='0s' dur='2s' repeatCount='3' calcMode='spline'/><animateTransform attributeName='transform' attributeType='XML' type='rotate' keyTimes='0; 0.5; 1' keySplines='0.42 0 1 1; 0.42 0 1 1' values='0; -10; 0' begin='0s' dur='2s' additive='sum' repeatCount='3' calcMode='spline'/>";
    }

    function _generateBlock(string memory _fill) internal pure returns (string memory) {
        return
            string.concat(
                "<path fill='",
                _fill,
                "' d='m868.8 459.6 67.199-8.3984c27.602-3.6016 54 4.8008 74.398 22.801l72 63.602c2.3984 2.3984 6 3.6016 10.801 3.6016h60c8.3984 0 15.602-7.1992 15.602-15.602 0-8.3984-7.1992-15.602-15.602-15.602h-54l-68.398-60c-27.602-24-63.602-34.801-99.602-30l-67.199 8.3945c-22.801 2.3984-40.801 15.602-51.602 33.602h-46.801c-22.801 0-43.199 13.199-50.398 34.801-4.8008 16.801-1.1992 32.398 7.1992 44.398-12 9.6016-20.398 24-20.398 40.801s8.3984 31.199 20.398 39.602c-6 8.3984-9.6016 18-9.6016 28.801 0 15.602 7.1992 30 19.199 39.602-6 8.3984-9.6016 18-9.6016 30 0 27.602 25.199 50.398 52.801 50.398l52.805-0.003906c6 0 10.801-1.1992 15.602-2.3984 54 12 112.8 14.398 153.6 14.398 39.602 0 76.801-20.398 97.199-55.199l4.8008-7.1992h54c8.3984 0 15.602-7.1992 15.602-15.602 0-8.3984-7.1992-15.602-15.602-15.602h-63.602c-6 0-10.801 2.3984-13.199 7.1992l-8.4023 13.203c-15.602 25.199-42 39.602-70.801 39.602-33.602 0-79.199-1.1992-123.6-8.3984 3.6016-6 4.8008-13.199 4.8008-21.602 0-10.801-3.6016-21.602-9.6016-30 12-9.6016 19.199-24 19.199-39.602 0-10.801-3.6016-20.398-8.3984-27.602 4.8008-2.3984 8.3984-4.8008 12-8.3984 7.1992-8.3984 12-18 13.199-28.801 28.801 27.602 66 34.801 87.602 32.398 8.3984-1.1992 14.398-7.1992 14.398-15.602l0.003906-1.1953c-1.1992-8.3984-8.3984-14.398-16.801-14.398-2.3984 0-56.398 4.8008-82.801-51.602l18-2.3984c8.3984-1.1992 14.398-9.6016 13.199-18-1.1992-8.3984-9.6016-14.398-18-13.199l-36 4.8008c-8.3984 1.1992-13.199 8.3984-13.199 16.801l2.3984 55.199c0 4.8008-1.1992 9.6016-4.8008 13.199-3.6016 3.6016-8.3984 6-13.199 6-9.6016 0-18-8.3984-18-18l-1.1992-81.602c-1.1992-16.801 13.203-34.801 32.402-37.199zm-104.4 34.797h39.602v6l1.1992 32.398h-39.602c-10.801 0-19.199-8.3984-19.199-19.199-1.1992-10.797 8.4023-19.199 18-19.199zm-30 87.602c0-10.801 8.3984-19.199 19.199-19.199h51.602v18c0 7.1992 2.3984 13.199 4.8008 20.398h-56.398c-10.801-1.1992-19.203-9.5977-19.203-19.199zm93.602 156h-54c-10.801 0-19.199-8.3984-19.199-19.199s8.3984-19.199 19.199-19.199h54c10.801 0 19.199 8.3984 19.199 19.199s-8.3984 19.199-19.199 19.199zm9.6016-105.6c10.801 0 19.199 8.3984 19.199 19.199 0 10.801-8.3984 19.199-19.199 19.199h-73.199c-10.801 0-19.199-8.3984-19.199-19.199 0-10.801 8.3984-19.199 19.199-19.199z'>"
            );
    }

    function _generatePaper(string memory _fill) internal pure returns (string memory) {
        return
            string.concat(
                "<path fill='",
                _fill,
                "' d='m532.8 547.2c3.6016-6 4.8008-13.199 4.8008-20.398 0-25.199-20.398-45.602-45.602-45.602h-109.2v-4.8008c-1.1992-24-20.398-43.199-44.398-44.398l-100.8-2.3984c-34.801-1.1992-67.199 12-92.398 36l-52.805 50.398h-48c-8.3984 0-14.398 6-14.398 14.398 0 8.3984 6 14.398 14.398 14.398h54c3.6016 0 7.1992-1.1992 9.6016-3.6016l57.602-54c19.199-19.199 45.602-28.801 72-27.602l100.8 2.3984c9.6016 0 16.801 7.1992 16.801 16.801 0 4.8008-1.1992 9.6016-4.8008 13.199-3.6016 3.6016-8.3984 6-13.199 6h-51.602c-10.801 0-22.801 1.1992-33.602 3.6016l-9.6016 2.3984c-7.1992 1.1992-12 9.6016-10.801 16.801 1.1992 7.1992 9.6016 12 16.801 10.801l9.6016-2.3984c4.8008-1.1992 9.6016-1.1992 14.398-2.3984-21.602 66-76.801 60-79.199 60-7.1992-1.1992-14.398 4.8008-15.602 12v2.3984c0 7.1992 4.8008 13.199 12 14.398 1.1992 0 86.398 9.6016 112.8-88.801h34.801c13.199 0 24-4.8008 33.602-14.398h122.4c9.6016 0 16.801 7.1992 16.801 16.801 0 9.6016-7.1992 16.801-16.801 16.801l-128.4 0.003906c-8.3984 0-14.398 6-14.398 14.398 0 8.3984 6 14.398 14.398 14.398l151.2 0.003906c9.6016 0 16.801 7.1992 16.801 16.801 0 9.6016-7.1992 16.801-16.801 16.801l-164.4-0.003906c-8.3984 0-14.398 6-14.398 14.398 0 7.1992 6 14.398 14.398 14.398h140.4c9.6016 0 16.801 7.1992 16.801 16.801 0 9.6016-7.1992 16.801-16.801 16.801l-160.8 0.003906c-8.3984 0-14.398 6-14.398 14.398 0 8.3984 6 14.398 14.398 14.398h102c9.6016 0 16.801 7.1992 16.801 16.801 0 9.6016-7.1992 16.801-16.801 16.801h-69.602c-18 0-37.199 1.1992-60 4.8008h-2.3984c-43.199 6-159.6 24-187.2-42-2.3984-8.3984-7.1992-12-13.199-12h-56.402c-7.1992 0-14.398 6-14.398 14.398 0 8.3984 6 14.398 14.398 14.398h48c40.801 75.602 165.6 57.602 213.6 50.398h2.3984c21.602-3.6016 39.602-4.8008 56.398-4.8008h69.602c25.199 0 45.602-20.398 45.602-45.602 0-6-1.1992-12-3.6016-16.801h18c25.199 0 45.602-20.398 45.602-45.602 0-7.1992-2.3984-14.398-4.8008-20.398 16.801-7.1992 28.801-22.801 28.801-42-2.3984-17.992-14.398-34.793-31.199-40.793z'>"
            );
    }

    function _generateScissors(string memory _fill) internal pure returns (string memory) {
        return
            string.concat(
                "<path fill='",
                _fill,
                "' d='m1155.6 672h-56.398c-4.8008 0-9.6016 2.3984-12 7.1992l-8.3984 13.199c-14.398 22.801-38.398 36-63.602 36-27.602 0-64.801-1.1992-102-6 2.3984-6 4.8008-12 4.8008-19.199 0-2.3984 0-4.8008-1.1992-8.3984-1.1992-9.6016-6-18-13.199-25.199 7.1992-8.3984 12-19.199 12-30 0-2.3984 0-4.8008-1.1992-8.3984-1.1992-6-3.6016-12-7.1992-18 4.8008-2.3984 8.3984-4.8008 12-8.3984 7.1992-7.1992 10.801-15.602 12-25.199 26.398 25.199 60 31.199 79.199 28.801 7.1992-1.1992 13.199-7.1992 13.199-14.398v-1.1992c-1.1992-7.1992-7.1992-13.199-15.602-13.199-2.3984 0-51.602 3.6016-74.398-45.602l15.602-2.3984c7.1992-1.1992 13.199-8.3984 12-15.602-1.1992-7.1992-8.3984-13.199-15.602-12l-32.398 4.8008c-7.1992 1.1992-12 7.1992-12 14.398l2.3984 50.398c0 4.8008-1.1992 8.3984-4.8008 12-3.6016 3.6016-7.1992 4.8008-12 4.8008-8.3984 0-16.801-7.1992-16.801-16.801l-1.1992-73.199v-1.1992c0-18 13.199-32.398 30-34.801l61.199-8.3984c24-3.6016 49.199 4.8008 67.199 20.398l64.801 57.602c2.3984 2.3984 6 3.6016 9.6016 3.6016h54c8.3984 0 14.398-6 14.398-14.398 0-8.3984-6-14.398-14.398-14.398h-49.199l-61.199-54c-25.199-21.602-57.602-31.199-90-27.602l-61.199 8.3984c-21.602 2.3984-39.602 16.801-48 34.801l-160.8-28.801c-25.199-4.8008-48 12-52.801 37.199-4.8008 24 12 48 37.199 52.801l91.199 15.602-112.8 22.789c-12 2.3984-22.801 8.3984-28.801 19.199-7.1992 9.6016-9.6016 21.602-7.1992 33.602 2.3984 12 8.3984 22.801 18 28.801 9.6016 7.1992 21.602 9.6016 33.602 7.1992l96-16.801c-1.1992 6-1.1992 10.801 0 16.801 2.3984 12 8.3984 22.801 18 28.801 1.1992 1.1992 3.6016 2.3984 4.8008 2.3984-3.6016 8.3984-6 18-3.6016 27.602 4.8008 24 27.602 40.801 52.801 37.199l48-8.3984c2.3984 0 3.6016-1.1992 6-1.1992 46.801 9.6016 96 10.801 129.6 10.801 36 0 68.398-19.199 87.602-49.199l3.6016-6h49.199c8.3984 0 14.398-6 14.398-14.398-0.003906-8.4062-6.0039-14.406-14.402-14.406zm-481.2-165.6c-8.3984-1.1992-14.398-8.3984-14.398-16.801v-2.3984c1.1992-9.6016 10.801-15.602 19.199-13.199l160.8 27.602v1.1992l1.1992 32.398zm-15.598 124.8c-4.8008 1.1992-8.3984 0-12-2.3984-3.6016-2.3984-6-6-7.1992-10.801-1.1992-4.8008 0-9.6016 2.3984-13.199 2.3984-3.6016 6-6 10.801-7.1992l187.2-33.602v10.801c0 8.3984 2.3984 15.602 7.1992 21.602zm128.4 24v-2.3984c0-3.6016 1.1992-7.1992 2.3984-9.6016 2.3984-3.6016 6-6 10.801-7.1992l64.801-12c9.6016-1.1992 18 4.8008 19.199 13.199 1.1992 4.8008 0 8.3984-2.3984 12-2.3984 3.6016-6 6-10.801 7.1992l-64.801 12c-4.8008 1.1992-8.3984 0-12-2.3984-3.5977-3.6016-6-7.1992-7.1992-10.801zm98.402 57.602c-2.3984 3.6016-6 6-10.801 7.1992l-48 8.3984c-4.8008 1.1992-8.3984 0-13.199-2.3984-3.6016-2.3984-6-6-7.1992-10.801-1.1992-9.6016 4.8008-18 13.199-19.199l48-8.3984c4.8008-1.1992 8.3984 0 13.199 2.3984 3.6016 2.3984 6 6 7.1992 10.801 0 3.5977 0 8.3984-2.3984 12z'>"
            );
    }
}

// File: lib/openzeppelin-contracts/contracts/utils/Strings.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Strings.sol)

pragma solidity ^0.8.0;

import "./math/Math.sol";

/**
 * @dev String operations.
 */
library Strings {
    bytes16 private constant _SYMBOLS = "0123456789abcdef";
    uint8 private constant _ADDRESS_LENGTH = 20;

    /**
     * @dev Converts a `uint256` to its ASCII `string` decimal representation.
     */
    function toString(uint256 value) internal pure returns (string memory) {
        unchecked {
            uint256 length = Math.log10(value) + 1;
            string memory buffer = new string(length);
            uint256 ptr;
            /// @solidity memory-safe-assembly
            assembly {
                ptr := add(buffer, add(32, length))
            }
            while (true) {
                ptr--;
                /// @solidity memory-safe-assembly
                assembly {
                    mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
                }
                value /= 10;
                if (value == 0) break;
            }
            return buffer;
        }
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
     */
    function toHexString(uint256 value) internal pure returns (string memory) {
        unchecked {
            return toHexString(value, Math.log256(value) + 1);
        }
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
     */
    function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
        bytes memory buffer = new bytes(2 * length + 2);
        buffer[0] = "0";
        buffer[1] = "x";
        for (uint256 i = 2 * length + 1; i > 1; --i) {
            buffer[i] = _SYMBOLS[value & 0xf];
            value >>= 4;
        }
        require(value == 0, "Strings: hex length insufficient");
        return string(buffer);
    }

    /**
     * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation.
     */
    function toHexString(address addr) internal pure returns (string memory) {
        return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
    }
}

// File: src/interfaces/IERC4906.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

/// @title EIP-721 Metadata Update Extension
interface IERC4906 {
    event MetadataUpdate(uint256 _tokenId);

    event BatchMetadataUpdate(uint256 _fromTokenId, uint256 _toTokenId);
}

// File: src/interfaces/IRoshambo.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

enum Stage {
    PENDING,
    COMMIT,
    REVEAL,
    SETTLE,
    DRAW,
    SUCCESS
}

enum Choice {
    NONE,
    HIDDEN,
    BLOCK,
    PAPER,
    SCISSORS
}

struct Game {
    Player p1;
    Player p2;
    Stage stage;
    uint8 totalRounds;
    uint8 currentRound;
    uint72 pot;
    address winner;
    mapping(uint8 => Round) rounds;
}

struct Round {
    Choice p1Choice;
    Choice p2Choice;
    uint40 commitBlock;
    uint40 revealBlock;
    address winner;
}

struct Player {
    address player;
    uint96 wins;
    bytes32 commitment;
}

/// @title Interface for Roshambo contract
interface IRoshambo {
    error AlreadyCommitted();
    error AlreadyRevealed();
    error InsufficientBalance();
    error InsufficientWager();
    error InvalidChoice();
    error InvalidGame();
    error InvalidPlayer();
    error InvalidReveal();
    error InvalidRounds();
    error InvalidStage();
    error InvalidWager();
    error TimeElapsed();
    error TransferFailed();

    event NewGame(
        uint40 indexed _gameId,
        uint8 indexed _rounds,
        address indexed _player1,
        uint72 _wager
    );
    event JoinGame(uint40 indexed _gameId, address indexed _player2, uint72 indexed _pot);
    event Commit(
        uint40 indexed _gameId,
        uint8 indexed _round,
        address indexed _player,
        bytes32 _commitment,
        Stage _stage
    );
    event Reveal(
        uint40 indexed _gameId,
        uint8 indexed _round,
        address indexed _player,
        Choice _choice,
        Stage _stage
    );
    event ResetRound(uint40 indexed _gameId, uint8 indexed _round, uint40 indexed _commitBlock);
    event NextRound(uint40 indexed _gameId, uint8 indexed _round, uint40 indexed _commitBlock);
    event CurrentRound(uint40 indexed _gameId, uint8 indexed _round, uint40 indexed _revealBlock);
    event Settle(
        uint40 indexed _gameId,
        uint8 indexed _round,
        Stage indexed _stage,
        address _winner,
        address _player1,
        Choice _choice1,
        uint96 _p1Wins,
        address _player2,
        Choice _choice2,
        uint96 _p2Wins
    );
    event Cancel(uint40 indexed _gameId, address indexed _player1, uint72 indexed _wager);
    event Withdraw(address indexed _sender, address indexed _to, uint256 indexed _balance);

    function BLOCK_DURATION() external view returns (uint256);

    function MIN_WAGER() external view returns (uint256);

    function balances(address) external view returns (uint256);

    function beneficiary() external view returns (address);

    function cancel(uint40 _gameId) external;

    function commit(uint40 _gameId, bytes32 _commit) external;

    function currentId() external view returns (uint40);

    function getCommit(
        address _player,
        Choice _choice,
        string calldata _secret
    ) external pure returns (bytes32);

    function getUsageRate(
        address _player,
        Choice _choice
    ) external view returns (uint256, uint256, uint256);

    function getWinRate(address _player) external view returns (uint256, uint256, uint256, uint256);

    function getProfitMargin(address _player) external view returns (uint256, uint256, uint256);

    function getRound(
        uint40 _gameId,
        uint8 _round
    )
        external
        view
        returns (
            Choice player1Choice,
            Choice player2Choice,
            uint40 commitBlock,
            uint40 revealBlock,
            address winner
        );

    function joinGame(uint40 _gameId, bytes32 _commitment) external payable;

    function newGame(uint8 _rounds, bytes32 _commitment) external payable returns (uint40);

    function pause() external payable;

    function rake() external view returns (uint16);

    function reveal(uint40 _gameId, Choice _choice, string calldata _secret) external;

    function setBeneficiary(address _beneficiary) external payable;

    function setRake(uint16 _rake) external payable;

    function settle(uint40 _gameId) external;

    function totalSupply() external view returns (uint40);

    function unpause() external payable;

    function withdraw(address _to) external;
}

// File: lib/openzeppelin-contracts/contracts/token/ERC721/IERC721.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC721/IERC721.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721 is IERC165 {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata data
    ) external;

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721
     * or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
     * understand this adds an external call which potentially creates a reentrancy vulnerability.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool _approved) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);
}

// File: lib/openzeppelin-contracts/contracts/token/ERC721/IERC721Receiver.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC721/IERC721Receiver.sol)

pragma solidity ^0.8.0;

/**
 * @title ERC721 token receiver interface
 * @dev Interface for any contract that wants to support safeTransfers
 * from ERC721 asset contracts.
 */
interface IERC721Receiver {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
     *
     * The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

// File: lib/openzeppelin-contracts/contracts/token/ERC721/extensions/IERC721Metadata.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC721/extensions/IERC721Metadata.sol)

pragma solidity ^0.8.0;

import "../IERC721.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional metadata extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721Metadata is IERC721 {
    /**
     * @dev Returns the token collection name.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the token collection symbol.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
     */
    function tokenURI(uint256 tokenId) external view returns (string memory);
}

// File: lib/openzeppelin-contracts/contracts/utils/Address.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Address.sol)

pragma solidity ^0.8.1;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     *
     * [IMPORTANT]
     * ====
     * You shouldn't rely on `isContract` to protect against flash loan attacks!
     *
     * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
     * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
     * constructor.
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize/address.code.length, which returns 0
        // for contracts in construction, since the code is only stored at the end
        // of the constructor execution.

        return account.code.length > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain `call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
     * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
     *
     * _Available since v4.8._
     */
    function verifyCallResultFromTarget(
        address target,
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        if (success) {
            if (returndata.length == 0) {
                // only check isContract if the call was successful and the return data is empty
                // otherwise we already know that it was a contract
                require(isContract(target), "Address: call to non-contract");
            }
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    /**
     * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason or using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    function _revert(bytes memory returndata, string memory errorMessage) private pure {
        // Look for revert reason and bubble it up if present
        if (returndata.length > 0) {
            // The easiest way to bubble the revert reason is using memory via assembly
            /// @solidity memory-safe-assembly
            assembly {
                let returndata_size := mload(returndata)
                revert(add(32, returndata), returndata_size)
            }
        } else {
            revert(errorMessage);
        }
    }
}

// File: lib/openzeppelin-contracts/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

// File: lib/openzeppelin-contracts/contracts/utils/introspection/ERC165.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/ERC165.sol)

pragma solidity ^0.8.0;

import "./IERC165.sol";

/**
 * @dev Implementation of the {IERC165} interface.
 *
 * Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
 * for the additional interface id that will be supported. For example:
 *
 * ```solidity
 * function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
 *     return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
 * }
 * ```
 *
 * Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.
 */
abstract contract ERC165 is IERC165 {
    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IERC165).interfaceId;
    }
}

// File: lib/solmate/src/auth/Owned.sol



// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @notice Simple single owner authorization mixin.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/auth/Owned.sol)
abstract contract Owned {
    /*//////////////////////////////////////////////////////////////
                                 EVENTS
    //////////////////////////////////////////////////////////////*/

    event OwnershipTransferred(address indexed user, address indexed newOwner);

    /*//////////////////////////////////////////////////////////////
                            OWNERSHIP STORAGE
    //////////////////////////////////////////////////////////////*/

    address public owner;

    modifier onlyOwner() virtual {
        require(msg.sender == owner, "UNAUTHORIZED");

        _;
    }

    /*//////////////////////////////////////////////////////////////
                               CONSTRUCTOR
    //////////////////////////////////////////////////////////////*/

    constructor(address _owner) {
        owner = _owner;

        emit OwnershipTransferred(address(0), _owner);
    }

    /*//////////////////////////////////////////////////////////////
                             OWNERSHIP LOGIC
    //////////////////////////////////////////////////////////////*/

    function transferOwnership(address newOwner) public virtual onlyOwner {
        owner = newOwner;

        emit OwnershipTransferred(msg.sender, newOwner);
    }
}

// File: src/interfaces/IRecorder.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

struct Record {
    uint64 playerId;
    uint48 roundsWon;
    uint48 seriesWon;
    uint48 roundsLost;
    uint48 seriesLost;
    uint128 wagers;
    uint128 profits;
    uint40[] games;
}

/// @title Interface for Recorder contract
interface IRecorder {
    event SetRecord(
        address indexed _player,
        uint64 indexed _playerId,
        uint40 indexed _gameId,
        uint48 roundsWon,
        uint48 seriesWon,
        uint48 roundsLost,
        uint48 seriesLost,
        uint128 _wagers,
        uint128 _profits
    );

    function adjustRake(uint256 _pot, uint256 _rake) external pure returns (uint256);

    function getGameIds(address _player) external view returns (uint40[] memory);

    function setRecord(
        uint40 _gameId,
        uint72 _pot,
        uint8 _totalRounds,
        uint8 _currentRound,
        address _winner,
        address _loser
    ) external;

    function weiToEth(uint256 _weiAmount) external pure returns (string memory);
}

// File: src/interfaces/IRenderer.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {Choice, Stage} from "src/interfaces/IRoshambo.sol";

string constant ROOT = "<svg version='1.1' viewBox='0 0 1200 1200' xmlns='http://www.w3.org/2000/svg'>";
string constant TRANSFORM = "<g opacity='0' transform='scale (-1, 1)' transform-origin='center'>";
string constant REVEAL = "</path><animate attributeName='opacity' from='0' to='1' begin='6s' dur='3s' fill='freeze'/></g>";

/// @title Interface for Renderer contract
interface IRenderer {
    function generateImage(
        uint256 _tokenId,
        uint256 _pot,
        address _player1,
        Choice _choice1,
        address _player2,
        Choice _choice2
    ) external pure returns (string memory);

    function generatePalette(
        uint256 _tokenId,
        uint256 _pot,
        address _player1,
        Choice _choice1,
        address _player2,
        Choice _choice2
    ) external pure returns (string memory, string memory, string memory);

    function getChoice(Choice _choice) external pure returns (string memory);

    function getStage(Stage _stage) external pure returns (string memory);
}

// File: lib/openzeppelin-contracts/contracts/utils/math/Math.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/Math.sol)

pragma solidity ^0.8.0;

/**
 * @dev Standard math utilities missing in the Solidity language.
 */
library Math {
    enum Rounding {
        Down, // Toward negative infinity
        Up, // Toward infinity
        Zero // Toward zero
    }

    /**
     * @dev Returns the largest of two numbers.
     */
    function max(uint256 a, uint256 b) internal pure returns (uint256) {
        return a > b ? a : b;
    }

    /**
     * @dev Returns the smallest of two numbers.
     */
    function min(uint256 a, uint256 b) internal pure returns (uint256) {
        return a < b ? a : b;
    }

    /**
     * @dev Returns the average of two numbers. The result is rounded towards
     * zero.
     */
    function average(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b) / 2 can overflow.
        return (a & b) + (a ^ b) / 2;
    }

    /**
     * @dev Returns the ceiling of the division of two numbers.
     *
     * This differs from standard division with `/` in that it rounds up instead
     * of rounding down.
     */
    function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b - 1) / b can overflow on addition, so we distribute.
        return a == 0 ? 0 : (a - 1) / b + 1;
    }

    /**
     * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
     * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
     * with further edits by Uniswap Labs also under MIT license.
     */
    function mulDiv(
        uint256 x,
        uint256 y,
        uint256 denominator
    ) internal pure returns (uint256 result) {
        unchecked {
            // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
            // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
            // variables such that product = prod1 * 2^256 + prod0.
            uint256 prod0; // Least significant 256 bits of the product
            uint256 prod1; // Most significant 256 bits of the product
            assembly {
                let mm := mulmod(x, y, not(0))
                prod0 := mul(x, y)
                prod1 := sub(sub(mm, prod0), lt(mm, prod0))
            }

            // Handle non-overflow cases, 256 by 256 division.
            if (prod1 == 0) {
                return prod0 / denominator;
            }

            // Make sure the result is less than 2^256. Also prevents denominator == 0.
            require(denominator > prod1);

            ///////////////////////////////////////////////
            // 512 by 256 division.
            ///////////////////////////////////////////////

            // Make division exact by subtracting the remainder from [prod1 prod0].
            uint256 remainder;
            assembly {
                // Compute remainder using mulmod.
                remainder := mulmod(x, y, denominator)

                // Subtract 256 bit number from 512 bit number.
                prod1 := sub(prod1, gt(remainder, prod0))
                prod0 := sub(prod0, remainder)
            }

            // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
            // See https://cs.stackexchange.com/q/138556/92363.

            // Does not overflow because the denominator cannot be zero at this stage in the function.
            uint256 twos = denominator & (~denominator + 1);
            assembly {
                // Divide denominator by twos.
                denominator := div(denominator, twos)

                // Divide [prod1 prod0] by twos.
                prod0 := div(prod0, twos)

                // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
                twos := add(div(sub(0, twos), twos), 1)
            }

            // Shift in bits from prod1 into prod0.
            prod0 |= prod1 * twos;

            // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
            // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
            // four bits. That is, denominator * inv = 1 mod 2^4.
            uint256 inverse = (3 * denominator) ^ 2;

            // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
            // in modular arithmetic, doubling the correct bits in each step.
            inverse *= 2 - denominator * inverse; // inverse mod 2^8
            inverse *= 2 - denominator * inverse; // inverse mod 2^16
            inverse *= 2 - denominator * inverse; // inverse mod 2^32
            inverse *= 2 - denominator * inverse; // inverse mod 2^64
            inverse *= 2 - denominator * inverse; // inverse mod 2^128
            inverse *= 2 - denominator * inverse; // inverse mod 2^256

            // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
            // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
            // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
            // is no longer required.
            result = prod0 * inverse;
            return result;
        }
    }

    /**
     * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
     */
    function mulDiv(
        uint256 x,
        uint256 y,
        uint256 denominator,
        Rounding rounding
    ) internal pure returns (uint256) {
        uint256 result = mulDiv(x, y, denominator);
        if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
            result += 1;
        }
        return result;
    }

    /**
     * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
     *
     * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
     */
    function sqrt(uint256 a) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }

        // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
        //
        // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
        // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
        //
        // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
        // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
        // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
        //
        // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
        uint256 result = 1 << (log2(a) >> 1);

        // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
        // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
        // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
        // into the expected uint128 result.
        unchecked {
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            return min(result, a / result);
        }
    }

    /**
     * @notice Calculates sqrt(a), following the selected rounding direction.
     */
    function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = sqrt(a);
            return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 2, rounded down, of a positive value.
     * Returns 0 if given 0.
     */
    function log2(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >> 128 > 0) {
                value >>= 128;
                result += 128;
            }
            if (value >> 64 > 0) {
                value >>= 64;
                result += 64;
            }
            if (value >> 32 > 0) {
                value >>= 32;
                result += 32;
            }
            if (value >> 16 > 0) {
                value >>= 16;
                result += 16;
            }
            if (value >> 8 > 0) {
                value >>= 8;
                result += 8;
            }
            if (value >> 4 > 0) {
                value >>= 4;
                result += 4;
            }
            if (value >> 2 > 0) {
                value >>= 2;
                result += 2;
            }
            if (value >> 1 > 0) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log2(value);
            return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 10, rounded down, of a positive value.
     * Returns 0 if given 0.
     */
    function log10(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >= 10**64) {
                value /= 10**64;
                result += 64;
            }
            if (value >= 10**32) {
                value /= 10**32;
                result += 32;
            }
            if (value >= 10**16) {
                value /= 10**16;
                result += 16;
            }
            if (value >= 10**8) {
                value /= 10**8;
                result += 8;
            }
            if (value >= 10**4) {
                value /= 10**4;
                result += 4;
            }
            if (value >= 10**2) {
                value /= 10**2;
                result += 2;
            }
            if (value >= 10**1) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log10(value);
            return result + (rounding == Rounding.Up && 10**result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 256, rounded down, of a positive value.
     * Returns 0 if given 0.
     *
     * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
     */
    function log256(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >> 128 > 0) {
                value >>= 128;
                result += 16;
            }
            if (value >> 64 > 0) {
                value >>= 64;
                result += 8;
            }
            if (value >> 32 > 0) {
                value >>= 32;
                result += 4;
            }
            if (value >> 16 > 0) {
                value >>= 16;
                result += 2;
            }
            if (value >> 8 > 0) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log256(value);
            return result + (rounding == Rounding.Up && 1 << (result * 8) < value ? 1 : 0);
        }
    }
}

// File: lib/openzeppelin-contracts/contracts/utils/introspection/IERC165.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}