|
| 1 | +// contracts/DungeonsAndDragonsCharacter.sol |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +pragma solidity >=0.8.0; |
| 4 | + |
| 5 | +import "./ERC721PresetMinterPauserAutoId.sol"; |
| 6 | +import "./extensions/Strings.sol"; |
| 7 | +import '@chainlink/contracts/src/v0.8/interfaces/LinkTokenInterface.sol'; |
| 8 | +import '@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol'; |
| 9 | +import '@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol'; |
| 10 | + |
| 11 | +contract DungeonsAndDragonsCharacter is ERC721PresetMinterPauserAutoId, VRFConsumerBaseV2 { |
| 12 | + using Strings for string; |
| 13 | + |
| 14 | + event FulfillRandomness(uint256,uint256[]); |
| 15 | + event RequestId(address,uint256); |
| 16 | + |
| 17 | + VRFCoordinatorV2Interface COORDINATOR; |
| 18 | + LinkTokenInterface LINKTOKEN; |
| 19 | + |
| 20 | + // Your subscription ID. |
| 21 | + uint64 s_subscriptionId; |
| 22 | + |
| 23 | + // Rinkeby coordinator. For other networks, |
| 24 | + // see https://docs.chain.link/docs/vrf-contracts/#configurations |
| 25 | + address vrfCoordinator = 0x6168499c0cFfCaCD319c818142124B7A15E857ab; |
| 26 | + |
| 27 | + // Rinkeby LINK token contract. For other networks, |
| 28 | + // see https://docs.chain.link/docs/vrf-contracts/#configurations |
| 29 | + address link = 0x01BE23585060835E02B77ef475b0Cc51aA1e0709; |
| 30 | + |
| 31 | + // The gas lane to use, which specifies the maximum gas price to bump to. |
| 32 | + // For a list of available gas lanes on each network, |
| 33 | + // see https://docs.chain.link/docs/vrf-contracts/#configurations |
| 34 | + bytes32 keyHash = 0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc; |
| 35 | + |
| 36 | + // Depends on the number of requested values that you want sent to the |
| 37 | + // fulfillRandomWords() function. Storing each word costs about 20,000 gas, |
| 38 | + // so 100,000 is a safe default for this example contract. Test and adjust |
| 39 | + // this limit based on the network that you select, the size of the request, |
| 40 | + // and the processing of the callback request in the fulfillRandomWords() |
| 41 | + // function. |
| 42 | + uint32 callbackGasLimit = 100000; |
| 43 | + |
| 44 | + // The default is 3, but you can set this higher. |
| 45 | + uint16 requestConfirmations = 3; |
| 46 | + |
| 47 | + // For this example, retrieve 2 random values in one request. |
| 48 | + // Cannot exceed VRFCoordinatorV2.MAX_NUM_WORDS. |
| 49 | + uint32 numWords = 2; |
| 50 | + |
| 51 | + bool public flag; |
| 52 | + uint256 public tag; |
| 53 | + |
| 54 | + struct Character { |
| 55 | + uint256 strength; |
| 56 | + uint256 dexterity; |
| 57 | + uint256 constitution; |
| 58 | + uint256 intelligence; |
| 59 | + uint256 wisdom; |
| 60 | + uint256 charisma; |
| 61 | + uint256 experience; |
| 62 | + string name; |
| 63 | + } |
| 64 | + |
| 65 | + Character[] public characters; |
| 66 | + |
| 67 | + mapping(uint256 => string) requestToCharacterName; |
| 68 | + mapping(uint256 => address) requestToSender; |
| 69 | + mapping(uint256 => uint256) requestToRandnum; |
| 70 | + |
| 71 | + constructor(uint64 subscriptionId,string memory tokenURI) |
| 72 | + public |
| 73 | + VRFConsumerBaseV2(vrfCoordinator) |
| 74 | + ERC721PresetMinterPauserAutoId("DungeonsAndDragonsCharacter", "D&D",tokenURI) |
| 75 | + { |
| 76 | + COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator); |
| 77 | + LINKTOKEN = LinkTokenInterface(link); |
| 78 | + s_subscriptionId = subscriptionId; |
| 79 | + } |
| 80 | + |
| 81 | + function requestNewRandomCharacter( |
| 82 | + string memory name |
| 83 | + ) public returns (uint256) { |
| 84 | + // Will revert if subscription is not set and funded. |
| 85 | + uint256 requestId = COORDINATOR.requestRandomWords(keyHash, s_subscriptionId, requestConfirmations, callbackGasLimit, numWords); |
| 86 | + emit RequestId(msg.sender,requestId); |
| 87 | + |
| 88 | + requestToCharacterName[requestId] = name; |
| 89 | + requestToSender[requestId] = msg.sender; |
| 90 | + return requestId; |
| 91 | + } |
| 92 | + |
| 93 | + function setTokenURI(uint256 tokenId, string memory _tokenURI) public { |
| 94 | + require( |
| 95 | + _isApprovedOrOwner(_msgSender(), tokenId), |
| 96 | + "ERC721: transfer caller is not owner nor approved" |
| 97 | + ); |
| 98 | + _setTokenURI(tokenId, _tokenURI); |
| 99 | + } |
| 100 | + |
| 101 | + function fulfillRandomWords( |
| 102 | + uint256 requestId, /* requestId */ |
| 103 | + uint256[] memory randomWords) |
| 104 | + internal override |
| 105 | + { |
| 106 | + requestToRandnum[requestId] = randomWords[0]; |
| 107 | + |
| 108 | + emit FulfillRandomness(requestId,randomWords); |
| 109 | + } |
| 110 | + |
| 111 | + function blindCharacter(uint256 requestId) public { |
| 112 | + uint256 randomNum = requestToRandnum[requestId]; |
| 113 | + uint256 newId = characters.length; |
| 114 | + uint256 strength = (randomNum % 100); |
| 115 | + uint256 dexterity = ((randomNum % 10000) / 100 ); |
| 116 | + uint256 constitution = ((randomNum % 1000000) / 10000 ); |
| 117 | + uint256 intelligence = ((randomNum % 100000000) / 1000000 ); |
| 118 | + uint256 wisdom = ((randomNum % 10000000000) / 100000000 ); |
| 119 | + uint256 charisma = ((randomNum % 1000000000000) / 10000000000); |
| 120 | + uint256 experience = 0; |
| 121 | + |
| 122 | + characters.push( |
| 123 | + Character( |
| 124 | + strength, |
| 125 | + dexterity, |
| 126 | + constitution, |
| 127 | + intelligence, |
| 128 | + wisdom, |
| 129 | + charisma, |
| 130 | + experience, |
| 131 | + requestToCharacterName[requestId] |
| 132 | + ) |
| 133 | + ); |
| 134 | + _safeMint(requestToSender[requestId], newId); |
| 135 | + } |
| 136 | + |
| 137 | + function getLevel(uint256 tokenId) public view returns (uint256) { |
| 138 | + return sqrt(characters[tokenId].experience); |
| 139 | + } |
| 140 | + |
| 141 | + function getNumberOfCharacters() public view returns (uint256) { |
| 142 | + return characters.length; |
| 143 | + } |
| 144 | + |
| 145 | + function getCharacterOverView(uint256 tokenId) |
| 146 | + public |
| 147 | + view |
| 148 | + returns ( |
| 149 | + string memory, |
| 150 | + uint256, |
| 151 | + uint256, |
| 152 | + uint256 |
| 153 | + ) |
| 154 | + { |
| 155 | + return ( |
| 156 | + characters[tokenId].name, |
| 157 | + characters[tokenId].strength + characters[tokenId].dexterity + characters[tokenId].constitution + characters[tokenId].intelligence + characters[tokenId].wisdom + characters[tokenId].charisma, |
| 158 | + getLevel(tokenId), |
| 159 | + characters[tokenId].experience |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + function sqrt(uint256 x) internal view returns (uint256 y) { |
| 164 | + uint256 z = (x + 1) / 2; |
| 165 | + y = x; |
| 166 | + while (z < y) { |
| 167 | + y = z; |
| 168 | + z = (x / z + z) / 2; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +// expand to more random |
| 173 | +// function expand(uint256 randomValue, uint256 n) public pure returns (uint256[] memory expandedValues) { |
| 174 | +// expandedValues = new uint256[](n); |
| 175 | +// for (uint256 i = 0; i < n; i++) { |
| 176 | +// expandedValues[i] = uint256(keccak256(abi.encode(randomValue, i))); |
| 177 | +// } |
| 178 | +// return expandedValues; |
| 179 | +// } |
| 180 | +} |
0 commit comments