Abstract Testnet

Contract Diff Checker

Contract Name:
FactionSystem

Contract Source Code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {IGameItems, ID as GAME_ITEMS_CONTRACT_ID} from "../tokens/IGameItems.sol";
import {IGigaNoobNFT, ID as NOOB_NFT_CONTRACT_ID} from "../tokens/giganoobnft/IGigaNoobNFT.sol";
import {IFactionSystem, ID as ID} from "./IFactionSystem.sol";
import {DataTable} from "../db/DataTable.sol";

import "@openzeppelin/contracts/utils/introspection/IERC165.sol";

import {MANAGER_ROLE, GAME_LOGIC_CONTRACT_ROLE, DEPLOYER_ROLE, SERVER_JUDGE_ROLE} from "../constants/RoleConstants.sol";
import {FACTION_CID, NAME_CID, NOOB_TOKEN_CID, MAX_SUPPLY_CID, MINT_COUNT_CID, PLAYER_CID} from "../constants/ColumnConstants.sol";

contract FactionSystem is IFactionSystem, DataTable {

    constructor(address gameRegistryAddress) DataTable(gameRegistryAddress, ID) {}

    function initialize() external override onlyRole(DEPLOYER_ROLE) {
        initializeTable("FactionSystem", ID);
    }

    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IFactionSystem).interfaceId
            || interfaceId == type(IERC165).interfaceId;
    }

    ////////////////////
    // Public Writes //
    //////////////////

    function chooseFaction(uint256 noobId, uint256 factionId) override external whenNotPaused {
        _confirmNoobToFaction(msg.sender, noobId, factionId);
    }

    function setFactionForNoob(address player, uint256 noobId, uint256 factionId) external onlyRole(SERVER_JUDGE_ROLE) {
        _confirmNoobToFaction(player, noobId, factionId);
    }

    ////////////////////
    // Public Reads ///
    //////////////////

    function isNoobInFaction(uint256 noobId, uint256 factionId) public view returns (bool) {
        return getDocUint256Value(getNoobFactionDocId(noobId), FACTION_CID) == factionId;
    }

    function getNoobFactionDocId(uint256 noobId) public pure returns (uint256) {
        return uint256(keccak256(abi.encodePacked("noob", noobId)));
    }
    
    function getFactionCurrentMemberCount(uint256 factionId) public view returns (uint256) {
        return getDocUint256Value(factionId, MINT_COUNT_CID);
    }

    function getFactionMaxMembers(uint256 factionId) public view returns (uint256) {
        return getDocUint256Value(factionId, MAX_SUPPLY_CID);
    }

    function getFactionName(uint256 factionId) public view returns (string memory) {
        return getDocStringValue(factionId, NAME_CID);
    }

    function getFactionOfNoob(uint256 noobId) public view returns (uint256) {
        return getDocUint256Value(getNoobFactionDocId(noobId), FACTION_CID);
    }

    //////////////
    // Manager //
    ////////////

    function setFaction(uint256 factionId, uint256 maxMembers, string memory factionName) external onlyRole(MANAGER_ROLE) {
        _setDocStringValue(factionId, NAME_CID, factionName);
        _setDocUint256Value(factionId, FACTION_CID, factionId);
        _setDocUint256Value(factionId, MAX_SUPPLY_CID, maxMembers);
    }

    /////////////////////
    // Game Contracts //
    ///////////////////



    ////////////////////////
    // Internal Helpers ///
    //////////////////////
 
    function _factionExists(uint256 factionId) private view returns (bool) {
        return getDocUint256Value(factionId, FACTION_CID) != 0;
    }

    function _confirmNoobToFaction(address player, uint256 noobId, uint256 factionId) private returns (bool) {
        require(_factionExists(factionId), "Faction not found");
        require(IGigaNoobNFT(_gameRegistry.getSystem(NOOB_NFT_CONTRACT_ID)).ownerOf(noobId) == player, "Not owner of noob");
        uint256 currentMemberCount = getFactionCurrentMemberCount(factionId);
        require(currentMemberCount <  getFactionMaxMembers(factionId), "Faction is full");
        uint256 noobFactionDocId = getNoobFactionDocId(noobId);
        uint256 currentFactionId = getFactionOfNoob(noobId);
        require(currentFactionId == 0, "Noob already in faction");

        _setDocUint256Value(noobFactionDocId, FACTION_CID, factionId);
        _setDocAddressValue(noobFactionDocId, PLAYER_CID, player);
        _setDocUint256Value(noobFactionDocId, NOOB_TOKEN_CID, noobId);
        _setDocUint256Value(factionId, MINT_COUNT_CID, currentMemberCount + 1);
        return true;
    }

}

// SPDX-License-Identifier: MIT LICENSE

pragma solidity ^0.8.9;

import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";

uint256 constant ID = uint256(keccak256("game.gigaverse.gameitems"));

interface IGameItems is IERC1155 {
    /**
     * Mints a ERC1155 token
     *
     * @param to        Recipient of the token
     * @param id        Id of token to mint
     * @param amount    Quantity of token to mint
     */
    function mint(address to, uint256 id, uint256 amount) external;

    /**
     * Burn a token - any payment / game logic should be handled in the game contract.
     *
     * @param from      Account to burn from
     * @param id        Id of the token to burn
     * @param amount    Quantity to burn
     */
    function burn(address from, uint256 id, uint256 amount) external;

    /**
     * @param id  Id of the type to get data for
     *
     * @return How many of the given token id have been minted
     */
    function minted(uint256 id) external view returns (uint256);

    function burnBatch(address from, uint256[] memory ids, uint256[] memory amounts) external;
}

// SPDX-License-Identifier: MIT LICENSE

pragma solidity ^0.8.9;

import {IGameNFT} from "../gamenft/IGameNFT.sol";

uint256 constant ID = uint256(keccak256("game.gigaverse.giganoobnft"));

interface IGigaNoobNFT is IGameNFT {
    function mint(address to) external returns (uint256);
    function burnByGameContract(uint256 id) external;
}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/introspection/IERC165.sol";

uint256 constant ID = uint256(keccak256("game.gigaverse.system.faction"));

interface IFactionSystem is IERC165 {
    function chooseFaction(uint256 noobId,uint256 factionId) external;
    function isNoobInFaction(uint256 noobId, uint256 factionId) external view returns (bool);
    function getFactionCurrentMemberCount(uint256 factionId) external view returns (uint256);
    function getFactionMaxMembers(uint256 factionId) external view returns (uint256);
    function getFactionName(uint256 factionId) external view returns (string memory);
    function getFactionOfNoob(uint256 noobId) external view returns (uint256);
}

// SPDX-License-Identifier: MIT LICENSE

pragma solidity ^0.8.9;

import {DataStore, ID as DATA_STORE_ID} from "./DataStore.sol";
import {DEPLOYER_ROLE} from "../constants/RoleConstants.sol";
import {NAME_CID, ADDRESS_CID, ID_CID, NEXT_DOCID_CID, OWNER_CID} from "../constants/ColumnConstants.sol";
import {GameRegistryConsumer} from "../core/GameRegistryConsumer.sol";

contract DataTable is GameRegistryConsumer {

    error AlreadyInitialized();

    bool private _initialized;

    constructor(address gameRegistryAddress, uint256 ID) GameRegistryConsumer(gameRegistryAddress, ID) {}   

    function owner() public view virtual returns (address) {
        return getTableAddressValue(OWNER_CID);
    }

    function name() public view virtual returns (string memory) {
        return getTableStringValue(NAME_CID);
    }

    function initializeTable(string memory nameToSet, uint256 id) internal {
        if (_initialized) {
            revert AlreadyInitialized();
        }
        _setTableAddressValue(ADDRESS_CID, address(this));
        _setTableAddressValue(OWNER_CID, msg.sender);
        _setTableStringValue(NAME_CID, nameToSet);
        _setTableUint256Value(ID_CID, id);
        _initialized = true;
    }

    function getTableId() public virtual view returns (uint256) {
        return getId();
    }

    function _getAndIncrementAutoIncId() internal returns (uint256) {
        uint256 currentId = getTableUint256Value(NEXT_DOCID_CID);
        _setTableUint256Value(NEXT_DOCID_CID, currentId + 1);
        return currentId + 1;
    }

    function _incrementAmount(uint256 docId, uint256 columnId, uint256 amount) internal {
        uint256 currentAmount = getDocUint256Value(docId, columnId);
        _setDocUint256Value(docId, columnId, currentAmount + amount);
    }

    function _decrementAmount(uint256 docId, uint256 columnId, uint256 amount) internal {
        uint256 currentAmount = getDocUint256Value(docId, columnId);
        _setDocUint256Value(docId, columnId, currentAmount - amount);
    }

    function _setTableStringValue(uint256 columnId, string memory value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setString(getTableId(), 0, columnId, value);
    }

    function _setTableUint256Value(uint256 columnId, uint256 value) internal {
       DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setUint256(getTableId(), 0, columnId, value);
    }

    function _setTableAddressValue(uint256 columnId, address value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setAddress(getTableId(), 0, columnId, value);
    }

    function _setTableBoolValue(uint256 columnId, bool value) internal {
         DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setBool(getTableId(), 0, columnId, value);
    }

    function _setTableAdddressValue(uint256 columnId, address value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setAddress(getTableId(), 0, columnId, value);
    }

    function _setTableUint256ArrayValue(uint256 columnId, uint256[] memory value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setUint256ArrayValue(getTableId(), 0, columnId, value);
    }

    function _setTableBoolArrayValue(uint256 columnId, bool[] memory value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setBoolArrayValue(getTableId(), 0, columnId, value);
    }

    function _setTableAddressArrayValue(uint256 columnId, address[] memory value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setAddressArrayValue(getTableId(), 0, columnId, value);
    }

    function _setDocAddressArrayValue(uint256 docId, uint256 columnId, address[] memory value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setAddressArrayValue(getTableId(), docId, columnId, value);
    }

    function _setDocBoolArrayValue(uint256 docId, uint256 columnId, bool[] memory value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setBoolArrayValue(getTableId(), docId, columnId, value);
    }

    function _setDocStringValue(uint256 docId, uint256 columnId, string memory value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setString(getTableId(), docId, columnId, value);
    }

    function _setDocUint256Value(uint256 docId, uint256 columnId, uint256 value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setUint256(getTableId(), docId, columnId, value);
    }

    function _setDocUint256ArrayValue(uint256 docId, uint256 columnId, uint256[] memory value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setUint256ArrayValue(getTableId(), docId, columnId, value);
    }

    function _setDocBoolValue(uint256 docId, uint256 columnId, bool value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setBool(getTableId(), docId, columnId, value);
    }

    function _setDocAddressValue(uint256 docId, uint256 columnId, address value) internal {
        DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).setAddress(getTableId(), docId, columnId, value);
    }
    
    function getTableBoolValue(uint256 columnId) public view returns (bool) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).getBool(getTableId(), 0, columnId);
    }

    function getTableUint256Value(uint256 columnId) public view returns (uint256) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).getUint256(getTableId(), 0, columnId);
    }

    function getTableStringValue(uint256 columnId) public view returns (string memory) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).getString(getTableId(), 0, columnId);
    }

    function getTableAddressValue(uint256 columnId) public view returns (address) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).getAddress(getTableId(), 0, columnId);
    }

    function getTableUint256ArrayValue(uint256 columnId) public view returns (uint256[] memory) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).getUint256Array(getTableId(), 0, columnId);
    }

    function getDocUint256ArrayValue(uint256 docId, uint256 columnId) public view returns (uint256[] memory) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).getUint256Array(getTableId(), docId, columnId);
    }

    function getDocStringValue(uint256 docId, uint256 columnId) public view returns (string memory) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).getString(getTableId(), docId, columnId);
    }

    function getDocUint256Value(uint256 docId, uint256 columnId) public view returns (uint256) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).getUint256(getTableId(), docId, columnId);
    }

    function getDocBoolValue(uint256 docId, uint256 columnId) public view returns (bool) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).getBool(getTableId(), docId, columnId);
    }

    function getDocAddressValue(uint256 docId, uint256 columnId) public view returns (address) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).getAddress(getTableId(), docId, columnId);
    }

    function hasDocStringValue(uint256 docId, uint256 columnId) public view returns (bool) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).hasStringValue(getTableId(), docId, columnId);
    }

    function hasDocValue(uint256 docId, uint256 columnId) public view returns (bool) {
        return DataStore(_gameRegistry.getSystem(DATA_STORE_ID)).hasValue(getTableId(), docId, columnId);
    }

    function getPlayerDocId(address player) public pure returns (uint256) {
        return uint256(uint160(player));
    }
}

// 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);
}

// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.9;
// Pauser Role - Can pause the game
bytes32 constant PAUSER_ROLE = keccak256("PAUSER_ROLE");

// Minter Role - Can mint items, NFTs, and ERC20 currency
bytes32 constant MINTER_ROLE = keccak256("MINTER_ROLE");

// Manager Role - Can manage the shop, loot tables, and other game data
bytes32 constant MANAGER_ROLE = keccak256("MANAGER_ROLE");

// Depoloyer Role - Can Deploy new Systems
bytes32 constant DEPLOYER_ROLE = keccak256("DEPLOYER_ROLE");

// Game Logic Contract - Contract that executes game logic and accesses other systems
bytes32 constant GAME_LOGIC_CONTRACT_ROLE = keccak256("GAME_LOGIC_CONTRACT_ROLE");

// For functions callable from game server
bytes32 constant SERVER_JUDGE_ROLE = keccak256("SERVER_JUDGE_ROLE");

// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.9;

uint256 constant NAME_CID = uint256(keccak256("name"));
uint256 constant DESCRIPTION_CID = uint256(keccak256("description"));
uint256 constant LEVEL_CID = uint256(keccak256("level"));
uint256 constant IS_NOOB_CID = uint256(keccak256("is_noob"));
uint256 constant NOOB_TOKEN_CID = uint256(keccak256("noob_tokend_id"));
uint256 constant GIGA_NAME_TOKENDID_CID = uint256(keccak256("giganame_tokend_id"));
uint256 constant IS_GIGA_NAME_CID = uint256(keccak256("is_giga_name"));
uint256 constant GAME_ITEM_ID_CID = uint256(keccak256("game_item_id"));
uint256 constant UINT256_CID = uint256(keccak256("int256"));
uint256 constant ETH_MINT_PRICE_CID = uint256(keccak256("eth_mint_price"));
uint256 constant NEXT_DOCID_CID = uint256(keccak256("next_token_id"));
uint256 constant ID_CID = uint256(keccak256("id"));
uint256 constant BASE_NAME_CID = uint256(keccak256("base_name"));
uint256 constant BASE_URI_CID = uint256(keccak256("base_uri"));
uint256 constant LAST_TRANSFER_TIME_CID = uint256(keccak256("last_transfer_time"));
uint256 constant OWNER_CID = uint256(keccak256("owner"));
uint256 constant INITIALIZED_CID = uint256(keccak256("initialized"));
uint256 constant MAX_SUPPLY_CID = uint256(keccak256("max_supply"));
uint256 constant ADDRESS_CID = uint256(keccak256("address"));
uint256 constant IS_SOULBOUND_CID = uint256(keccak256("soulbound"));
uint256 constant TIME_BETWEEN_CID = uint256(keccak256("time_between"));
uint256 constant TIMESTAMP_CID = uint256(keccak256("timestamp"));
uint256 constant IMG_URL_CID = uint256(keccak256("img_url"));
uint256 constant PLAYER_CID = uint256(keccak256("player"));
uint256 constant MINT_COUNT_CID = uint256(keccak256("mint_count"));
uint256 constant CONTRACT_URI_CID = uint256(keccak256("contract_uri"));
uint256 constant IS_RECYCLABLE_CID = uint256(keccak256("is_recyclable"));
uint256 constant BURN_COUNT_CID = uint256(keccak256("burn_count"));
uint256 constant BALANCE_CID = uint256(keccak256("balance"));
uint256 constant ICON_URL_CID = uint256(keccak256("icon_url"));
uint256 constant DUNGEON_ID_CID = uint256(keccak256("dungeon_id"));
uint256 constant ENERGY_CID = uint256(keccak256("energy"));
uint256 constant IS_CANCELLED_CID = uint256(keccak256("is_cancelled"));
uint256 constant SPRITE_SHEET_URL_CID = uint256(keccak256("sprite_sheet_url"));
uint256 constant IMPORT_AMOUNT_CID = uint256(keccak256("import_amount"));
uint256 constant EXPORT_AMOUNT_CID = uint256(keccak256("export_amount"));
uint256 constant EXPORT_LICENSE_CID = uint256(keccak256("export_license"));
uint256 constant UNLOCKED_CID = uint256(keccak256("unlocked"));
uint256 constant FACTION_CID = uint256(keccak256("faction"));

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC1155/IERC1155.sol)

pragma solidity ^0.8.0;

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

/**
 * @dev Required interface of an ERC1155 compliant contract, as defined in the
 * https://eips.ethereum.org/EIPS/eip-1155[EIP].
 *
 * _Available since v3.1._
 */
interface IERC1155 is IERC165 {
    /**
     * @dev Emitted when `value` tokens of token type `id` are transferred from `from` to `to` by `operator`.
     */
    event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);

    /**
     * @dev Equivalent to multiple {TransferSingle} events, where `operator`, `from` and `to` are the same for all
     * transfers.
     */
    event TransferBatch(
        address indexed operator,
        address indexed from,
        address indexed to,
        uint256[] ids,
        uint256[] values
    );

    /**
     * @dev Emitted when `account` grants or revokes permission to `operator` to transfer their tokens, according to
     * `approved`.
     */
    event ApprovalForAll(address indexed account, address indexed operator, bool approved);

    /**
     * @dev Emitted when the URI for token type `id` changes to `value`, if it is a non-programmatic URI.
     *
     * If an {URI} event was emitted for `id`, the standard
     * https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[guarantees] that `value` will equal the value
     * returned by {IERC1155MetadataURI-uri}.
     */
    event URI(string value, uint256 indexed id);

    /**
     * @dev Returns the amount of tokens of token type `id` owned by `account`.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function balanceOf(address account, uint256 id) external view returns (uint256);

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {balanceOf}.
     *
     * Requirements:
     *
     * - `accounts` and `ids` must have the same length.
     */
    function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids)
        external
        view
        returns (uint256[] memory);

    /**
     * @dev Grants or revokes permission to `operator` to transfer the caller's tokens, according to `approved`,
     *
     * Emits an {ApprovalForAll} event.
     *
     * Requirements:
     *
     * - `operator` cannot be the caller.
     */
    function setApprovalForAll(address operator, bool approved) external;

    /**
     * @dev Returns true if `operator` is approved to transfer ``account``'s tokens.
     *
     * See {setApprovalForAll}.
     */
    function isApprovedForAll(address account, address operator) external view returns (bool);

    /**
     * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
     *
     * Emits a {TransferSingle} event.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - If the caller is not `from`, it must have been approved to spend ``from``'s tokens via {setApprovalForAll}.
     * - `from` must have a balance of tokens of type `id` of at least `amount`.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
     * acceptance magic value.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes calldata data
    ) external;

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
     *
     * Emits a {TransferBatch} event.
     *
     * Requirements:
     *
     * - `ids` and `amounts` must have the same length.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
     * acceptance magic value.
     */
    function safeBatchTransferFrom(
        address from,
        address to,
        uint256[] calldata ids,
        uint256[] calldata amounts,
        bytes calldata data
    ) external;
}

// SPDX-License-Identifier: MIT LICENSE

pragma solidity ^0.8.13;

import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";

/**
 * @title Interface for game NFTs that have stats and other properties
 */
interface IGameNFT is IERC721 {
    /**
     * @param account Account to check hold time of
     * @param tokenId Id of the token
     * @return The time in seconds a given account has held a token
     */
    function getTimeHeld(
        address account,
        uint256 tokenId
    ) external view returns (uint32);

    function getLastTransfer(
        uint256 tokenId
    ) external view returns (uint32);

    /**
     * Mints a batch of ERC721 token
     *
     * @param to        Recipient of the token
     * @param amount    Quantity of token to mint
     */
    function mintBatch(address to, uint256 amount) external returns (uint256[] memory);

    function exists(uint256 tokenId) external view returns (bool);

    function isTradingLocked() external view returns (bool);
}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./DataTypes.sol";
import {IDataStore, ID} from "./IDataStore.sol";
import {GameRegistryConsumer} from "../core/GameRegistryConsumer.sol";
import {GAME_LOGIC_CONTRACT_ROLE} from "../constants/RoleConstants.sol";

contract DataStore is IDataStore, GameRegistryConsumer {
    using DataTypes for *;

    mapping(uint256 => mapping(uint256 => bytes32)) public datastore;
    mapping(uint256 => mapping(uint256 => bytes32[])) public arrayStore;
    mapping(uint256 => mapping(uint256 => string)) private stringStore;
    mapping(uint256 => bytes32) public columnTypes;
    

    constructor(address gameRegistryAddress) GameRegistryConsumer(gameRegistryAddress, ID) {}

    function generateKey(uint256 docId, uint256 colId) public pure returns (uint256) {
        return uint256(keccak256(abi.encodePacked(docId, colId)));
    }

    function generateArrayKey (uint256 docId, uint256 colId) public pure returns (uint256) {
        return uint256(keccak256(abi.encodePacked(docId, colId, "__array")));
    }

    function setValue(uint256 tableId, uint256 docId, uint256 colId, bytes32 value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
        datastore[tableId][uint256(keccak256(abi.encodePacked(docId, colId)))] = value;
        emit ValueSet(tableId, docId, colId, value);
    }

    function setArrayValue(uint256 tableId, uint256 docId, uint256 colId, bytes32[] memory value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
        arrayStore[tableId][generateArrayKey(docId, colId)] = value;
        emit ArrayValueSet(tableId, docId, colId, value);
    }

    function setUint256ArrayValue(uint256 tableId, uint256 docId, uint256 colId, uint256[] memory value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
       require (getColumnType(colId) == IDataStore.ColumnType.UINT256, "Column is not UINT256ARRAY");
        bytes32[] memory packedValues = new bytes32[](value.length);
        for (uint256 i = 0; i < value.length; i++) {
            packedValues[i] = value[i].packUint256();
        }
        setArrayValue(tableId, docId, colId, packedValues);
    }

    function setBoolArrayValue(uint256 tableId, uint256 docId, uint256 colId, bool[] memory value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
        require (getColumnType(colId) == IDataStore.ColumnType.BOOL, "Column is not BOOLARRAY");
        bytes32[] memory packedValues = new bytes32[](value.length);
        for (uint256 i = 0; i < value.length; i++) {
            packedValues[i] = value[i].packBool();
        }
        setArrayValue(tableId, docId, colId, packedValues);
    }

    function setAddressArrayValue(uint256 tableId, uint256 docId, uint256 colId, address[] memory value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
        require (getColumnType(colId) == IDataStore.ColumnType.ADDRESS, "Column is not ADDRESSARRAY");
        bytes32[] memory packedValues = new bytes32[](value.length);
        for (uint256 i = 0; i < value.length; i++) {
            packedValues[i] = value[i].packAddress();
        }
        setArrayValue(tableId, docId, colId, packedValues);
    }

    function getUint256Array(uint256 tableId, uint256 docId, uint256 colId) public view returns (uint256[] memory) {
        require (getColumnType(colId) == IDataStore.ColumnType.UINT256, "Column is not UINT256");
        bytes32[] memory byteArray = arrayStore[tableId][generateArrayKey(docId, colId)];
        uint256[] memory uintArray = new uint256[](byteArray.length);
        for (uint256 i = 0; i < byteArray.length; i++) {
            uintArray[i] = byteArray[i].unpackUint256();
        }
        return uintArray;
    }

    function getBoolArray(uint256 tableId, uint256 docId, uint256 colId) public view returns (bool[] memory) {
        require (getColumnType(colId) == IDataStore.ColumnType.BOOL, "Column is not BOOL");
        bytes32[] memory byteArray = arrayStore[tableId][generateArrayKey(docId, colId)];
        bool[] memory boolArray = new bool[](byteArray.length);
        for (uint256 i = 0; i < byteArray.length; i++) {
            boolArray[i] = byteArray[i].unpackBool();
        }
        return boolArray;
    }

    function getAddressArray(uint256 tableId, uint256 docId, uint256 colId) public view returns (address[] memory) {
        require (getColumnType(colId) == IDataStore.ColumnType.ADDRESS, "Column is not ADDRESS");
        bytes32[] memory byteArray = arrayStore[tableId][generateArrayKey(docId, colId)];
        address[] memory addressArray = new address[](byteArray.length);
        for (uint256 i = 0; i < byteArray.length; i++) {
            addressArray[i] = byteArray[i].unpackAddress();
        }
        return addressArray;
    }

    function getValue(uint256 tableId, uint256 docId, uint256 colId) public view returns (bytes32) {
        return datastore[tableId][uint256(keccak256(abi.encodePacked(docId, colId)))];
    }

    function setColumnType(uint256 colId, IDataStore.ColumnType columnType) public onlyRole(GAME_LOGIC_CONTRACT_ROLE) {
        require(!isColumnTypeSet(colId), "Column type already set");
        columnTypes[colId] = bytes32(uint256(columnType));
        emit ColumnTypeSet(colId, columnType);
    }

    function isColumnTypeSet(uint256 colId) public view returns (bool) {
        return columnTypes[colId] != bytes32(0);
    }

    function getColumnType(uint256 colId) public view returns (IDataStore.ColumnType) {
        bytes32 typeValue = columnTypes[colId];
        require(typeValue != bytes32(0), "Column type not set");
        return IDataStore.ColumnType(uint8(uint256(typeValue)));
    }

    // Type-specific setters
    function setUint256(uint256 tableId, uint256 docId, uint256 colId, uint256 value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
        require(getColumnType(colId) == IDataStore.ColumnType.UINT256, "Column is not UINT256");
        setValue(tableId, docId, colId, value.packUint256());
    }

    function setInt256(uint256 tableId, uint256 docId, uint256 colId, int256 value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
        require(getColumnType(colId) == IDataStore.ColumnType.INT256, "Column is not INT256");
        setValue(tableId, docId, colId, value.packInt256());
    }

    function setBool(uint256 tableId, uint256 docId, uint256 colId, bool value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
        require(getColumnType(colId) == IDataStore.ColumnType.BOOL, "Column is not BOOL");
        setValue(tableId, docId, colId, value.packBool());
    }

    function setAddress(uint256 tableId, uint256 docId, uint256 colId, address value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
        require(getColumnType(colId) == IDataStore.ColumnType.ADDRESS, "Column is not ADDRESS");
        setValue(tableId, docId, colId, value.packAddress());
    }

    function setBytes32(uint256 tableId, uint256 docId, uint256 colId, bytes32 value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
        require(getColumnType(colId) == IDataStore.ColumnType.BYTES32, "Column is not BYTES32");
        setValue(tableId, docId, colId, value);
    }

    function setString(uint256 tableId, uint256 docId, uint256 colId, string memory value) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
        require(getColumnType(colId) == IDataStore.ColumnType.STRING, "Column is not STRING");
        uint256 key = generateKey(docId, colId);
        stringStore[tableId][key] = value;
        emit StringValueSet(tableId, docId, colId, value);
    }

    function deleteValue(uint256 tableId, uint256 docId, uint256 colId) public onlyRole(GAME_LOGIC_CONTRACT_ROLE){
        uint256 key = generateKey(docId, colId);
        delete datastore[tableId][key];
    }

    // Type-specific getters
    function getUint256(uint256 tableId, uint256 docId, uint256 colId) public view returns (uint256) {
        require(getColumnType(colId) == IDataStore.ColumnType.UINT256, "Column is not UINT256");
        return getValue(tableId, docId, colId).unpackUint256();
    }

    function getInt256(uint256 tableId, uint256 docId, uint256 colId) public view returns (int256) {
        require(getColumnType(colId) == IDataStore.ColumnType.INT256, "Column is not INT256");
        return getValue(tableId, docId, colId).unpackInt256();
    }

    function getBool(uint256 tableId, uint256 docId, uint256 colId) public view returns (bool) {
        require(getColumnType(colId) == IDataStore.ColumnType.BOOL, "Column is not BOOL");
        return getValue(tableId, docId, colId).unpackBool();
    }

    function getAddress(uint256 tableId, uint256 docId, uint256 colId) public view returns (address) {
        require(getColumnType(colId) == IDataStore.ColumnType.ADDRESS, "Column is not ADDRESS");
        return getValue(tableId, docId, colId).unpackAddress();
    }

    function getBytes32(uint256 tableId, uint256 docId, uint256 colId) public view returns (bytes32) {
        require(getColumnType(colId) == IDataStore.ColumnType.BYTES32, "Column is not BYTES32");
        return getValue(tableId, docId, colId);
    }

    function getString(uint256 tableId, uint256 docId, uint256 colId) public view returns (string memory) {
        require(getColumnType(colId) == IDataStore.ColumnType.STRING, "Column is not STRING");
        uint256 key = generateKey(docId, colId);
        return stringStore[tableId][key];
    }

    function hasValue(uint256 tableId, uint256 docId, uint256 colId) public view returns (bool) {
        uint256 key = generateKey(docId, colId);
        return datastore[tableId][key] != bytes32(0);
    }

    function hasStringValue(uint256 tableId, uint256 docId, uint256 colId) public view returns (bool) {
        uint256 key = generateKey(docId, colId);
        return keccak256(bytes(stringStore[tableId][key])) != keccak256(bytes(""));
    }
}

// SPDX-License-Identifier: MIT LICENSE

pragma solidity ^0.8.13;

import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import {PAUSER_ROLE, MANAGER_ROLE} from "../constants/RoleConstants.sol";

import {ISystem} from "./ISystem.sol";
import {IGameRegistry, IERC165} from "./IGameRegistry.sol";
import {IDataStore, ID as DATA_STORE_ID} from "../db/IDataStore.sol";
import {DEPLOYER_ROLE} from "../constants/RoleConstants.sol";
/** @title Contract that lets a child contract access the GameRegistry contract */
abstract contract GameRegistryConsumer is
    ReentrancyGuard,
    ISystem
{
    /// @notice Whether or not the contract is paused
    bool private _paused;

    /// @notice Reference to the game registry that this contract belongs to
    IGameRegistry internal _gameRegistry;

    /// @notice Id for the system/component
    uint256 private _id;

    /** EVENTS **/

    /// @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);

    /** ERRORS **/

    /// @notice Not authorized to perform action
    error MissingRole(address account, bytes32 expectedRole);

    /** MODIFIERS **/

    /// @notice Modifier to verify a user has the appropriate role to call a given function
    modifier onlyRole(bytes32 role) {
        _checkRole(role, msg.sender);
        _;
    }

    /**
     * @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();
        _;
    }

    /** ERRORS **/

    /// @notice Error if the game registry specified is invalid
    error InvalidGameRegistry();

    /** SETUP **/

    /** @return ID for this system */
    function getId() public view override returns (uint256) {
        return _id;
    }

    /**
     * Pause/Unpause the contract
     *
     * @param shouldPause Whether or pause or unpause
     */
    function setPaused(bool shouldPause) external onlyRole(PAUSER_ROLE) {
        if (shouldPause) {
            _pause();
        } else {
            _unpause();
        }
    }

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

    /**
     * Sets the GameRegistry contract address for this contract
     *
     * @param gameRegistryAddress  Address for the GameRegistry contract
     */
    function setGameRegistry(
        address gameRegistryAddress
    ) external onlyRole(MANAGER_ROLE) {
        _gameRegistry = IGameRegistry(gameRegistryAddress);

        if (gameRegistryAddress == address(0)) {
            revert InvalidGameRegistry();
        }
    }

    /** @return GameRegistry contract for this contract */
    function getGameRegistry() external view returns (IGameRegistry) {
        return _gameRegistry;
    }

    /** INTERNAL **/

    /**
     * @dev Returns `true` if `account` has been granted `role`.
     */
    function _hasAccessRole(
        bytes32 role,
        address account
    ) internal view returns (bool) {
        return _gameRegistry.hasAccessRole(role, account);
    }

    /**
     * @dev Revert with a standard message if `account` is missing `role`.
     *
     * The format of the revert reason is given by the following regular expression:
     *
     *  /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
     */
    function _checkRole(bytes32 role, address account) internal view virtual {
        if (!_gameRegistry.hasAccessRole(role, account)) {
            revert MissingRole(account, role);
        }
    }

    /** @return Returns the dataStore for this contract */
    function _dataStore() internal view returns (IDataStore) {
        return IDataStore(_getSystem(DATA_STORE_ID));
    }

    /** @return Address for a given system */
    function _getSystem(uint256 systemId) internal view returns (address) {
        return _gameRegistry.getSystem(systemId);
    }


    /** PAUSABLE **/

    /**
     * @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 {
        require(_paused == false, "Pausable: not paused");
        _paused = true;
        emit Paused(msg.sender);
    }

    /**
     * @dev Returns to normal state.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    function _unpause() internal virtual {
        require(_paused == true, "Pausable: not paused");
        _paused = false;
        emit Unpaused(msg.sender);
    }

    function initialize() external virtual onlyRole(DEPLOYER_ROLE) { }

    /**
     * Constructor for this contract
     *
     * @param gameRegistryAddress Address of the GameRegistry contract
     * @param id                  Id of the system/component
     */
    constructor(
        address gameRegistryAddress,
        uint256 id
    ) {
        _gameRegistry = IGameRegistry(gameRegistryAddress);

        if (gameRegistryAddress == address(0)) {
            revert InvalidGameRegistry();
        }

        _paused = true;
        _id = id;
    }

}

// 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);
}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

library DataTypes {
    // Pack and unpack uint256
    function packUint256(uint256 value) internal pure returns (bytes32) {
        return bytes32(value);
    }

    function unpackUint256(bytes32 packed) internal pure returns (uint256) {
        return uint256(packed);
    }

    // Pack and unpack int256
    function packInt256(int256 value) internal pure returns (bytes32) {
        return bytes32(uint256(value));
    }

    function unpackInt256(bytes32 packed) internal pure returns (int256) {
        return int256(uint256(packed));
    }

    // Pack and unpack address
    function packAddress(address value) internal pure returns (bytes32) {
        return bytes32(uint256(uint160(value)));
    }

    function unpackAddress(bytes32 packed) internal pure returns (address) {
        return address(uint160(uint256(packed)));
    }

    // Pack and unpack bool
    function packBool(bool value) internal pure returns (bytes32) {
        return bytes32(uint256(value ? 1 : 0));
    }

    function unpackBool(bytes32 packed) internal pure returns (bool) {
        return uint256(packed) == 1;
    }
}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

uint256 constant ID = uint256(keccak256("game.gigaverse.datastore"));

interface IDataStore  {
    enum ColumnType { NONE, UINT256, INT256, BOOL, ADDRESS, BYTES32, STRING, UINT256_ARRAY }

    event ValueSet(uint256 indexed tableId, uint256 indexed docId, uint256 indexed colId, bytes32 value);
    event StringValueSet(uint256 indexed tableId, uint256 indexed docId, uint256 indexed colId, string value);
    event ArrayValueSet(uint256 indexed tableId, uint256 indexed docId, uint256 indexed colId, bytes32[] value);
    event ColumnTypeSet(uint256 indexed colId, ColumnType columnType);


    function setValue(uint256 tableId, uint256 docId, uint256 colId, bytes32 value) external;

    function getValue(uint256 tableId, uint256 docId, uint256 colId) external view returns (bytes32);

    function setColumnType(uint256 colId, ColumnType columnType) external;

    function getColumnType(uint256 colId) external view returns (ColumnType);

    // Type-specific setters
    function setUint256(uint256 tableId, uint256 docId, uint256 colId, uint256 value) external;
    function setInt256(uint256 tableId, uint256 docId, uint256 colId, int256 value) external;
    function setBool(uint256 tableId, uint256 docId, uint256 colId, bool value) external;
    function setAddress(uint256 tableId, uint256 docId, uint256 colId, address value) external;
    function setBytes32(uint256 tableId, uint256 docId, uint256 colId, bytes32 value) external;
    function setString(uint256 tableId, uint256 docId, uint256 colId, string memory value) external;


    // Type-specific getters
    function getUint256(uint256 tableId, uint256 docId, uint256 colId) external view returns (uint256);
    function getInt256(uint256 tableId, uint256 docId, uint256 colId) external view returns (int256);
    function getBool(uint256 tableId, uint256 docId, uint256 colId) external view returns (bool);
    function getAddress(uint256 tableId, uint256 docId, uint256 colId) external view returns (address);
    function getBytes32(uint256 tableId, uint256 docId, uint256 colId) external view returns (bytes32);
    function getString(uint256 tableId, uint256 docId, uint256 colId) external view returns (string memory);
    function deleteValue(uint256 tableId, uint256 docId, uint256 colId) external;
    function hasValue(uint256 tableId, uint256 docId, uint256 colId) external view returns (bool);
}

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

pragma solidity ^0.8.0;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    constructor() {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        _nonReentrantBefore();
        _;
        _nonReentrantAfter();
    }

    function _nonReentrantBefore() private {
        // On the first call to nonReentrant, _status will be _NOT_ENTERED
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;
    }

    function _nonReentrantAfter() private {
        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }
}

// SPDX-License-Identifier: MIT LICENSE

pragma solidity ^0.8.9;

import "@openzeppelin/contracts/utils/introspection/IERC165.sol";

/**
 * Defines a system the game engine
 */
interface ISystem {
    /** @return The ID for the system. Ex: a uint256 casted keccak256 hash */
    function getId() external view returns (uint256);

    function initialize() external;
}

// SPDX-License-Identifier: MIT LICENSE

pragma solidity ^0.8.9;

import "@openzeppelin/contracts/utils/introspection/IERC165.sol";

// @title Interface the game's ACL / Management Layer
interface IGameRegistry is IERC165 {
    /**
     * @dev Returns `true` if `account` has been granted `role`.
     * @param role The role to query
     * @param account The address to query
     */
    function hasAccessRole(
        bytes32 role,
        address account
    ) external view returns (bool);

    /**
     * @return Whether or not the registry is paused
     */
    function paused() external view returns (bool);

    /**
     * Registers a system by id
     *
     * @param systemId          Id of the system
     * @param systemAddress     Address of the system contract
     */
    function registerSystem(uint256 systemId, address systemAddress, bool isGameLogicContract) external;

    /**
     * @param systemId Id of the system
     * @return System based on an id
     */
    function getSystem(uint256 systemId) external view returns (address);
}

Please enter a contract address above to load the contract details and source code.

Context size (optional):