niwatori-dice
The javascript dice engine used in https://rc.folia.me. If English of the document is wrong, please correct it if you ok.
Motivation
Why does not use BCDice or other one? You will have doubts.
- BCDice is a bit slow. because it written in ruby.
- Many libraries use built-in random functions. It is difficult to test.
- Client applications can easily impersonate dice number. We need a means to verify.
niwatori-dice will have the same results as long as the given seed is the same. If you use timestamp for seed, you can verify that it is a spoofed dice by verifying with other clients using the same timestamp.
Usage
$ npm install niwatori-dice
// import { NiwatoriDice } from 'niwatori-dice'const NiwatoriDice = const roll = // => { result: 60, verbose: [{ type: 'dice', formula: '1d100', result: 60, text:'' }] }
NiwatoriDice(oparators)
You can expand grammar freely by the oparators.
Input is converted to inverse Polish notation once and processed.
The meaning is that you can implement operators such as add
andsum
yourself.
eg.
const roll = // => { result: 3, verbose: [] }
oparators[].priority (require)
The one with the larger value takes precedence
oparators[].fn (require)
It receives and executes stack as much as specified by argsLen.
When fn runs it has references to rand
andverbose
through this.
eg.
const createVerbose = // createVerbose(type, formula, result, extendText)... const roll = // => { result: 5, verbose: [{ type: 'original', formula: 'fortune', result: 5, text: 'You are 5 stars.' }] }
oparators[].argLen (default: 2)
The number of arguments.
eg.
const roll = // => 5 // => 5
Contribute
Everyone's power is necessary to deal with various TRPG rules!
You can create the new oparators to ./src/plugins
.
Test
$ npm run test
License
MIT