Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.7 kB
1
Indexable
Never
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IGamingEcosystemNFT {
    function mintNFT(address to) external;
    function burnNFT(uint256 tokenId) external;
    function transferNFT(uint256 tokenId, address from, address to) external;
    function ownerOf(uint256 tokenId) external view returns (address);
}

contract BlockchainGamingEcosystem {
    address public owner;
    IGamingEcosystemNFT public nftContract;

    struct Player {
        string userName;
        uint256 balance;
        uint256 numberOfNFTs;
    }

    struct Game {
        string gameName;
        uint256 price;
    }

    struct NFT {
        address owner;
        uint256 gameID;
    }

    mapping(address => Player) public players;
    mapping(uint256 => Game) public games;
    mapping(uint256 => NFT) public nfts;

    uint256 public nextTokenID = 0;

    constructor(address _nftAddress) {
        owner = msg.sender;
        nftContract = IGamingEcosystemNFT(_nftAddress);
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Only the owner can call this function");
        _;
    }

    modifier notRegistered() {
        require(players[msg.sender].balance == 0, "You are already registered");
        _;
    }

    modifier gameExists(uint256 gameID) {
        require(bytes(games[gameID].gameName).length != 0, "Game does not exist");
        _;
    }

    modifier assetOwner(uint256 tokenID) {
        require(nfts[tokenID].owner == msg.sender, "You do not own this asset");
        _;
    }

    function registerPlayer(string memory userName) public notRegistered {
        require(bytes(userName).length >= 3, "Username must have a minimum length of 3 characters");
        require(bytes(players[msg.sender].userName).length == 0, "Username must be unique");

        players[msg.sender].userName = userName;
        players[msg.sender].balance = 1000;
    }

    function createGame(string memory gameName, uint256 gameID) public onlyOwner {
        require(bytes(games[gameID].gameName).length == 0, "Game already exists");
        games[gameID] = Game(gameName, 250);
    }

    function removeGame(uint256 gameID) public onlyOwner gameExists(gameID) {
        Game storage game = games[gameID];
        for (uint256 i = 0; i < nextTokenID; i++) {
            if (nfts[i].owner != address(0) && nfts[i].gameID == gameID) {
                players[nfts[i].owner].balance += game.price;
                nftContract.burnNFT(i);
                delete nfts[i];
            }
        }
        delete games[gameID];
    }

    function buyAsset(uint256 gameID) public {
        Game storage game = games[gameID];
        require(bytes(game.gameName).length != 0, "Game does not exist");

        uint256 price = game.price;
        require(players[msg.sender].balance >= price, "Insufficient balance");

        players[msg.sender].balance -= price;
        game.price = price * 11 / 10;
        nftContract.mintNFT(msg.sender);
        uint256 tokenID = nextTokenID;
        nfts[tokenID] = NFT(msg.sender, gameID);
        nextTokenID++;
        players[msg.sender].numberOfNFTs++;
    }

    function sellAsset(uint256 tokenID) public assetOwner(tokenID) {
        uint256 gameID = nfts[tokenID].gameID;
        Game storage game = games[gameID];
        require(bytes(game.gameName).length != 0, "Game does not exist");

        uint256 price = game.price;
        players[msg.sender].balance += price;
        nftContract.burnNFT(tokenID);
        delete nfts[tokenID];
        players[msg.sender].numberOfNFTs--;
    }

    function transferAsset(uint256 tokenID, address to) public assetOwner(tokenID) {
        require(players[to].balance != 0, "Recipient is not registered");
        uint256 gameID = nfts[tokenID].gameID;
        Game storage game = games[gameID];
        require(bytes(game.gameName).length != 0, "Game does not exist");

        nftContract.transferNFT(tokenID, msg.sender, to);
        players[msg.sender].numberOfNFTs--;
        players[to].numberOfNFTs++;
        nfts[tokenID].owner = to;
    }

    function viewProfile(address playerAddress) public view returns (string memory userName, uint256 balance, uint256 numberOfNFTs) {
        Player memory player = players[playerAddress];
        return (player.userName, player.balance, player.numberOfNFTs);
    }

    function viewAsset(uint256 tokenID) public view returns (address owner, string memory gameName, uint price) {
        require(nfts[tokenID].owner != address(0), "Asset does not exist");
        uint256 gameID = nfts[tokenID].gameID;
        Game memory game = games[gameID];
        return (nfts[tokenID].owner, game.gameName, game.price);
    }
}