| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.20; |
| 3 | |
| 4 | import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; |
| 5 | import "@openzeppelin/contracts/access/Ownable.sol"; |
| 6 | import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; |
| 7 | import "@openzeppelin/contracts/utils/Strings.sol"; |
| 8 | |
| 9 | contract CosmicCowboys is ERC721, Ownable, ERC721URIStorage { |
| 10 | uint256 private _nextTokenId; |
| 11 | uint256 public latestTokenId; |
| 12 | enum Location { |
| 13 | Home, |
| 14 | Bar, |
| 15 | SupplyDepot |
| 16 | } |
| 17 | mapping(uint256 => Location) public tokenLocation; |
| 18 | mapping(uint256 => uint8) public tokenHealth; // Mapping to store health of each token |
| 19 | |
| 20 | // Events |
| 21 | event VisitArea(uint256 indexed tokenId, string location); |
| 22 | event GetCurrentLocation(uint256 indexed tokenId, string location); |
| 23 | event SetHealth(uint256 indexed tokenId, uint8 health); // Event emitted when health is set |
| 24 | |
| 25 | constructor( |
| 26 | address initialOwner |
| 27 | ) ERC721("Cosmic Cowboys", "CCNPC") Ownable(initialOwner) {} |
| 28 | |
| 29 | //function to set health |
| 30 | function setHealth(uint256 tokenId, uint8 health) public onlyOwner { |
| 31 | require(health >= 0 && health <= 10, "Health out of range"); // Check health is within valid range |
| 32 | tokenHealth[tokenId] = health; |
| 33 | emit SetHealth(tokenId, health); // Emit event |
| 34 | } |
| 35 | |
| 36 | function safeMint(address to, string memory uri) public onlyOwner { |
| 37 | uint256 tokenId = _nextTokenId++; |
| 38 | latestTokenId = tokenId; |
| 39 | _safeMint(to, tokenId); |
| 40 | _setTokenURI(tokenId, uri); |
| 41 | setHealth(tokenId, 6); |
| 42 | tokenLocation[tokenId] = Location.Bar; |
| 43 | } |
| 44 | |
| 45 | // Function to get health of a token |
| 46 | function getHealth(uint256 tokenId) external view returns (uint8) { |
| 47 | return tokenHealth[tokenId]; |
| 48 | } |
| 49 | |
| 50 | // Function to visit a location |
| 51 | |
| 52 | function goToHome(uint256 tokenId) external { |
| 53 | tokenLocation[tokenId] = Location.Home; |
| 54 | emit VisitArea(tokenId, locationToString(Location.Home)); |
| 55 | } |
| 56 | |
| 57 | function goToBar(uint256 tokenId) external { |
| 58 | tokenLocation[tokenId] = Location.Bar; |
| 59 | emit VisitArea(tokenId, locationToString(Location.Bar)); |
| 60 | } |
| 61 | |
| 62 | function goToSupplyDepot(uint256 tokenId) external { |
| 63 | tokenLocation[tokenId] = Location.SupplyDepot; |
| 64 | emit VisitArea(tokenId, locationToString(Location.SupplyDepot)); |
| 65 | } |
| 66 | |
| 67 | // Function to get the current location of a token |
| 68 | function getCurrentLocation( |
| 69 | uint256 tokenId |
| 70 | ) external view returns (string memory) { |
| 71 | Location _location = tokenLocation[tokenId]; |
| 72 | return locationToString(_location); |
| 73 | } |
| 74 | |
| 75 | // Utility function to convert enum to string |
| 76 | function locationToString( |
| 77 | Location _location |
| 78 | ) internal pure returns (string memory) { |
| 79 | if (_location == Location.SupplyDepot) return "Supply Depot"; |
| 80 | if (_location == Location.Bar) return "Bar"; |
| 81 | return "Home"; |
| 82 | } |
| 83 | |
| 84 | // The following functions are overrides required by Solidity. |
| 85 | |
| 86 | function tokenURI( |
| 87 | uint256 tokenId |
| 88 | ) public view override(ERC721, ERC721URIStorage) returns (string memory) { |
| 89 | return super.tokenURI(tokenId); |
| 90 | } |
| 91 | |
| 92 | function supportsInterface( |
| 93 | bytes4 interfaceId |
| 94 | ) public view override(ERC721, ERC721URIStorage) returns (bool) { |
| 95 | return super.supportsInterface(interfaceId); |
| 96 | } |
| 97 | } |