Coder Social home page Coder Social logo

clevett / fdp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 3d-dice/dice-parser-interface

0.0 0.0 0.0 154 KB

Dice Parser that handles advanced roll notation for @3d-dice/dice-box

Home Page: https://fantasticdice.games/

License: MIT License

JavaScript 0.51% TypeScript 99.49%

fdp's Introduction

FDP

Fantastic Dice Parser

This module simply provides an interface between @3d-dice/dice-roller-parser and @3d-dice/dice-box. Since dice-roller-parser is a fork of another person's module, I did not want to include this interface in that package.

What it does

This module allows for more advanced rolls with dice-box. All the rolls supported are documented at Roll20 Dice Specification

How to use it

Install the library using:

npm install @3d-dice/fdp

Then create a new instance of dice-roller-parser

import DiceParser from "@3d-dice/fdp";

const DRP = new DiceParser();

The DRP class now has methods to parse raw notations, process re-rolls and compute the final results from dice-box

<form id="dice-to-roll">
  <input
    id="input--notation"
    class="input"
    placeholder="2d20"
    autocomplete="off"
  />
</form>
const form = document.getElementById("dice-to-roll");
const notationInput = document.getElementById("input--notation");

const submitForm = (e) => {
  e.preventDefault();
  const notation = DRP.parseNotation(notationInput.value);
};

form.addEventListener("submit", submitForm);

Methods

parseNotation(string, default: '')

Accepts a dice string input, parses it and returns a JSON representation of the parsed input.

Example: DRP.parseNotation('4d6')

[
  {
    "qty": 4,
    "sides": 6,
    "mods": [],
    "rolls": [
      {
        "sides": 6,
        "groupId": 0,
        "rollId": 0,
        "id": 0,
        "theme": "sunset",
        "result": 1
      },
      {
        "sides": 6,
        "groupId": 0,
        "rollId": 1,
        "id": 1,
        "theme": "sunset",
        "result": 3
      },
      {
        "sides": 6,
        "groupId": 0,
        "rollId": 2,
        "id": 2,
        "theme": "sunset",
        "result": 6
      },
      {
        "sides": 6,
        "groupId": 0,
        "rollId": 3,
        "id": 3,
        "theme": "sunset",
        "result": 6
      }
    ],
    "value": 16
  }
]

See also: Just parse the value

handleRerolls(array, default:[])

This method accepts an array of dice rolls (generated by parseNotation, updated by dice-box) and returns a new array of dice objects that need to be re-rolled. Examples of rolls that could generate rerolls include, exploding, penetrating, and compounding rolls (e.g.: 6d6!). Reroll and reroll-once notation is also supported (e.g.: 2d12r1).

Dice Object:

Property Type Description
groupId int The group the reroll target belongs to
rollId int or string The roll id of the die being rerolled. This will be incremented by .1 for every reroll made
side int The number of sides the reroll die has
qty int The number of dice to be rolled. This will always be 1 on rerolls but is needed by dice-box

Example:

[
  {
    "groupId": 0,
    "rollId": "2.1",
    "sides": 6,
    "qty": 1
  },
  {
    "groupId": 0,
    "rollId": "3.1",
    "sides": 6,
    "qty": 1
  }
]

parseFinalResults(array, default: [])

After all rolls and rerolls have completed, you can pass the results object to parseFinalResults to get the final results of the dice roll. This typically happens inside dice-box's onRollComplete callback method.

Example:

const results = DRP.parseFinalResults(results);

Putting it all together

Example HTML

Example JavaScript

Caveats

One thing this modules does not do is provide the interface for providing an input for the roll notation string or displaying the final results. It is expected that the developer will create their own inputs and outputs or use modules from @3d-dice/fui

fdp's People

Contributors

clevett avatar frankieali avatar

fdp's Issues

Tests initParser when rolls are not empty

initParser needs tests to check for what happens when this.rollsAsFloats is not empty. This is not set on initialization or by the constructor. Need find a way to test this and its likely impacted by issue #2 and issue #5.

updateFloats return NaN[]

updateFloats appears to be incorrectly looping through array/object. Its trying to do math on a string and results in NaN.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.