Skip to content

Commit

Permalink
Merge pull request #157 from nftchef/1.0.9/bypass-DNA
Browse files Browse the repository at this point in the history
Add bypassDNA flag
  • Loading branch information
HashLips authored Oct 27, 2021
2 parents 714c371 + 2ee66f7 commit b983a32
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ If you want to play around with different blending modes, you can add a `blend:

If you need a layers to have a different opacity then you can add the `opacity: 0.7` field to the layersOrder `options` object as well.

If you want to have a layer _ignored_ in the DNA uniqueness check, you can set `bypassDNA: true` in the `options` object. This has the effect of making sure the rest of the traits are unique while not considering the `Background` Layers as traits, for example. The layers _are_ included in the final image.

To use a different metadata attribute name you can add the `displayName: "Awesome Eye Color"` to the `options` object. All options are optional and can be addes on the same layer if you want to.

Here is an example on how you can play around with both filter fields:
Expand All @@ -120,7 +122,11 @@ const layerConfigurations = [
{
growEditionSizeTo: 5,
layersOrder: [
{ name: "Background" },
{ name: "Background" , {
options: {
bypassDNA: false;
}
}},
{ name: "Eyeball" },
{
name: "Eye color",
Expand Down
6 changes: 5 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const layerConfigurations = [
{
growEditionSizeTo: 5,
layersOrder: [
{ name: "Background" },
{ name: "Background",
options: {
bypassDNA: true
}
},
{ name: "Eyeball" },
{ name: "Eye color" },
{ name: "Iris" },
Expand Down
54 changes: 50 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ const getRarityWeight = (_str) => {
};

const cleanDna = (_str) => {
var dna = Number(_str.split(":").shift());
const withoutOptions = removeQueryStrings(_str)
var dna = Number(withoutOptions.split(":").shift());
return dna;
};

Expand Down Expand Up @@ -107,6 +108,10 @@ const layersSetup = (layersOrder) => {
layerObj.options?.["opacity"] != undefined
? layerObj.options?.["opacity"]
: 1,
bypassDNA:
layerObj.options?.["bypassDNA"] !== undefined
? layerObj.options?.["bypassDNA"]
: false
}));
return layers;
};
Expand Down Expand Up @@ -231,8 +236,49 @@ const constructLayerToDna = (_dna = "", _layers = []) => {
return mappedDnaToLayers;
};

/**
* In some cases a DNA string may contain optional query parameters for options
* such as bypassing the DNA isUnique check, this function filters out those
* items without modifying the stored DNA.
*
* @param {String} _dna New DNA string
* @returns new DNA string with any items that should be filtered, removed.
*/
const filterDNAOptions = (_dna) => {
const dnaItems = _dna.split(DNA_DELIMITER)
const filteredDNA = dnaItems.filter(element => {
const query = /(\?.*$)/;
const querystring = query.exec(element);
if (!querystring) {
return true
}
const options = querystring[1].split("&").reduce((r, setting) => {
const keyPairs = setting.split("=");
return { ...r, [keyPairs[0]]: keyPairs[1] };
}, []);

return options.bypassDNA
})

return filteredDNA.join(DNA_DELIMITER)
}

/**
* Cleaning function for DNA strings. When DNA strings include an option, it
* is added to the filename with a ?setting=value query string. It needs to be
* removed to properly access the file name before Drawing.
*
* @param {String} _dna The entire newDNA string
* @returns Cleaned DNA string without querystring parameters.
*/
const removeQueryStrings = (_dna) => {
const query = /(\?.*$)/;
return _dna.replace(query, '')
}

const isDnaUnique = (_DnaList = new Set(), _dna = "") => {
return !_DnaList.has(_dna);
const _filteredDNA = filterDNAOptions(_dna);
return !_DnaList.has(_filteredDNA);
};

const createDna = (_layers) => {
Expand All @@ -249,7 +295,7 @@ const createDna = (_layers) => {
random -= layer.elements[i].weight;
if (random < 0) {
return randNum.push(
`${layer.elements[i].id}:${layer.elements[i].filename}`
`${layer.elements[i].id}:${layer.elements[i].filename}${layer.bypassDNA? '?bypassDNA=true' : ''}`
);
}
}
Expand Down Expand Up @@ -364,7 +410,7 @@ const startCreating = async () => {
)}`
);
});
dnaList.add(newDna);
dnaList.add(filterDNAOptions(newDna));
editionCount++;
abstractedIndexes.shift();
} else {
Expand Down

0 comments on commit b983a32

Please sign in to comment.