Coder Social home page Coder Social logo

rpgdice's Introduction

RPGDice

Build Status Language grade: JavaScript Total alerts

This project is an opinionated dice syntax and roller library designed to be used for any dice-based RPG system. Its main goal is to use a straightforward, easy to use grammar that has enough flexibility to allow players to easily codify their dice rolls. One of its central features, the ability to use variables, exists to facilitate a 'write once' philosophy of dice rolls.

Dice are Hard

The single largest complaint I ever hear about a given RPG system is that the dice are "too complicated", or "I can never remember how to roll that", or "I keep forgetting my bonuses." This is why RPGDice exists: computers are amazing at calculations; humans aren't. That's why we let the computer do the hard work of keeping track of everything, and the user just gets to see the results of what they told it to roll.

Opinionated

There is a semi-formal dice notation floating around. I personally find its syntax clunky, difficult to use, and nearly impossible to extend. Instead, I've created a syntax that is, in essence, mathematical operations, plus functions, variables, and XdY syntax. I feel my version is easy enough to learn for veterans and newbies alike, while leveraging some basic programing concepts, like the principal of least surprise.

Usage

Getting RPGDice

Our recommended way is via npm:

$ npm install --save rpgdicejs

Or with yarn:

$ yarn add rpgdicejs

Now, if you want to use this in a browser, any of the major bundlers should be able to handle this module just fine. It should be noted that as this is still a CJS module, you may be required to do some work, but it has no dependencies, and works in both node and the browser, so bundling it should be easy.

We used to provide a bundled version but have removed it in an interest of maintainability. In the future the code base will be converted to typescript, and at that time we'll provide UMD, CJS and ESM module versions for easy consumption.

Syntax Summary

d20 + floor(level / 2) + floor(('strength.score' - 10) / 2) + proficiency + 'Weapon Enhancement' + [Misc.Attack.Bonus]

As you can see, the syntax is very nearly a super-simplified version of javascript. It supports standard order of operations, XdY for rolls, function calls, and variables. (This particular roll is the formula for a D&D 4e attack, with the added pathology of showing all the various ways of escaping variables.)

When you make a roll, you will pass in a scope object, which RPGDice will use to look up all variables and functions. By default, we provide several mathematical functions, such as min(), max(), floor(), ceil(), round(). Additionally, we provide some common RPG rules: explode(), dropLowest(), dropHighest(), reroll().

If you set a variable on the scope to a function, but reference it without parenthesis, RPGDice will call it, passing in no arguments. Ex: 3d8 + strMod, where strMod was defined as:

function strMod()
{
    return Math.floor((scope.strength - 10) / 2);
} // end strMod

This gives you a lot of power in how you define your scope. You can additionally extend the functionality to support any rule set your heart desires, without needing explicit support in the syntax. For example, let's say you wanted to play with loaded dice. There's no special syntax support for that, but you can add it yourself:

function rollLoaded(sides)
{
    var roll = Math.floor(Math.random() * sides) + 1;

    // We make ourselves 3 times as likely to roll max, and impossible to roll the minimum.
    // Simply returning the max might look suspicious. :-p
    if(roll < sides/3)
    {
        return sides;
    } // end if

    return roll;
} // end strMod

Now, you can roll your loaded dice like such:

3(rollLoaded(6)) + 4

This expression calls rollLoaded(6) three times, and then adds 4. It's the equivalent to 3d4, except the dice rolling logic has been replaced by your loaded dice rules. Functions get the full results object, which includes the parse tree for each expression they get as an argument, which means functions can be incredibly powerful.

If you would like to dive further into the syntax, please check out our Syntax Documentation.

API

The API for rolling dice is super simple. There are exactly 2 functions, rpgdice.parse() and rpgdice.eval(). Each take a dice string to parse, and only differ in what they output; parse() simply returns you the tokenized roll as a parse tree, while eval() will return you a populated version of the parse tree. (The final result is in the value property of the root node.) Additionally, roll() can take a parse tree (such as the results of parse()) not just a string. This allows for a small optimization by only needing to tokenize the expression once, and calling eval() multiple times.

Here's a few examples:

// Roll a simple equation
var results = rpgDice.roll('3d6 + 4');

// Render the results as a string
console.log('Results:', results.render());

// Print the final result:
console.log('Total:', results.value);

//----------------------------------------------------------------

// Evaluate an expression
var eval = rpgDice.parse('3(4d10 - 2)')

// Maybe do something with the evaluated expresion

// Now, get the results for this roll
var results = rpgDice.roll(eval);

Expression API

The results of rpgdice.parse() and rpgdice.eval() are Expression objects. These represent the parse tree of the expression. While for a general use case you won't need the power they provide, they do expose a few useful functions:

  • render() - Renders a parse tree to a string. If the parse tree has been evaluated, it includes the intermediate results.
  • eval() - Evaluates the parse tree from this node down. (This is the same as passing the Expression object to rpgdice.eval().

For more details on the API, please check out our API Documentation.

rpgdice's People

Contributors

dependabot[bot] avatar morgul avatar mrfigg avatar whitelynx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

keanedeveloper

rpgdice's Issues

Exponential performance drop when deeply nesting parse trees (Repeat, Function, Parentheses)

So I noticed what seems to be an exponential performance drop while doing another stress test. I remade my fork to illustrate the issue here and made a few optimization tweaks to the side-associative helper functions to (unsuccessfully) try to help alleviate the issue.

When you get to around 6 nested parentheses, ((((((6)))))), it takes about 830ms to parse the expression, at least on my machine. Adding one more for 7 nested parentheses bumps that all the way up to about 5000ms.

When testing in 2.0.0-rc.2 the same performance drop is there, it just takes around 8 nested parentheses before getting to be about 800ms. 9 nested parentheses gets up to around 4100ms.

I don't know how severe you consider this issue to be right now, but it seems like it will only get worse once/if we proceed with adding conditionals, as that would probably add another 5 or 6 peg rules.

I could be too inexperienced with PEG.js to be coming to this conclusion given that I only became aware of it two weeks ago, but this seems to be an issue on their end as your peg file seems to match the format of the examples they give. I'm really not sure what we can do about this one.

What to do about conditionals

Starting a separate issue to further talk about proposed change 1 in #7.

mrfigg:

1. Add logical support
   
   1. Add `if(conditionExpr, thenExpr[, elseExpr])` function to default scope, internally it would use `if(conditionExpr.value !== 0) { value = thenExpr.value } else { value = /* elseExpr.value or 0 */ }`
   2. Add `equal`, `notEqual`, `greaterThan`, `lessThan`, `greaterThanOrEqualTo`, `lessThanOrEqualTo`, `or`, `and`, & `not` operators
      This can be done in two ways;
      
      1. Via functions in default scope `if(greaterThanOrEqualTo(1d20+5, enemyArmor), 2d8+3)`
         This is simpler to implement, but not by much, and it is uglier
      2. Via operators in peg file `if(1d20+5 >= enemyArmor, 2d8+3)`
         The `not` operator (`!expr`) probably needs its own expression, but the rest would fit nicely in the `Operation` expression
      
      I would pick option b, but both are reasonable.

Morgul:

1. My first thought was, "If I've implemented a Turing complete Domain Specific Language for my dice roller, I'm doing something wrong." Now, that's not _entirely_ true; but it does feel like you're trying to put game specific logic into a DSL, when more than likely it should be implemented in side a 'real' language, like Javascript. So as far as `if`, `else`, `not`, `and`, `or` go, I'm rather against it. _However_, all of that being said, I'm open to the idea of expanding the parser to support user extensions for these use cases. Here's a couple of ideas:
   
   1. **Compromise:** Support the `?` operator.
      
      1. Yes, I know what I said above, but I can definitely see some places where something like `attackBonus > 5 ? 2d8 + 1 : 2d6 + 1` could be really handy. However, it would have the following caveats: it would use the short form javascript ternary, to discourage chained usage, and only `<, >, =` (and combinations) would be implemented, probably as their own `BooleanOperation` class. There would be _no_ `and`, `or`, `is`, or `not` implemented as operators. _(More on that below.)_
      2. We _might_ need to implement `!`, and combinations like `!=`, depending on how awkward it is to use `not` with one of the other proposals below. We would **not** implement `===` or `!==`, instead `==` would be equivalent to `===` and `!=` would be equivalent to `!==`. (I do not want javascript's weird implicit casting part of this.).
   2. **Implicit / Operator functions:** We could make the following valid: `not 3d6` where `not` is a function on the scope that takes one argument, basically `not 3d6 === not(3d6)`. _I have not thought through the parser implications of this!_ It would get us much closer to being able to have user defined operator functions like: `1d10 > 5 and 3d6 < 5`. In order to do that, however, we have to make a few definitions:
      
      1. An "implicit" function (I'd rather call them 'operator functions') could only be of arity one, or arity two.
      2. If an operator function is of arity one, its signature would be: `op(right)`.
      3. If an operator function is of arity two, its signature would be: `op(right, left)`.
      4. If `Variable` evaluates to a function, it will be called with `fun(right, left)`, where `left` could be undefined.
      5. I'm honestly not sure that the complexity of this is worth it. Even as I was typing this out I became concerned.
   3. **Pipelines:** We could implement the minimal pipeline operator instead of the implicit operators. It would mean the following would be valid: `attackBonus |> greaterThan(3) ? 3d8 : 2d6`, where `greaterThan` was defined as `greaterThan(n) { return (m) => n > m; }`. This would also likely mean we'd need to ensure we support currying: `add(2)(3) === add: n => m => n + m`. I would also be open to a `lodash.fp.flow` way of doing custom operations: `flow(double(3(5d6 + 1)), negate, add(30))`. That's probably overkill, but as long as we support currying, a user could implement that.
   4. **RPGDice Extras:** If we have all the base required support in the main lib, I'd be open to adding some of the more controversial features into a secondary library. It would, basically, be an expanded default scope, with all the function versions of the features you were suggesting. That way, if someone really wants those, they can have a nice, supported version, but for people who just want to roll some dice (my main use case), we keep the core lib nice and small.

Can't use a variable as a dice roll

Let's assume I have the following scope:

{
    Weapon: {
        Damage: '1d6'
    }
}

I cannot do the following: 'Weapon.Damage' + 1; Weapon.Damage evaluates to a string, and I get '1d61' out.

Parentheses not preserved in parse tree

Parentheses are not preserved in the parse tree, which limits the ability to create a proper breakdown of the dice roll and results in Expression.render() returning an incorrect value. (For anyone reading this in the future, the broken math in this issue is being addressed in #4.)
screenshot_2019-02-28
Expression.render() output: 3 - { 1d20: [ 8 ] } + 5 + 2

{
  "type": "subtract",
  "left": {
    "type": "number",
    "value": 3
  },
  "right": {
    "type": "add",
    "left": {
      "type": "add",
      "left": {
        "type": "roll",
        "count": 1,
        "sides": 20,
        "results": [
          8
        ],
        "value": 8
      },
      "right": {
        "type": "number",
        "value": 5
      },
      "value": 13
    },
    "right": {
      "type": "number",
      "value": 2
    },
    "value": 15
  },
  "value": -12
}

Maybe add a new parentheses Expression type?

{
  "type": "parentheses",
  "content": {},
  "value": 1234
}

Incorrect left-to-right order of operations

I ran a couple of basic order of operations tests with version 2.0.0-rc.2 and got some illogical results.

Input: 2+8-2+8
Expected output: 16
Generated output: 0

Input: 1-(1+1)+1
Expected output: 0
Generated output: -2

Both tests seem to be going outside-in instead of left-to-right.

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.