Skip to content

Commit b983a32

Browse files
authored
Merge pull request #157 from nftchef/1.0.9/bypass-DNA
Add bypassDNA flag
2 parents 714c371 + 2ee66f7 commit b983a32

3 files changed

Lines changed: 62 additions & 6 deletions

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ If you want to play around with different blending modes, you can add a `blend:
111111

112112
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.
113113

114+
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.
115+
114116
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.
115117

116118
Here is an example on how you can play around with both filter fields:
@@ -120,7 +122,11 @@ const layerConfigurations = [
120122
{
121123
growEditionSizeTo: 5,
122124
layersOrder: [
123-
{ name: "Background" },
125+
{ name: "Background" , {
126+
options: {
127+
bypassDNA: false;
128+
}
129+
}},
124130
{ name: "Eyeball" },
125131
{
126132
name: "Eye color",

src/config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ const layerConfigurations = [
3030
{
3131
growEditionSizeTo: 5,
3232
layersOrder: [
33-
{ name: "Background" },
33+
{ name: "Background",
34+
options: {
35+
bypassDNA: true
36+
}
37+
},
3438
{ name: "Eyeball" },
3539
{ name: "Eye color" },
3640
{ name: "Iris" },

src/main.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ const getRarityWeight = (_str) => {
6666
};
6767

6868
const cleanDna = (_str) => {
69-
var dna = Number(_str.split(":").shift());
69+
const withoutOptions = removeQueryStrings(_str)
70+
var dna = Number(withoutOptions.split(":").shift());
7071
return dna;
7172
};
7273

@@ -107,6 +108,10 @@ const layersSetup = (layersOrder) => {
107108
layerObj.options?.["opacity"] != undefined
108109
? layerObj.options?.["opacity"]
109110
: 1,
111+
bypassDNA:
112+
layerObj.options?.["bypassDNA"] !== undefined
113+
? layerObj.options?.["bypassDNA"]
114+
: false
110115
}));
111116
return layers;
112117
};
@@ -231,8 +236,49 @@ const constructLayerToDna = (_dna = "", _layers = []) => {
231236
return mappedDnaToLayers;
232237
};
233238

239+
/**
240+
* In some cases a DNA string may contain optional query parameters for options
241+
* such as bypassing the DNA isUnique check, this function filters out those
242+
* items without modifying the stored DNA.
243+
*
244+
* @param {String} _dna New DNA string
245+
* @returns new DNA string with any items that should be filtered, removed.
246+
*/
247+
const filterDNAOptions = (_dna) => {
248+
const dnaItems = _dna.split(DNA_DELIMITER)
249+
const filteredDNA = dnaItems.filter(element => {
250+
const query = /(\?.*$)/;
251+
const querystring = query.exec(element);
252+
if (!querystring) {
253+
return true
254+
}
255+
const options = querystring[1].split("&").reduce((r, setting) => {
256+
const keyPairs = setting.split("=");
257+
return { ...r, [keyPairs[0]]: keyPairs[1] };
258+
}, []);
259+
260+
return options.bypassDNA
261+
})
262+
263+
return filteredDNA.join(DNA_DELIMITER)
264+
}
265+
266+
/**
267+
* Cleaning function for DNA strings. When DNA strings include an option, it
268+
* is added to the filename with a ?setting=value query string. It needs to be
269+
* removed to properly access the file name before Drawing.
270+
*
271+
* @param {String} _dna The entire newDNA string
272+
* @returns Cleaned DNA string without querystring parameters.
273+
*/
274+
const removeQueryStrings = (_dna) => {
275+
const query = /(\?.*$)/;
276+
return _dna.replace(query, '')
277+
}
278+
234279
const isDnaUnique = (_DnaList = new Set(), _dna = "") => {
235-
return !_DnaList.has(_dna);
280+
const _filteredDNA = filterDNAOptions(_dna);
281+
return !_DnaList.has(_filteredDNA);
236282
};
237283

238284
const createDna = (_layers) => {
@@ -249,7 +295,7 @@ const createDna = (_layers) => {
249295
random -= layer.elements[i].weight;
250296
if (random < 0) {
251297
return randNum.push(
252-
`${layer.elements[i].id}:${layer.elements[i].filename}`
298+
`${layer.elements[i].id}:${layer.elements[i].filename}${layer.bypassDNA? '?bypassDNA=true' : ''}`
253299
);
254300
}
255301
}
@@ -364,7 +410,7 @@ const startCreating = async () => {
364410
)}`
365411
);
366412
});
367-
dnaList.add(newDna);
413+
dnaList.add(filterDNAOptions(newDna));
368414
editionCount++;
369415
abstractedIndexes.shift();
370416
} else {

0 commit comments

Comments
 (0)