Skip to content

Commit

Permalink
Write rarity with other json metadata - HashLips#333
Browse files Browse the repository at this point in the history
  • Loading branch information
zquestz committed Oct 1, 2023
1 parent 2b1d460 commit be0fbc4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
55 changes: 29 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,40 +305,43 @@ const gif = {

### Printing rarity data (Experimental feature)

To see the percentages of each attribute across your collection, run:
To see the percentages of each attribute across your collection and write rarity data to `build/json/_rarity.json`, run:

```sh
npm run rarity
```

The output will look something like this:

```sh
Trait type: Weapons
{
trait: 'Bow and Arrow',
weight: '1',
occurrence: '7 in 25 editions (28.00 %)'
}
{
trait: 'Double Sword',
weight: '1',
occurrence: '3 in 25 editions (12.00 %)'
}
{
trait: 'Scythe',
weight: '1',
occurrence: '4 in 25 editions (16.00 %)'
}
{
trait: 'Staff',
weight: '1',
occurrence: '7 in 25 editions (28.00 %)'
}
```json
{
trait: 'Sword',
weight: '1',
occurrence: '4 in 25 editions (16.00 %)'
Weapons: [
{
trait: 'Bow and Arrow',
weight: '1',
occurrence: '0 in 11 editions (0.00 %)'
},
{
trait: 'Double Sword',
weight: '1',
occurrence: '2 in 11 editions (18.18 %)'
},
{
trait: 'Scythe',
weight: '1',
occurrence: '2 in 11 editions (18.18 %)'
},
{
trait: 'Staff',
weight: '1',
occurrence: '3 in 11 editions (27.27 %)'
},
{
trait: 'Sword',
weight: '1',
occurrence: '4 in 11 editions (36.36 %)'
}
]
}
```

Expand Down
52 changes: 25 additions & 27 deletions utils/rarity.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ let rawdata = fs.readFileSync(`${basePath}/build/json/_metadata.json`);
let data = JSON.parse(rawdata);
let editionSize = data.length;

let rarityData = [];
let rarityData = {};

const getRarity = () => {
// intialize layers to chart
layerConfigurations.forEach((config) => {
let layers = config.layersOrder;

layers.forEach((layer) => {
// get elements for each layer
let elementsForLayer = [];
let elements = getElements(`${layersDir}/${layer.name}/`);
elements.forEach((element) => {
// just get name and weight for each element
let rarityDataElement = {
trait: element.name,
weight: element.weight.toFixed(0),
occurrence: 0, // initialize at 0
};
elementsForLayer.push(rarityDataElement);
});
let layerName =
layer.options?.["displayName"] != undefined
? layer.options?.["displayName"]
: layer.name;
// don't include duplicate layers
if (!rarityData.includes(layer.name)) {
if (!(layer.name in rarityData)) {
// get elements for each layer
let elementsForLayer = [];
let elements = getElements(`${layersDir}/${layer.name}/`);
elements.forEach((element) => {
// just get name and weight for each element
let rarityDataElement = {
trait: element.name,
weight: element.weight.toFixed(0),
occurrence: 0, // initialize at 0
};
elementsForLayer.push(rarityDataElement);
});
let layerName =
layer.options?.["displayName"] != undefined
? layer.options?.["displayName"]
: layer.name;
// don't include duplicate layers
// add elements for each layer to chart
rarityData[layerName] = elementsForLayer;
}
Expand Down Expand Up @@ -72,14 +72,12 @@ const getRarity = () => {
}
}

// print out rarity data
for (var layer in rarityData) {
console.log(`Trait type: ${layer}`);
for (var trait in rarityData[layer]) {
console.log(rarityData[layer][trait]);
}
console.log();
}
console.log(rarityData);

fs.writeFileSync(
`${basePath}/build/json/_rarity.json`,
JSON.stringify(rarityData, null, 2)
);
}

// Generate rarity information.
Expand Down

0 comments on commit be0fbc4

Please sign in to comment.