npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fdw-dice

v1.1.1

Published

Dice system for dice games and D&D

Downloads

5

Readme

Dice

A dice system for D&D and other dice games

Build Jest Tests

npm language global coverage

Install

using npm

npm install @fdw/dice

using yarn

yarn add @fdw/dice

Dice notation

A dice in D&D can be represented as a string. This string starts with the number of dice (technically the string represent a roll of several dice of the same size) followed by the letter 'd' and the number of faces of the dice:

'2d6' represents 2 dices of 6 faces

A dice can also have a modificator value (positive or negative) that influences the result of the roll:

'1d8+3' representents 1 dice of 8 faces plus 3 (the values will be in range [4, 11])

You sometimes may need to roll different types of dices. In this case each dice pattern is separated from the next one with ' ' (space character):

'2d8+3 1d4' represents 2 dices of 8 faces plus 3 and 1 dice of 4 faces

How it works

If you need to roll a single dice type you can use the Dice class:

const Dice = require('dice').Dice;

// create a Dice from a string
const d1 = new Dice('4d4+3');

/* log a DiceRoll object containing:
{
  total: random number in range [d1.min, d1.max],
  diceValues: an array containing the roll value for each dice,
  dice: the dice rolled
}
*/
console.log(d1.roll());

const d2 = new Dice({
  size: 6,
  count: 2, // count is optional, default value is 1
  modificator: 3 // modificator is optional, default value is 0
}); // equivalent to new Dice('2d6+3')

const gen = d2.generator() // create a generator for the dice

console.log(gen.next().value) // same as d2.roll() but the generator is iterable

Dice also provide information to allow probability analysis:

const d = new Dice('2d6+3');

console.log(d.min); // the smallest value possible with this dice (i.e. 5 for this dice)

console.log(d.max); // the greatest value possible with this dice (i.e. 15 for this dice)

console.log(d.cardinal); // the number of possible combinations (i.e. size<sup>count</sup>)

console.log(d.valuesCardinal); // the number of possible values (i.e. max - min + 1)

Dice is inmutable, it means you cannot modify size, count or modificator once the Dice is instantiated.

If you want to roll several types of dice you can use the DiceSet class:

const DiceSet = require('dice').DiceSet;

// create a DiceSet from a string
const d1 = new DiceSet('4d4+3', '2d6+3');

/* log a DiceSetRoll object containing:
{
  total: random number in range [d1.min, d1.max],
  diceRolls: an array containing the DiceRoll of each Dice
}
*/
console.log(d1.roll());

const d2 = new DiceSet(diceOptions: [{
/*
 * you may also pass an array of dices using the 'dices' option.
 * if you pass both options the set wil contain:
 * [
 *   ...dices,
 *   ...diceOptions.map((opt) => new Dice(opt))
 * ]
 */
  size: 4,
  count: 4,
  modificator: 3
}, {
  size: 6,
  count: 2,
  modificator: 3
}]); // equivalent to new DiceSet('4d4+3 2d6+3')

const gen = d2.generator() // create a generator for the dice set

console.log(gen.next().value) // same as d2.roll() but the generator is iterable

DiceSet provide information to allow probability analysis too:

const d = new DiceSet('2d6+3 1d4');

console.log(d.min); // the smallest value possible with this dice (i.e. 6 for this dice)

console.log(d.max); // the greatest value possible with this dice (i.e. 19 for this dice)

console.log(d.cardinal); // the number of possible combinations (i.e. 6<sup>2</sup>*4<sup>1</sup>)

console.log(d.valuesCardinal); // the number of possible values (i.e. max - min + 1)