Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
frankieali opened this issue Jul 7, 2021 · 1 comment

Comments

@frankieali
Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant