@3d-dice/dice-parser-interface

中文文档
0.2.1发布于 2023/02/08MITCommonJS近 30 天下载 3594

a parser interface that parses roll20 format strings using @3d-dice/dice-roller-parser for @3d-dice/dice-box

原始使用文档
文档目录10 节
  1. What it does
  2. How to use it
  3. Methods
  4. parseNotation ( string , default: '' )
  5. handleRerolls ( array , default: [] )
  6. parseFinalResults ( array , default: [] )
  7. Putting it all together
  8. Example HTML
  9. Example JavaScript
  10. Caveats

Dice Parser Interface

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.

FDP (Fantastic Dice Parcer) has been renamed to Dice Parser Interface for symantics. The @3d-dice/fdp npm module has been depricated

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/dice-parser-interface

Then create a new instace of dice-parser-interface

import DiceParser from '@3d-dice/dice-parser-interface'

const DP = new DiceParser()

The DP 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 = DP.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: DP.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:

PropertyTypeDescription
groupIdintThe group the reroll target belongs to
rollIdint or stringThe roll id of the die being rerolled. This will be incremented by .1 for every reroll made
sideintThe number of sides the reroll die has
qtyintThe 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 = DP.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/dice-ui

版本与发布记录

@3d-dice/dice-parser-interface 共发布 2 个版本,首次发布于 2022/05/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.1latest2023/02/083 年前
0.2.02022/05/274 年前

关于 @3d-dice/dice-parser-interface 的常见问题

@3d-dice/dice-parser-interface 是什么?

a parser interface that parses roll20 format strings using @3d-dice/dice-roller-parser for @3d-dice/dice-box

如何安装 @3d-dice/dice-parser-interface?

在项目目录运行 npm install @3d-dice/dice-parser-interface,或使用 pnpm add / yarn add / bun add。

@3d-dice/dice-parser-interface 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/3d-dice__dice-parser-interface 是否存在。

@3d-dice/dice-parser-interface 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@3d-dice/dice-parser-interface 可以商用吗?

@3d-dice/dice-parser-interface 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@3d-dice/dice-parser-interface 的下载量有多少?

根据 npm 官方下载统计,@3d-dice/dice-parser-interface 在 2026/10/05 – 2026/11/03 这 30 天内被下载 3,594 次,日均约 120 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@3d-dice/dice-parser-interface 还在维护吗?

最新版本 0.2.1 发布于 2023/02/08(3 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/05/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。