Skip to content

Commit 8f89c3e

Browse files
authored
Merge pull request #15 from jjohnson5253/rarityData
Script to print out percentage of each attribute's occurrence
2 parents 8a0e7bb + cb75243 commit 8f89c3e

3 files changed

Lines changed: 96 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,6 @@ The program will output all the images in the `build` directory along with the m
137137
```
138138

139139
That's it, you're done. Hope you create some awesome artworks with this code 👄.
140+
141+
### Printing rarity data
142+
To see the percentages of each attribute across your collection, run `node rarityData.js`

rarityData.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require("path");
5+
const isLocal = typeof process.pkg === "undefined";
6+
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
7+
const layersDir = `${basePath}/layers`;
8+
9+
const {
10+
layerConfigurations
11+
} = require("./src/config.js");
12+
13+
const {
14+
getElements
15+
} = require("./src/main.js");
16+
17+
// read json data
18+
let rawdata = fs.readFileSync('build/_metadata.json');
19+
let data = JSON.parse(rawdata);
20+
let editionSize = data.length;
21+
22+
let rarityData = [];
23+
24+
// intialize layers to chart
25+
layerConfigurations.forEach((config) => {
26+
let layers = config.layersOrder;
27+
28+
layers.forEach((layer) => {
29+
30+
// get elements for each layer
31+
let elementsForLayer = [];
32+
let elements = getElements(`${layersDir}/${layer.name}/`);
33+
elements.forEach((element) => {
34+
// just get name and weight for each element
35+
let rarityDataElement = {
36+
trait: element.name,
37+
chance: element.weight.toFixed(2),
38+
occurrence: 0 // initialize at 0
39+
}
40+
elementsForLayer.push(rarityDataElement)
41+
});
42+
43+
// don't include duplicate layers
44+
if (!rarityData.includes(layer.name))
45+
{
46+
// add elements for each layer to chart
47+
rarityData[layer.name] = elementsForLayer;
48+
}
49+
});
50+
});
51+
52+
// fill up rarity chart with occurrences from metadata
53+
data.forEach((element) => {
54+
let attributes = element.attributes;
55+
56+
attributes.forEach((attribute) => {
57+
let traitType = attribute.trait_type;
58+
let value = attribute.value;
59+
60+
let rarityDataTraits = rarityData[traitType];
61+
rarityDataTraits.forEach((rarityDataTrait) => {
62+
if (rarityDataTrait.trait == value){
63+
// keep track of occurrences
64+
rarityDataTrait.occurrence++;
65+
}
66+
});
67+
});
68+
});
69+
70+
// convert occurrences to percentages
71+
for (var layer in rarityData) {
72+
for (var attribute in rarityData[layer])
73+
{
74+
// convert to percentage
75+
rarityData[layer][attribute].occurrence =
76+
(rarityData[layer][attribute].occurrence / editionSize) * 100;
77+
78+
// show two decimal places in percent
79+
rarityData[layer][attribute].occurrence =
80+
rarityData[layer][attribute].occurrence.toFixed(2);
81+
}
82+
}
83+
84+
// print out rarity data
85+
for (var layer in rarityData) {
86+
console.log(`Trait type: ${layer}`)
87+
for (var trait in rarityData[layer])
88+
{
89+
console.log(rarityData[layer][trait])
90+
}
91+
console.log()
92+
}

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,4 @@ const startCreating = async () => {
235235
writeMetaData(JSON.stringify(metadataList));
236236
};
237237

238-
module.exports = { startCreating, buildSetup };
238+
module.exports = { startCreating, buildSetup, getElements };

0 commit comments

Comments
 (0)