Crate gurgle[][src]

Expand description

Rolling dice using TRPG-like syntax.

Example

Only need result value

let attack = "3d6+2d4+1";
println!("roll your attack({}), result: {}", attack, gurgle::roll(attack).unwrap());

// output: roll your attack(3d6+2d4+1), result: 16

Need check if rolling result is success(pass)

use gurgle::Gurgle;

let attack = "3d6+2d4+1>15";
let dice = Gurgle::compile(attack).unwrap();
let result = dice.roll();

println!(
    "roll your attack({}), result: {}, {}",
    attack, result.value(),
    if result.success().unwrap() { "success" } else { "miss" },
);

// output: roll your attack(3d6+2d4+1>15), result: 16, success

Need get rolling result of every dice

use gurgle::Gurgle;

let attack = "3d6+2d4+1>15";
let dice = Gurgle::compile(attack).unwrap();
let result = dice.roll();

println!("roll your attack({}), result: {}", attack, result);

// output: roll your attack(3d6+2d4+1>15), result: (4+3+1) + (1+3) + 1 = 15, target is >15, failed

Notice: Display trait for rolling result is implemented only if feature detail(which is enabled by default) is enabled.

You can see source code detail.rs for how to can walk through result tree and construct you own output message format.

Command Syntax

A Gurgle command is consists of two parts: dice expression(AstTreeNode) and a optional Checker.

Dice expression is addition or minus of one or more item, item can be a const number or a dice rolling round.

Dice rolling round can be write as xdy: x is rolling times, y is dice sided, so it means rolling a y sided dice x times and sum the result points.

In addition to summing, a dice rolling round can use avg, max, and min to get the final result of this round.

Some example for easily understand:

  • ✅️ 3d6
  • ✅️ 3d6+1
  • ✅️ 3d6+2d4+2
  • ✅️ 3d6max+2d4max+1
  • ✅️ 3d6max + 2d4max + 1,space between item and operator(+/-) is acceptable
  • ❌️ 3d6 max, space can’t appear in inner of a item
  • ❌️ 0d-10, x and y can’t be zero or negative value
  • ✅️ 2d10-3d2-1, minus ok
  • ✅️ 2d10*3+4, multiply ok
  • ✅️ (2d6+1)*4+1, parentheses ok

And you can add checker, it a compare with a value, that is, right side of a (in)equation:

  • >=10
  • >10
  • <=10
  • <10
  • =10

A full example: 3d6+(2d4+1)*2+1 > 20.

space between expr and checker, between compare and value is optional.

So it’s the same as: 3d6+(2d4+1)*2+1>20.

Re-exports

pub use expr::Dice;

Modules

check whether a roll result is a success

show roll result in detail

errors in gurgle command parsing and execution

gurgle expression

rolling result

Structs

Gurgle command limitation configuration

A Compiled gurgle command

Functions

Compile then execute a gurgle command immediately, get result value