# N合约分析

Loot is randomized adventurer gear generated and stored on chain. Stats, images, and other functionality are intentionally omitted for others to interpret.Feel free to use Loot in any way you want.

Loot是随机生成的冒险者装备，并存储在区块链上。统计数字、图像和其他功能被有意省略，供他人解释。

1.random

``````function random(string memory input) internal pure returns (uint256) {
return uint256(keccak256(abi.encodePacked(input)));
}
``````

2.pluck

``````		function getFirst(uint256 tokenId) public view returns (uint256) {
return pluck(tokenId, "FIRST", units);
}
function getSecond(uint256 tokenId) public view returns (uint256) {
return pluck(tokenId, "SECOND", units);
}
.......
function pluck(
uint256 tokenId,
string memory keyPrefix,
uint8[] memory sourceArray
) internal view returns (uint256) {
//传进tokenId和例如"FIRST"这样的字符，然后返回一个随机数
uint256 rand = random(string(abi.encodePacked(keyPrefix, toString(tokenId))));
//对rand % sourceArray.length取余，获取sourceArray里面的一个值
uint256 output = sourceArray[rand % sourceArray.length];
//对随机数进行取余
uint256 luck = rand % 21;
if (luck > 14) {
//output+1或output+2
output += suffixes[rand % suffixes.length];
}
if (luck >= 19) {
if (luck == 19) {
//（output*1或output*0）再+1或+2
output = (output * multipliers[rand % multipliers.length]) + suffixes[rand % suffixes.length];
} else {
//output*1或output*0
output = (output * multipliers[rand % multipliers.length]);
}
}
return output;
}
``````

3.tokenURI

``````function tokenURI(uint256 tokenId) public view override returns (string memory) {
string[17] memory parts;
//svg图片格式的前缀
parts[
0
] = '<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 350 350"><style>.base { fill: white; font-family: serif; font-size: 14px; }</style><rect width="100%" height="100%" fill="black" /><text x="10" y="20" class="base">';
//数字1
parts[1] = toString(getFirst(tokenId));
parts[2] = '</text><text x="10" y="40" class="base">';
//数字2
parts[3] = toString(getSecond(tokenId));

parts[4] = '</text><text x="10" y="60" class="base">';
//数字3
parts[5] = toString(getThird(tokenId));

parts[6] = '</text><text x="10" y="80" class="base">';
//数字4
parts[7] = toString(getFourth(tokenId));

parts[8] = '</text><text x="10" y="100" class="base">';
//数字5
parts[9] = toString(getFifth(tokenId));

parts[10] = '</text><text x="10" y="120" class="base">';
//数字6
parts[11] = toString(getSixth(tokenId));

parts[12] = '</text><text x="10" y="140" class="base">';
//数字7
parts[13] = toString(getSeventh(tokenId));

parts[14] = '</text><text x="10" y="160" class="base">';
//数字8
parts[15] = toString(getEight(tokenId));

parts[16] = "</text></svg>";
//接下来就是对上面的各部分进行拼接，9个一组
string memory output = string(
abi.encodePacked(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7], parts[8])
);
output = string(
abi.encodePacked(
output,
parts[9],
parts[10],
parts[11],
parts[12],
parts[13],
parts[14],
parts[15],
parts[16]
)
);
//拼接完毕，使用Base64编码库函数进行整体编码，方便传输
string memory json = Base64.encode(
bytes(
string(
abi.encodePacked(
'{"name": "N #',
toString(tokenId),
'", "description": "N is just numbers.", "image": "data:image/svg+xml;base64,',
//这里对于图片的内容单独进行了一次Base64编码
Base64.encode(bytes(output)),
'"}'
)
)
)
);
output = string(abi.encodePacked("data:application/json;base64,", json));
return output;
}

``````

``````data:application/json;base64,eyJuYW1lIjogIk4gIzEiLCAiZGVzY3JpcHRpb24iOiAiTiBpcyBqdXN0IG51bWJlcnMuIiwgImltYWdlIjogImRhdGE6aW1hZ2Uvc3ZnK3htbDtiYXNlNjQsUEhOMlp5QjRiV3h1Y3owaWFIUjBjRG92TDNkM2R5NTNNeTV2Y21jdk1qQXdNQzl6ZG1jaUlIQnlaWE5sY25abFFYTndaV04wVW1GMGFXODlJbmhOYVc1WlRXbHVJRzFsWlhRaUlIWnBaWGRDYjNnOUlqQWdNQ0F6TlRBZ016VXdJajQ4YzNSNWJHVStMbUpoYzJVZ2V5Qm1hV3hzT2lCM2FHbDBaVHNnWm05dWRDMW1ZVzFwYkhrNklITmxjbWxtT3lCbWIyNTBMWE5wZW1VNklERTBjSGc3SUgwOEwzTjBlV3hsUGp4eVpXTjBJSGRwWkhSb1BTSXhNREFsSWlCb1pXbG5hSFE5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k9IjQwIiBjbGFzcz0iYmFzZSI+NDwvdGV4dD48dGV4dCB4PSIxMCIgeT0iNjAiIGNsYXNzPSJiYXNlIj43PC90ZXh0Pjx0ZXh0IHg9IjEwIiB5PSI4MCIgY2xhc3M9ImJhc2UiPjM8L3RleHQ+PHRleHQgeD0iMTAiIHk9IjEwMCIgY2xhc3M9ImJhc2UiPjk8L3RleHQ+PHRleHQgeD0iMTAiIHk9IjEyMCIgY2xhc3M9ImJhc2UiPjg8L3RleHQ+PHRleHQgeD0iMTAiIHk9IjE0MCIgY2xhc3M9ImJhc2UiPjY8L3RleHQ+PHRleHQgeD0iMTAiIHk9IjE2MCIgY2xhc3M9ImJhc2UiPjM8L3RleHQ+PC9zdmc+"}
``````

``````<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 350 350"><style>.base { fill: white; font-family: serif; font-size: 14px; }</style><rect width="100%" height="100%" fill="black" /><text x="10" y="20" class="base">5</text><text x="10" y="40" class="base">4</text><text x="10" y="60" class="base">7</text><text x="10" y="80" class="base">3</text><text x="10" y="100" class="base">9</text><text x="10" y="120" class="base">8</text><text x="10" y="140" class="base">6</text><text x="10" y="160" class="base">3</text></svg>
``````

4.claim

``````  function claim(uint256 tokenId) public nonReentrant {
require(tokenId > 0 && tokenId < 8889, "Token ID invalid");
_safeMint(_msgSender(), tokenId);
}
``````

THE END

)">