BTMorton / dice_roller

A node dice roller in the style of roll20
MIT License
9 stars 8 forks source link

Feature Request: public function to parse RootType with provided DiceRollResult #11

Open frankieali opened 3 years ago

frankieali commented 3 years ago

This is a feature request to add a new public function to parse previously parsed dice roll input strings (RootType) along with a provided DiceRollResult object so it can be parsed into a final RollBase object.

The application here is that I have created a 3D dice roller (using BabylonJS) that is capable of rolling dice on a canvas element and returns the face up value of the die rolled. I'd like to be able to return a "previously parsed dice roll input string" along with the roll results as an array of DiceRollResult objects to then have dice-roller-parser compute the final RollBase object. I really enjoy all the features of this package and would like to leverage it for my app.

Based on a quick review of the code, I think a good approach may be to allow the private RollType function to accept another argument of rolls: DiceRollResult that it can then pass onto the various rollType function. Then, if the rolls argument is undefined, rolls will be calculated by generateDiceRoll. Otherwise it will use the provided object. Is this feasible?

frankieali commented 3 years ago

So I figured out a way to make this work using the public randFunction.

I set up a callback function to return explicit float values.

let externalCount = 0
let rollsAsFloats = []

const rollParser = new DiceRoller((rolls = rollsAsFloats) => {
    if(rolls.length > 0) {
        return rolls[externalCount++]
    } else {
        console.warn("No results passed to the dice-roller-parser. Using fallback Math.random")
        return Math.random()
    }
})

I then parse a roll and save it for later

let parsedNotation = rollParser.parse('3d20')

I use the parsedNotation to make a roll in my 3D dice roller. I gather the results from my 3D dice roller. In this case 20, 12, and 7. I convert those values to floats using (roll result - 1)/# of sides. I save those values in my previously declared array rollsAsFloats = [.95, .55, .3] Then I'm ready to get the final results

const fixedResults = rollParser.rollParsed(parsedNotation)

I'm currently working on handling special cases that require rerolls. So far it's working well.