Skip to content

Commit 89eb28e

Browse files
committed
Adds DNA bypass feature for layer configuration options
1 parent 8890c4d commit 89eb28e

2 files changed

Lines changed: 55 additions & 5 deletions

File tree

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)