obsidian-dice-roller icon indicating copy to clipboard operation
obsidian-dice-roller copied to clipboard

[Feature]: Commit File Results From Command Palette

Open mardybardy opened this issue 1 year ago • 4 comments

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

  • [X] I searched previous Issues and didn't find any similar feature requests.

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

  • Automatically append ^dr-$HASH to each inline dice roller when running for first time.
  • Change structure of results object in data.json
"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

mardybardy avatar May 27 '23 00:05 mardybardy