Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rarity data: Show percentage of each attribute's occurrence in collection #149

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ Use the existing `addLayers` calls as guidance for how to add layers. This can e
It is possible to provide a percentage at which e.g. a rare item would contain a rare vs. common part in a given layer. This can be done via the `addRarityPercentForLayer` that can be found in the `config.js` as well.
This allows for more fine grained control over how much randomness there should be during the generation process, and allows a combination of common and rare parts.

### Printing rarity data
To see the percentages of each attribute across your collection, run `node rarityData.js`

# Development suggestions
- Preferably use VSCode with the prettifier plugin for a consistent coding style (or equivalent js formatting rules)
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const generateMetadata = (_dna, _edition, _attributesList) => {
const getAttributeForElement = (_element) => {
let selectedElement = _element.layer.selectedElement;
let attribute = {
trait_type: _element.layer.name,
name: selectedElement.name,
rarity: selectedElement.rarity,
};
Expand Down Expand Up @@ -94,6 +95,7 @@ const constructLayerToDna = (_dna = [], _layers = [], _rarity) => {
let mappedDnaToLayers = _layers.map((layer, index) => {
let selectedElement = layer.elements.find(element => element.id === _dna[index]);
return {
name: layer.id,
location: layer.location,
position: layer.position,
size: layer.size,
Expand Down
73 changes: 73 additions & 0 deletions rarityData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

const fs = require('fs');
const {
layers,
editionSize,
startEditionFrom
} = require("./input/config.js");

let collectionSize = editionSize - startEditionFrom + 1;

let rarityChart = [];

// initialize rarity chart
layers.forEach((layer) => {
let elementsList = [];

let rarityForLayer = {
traits: elementsList
};

for(let i = 0; i < layer.elements.length; i++)
{
elementsList = {
value : layer.elements[i].name,
percentage : 0,
};
rarityForLayer.traits.push(elementsList);
}

rarityChart[layer.id] = rarityForLayer;
});

// read metadata data
let rawdata = fs.readFileSync('./output/_metadata.json');
let data = JSON.parse(rawdata);

// fill up rarity chart with occurrences from metadata
data.forEach((element) => {

for(let i = 0; i < element.attributes.length; i++)
{
let traitType = element.attributes[i].trait_type;
let value = element.attributes[i].name;

let rarityChartTrait = rarityChart[traitType];

for (let i = 0; i < rarityChartTrait.traits.length; i++)
{
if (rarityChartTrait.traits[i].value == value){
rarityChartTrait.traits[i].percentage++;
}
}
}

});

// iterate through rarity list and convert the occurrences to percentages
for (const [layer, traits] of Object.entries(rarityChart)) {
for (const [trait, value] of Object.entries(traits)) {
for (const [key, val] of Object.entries(value)) {
val.percentage = (val.percentage / collectionSize) * 100;
}
}
}

// print out rarity data to console
for (const [layer, traits] of Object.entries(rarityChart)) {
console.log(`Layer: ${layer}`);
for (const [trait, value] of Object.entries(traits)) {
console.log(value);
}
}