The javascript dice engine used in https://rc.folia.me. If English of the document is wrong, please correct it if you ok.
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.
$ npm install niwatori-dice
// import { NiwatoriDice } from 'niwatori-dice'
const { NiwatoriDice } = require('niwatori-dice')
const roll = NiwatoriDice()
roll('1d100') // => { result: 60, verbose: [{ type: 'dice', fomula: '1d100', reesult: 60, text:'' }] }
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
and sum
yourself.
eg.
const roll = NiwatoriDice({
'add': {
priority: 10,
argsLen: 2,
fn(a, b) {
return a + b
}
}
})
roll('1 add 2') // => { result: 3, verbose: [] }
The one with the larger value takes precedence
It receives and executes stack as much as specified by argsLen.
When fn runs it has references to rand
and verbose
through this.
eg.
const { createVerbose } = require('niwatori-dice')
// createVerbose(type, fomula, result, extendText)
...
const roll = NiwatoriDice({
'fortune': {
priority: -1,
argsLen: 0,
fn() {
const { rand, verbose } = this
const stars = rand.next().value % 5 + 1
verbose.push(createVerbose('original', 'fortune', stars, `You are ${stars} stars.`))
return stars
}
}
})
roll('fortune')
// => { result: 5, verbose: [{ type: 'original', fomula: 'fortune', result: 5, text: 'You are 5 stars.' }] }
The number of arguments.
eg.
const roll = NiwatoriDice({
'concat': {
priority: 10,
argsLen: 5,
fn(a, b, c, d, e) {
return a + b + c + d + e
}
}
})
roll('concat 1 1 1 1 1') // => 5
roll('concat(1,1,1,1,1)') // => 5
Everyone's power is necessary to deal with various TRPG rules!
You can create the new oparators to ./src/plugins
.
$ npm run test
MIT