@3d-dice/dice-roller-parser 中文文档教程

发布于 3年前 浏览 27 项目主页 更新于 3年前

Dice Roller & Parser

这个掷骰子是一个字符串解析器,它返回一个包含掷骰子组成部分的对象。 它支持完整的Roll20 骰子规范。 它使用 pegjs 语法来创建骰子滚动格式的表示< /a>。 然后可以将其转换为简单的数值,或转换为用于显示完整滚动详细信息的复杂对象

Credit

这是 Ben Morton 的 dice_roller 项目的分支。 它有一些错误,所以我重新发布了我的项目所需的修复和功能。

Quickstart

使用以下方法安装库:

npm install dice-roller-parser

安装后,只需在浏览器中加载库:

<script src="node_modules/dice-roller-parser/dist/index.js"></script>

或在节点中:

import { DiceRoller } from "dice-roller-parser";

然后创建 DiceRoller 的新实例类,并用它来进行一些骰子掷骰。

const diceRoller = new DiceRoller();

//    Returns the total rolled value
const roll = diceRoller.rollValue("2d20kh1");
console.log(roll);

//    Returns an object representing the dice roll, use to display the component parts of the roll
const rollObject = diceRoller.roll("2d20kh1");
console.log(rollObject.value);

Usage

这个库公开了两个类,DiceRollerDiscordRollRenderer

DiceRoller

DiceRoller 类管理骰子字符串的解析并根据结果执行掷骰。

//    Creates a new instance of the DiceRoller class
const roller = new DiceRoller();

Constructor options

默认构造函数使用 Math.random 并应用每个骰子的最大掷骰数 1000。这些可以使用以下构造函数重载指定。

DiceRoller(GeneratorFunction)

您可以指定一个函数作为掷骰器的随机数生成器。 这个函数应该是 () => 类型的。 number 并返回一个介于 0 和 1 之间的数字。默认情况下,它使用内置的 Math.random 方法。

//    Default constructor using Math.random
const roller = new DiceRoller();

//    Uses a custom random generator that always returns 0.5
const roller = new DiceRoller(() => 0.5);

这可以使用 randFunction 属性读取或修改。

roller.randFunction = () => 0.5;
DiceRoller(GeneratorFunction, MaxRollsPerDie)

为了防止尝试解析大量的骰子,可以指定一个骰子的最大掷骰数。 默认值设置为 1000。

//    Uses the default constructor with a limit of 100 rolls per die
const roller = new DiceRoller(null, 100);

//    Uses a custom random generator that always returns 0.5, and a limit of 10 rolls per die
const roller = new DiceRoller(() => 0.5, 10);

这可以使用 maxRollCount 属性读取或修改。

roller.maxRollCount = 75;

Class Usage

类,就可以通过三个选项来执行掷骰子:

  • Getting a roll result directly
  • Generate an object to represent the dice roll
  • Just parse the input and add your own rolling logic
Getting a direct roll result

一旦构造了 DiceRoller 计算出的数值结果。

//    Rolls 2 d20 dice and keeps the value of the highest
const roll = roller.rollValue("2d20kh1");

//    Prints out the numeric value result
console.log(roll);
Generate an object representing the dice roll

roll 方法接受骰子字符串输入,对其进行解析,执行掷骰,然后返回表示掷骰的对象。 使用滚动对象,您可以构建自己的滚动显示功能,而不仅仅是输出最终值。

//    Rolls 2 d20 dice and keeps the value of the highest
const roll = roller.roll("2d20kh1");

//    Print out the full roll breakdown
printDiceRoll(roll);
//    Prints out the numeric value result
console.log(`Final roll value: ${roll.Value}`);

有关返回对象的更多详细信息,请参阅下面输出类型部分中的滚动结果输出 .

Just parse the value

parse 方法接受骰子字符串输入,对其进行解析并返回已解析输入的表示形式。 这可以用于执行掷骰子或重新构建原始输入。 rollParsed 方法将此解析结果作为输入,执行掷骰并返回与 相同的输出roll 方法。

//    Rolls 2 d20 dice and keeps the value of the highest
const parsedInput = roller.parse("2d20kh1");

//    Print out a re-constructed input string
printParsedInput(parsedInput);

//    Run the roller on the parsed object
const roll = roller.rollParsed(parsedInput);

//    Print out the full roll breakdown
printDiceRoll(roll);
//    Print out the numeric value result
console.log(`Final roll value: ${roll.Value}`);

有关返回对象的更多详细信息,请参阅下面输出类型部分中的解析的滚动输出 .

DiscordRollRenderer

DiscordRollRenderer 类是一个示例渲染器类,它接受由 RollBase 对象表示的滚动输入并将其渲染到降价格式的字符串,与 Discord 兼容。

//    Creates a new instance of the DiceRoller class
const renderer = new DiscordRollRenderer();

Class Usage

DiscordRollRenderer 公开了一个带有单个参数的 render 方法,RollBase 对象到渲染,并返回渲染后的字符串。

//    Rolls 2 d20 dice and keeps the value of the highest
const roll = roller.rollValue("2d20kh1");

//    Get the formatted string
const render = renderer.render(roll);
console.log(render);

Development

要开发这个库,只需克隆存储库,运行安装:

npm install

然后进行构建:

npm run build

这会做四件事:

# Clean any existing builds
npm run clean

# Build the dice grammer
npx pegjs src/diceroll.pegjs

# Run tslint against the project
tslint -c tslint.json --project tsconfig.json

# Then run webpack to build and package everything up nicely
webpack

要运行测试套件,请使用:

npm run test

仅此而已!

Output Types

以下对象类型是 DiceRoller 类的输出,可用作 typescript 用户的接口。

Roll Result Output

滚动结果返回的对象由以下类型组成。

RollBase

所有掷骰子的基类,基于 type 属性进行扩展。

PropertyTypeDescription
successbooleanWas the roll a success, for target number rolls. Example: 3d6 > 3
typeRollTypeThe type of roll that this object represents.
validbooleanIs the roll still valid, and included in calculations.
valuenumberThe rolled or calculated value of this roll.
labelstringThe display label for this roll. This property is optional.
ordernumberA property used to maintain ordering of dice rolls within groups.

RollType

有效类型的 roll 的枚举。 可能的值是:

GroupedRoll

为骰子组扩展的中间接口。 此接口扩展 RollBase

PropertyTypeDescription
diceArray<RollBase>The rolls included as part of this group.

DiceExpressionRoll

骰子表达式的表示。 此接口扩展 GroupedRoll

示例

2d20 + 6d6

PropertyTypeDescription
type"diceexpressionroll"The type of roll that this object represents.
opsArray<DiceGroupMathOperation>The operations to perform on the rolls.

ExpressionRoll

数学表达式的表示。 此接口扩展 GroupedRoll

示例

20 * 17

PropertyTypeDescription
type"expressionroll"The type of roll that this object represents.
opsArray<MathOperation>The operations to perform on the rolls.

MathFunctionRoll

数学函数的表示。 此接口扩展 RollBase

Example

floor(20 / 17)

PropertyTypeDescription
type"expressionfunc"The type of roll that this object represents.
opMathFunctionThe operations to perform on the rolls.
exprRollBaseThe expression that the function is applied upon.

GroupRoll

一组卷的表示

Example

{4d6,3d6}。 此接口扩展 GroupedRoll

PropertyTypeDescription
type"grouproll"The type of roll that this object represents.

DiceRollResult

一组骰子的滚动结果。 此接口扩展 RollBase

示例

6d20

PropertyTypeDescription
dieRollBaseThe die this result represents.
type"die"The type of roll that this object represents.
rollsDieRollBase[]Each roll of the die.
countRollBaseThe number of rolls of the die.
matchedbooleanWhether this is a match result.

DieRollBase

为单个掷骰子扩展的中间接口(见下文)。 此接口扩展 RollBase

PropertyTypeDescription
rollnumberThe rolled result of the die.
matchedbooleanWhether this roll is a match.

DieRoll

在普通模具上滚动。 此接口扩展 DieRollBase

示例

d20 掷骰子

PropertyTypeDescription
dienumberThe die number to be rolled.
type"roll"The type of roll that this object represents.
criticalCriticalTypeIf this role is a critical success or failure (for rendering).

FateDieRoll

。 此接口扩展 DieRollBase

示例

dF

PropertyTypeDescription
type"fateroll"The type of roll that this object represents.

Parsed Roll Output

以下接口由库公开为已解析输入字符串的重新表示。 parse 方法的响应是一个 RootType 对象,可以是扩展它的任何接口。

ParsedObjectType

有效类型的 roll 的枚举。 可能的值是:

ParsedType

这是所有解析类型的基本接口。

PropertyTypeDescription
typestringThe type of parsed item this object represents.

RootType

这是解析类型子集的基本接口,只有那些可以是根类型的类型。 该对象扩展了 ParsedType 接口。

PropertyTypeDescription
label?stringThe text label attached to this roll. This property is optional.
rootbooleanA boolean flag to indicate if this is the root of the parse tree.

NumberType

此对象表示输入中的单个数字。 此对象扩展了 RootType 接口。

PropertyTypeDescription
type"number"The type of parsed item this object represents.
valuenumberThe value of the number.

InlineExpression

此对象表示字符串中的内联骰子表达式,用双方括号括起来。 此对象扩展了 RootType 接口。

示例

我想掷 [[2d20]] 骰子

PropertyTypeDescription
type"inline"The type of parsed item this object represents.
exprExpressionThe expression that was parsed as the inline string.

AnyRoll

表示任何有效掷骰的组合类型。 这是以下类型的组合:

ModGroupedRoll

该对象表示带有可选修饰符的分组卷。 此对象扩展了 RootType 接口。

示例

{4d6+3d8}kh1

PropertyTypeDescription
modsArray<KeepDropModType,SuccessFailureModType>The modifiers to be applied to the grouped roll.

SuccessFailureCritModType

表示成功测试修饰符的对象。 该对象扩展了 ParsedType 接口。 "success""failure" 修饰符将结果转换为成功类型结果,返回满足目标的掷骰数。 “crit”“critfail” 修饰符测试掷骰是否应显示为关键成功或关键失败。

示例

成功:3d6>3 失败:3d6f<3

PropertyTypeDescription
type"success", "failure", "crit", "critfail"The type of parsed item this object represents.
modCompareOperationThe check type to use for the condition.
exprRollExpressionAn expression representing the success condition.

SuccessFailureModType

等同于 SuccessFailureCritModType 但仅支持“成功”和“失败”修饰符。 此对象扩展了 SuccessFailureCritModType 接口。

示例

成功:3d6>3 失败:3d6f<3

PropertyTypeDescription
type"success", "failure"The type of parsed item this object represents.
modCompareOperationThe check type to use for the condition.
exprRollExpressionAn expression representing the success condition.

MatchModType

表示匹配类型修饰符的对象,用于修改 roll20 中骰子输出的显示。 该对象扩展了 ParsedType 接口。

示例

2d6m

mt 扩展一起使用时,将返回找到的匹配项数。

示例

20d6mt

可以指定额外的参数来增加所需的匹配数或向匹配添加约束。

示例

20d6mt3 计算 3 个项目的匹配项

示例

20d6m>3 仅计算滚动值为 >; 的匹配项 3

PropertyTypeDescription
type"match"The type of parsed item this object represents.
minNumberTypeThe minimum number of matches to accept. This property defaults to 2 as a NumberType.
countbooleanWhether or not to count the matches.
mod?CompareOperationThe check type to use for the match condition, if specified. This field is optional.
expr?RollExpressionAn expression representing the match condition, if specified. This field is optional.

KeepDropModType

表示保留或丢弃修饰符的对象,指定要保留或丢弃的骰子掷骰数,最高或最低掷骰数。 该对象扩展了 ParsedType 接口。

示例

保留:2d20kh1 Drop: 2d20dl1

PropertyTypeDescription
type"keep", "drop"The type of parsed item this object represents.
highlowHighLowTypeWhether to keep/drop the highest or lowest roll.
exprRollExpressionAn expression representing the number of rolls to keep/drop. This property defaults to 1 as a NumberType. Example: 2d6

GroupedRoll

这个对象代表一组组合的卷,带有可选的修饰符。 此对象扩展了 ModGroupedRoll 接口。

示例

{2d6,3d6}

PropertyTypeDescription
type"group"The type of parsed item this object represents.
rollsArray<RollExpression>The group of rolls included in this group.

RollExpressionType

表示包含复杂滚动和组的滚动表达式的对象,只允许进行加法运算。 此对象扩展了 RootType 接口。

示例

{2d6,3d6}kh1 + {3d6 + 2d6}kh2

PropertyTypeDescription
headRollOrExpressionThe initial roll or expression for the roll expression.
type"diceExpression"The type of parsed item this object represents.
opsArray<MathType<RollOrExpression,DiceGroupMathOperation>>The operations to apply to the initial roll or expression.

RollExpression

复杂滚动表达式、滚动或数学表达式的辅助类型组合。 表示以下类型:

RollOrExpression

roll 或数学表达式的辅助类型组合。 表示以下类型:

FullRoll

表示掷骰子的对象,包括掷骰子和任何修改器。 该对象扩展了 DiceRoll 接口。

示例

2d6kh1

PropertyTypeDescription
mods?Array<ReRollMod,KeepDropModType>Any modifiers attached to the roll. This property is optional.
targets?Array<SuccessFailureCritModType>Any success or failure targets for the roll. This property is optional.
match?MatchModTypAny match modifiers for the roll. This property is optional.
sort?SortRollTypeAny sort operations to apply to the roll. This property is optional.

SortRollType

应用于滚动的排序操作。 该对象扩展了 ParsedType 接口。

示例

10d6sa

PropertyTypeDescription
type"sort"The type of parsed item this object represents.
ascbooleanWhether to sort ascending or descending.

ReRollMod

表示应用于掷骰的重新掷骰操作的对象。 可以是以下类型之一:

  • "explode": re-rolls any dice that meet the target, continuing if the new roll matches
  • "compound": re-rolls any dice that meet the target, continuing if the new roll matches and adding the results into a single roll
  • "penetrate": re-rolls any dice that meet the target subtracting 1 from the new value, continuing if the new roll matches
  • "reroll": re-rolls a die as long as it meets the target, keeping the final roll
  • "rerollOnce": re-rolls a die once if it meets the target, keeping the new roll

Example

2d6!

PropertyTypeDescription
type"explode", "compound", "penetrate", "reroll", "rerollOnce"The type of parsed item this object represents.
targetTargetModThe target modifier to compare the roll value against.

TargetMod

一个对象 represt 一个目标修饰符以应用于一个 roll。 该对象扩展了 ParsedType 接口。

PropertyTypeDescription
type"target"The type of parsed item this object represents.
modCompareOperationThe check type to use for the condition.
valueRollExprAn expression representing the target condition value.

DiceRoll

掷骰子的表示。 此对象扩展了 RootType 接口。

示例

2d6

PropertyTypeDescription
dieRollExpr,FateExprThe die value to roll against, can be a fate die, a number or a complex roll expression.
countRollExprThe number of time to roll this die.
type"die"The type of parsed item this object represents.

FateExpr

命运骰子的表示。 该对象扩展了 ParsedType 接口。

示例

2dF

PropertyTypeDescription
type"fate"The type of parsed item this object represents.

RollExpr

不是表达式的数字或值的辅助类型组合。 表示以下类型:

Expression

表达式类型的辅助类型组合。 表示以下类型:

MathExpression

两个或多个骰子掷骰之间的数学类型表达式。 此对象扩展了 RootType 接口。

示例

2d6 + 3d6 * 4d6

PropertyTypeDescription
headAnyRollThe initial roll to perform operations against.
type"expression"The type of parsed item this object represents.
opsArray<MathType<AnyRoll>>The operations to apply to the initial roll.

MathType

表示要应用的滚动数学运算的对象及其应用的值。 该对象扩展了 ParsedType 接口。 此对象的接口采用模板化类型 TailType,它指定操作中使用的第二个值的类型。 还有第二个模板化类型 OpValues,它指定可以使用的操作类型。 这默认为 Array<MathOperation>`。

示例

+ 3d6(作为 2d6 + 3d6 的一部分)

PropertyTypeDescription
type"math"The type of parsed item this object represents.
opOpValuesThe math operation to perform.
tailTailTypeThe second value to use in the operation.

MathFunctionExpression

表示要应用的数学函数的对象以及要应用它的表达式。 此对象扩展了 RootType 接口。

示例

floor(3d6 / 2d4)

PropertyTypeDescription
type"mathfunction"The type of parsed item this object represents.
opMathFunctionThe function to be applied.
exprAnyRollThe expression to apply the function on.

Helper Types

以下是上述接口使用的支持类型。

DiceGroupMathOperation

表示一组骰子上数学运算的有效运算的辅助类型。

<代码>"+" | “-”

MathOperation

表示数学运算的有效运算的辅助类型。

<代码>"+" | “-” | “*” | “/” | “%” | "**"

MathFunction

表示数学运算的有效运算的辅助类型。

<代码>“地板” | “天花板” | “圆” | “abs”

CriticalType

将掷骰标记为关键成功或失败时使用的辅助类型。

<代码>“成功” | “失败” | null

CompareOperation

比较点的可用操作的辅助类型。

<代码>">" | “<” | "="

HighLowType

一种辅助类型,用于确定要保留或丢弃哪些卷。

<代码>“h” | “我” | 空

Dice Roller & Parser

This dice roller is a string parser that returns an object containing the component parts of the dice roll. It supports the full Roll20 Dice Specification. It uses a pegjs grammar to create a representation of the dice roll format. This can then be converted into a simple number value, or to a complex object used to display the full roll details.

Credit

This is a fork of Ben Morton's dice_roller project. It had a few bugs so I'm republishing with fixes and features needed for my projects.

Quickstart

Install the library using:

npm install dice-roller-parser

Once installed, simply load the library, either in the browser:

<script src="node_modules/dice-roller-parser/dist/index.js"></script>

Or in node:

import { DiceRoller } from "dice-roller-parser";

Then create a new instance of the DiceRoller class, and use it to perform some dice rolls.

const diceRoller = new DiceRoller();

//    Returns the total rolled value
const roll = diceRoller.rollValue("2d20kh1");
console.log(roll);

//    Returns an object representing the dice roll, use to display the component parts of the roll
const rollObject = diceRoller.roll("2d20kh1");
console.log(rollObject.value);

Usage

This library exposes two classes, a DiceRoller and a DiscordRollRenderer.

DiceRoller

The DiceRoller class manages parsing of a dice string and performing rolls based upon the result.

//    Creates a new instance of the DiceRoller class
const roller = new DiceRoller();

Constructor options

The default constructor uses Math.random and applies a maximum number of rolls per die of 1000. These can be specified using the following constructor overloads.

DiceRoller(GeneratorFunction)

You can specify a function to be used as the random number generator by the dice roller. This function should be of the type () => number and return a number between 0 and 1. By default, it uses the built-in Math.random method.

//    Default constructor using Math.random
const roller = new DiceRoller();

//    Uses a custom random generator that always returns 0.5
const roller = new DiceRoller(() => 0.5);

This can be read or modified using the randFunction property.

roller.randFunction = () => 0.5;
DiceRoller(GeneratorFunction, MaxRollsPerDie)

To prevent attempting to parse very large numbers of die rolls, a maximum number of rolls for a die can be specified. The default value is set to 1000.

//    Uses the default constructor with a limit of 100 rolls per die
const roller = new DiceRoller(null, 100);

//    Uses a custom random generator that always returns 0.5, and a limit of 10 rolls per die
const roller = new DiceRoller(() => 0.5, 10);

This can be read or modified using the maxRollCount property.

roller.maxRollCount = 75;

Class Usage

Once the DiceRoller class has been constructed, there are three options for performing a dice roll:

  • Getting a roll result directly
  • Generate an object to represent the dice roll
  • Just parse the input and add your own rolling logic
Getting a direct roll result

The rollValue method takes a dice string input, parses it, performs a roll and returns the calculated number value result.

//    Rolls 2 d20 dice and keeps the value of the highest
const roll = roller.rollValue("2d20kh1");

//    Prints out the numeric value result
console.log(roll);
Generate an object representing the dice roll

The roll method takes a dice string input, parses it, performs a roll and then returns an object that represents the roll. Using the roll objects, you can build your own roll display functionality, rather than just outputting the final value.

//    Rolls 2 d20 dice and keeps the value of the highest
const roll = roller.roll("2d20kh1");

//    Print out the full roll breakdown
printDiceRoll(roll);
//    Prints out the numeric value result
console.log(`Final roll value: ${roll.Value}`);

See the roll result output in the output types section below for more details on the returned object.

Just parse the value

The parse method takes a dice string input, parses it and returns a representation of the parsed input. This can either be used to perform a dice roll or re-construct the original input. The rollParsed method takes this parsed result as an input, performs the roll and returns the same output as from the roll method.

//    Rolls 2 d20 dice and keeps the value of the highest
const parsedInput = roller.parse("2d20kh1");

//    Print out a re-constructed input string
printParsedInput(parsedInput);

//    Run the roller on the parsed object
const roll = roller.rollParsed(parsedInput);

//    Print out the full roll breakdown
printDiceRoll(roll);
//    Print out the numeric value result
console.log(`Final roll value: ${roll.Value}`);

See the parsed roll output in the output types section below for more details on the returned object.

DiscordRollRenderer

The DiscordRollRenderer class is an example renderer class that takes a rolled input represented by a RollBase object and renders it to a string in a markdown format, compatible with Discord.

//    Creates a new instance of the DiceRoller class
const renderer = new DiscordRollRenderer();

Class Usage

The DiscordRollRenderer exposes a single render method with a single parameter, the RollBase object to render, and returns the rendered string.

//    Rolls 2 d20 dice and keeps the value of the highest
const roll = roller.rollValue("2d20kh1");

//    Get the formatted string
const render = renderer.render(roll);
console.log(render);

Development

To develop this library, simply clone the repository, run an install:

npm install

Then do a build:

npm run build

This does four things:

# Clean any existing builds
npm run clean

# Build the dice grammer
npx pegjs src/diceroll.pegjs

# Run tslint against the project
tslint -c tslint.json --project tsconfig.json

# Then run webpack to build and package everything up nicely
webpack

To run the test suite, use:

npm run test

That's all there is to it!

Output Types

The following object types are output from the DiceRoller class, and are available as interfaces for typescript users.

Roll Result Output

The object returned by a roll result is made up of the following types.

RollBase

The base class for all die rolls, extended based upon the type property.

PropertyTypeDescription
successbooleanWas the roll a success, for target number rolls. Example: 3d6 > 3
typeRollTypeThe type of roll that this object represents.
validbooleanIs the roll still valid, and included in calculations.
valuenumberThe rolled or calculated value of this roll.
labelstringThe display label for this roll. This property is optional.
ordernumberA property used to maintain ordering of dice rolls within groups.

RollType

An enum of the valid types of roll. The possible values are:

GroupedRoll

An intermediate interface extended for groups of dice. This interface extends RollBase.

PropertyTypeDescription
diceArray<RollBase>The rolls included as part of this group.

DiceExpressionRoll

A representation of a dice expression. This interface extends GroupedRoll.

Example

2d20 + 6d6

PropertyTypeDescription
type"diceexpressionroll"The type of roll that this object represents.
opsArray<DiceGroupMathOperation>The operations to perform on the rolls.

ExpressionRoll

A representation of a mathematic expression. This interface extends GroupedRoll.

Example

20 * 17

PropertyTypeDescription
type"expressionroll"The type of roll that this object represents.
opsArray<MathOperation>The operations to perform on the rolls.

MathFunctionRoll

A representation of a mathematic function. This interface extends RollBase.

Example

floor(20 / 17)

PropertyTypeDescription
type"expressionfunc"The type of roll that this object represents.
opMathFunctionThe operations to perform on the rolls.
exprRollBaseThe expression that the function is applied upon.

GroupRoll

A representation of a group of rolls

Example

{4d6,3d6}. This interface extends GroupedRoll.

PropertyTypeDescription
type"grouproll"The type of roll that this object represents.

DiceRollResult

The rolled result of a group of dice. This interface extends RollBase.

Example

6d20

PropertyTypeDescription
dieRollBaseThe die this result represents.
type"die"The type of roll that this object represents.
rollsDieRollBase[]Each roll of the die.
countRollBaseThe number of rolls of the die.
matchedbooleanWhether this is a match result.

DieRollBase

An intermediate interface extended for individual die rolls (see below). This interface extends RollBase.

PropertyTypeDescription
rollnumberThe rolled result of the die.
matchedbooleanWhether this roll is a match.

DieRoll

A roll on a regular die. This interface extends DieRollBase.

Example

d20

PropertyTypeDescription
dienumberThe die number to be rolled.
type"roll"The type of roll that this object represents.
criticalCriticalTypeIf this role is a critical success or failure (for rendering).

FateDieRoll

A roll on a fate die. This interface extends DieRollBase.

Example

dF

PropertyTypeDescription
type"fateroll"The type of roll that this object represents.

Parsed Roll Output

The following interfaces are exposed by the library as a reresentation of the parsed input string. The response from the parse method is a RootType object and could be any of the interfaces that extend it.

ParsedObjectType

An enum of the valid types of roll. The possible values are:

ParsedType

This is the base interface for all parsed types.

PropertyTypeDescription
typestringThe type of parsed item this object represents.

RootType

This is the base interface for a subset of parsed types, only those that can be the root type. This object extends the ParsedType interface.

PropertyTypeDescription
label?stringThe text label attached to this roll. This property is optional.
rootbooleanA boolean flag to indicate if this is the root of the parse tree.

NumberType

This object represents a single number in the input. This object extends the RootType interface.

PropertyTypeDescription
type"number"The type of parsed item this object represents.
valuenumberThe value of the number.

InlineExpression

This object represents an inline dice expression within a string, wrapped in double square brackets. This object extends the RootType interface.

Example

I want to roll [[2d20]] dice

PropertyTypeDescription
type"inline"The type of parsed item this object represents.
exprExpressionThe expression that was parsed as the inline string.

AnyRoll

A combined type representing any valid roll. This is a combination of the following types:

ModGroupedRoll

This object represents a grouped roll with an optional modifier. This object extends the RootType interface.

Example

{4d6+3d8}kh1

PropertyTypeDescription
modsArray<KeepDropModType,SuccessFailureModType>The modifiers to be applied to the grouped roll.

SuccessFailureCritModType

An object representing a success test modifier. This object extends the ParsedType interface. A "success" or "failure" modifier converts the result into a success type result which returns the number of rolls that meet the target. A "crit" or "critfail" modifier tests the roll for whether or not the roll should be displayed as a critical success or critical failure.

Example

Success: 3d6>3 Failure: 3d6f<3

PropertyTypeDescription
type"success", "failure", "crit", "critfail"The type of parsed item this object represents.
modCompareOperationThe check type to use for the condition.
exprRollExpressionAn expression representing the success condition.

SuccessFailureModType

Equivalent to the SuccessFailureCritModType but only supporting "success" and "failure" modifiers. This object extends the SuccessFailureCritModType interface.

Example

Success: 3d6>3 Failure: 3d6f<3

PropertyTypeDescription
type"success", "failure"The type of parsed item this object represents.
modCompareOperationThe check type to use for the condition.
exprRollExpressionAn expression representing the success condition.

MatchModType

An object representing a match type modifier, used to modify the display of dice output in roll20. This object extends the ParsedType interface.

Example

2d6m

When used with the mt extension, will return the number of matches found.

Example

20d6mt

Additional arguments can be specified that increase the required number of matches or to add a constraint to matches.

Example

20d6mt3 counts matches of 3 items

Example

20d6m>3 Only counts matches where the rolled value is > 3

PropertyTypeDescription
type"match"The type of parsed item this object represents.
minNumberTypeThe minimum number of matches to accept. This property defaults to 2 as a NumberType.
countbooleanWhether or not to count the matches.
mod?CompareOperationThe check type to use for the match condition, if specified. This field is optional.
expr?RollExpressionAn expression representing the match condition, if specified. This field is optional.

KeepDropModType

An object representing a keep or drop modifier, specifying a number of dice rolls to keep or drop, either the highest or lowest rolls. This object extends the ParsedType interface.

Example

Keep: 2d20kh1 Drop: 2d20dl1

PropertyTypeDescription
type"keep", "drop"The type of parsed item this object represents.
highlowHighLowTypeWhether to keep/drop the highest or lowest roll.
exprRollExpressionAn expression representing the number of rolls to keep/drop. This property defaults to 1 as a NumberType. Example: 2d6

GroupedRoll

This object represents a group of rolls combined, with optional modifiers. This object extends the ModGroupedRoll interface.

Example

{2d6,3d6}

PropertyTypeDescription
type"group"The type of parsed item this object represents.
rollsArray<RollExpression>The group of rolls included in this group.

RollExpressionType

An object representing a roll expression including complex rolls and groups, only allows addition operations. This object extends the RootType interface.

Example

{2d6,3d6}kh1 + {3d6 + 2d6}kh2

PropertyTypeDescription
headRollOrExpressionThe initial roll or expression for the roll expression.
type"diceExpression"The type of parsed item this object represents.
opsArray<MathType<RollOrExpression,DiceGroupMathOperation>>The operations to apply to the initial roll or expression.

RollExpression

A helper type combination of a complex roll expression, a roll, or a math expression. Represents the following types:

RollOrExpression

A helper type combination of a roll, or a math expression. Represents the following types:

FullRoll

An object representing a roll including the dice roll, and any modifiers. This object extends the DiceRoll interface.

Example

2d6kh1

PropertyTypeDescription
mods?Array<ReRollMod,KeepDropModType>Any modifiers attached to the roll. This property is optional.
targets?Array<SuccessFailureCritModType>Any success or failure targets for the roll. This property is optional.
match?MatchModTypAny match modifiers for the roll. This property is optional.
sort?SortRollTypeAny sort operations to apply to the roll. This property is optional.

SortRollType

A sort operation to apply to a roll. This object extends the ParsedType interface.

Example

10d6sa

PropertyTypeDescription
type"sort"The type of parsed item this object represents.
ascbooleanWhether to sort ascending or descending.

ReRollMod

An object representing a re-roll operation to apply to a roll. Can be one of the following types:

  • "explode": re-rolls any dice that meet the target, continuing if the new roll matches
  • "compound": re-rolls any dice that meet the target, continuing if the new roll matches and adding the results into a single roll
  • "penetrate": re-rolls any dice that meet the target subtracting 1 from the new value, continuing if the new roll matches
  • "reroll": re-rolls a die as long as it meets the target, keeping the final roll
  • "rerollOnce": re-rolls a die once if it meets the target, keeping the new roll

Example

2d6!

PropertyTypeDescription
type"explode", "compound", "penetrate", "reroll", "rerollOnce"The type of parsed item this object represents.
targetTargetModThe target modifier to compare the roll value against.

TargetMod

An object represting a target modifier to apply to a roll. This object extends the ParsedType interface.

PropertyTypeDescription
type"target"The type of parsed item this object represents.
modCompareOperationThe check type to use for the condition.
valueRollExprAn expression representing the target condition value.

DiceRoll

The representation of a die roll. This object extends the RootType interface.

Example

2d6

PropertyTypeDescription
dieRollExpr,FateExprThe die value to roll against, can be a fate die, a number or a complex roll expression.
countRollExprThe number of time to roll this die.
type"die"The type of parsed item this object represents.

FateExpr

The representation of a fate die roll. This object extends the ParsedType interface.

Example

2dF

PropertyTypeDescription
type"fate"The type of parsed item this object represents.

RollExpr

A helper type combination of a number or value that is not an expression. Represents the following types:

Expression

A helper type combination of expression types. Represents the following types:

MathExpression

A math type expression between two or more dice rolls. This object extends the RootType interface.

Example

2d6 + 3d6 * 4d6

PropertyTypeDescription
headAnyRollThe initial roll to perform operations against.
type"expression"The type of parsed item this object represents.
opsArray<MathType<AnyRoll>>The operations to apply to the initial roll.

MathType

An object representating an roll math operation to be applied and the value to apply it to. This object extends the ParsedType interface. The interface for this object takes a templated type TailType which specifies the type of the second value used in the operation. There is a second templated type OpValues which specifies the type of operations that can be used. This defaults to Array<MathOperation>`.

Example

+ 3d6 (as part of 2d6 + 3d6)

PropertyTypeDescription
type"math"The type of parsed item this object represents.
opOpValuesThe math operation to perform.
tailTailTypeThe second value to use in the operation.

MathFunctionExpression

An object representing a math function to be applied and the expression to apply it to. This object extends the RootType interface.

Example

floor(3d6 / 2d4)

PropertyTypeDescription
type"mathfunction"The type of parsed item this object represents.
opMathFunctionThe function to be applied.
exprAnyRollThe expression to apply the function on.

Helper Types

The following are support types used by the above interfaces.

DiceGroupMathOperation

A helper type representing the valid operations for a math operation on a group of dice.

"+" | "-"

MathOperation

A helper type representing the valid operations for a math operation.

"+" | "-" | "*" | "/" | "%" | "**"

MathFunction

A helper type representing the valid operations for a math operation.

"floor" | "ceil" | "round" | "abs"

CriticalType

A helper type used when marking a roll as a critical success or failure.

"success" | "failure" | null

CompareOperation

A helper type for the available operations for a comparison point.

">" | "<" | "="

HighLowType

A helper type used to determine which rolls to keep or drop.

"h" | "l" | null

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文