OhhLoz / Harvester

A QoL FoundryVTT module to improve the harvesting and looting experience.
GNU General Public License v3.0
2 stars 2 forks source link

Add module setting for choose you own harvest compendium by id #12

Closed p4535992 closed 11 months ago

OhhLoz commented 1 year ago

Hi,

I'm not sure what you want to do by letting the user change the default compendium by id, they can already use a custom compendium to add their own content which can also be used to overwrite the default one. Any custom compendium should be in addition to the default, if I wanted to add 1 monster with custom drops I lose access to around ~800.

Unless you want the users to be able to name the compendium anything they want, this is present in the v10 version (1.3.9) 8de392a95b82834c92641ca39b5ac2d12a581d3a as there wasn't a way to add persistent storage into modules, so users needed their own compendiums to avoid them being wiped on updating Harvester. This hasn't been necessary in v11 because of persistent storage so Custom is sufficient.

Maybe im missing something im not sure

p4535992 commented 1 year ago

The problem is the items with the numeric suffix. Let me explain better "Eyes of the Snake (x2)" is actually one item representing two items, but the quantity is one, instead it should be "Eyes of the Snake" with quantity two.

I can't edit your compendium directly because I have no idea of the updates you make, so I was thinking of making an update code and change the pointing of the compendium without having you make code from your side.

If you may be interested this is the code I had prepared to update either from the compnedium or from a folder, there is a strange bug on the compendium, so as a solution I would export the compendium to the folder (keepId = true) and then re-import to the empty compendium again.

 static async updateHarvesterQuantityByRegExOnFolder(uuidFolder) {
    const folder = await fromUuid(uuidFolder);
    if (!folder) {
      warn(`No folder founded with uuid :` + uuid);
      return;
    }
    function containsNumbers(str) {
      return /\d/.test(str);
    }
    const updates = folder.contents
      .filter((currentValue, index, array) => {
        if (containsNumbers(currentValue.name)) {
          return true;
        }
        return false;
      })
      .map((i) => {
        var nameTmp = i.name;
        if (nameTmp.includes("1d")) {
          nameTmp = nameTmp.replace("1d", "");
        }
        if (!containsNumbers(nameTmp)) {
          return i;
        }
        var numStr = nameTmp.match(/\d+/)[0]; //nameTmp.replace(/\D/g, "");
        var num = parseInt(numStr);
        var arrNames = nameTmp.replace(numStr, "").split("(");
        var newName = arrNames?.length > 0 ? arrNames[0] : nameTmp;

        var newItem = mergeObject(i, {
          // _id: i.id,
          name: newName.trim(),
          "system.quantity": num,
          "system.price.value": Math.round(i.system.price.value / num),
          "system.weight": Math.round(i.system.weight / num),
        });
        return newItem;
      }, []);

    // Foundry has some problem ....
    info("Stai aggiornando un numero di coumenti pari a :" + updates.length, true);
    const chunkSize = 1;
    for (let i = 0; i < updates.length; i += chunkSize) {
      const chunk = updates.slice(i, i + chunkSize);
      if (containsNumbers(chunk[0].name)) {
        error("[" + i + "] Update: " + chunk[0].name, true);
      } else {
        await Item.updateDocuments(chunk);
      }
    }
    warn("COMPLETATO AGIORNAMENTO");
  }

  static async updateHarvesterQuantityByRegExOnCompendium(packToExportKey) {
    function containsNumbers(str) {
      return /\d/.test(str);
    }
    const key = "harvester.harvest";
    const items = await game.packs.get(key).getDocuments();
    const updates = items
      .filter((currentValue, index, array) => {
        if (containsNumbers(currentValue.name)) {
          return true;
        }
        return false;
      })
      .map((i) => {
        var nameTmp = i.name;
        if (nameTmp.includes("1d")) {
          nameTmp = nameTmp.replace("1d", "");
        }
        if (!containsNumbers(nameTmp)) {
          return i;
        }
        var numStr = nameTmp.match(/\d+/)[0]; //nameTmp.replace(/\D/g, "");
        var num = parseInt(numStr);
        var arrNames = nameTmp.replace(numStr, "").split("(");
        var newName = arrNames?.length > 0 ? arrNames[0] : nameTmp;

        var newItem = mergeObject(i, {
          name: newName.trim(),
          "system.quantity": num,
          "system.price.value": Math.round(i.system.price.value / num),
          "system.weight": Math.round(i.system.weight / num),
        });
        return newItem;
      }, []);

    // Foundry has some problem ....
    info("Stai aggiornando un numero di coumenti pari a :" + updates.length, true);
    const chunkSize = 1;
    for (let i = 0; i < updates.length; i += chunkSize) {
      const chunk = updates.slice(i, i + chunkSize);
      if (containsNumbers(chunk[0].name)) {
        error("[" + i + "] Update: " + chunk[0].name, true);
      } else {
        await Item.updateDocuments(chunk, { pack: packToExportKey });
      }
    }
    warn("COMPLETATO AGIORNAMENTO");
  }
OhhLoz commented 11 months ago

It would probably be easier to replace those faulty entries in the source compendium than allowing users to change the source compendium

p4535992 commented 11 months ago

Never mind I fixed it by hand, I'm still updating better tables with a new dedicated harvest table type, when I'm done with it I'll try to contact you for a API level integration.