Skip to content

Commit 0ed8128

Browse files
committed
added rarity estimation
1 parent 84e30e4 commit 0ed8128

2 files changed

Lines changed: 75 additions & 62 deletions

File tree

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,28 @@ The program will output all the images in the `build` directory along with the m
190190
}
191191
```
192192

193-
That's it, you're done. Hope you create some awesome artworks with this code 👄.
193+
That's it, you're done.
194194

195-
### Printing rarity data
196-
To see the percentages of each attribute across your collection, run `node rarityData.js`
195+
### Printing rarity data (Experimental feature)
196+
197+
To see the percentages of each attribute across your collection, run:
198+
199+
```sh
200+
node rarityData.js
201+
```
202+
203+
The output will look something like this:
204+
205+
```sh
206+
Trait type: Bottom lid
207+
{ trait: 'High', chance: '20', occurrence: '40' }
208+
{ trait: 'Low', chance: '40', occurrence: '60' }
209+
{ trait: 'Middle', chance: '40', occurrence: '0' }
210+
211+
Trait type: Top lid
212+
{ trait: 'High', chance: '30', occurrence: '20' }
213+
{ trait: 'Low', chance: '20', occurrence: '40' }
214+
{ trait: 'Middle', chance: '50', occurrence: '40' }
215+
```
216+
217+
Hope you create some awesome artworks with this code 👄

rarityData.js

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,84 @@
1-
'use strict';
1+
"use strict";
22

3-
const fs = require('fs');
3+
const fs = require("fs");
44
const path = require("path");
55
const isLocal = typeof process.pkg === "undefined";
66
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
77
const layersDir = `${basePath}/layers`;
88

9-
const {
10-
layerConfigurations
11-
} = require("./src/config.js");
9+
const { layerConfigurations } = require("./src/config.js");
1210

13-
const {
14-
getElements
15-
} = require("./src/main.js");
11+
const { getElements } = require("./src/main.js");
1612

1713
// read json data
18-
let rawdata = fs.readFileSync('build/_metadata.json');
14+
let rawdata = fs.readFileSync("build/_metadata.json");
1915
let data = JSON.parse(rawdata);
2016
let editionSize = data.length;
2117

2218
let rarityData = [];
2319

2420
// intialize layers to chart
2521
layerConfigurations.forEach((config) => {
26-
let layers = config.layersOrder;
22+
let layers = config.layersOrder;
2723

28-
layers.forEach((layer) => {
24+
layers.forEach((layer) => {
25+
// get elements for each layer
26+
let elementsForLayer = [];
27+
let elements = getElements(`${layersDir}/${layer.name}/`);
28+
elements.forEach((element) => {
29+
// just get name and weight for each element
30+
let rarityDataElement = {
31+
trait: element.name,
32+
chance: element.weight.toFixed(0),
33+
occurrence: 0, // initialize at 0
34+
};
35+
elementsForLayer.push(rarityDataElement);
36+
});
2937

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-
});
38+
// don't include duplicate layers
39+
if (!rarityData.includes(layer.name)) {
40+
// add elements for each layer to chart
41+
rarityData[layer.name] = elementsForLayer;
42+
}
43+
});
5044
});
5145

5246
// fill up rarity chart with occurrences from metadata
5347
data.forEach((element) => {
54-
let attributes = element.attributes;
48+
let attributes = element.attributes;
5549

56-
attributes.forEach((attribute) => {
57-
let traitType = attribute.trait_type;
58-
let value = attribute.value;
50+
attributes.forEach((attribute) => {
51+
let traitType = attribute.trait_type;
52+
let value = attribute.value;
5953

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-
});
54+
let rarityDataTraits = rarityData[traitType];
55+
rarityDataTraits.forEach((rarityDataTrait) => {
56+
if (rarityDataTrait.trait == value) {
57+
// keep track of occurrences
58+
rarityDataTrait.occurrence++;
59+
}
60+
});
61+
});
6862
});
6963

7064
// convert occurrences to percentages
7165
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;
66+
for (var attribute in rarityData[layer]) {
67+
// convert to percentage
68+
rarityData[layer][attribute].occurrence =
69+
(rarityData[layer][attribute].occurrence / editionSize) * 100;
7770

78-
// show two decimal places in percent
79-
rarityData[layer][attribute].occurrence =
80-
rarityData[layer][attribute].occurrence.toFixed(2);
81-
}
71+
// show two decimal places in percent
72+
rarityData[layer][attribute].occurrence =
73+
rarityData[layer][attribute].occurrence.toFixed(0);
74+
}
8275
}
8376

8477
// print out rarity data
8578
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-
}
79+
console.log(`Trait type: ${layer}`);
80+
for (var trait in rarityData[layer]) {
81+
console.log(rarityData[layer][trait]);
82+
}
83+
console.log();
84+
}

0 commit comments

Comments
 (0)