dimanche 12 février 2023

How to modify the code to only use one of the folders with the similar first word in their name using javascript?

The following code is used to generate NFTs by combining layers which are located inside folders. What if I wanted to use the entire collection of folders provided in props.folderNames but also force the program that if among the folders provided there were more than one folder with the similar first word (for example "Face Old" and "Face New") it only use one of these folders for the generation process randomly and not both of the at once. For example the the first artwork is generated by combining the layers inside the "Face Old" and "Hats New" folder and the second artwork is generated by combining the layers inside the "Face new" and "Hats futuristic" folder etc.

  const constructLayerToDna = (_dna = "", _layers = []) => {
    let mappedDnaToLayers = _layers.map((layer, index) => {
      let selectedElement = layer.elements.find(
        (e) => e.id == cleanDna(_dna.split("-")[index])
      );
      return {
        name: layer.name,
        selectedElement: selectedElement,
      };
    });
    return mappedDnaToLayers;
  };

  const startCreating = async () => {
    props.setProgress(0);
    let editionCount = 1;
    let failedCount = 0;
    while (editionCount <= props.config.supply) {
      let newDna = createDna(props.folderNames);
      if (isDnaUnique(dnaList, newDna)) {
        let results = constructLayerToDna(newDna, props.folderNames);
        let loadedElements = [];

        results.forEach((layer) => {
          loadedElements.push(loadLayerImg(layer));
        });

        await Promise.all(loadedElements).then((renderObjectArray) => {
          ctx.clearRect(0, 0, props.config.width, props.config.height);
          renderObjectArray.forEach((renderObject, index) => {
            drawElement(renderObject, index);
          });

          saveImage(editionCount);
          addMetadata(newDna, editionCount);
          saveMetaDataSingleFile(editionCount);
          console.log(`Created edition: ${editionCount}`);
        });
        dnaList.add(filterDNAOptions(newDna));
        editionCount++;
        props.setProgress(editionCount - 1);
      } else {
        console.log("DNA exists!");
        failedCount++;
        if (failedCount >= 1000) {
          console.log(
            `You need more layers or elements to grow your edition to ${props.config.supply} artworks!`
          );
          process.exit();
        }
      }
    }
    writeMetaData(JSON.stringify(metadataList, null, 2));
  };

I tried adding the following code:

const folderGroups = props.folderNames.reduce((groupedFolders, folder) => {
    const folderName = folder.toString().split(" ")[0];
    groupedFolders[folderName] = groupedFolders[folderName] || [];
    groupedFolders[folderName].push(folder);
    return groupedFolders;
  }, {});

const selectedFolders = Object.values(folderGroups).map((folderGroup) => {
  const randomIndex = Math.floor(Math.random() * folderGroup.length);
  return folderGroup[randomIndex];
});

and also changed the startCreating function to this:


  const startCreating = async () => {
    props.setProgress(0);
    let editionCount = 1;
    let failedCount = 0;
    while (editionCount <= props.config.supply) {
      let newDna = createDna(selectedFolders);
      if (isDnaUnique(dnaList, newDna)) {
        let results = constructLayerToDna(newDna, selectedFolders);
// rest of the code

However, this didn't work and resulted in the program to only use one of the folders provided to it out of the entire collection of folders.




Aucun commentaire:

Enregistrer un commentaire