Untitled
unknown
plain_text
2 years ago
4.7 kB
7
Indexable
/ 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 contractOwner; IGamingEcosystemNFT public nftContract; struct Player { string userName; uint256 balance; uint256 numberOfNFTs; } struct Game { string gameName; uint256 price; } struct NFT { address nftOwner; uint256 gameID; } mapping(address => Player) public players; mapping(uint256 => Game) public games; mapping(uint256 => NFT) public nfts; uint256 public nextTokenID = 0; constructor(address _nftAddress) { contractOwner = msg.sender; nftContract = IGamingEcosystemNFT(_nftAddress); } modifier onlyContractOwner() { require(msg.sender == contractOwner, "Only the contract 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].nftOwner == 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 onlyContractOwner { require(bytes(games[gameID].gameName).length == 0, "Game already exists"); games[gameID] = Game(gameName, 250); } function removeGame(uint256 gameID) public onlyContractOwner gameExists(gameID) { Game storage game = games[gameID]; for (uint256 i = 0; i < nextTokenID; i++) { if (nfts[i].nftOwner != address(0) && nfts[i].gameID == gameID) { players[nfts[i].nftOwner].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].nftOwner = 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, string memory, uint256) { require(nfts[tokenID].nftOwner != address(0), "Asset does not exist"); uint256 gameID = nfts[tokenID].gameID; Game memory game = games[gameID]; return (nfts[tokenID].nftOwner, game.gameName, game.price); } }
Editor is loading...