forked from UniqueNetwork/unique-workshops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0-generate-nfts.js
68 lines (57 loc) · 1.95 KB
/
0-generate-nfts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// @ts-ignore
const {convertArrayToCSV} = require('convert-array-to-csv');
const fs = require('fs');
const path = require('path');
const config = require('./config');
const formatAttributes = require('./utils/format-attributes');
const generateImages = require('./utils/generate-images');
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
function selectAttribute(attribute) {
const weightedValues = [];
if (attribute.chance < 100) {
weightedValues.push(...new Array(100 - parseInt(attribute.chance)).fill(undefined));
}
for (const value of attribute.values) {
weightedValues.push(...new Array(parseInt(value.weight)).fill(value.value));
}
const random = getRandomInt(weightedValues.length);
return weightedValues[random];
}
function getNFTAttributes(attributes) {
const nft = [];
for (const attribute of attributes) {
nft.push(selectAttribute(attribute));
}
return nft;
}
function generateNftsData(attributes) {
const NFT_DATA_PATH = path.resolve(config.dataDir, 'nfts.csv');
const nfts = new Array(config.desiredCount)
.fill(undefined).map((_v, i) => [i+1, ...getNFTAttributes(attributes)]);
const nftsToCSV = convertArrayToCSV(nfts, {
separator: ',',
header: ['id', attributes.map((attr) => attr.name)],
});
fs.writeFileSync(NFT_DATA_PATH, nftsToCSV);
console.log(`💾 Nft data saved: ${NFT_DATA_PATH}`);
}
async function generateNFTs() {
if (fs.existsSync(config.dataDir)) {
fs.rmSync(config.dataDir, {recursive: true, force: true});
fs.mkdirSync(config.dataDir);
} else {
fs.mkdirSync(config.dataDir);
}
const attributes = await formatAttributes();
generateNftsData(attributes);
await generateImages();
if (fs.existsSync(path.resolve(config.generationDir, config.coverFileName))) {
fs.copyFileSync(
path.resolve(config.generationDir, config.coverFileName),
path.resolve(config.dataDir, config.coverFileName)
);
}
}
generateNFTs().catch(console.error);