javalent / dice-roller

Inline dice rolling for Obsidian.md
274 stars 28 forks source link

[Feature]: Commit File Results From Command Palette #214

Open mardybardy opened 1 year ago

mardybardy commented 1 year ago

Please make sure this feature request hasn't been suggested before.

Feature description

I would like to be able to create a character sheet that pulls in content from several different table dice rollers. I would then be able to click the button to re-roll any results I feel don't fit the character. Once I'm happy with the character I would then access the "Dice Roller: Commit Results" command which would then replace the dice rollers with their results permanently.

Solution

"results": {
"aa.md": {
  "dr-02dn29": {
      "type": "table",
      "result": "96-100 | Nearly Impossible"
    }
  },
  "dr-02ndk2": {
      "type": "table",
      "result": "0-5 | Trivial"
  }
}

something like this for command

const fs = require('fs');
const readline = require('readline');

async function commitResults(filename) {
    const file = fs.readFile("data.json", "utf8");
    const { results } = JSON.parse(file);
    const rollers = results[filename];

    if (rollers) {
        const writeMap = await getWriteMap(filename, rollers);
        await writeResults(writeMap);
    }
}

async function getWriteMap(filename, rollers) {
  const fileStream = fs.createReadStream(filename);

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });

  const hashes = Object.keys(rollers);
  const writeMap = new Map();

  for await (const line of rl) {
    for (const hash of hashes) {
        if (line.contains(hash)) {
            writeMap.set(line, rollers[hash].result);
        }
    }
  }

  return writeMap;
}

async function writeResults() {
    // need to refresh my knowledge on write streams
}

Alternatives

I am not sure it is even possible to do as switching between edit and view modes seems to trigger a reroll of the dice.

Additional Information

nothing to add

valentine195 commented 1 year ago

This request is essentially “dice-mod on command” ?

mardybardy commented 1 year ago

Sort of, I don't want it to replace the command dice: ... with a dice-mod: ... because that would presumably trigger a re-roll, I want to just turn all the results that are in the file and make them permanent as if they were dice-mod when I last called them. It would be really handy for lots of things e.g character sheets, settings, npcs etc because you could have it generate, tweak to your liking, then commit it to canon. another benefit to being able to trigger via command is it can be triggered easily via the buttons plugin

sigrunixia commented 1 year ago

Tagging this as medium because this is pretty cool of a suggestion.

I could see this feasibly being as a part of a command versus a specific dice: inline call. Without rerolling, it would need to:

Edit: This would solve #206 as well.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.