[GH-ISSUE #8489] [Issue]: Unify dice rolling and notation parsing services #8375

Closed
opened 2026-11-07 16:42:33 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @SpicyMarinara on GitHub (Oct 30, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/8489

Originally assigned to: @kolacheee on GitHub.

Summary

Dice rolling and notation parsing are currently distributed across multiple generators, RNG implementations, and parsers. Consolidating them into a shared service could improve maintainability, consistency, and support for extension-defined dice formats such as those needed by a Genesys extension.

Expected behavior

A shared dice service should consistently handle supported dice rolls and parsing across the application, while allowing extensions to register or provide custom dice formats and rolling behavior.

Actual behavior

Dice rolls currently use multiple implementations, including Math.random(), crypto.randomInt, seeded mulberry32, inline random logic, and macro-specific behavior. Dice notation is handled by at least seven separate parsers or formatters, resulting in duplicated and potentially inconsistent behavior.

Steps to reproduce

  1. Review the dice-related code paths under packages/.
  2. Compare roll implementations used by composer dice, combat, encounters, skill checks, multiplayer commands, tools, ruleset combat, markets, loot, and macros.
  3. Compare the separate parsers and formatters for standard notation, roll macros, placeholders, narration, animated dice, ruleset schemas, and live state.
  4. Attempt to implement an extension with a custom dice format, such as a Genesys system extension, and determine how many separate rolling and parsing integrations are required.

Environment

  • Marinara Engine version: Not provided
  • Install type: Not provided
  • OS + version: Not provided
  • Browser or app shell: Not provided

Logs, screenshots, or video

No logs, screenshots, or video provided. The Discord context includes a code-path inventory for the current dice rolling and parsing implementations.

Additional context

Current rolling mechanisms include rollParsedDice using Math.random(), an independent skill-check d20 implementation, inline Math.random() calls, crypto.randomInt through rollDieSecurely, seeded mulberry32 for ruleset combat, and macro-specific seeded or Math.random() behavior. The reported paths include shared/src/utils/dice-notation.ts, shared/src/utils/macro-engine.ts, server/src/services/game/, server/src/services/multiplayer/player-commands.ts, server/src/services/tools/tool-executor.ts, shared/src/features/ruleset-combat/, and related client parsers and formatters. The request is an architectural feature proposal rather than a report of a specific runtime error.

Discord thread: Unified Dice Rolling Service
Discord thread link: https://discord.com/channels/1417099416812392641/1565533348716089424
Requested by: ylliselmanii (ylliselmanii, Discord ID 177503556272783360)

Discord thread transcript (4 messages)
[2026-10-30T01:10:36.537Z] TotallyNotTheCIASurroundsIt: From taking a look at the code, it looks like there are multiple generators and places dice are rolled, in addition to multiple parsers. Is there a reason it's done like this today, and if no, would the dev team be open to PR's being done which unified those rolls and parsers into a single service which consistently handled dice rolls and allowed for extensions to provide their own dice formats? 

This would improve maintainability and extensibility. This came up when I was looking at making a personal extension for the Genesys system, and is something I would be able to implement. Is the proposed change to a unified rolling service the dev-team would be willing to accept, or is it fine the way it is currently?


**How dice are rolled today** (paths under `packages/`)

**1. `Math.random()` via `rollParsedDice`**: `shared/src/utils/dice-notation.ts:156`
- Composer dice: `POST /game/dice/roll` (`server/src/routes/game.routes.ts:10003`) → `rollDice` (`server/src/services/game/dice.service.ts:41`)
- Legacy combat: `server/src/services/game/combat.service.ts:453, 478, 483, 497`
- Encounters: `server/src/services/game/encounter.service.ts:153, 175`
- `/roll` slash command: `client/src/lib/slash-commands.ts:708`
- Multiplayer commands: `server/src/services/multiplayer/player-commands.ts:17`
- `roll_dice` tool: `server/src/services/tools/tool-executor.ts:588`

**2. `Math.random()`, its own d20**: `server/src/services/game/skill-check.service.ts:63`
- Skill checks when no `rollD20` is passed

**3. `Math.random()` inline**
- Combat AI target picks: `server/src/services/game/combat.service.ts:436–917`
- Encounter tables: `server/src/services/game/encounter.service.ts:115, 121`

**4. `crypto.randomInt` via `rollDieSecurely`**: `server/src/services/game/dice-rng.ts:23`
(files in `server/src/services/game/` unless noted)
- Dice pool: `dice-pool.service.ts:266`
- One-request dice: `one-request-dice.ts:177`
- Skill-check resolution: `skill-check-resolution.service.ts:1112`
- Multiplayer commands: `server/src/services/multiplayer/player-commands.ts:17`
- `roll_dice` tool: `server/src/services/tools/tool-executor.ts:588`

**5. Seeded `mulberry32`**: `shared/src/features/tactical-combat/rng.ts:13`, wrapped by `rulesetCombatRoller` (`shared/src/features/ruleset-combat/dice.ts:12`)
- Ruleset fights: `server/src/services/game/ruleset-combat-director.service.ts:580`, `shared/src/features/ruleset-combat/encounter.ts:4185`
- Markets: `shared/src/features/rulesets/market.ts:211, 237`
- Item use, loot: `game-item-use.service.ts:77, 169`, `game-loot.service.ts:141`

**6. `{{roll:NdM}}` macro**: `shared/src/utils/macro-engine.ts:3457`
- Seeded hash when `randomSeed` is set, otherwise `Math.random()` (`macro-engine.ts:376`)

**Seeds are mixed**
- `Math.random()`: `server/src/routes/combat-director.routes.ts:780`, `server/src/services/turn-games/turn-game-runner.service.ts:265`
- `crypto.randomInt`: `server/src/routes/game.routes.ts:10957`, `server/src/routes/generate.routes.ts:9867`

**Seven notation parsers**
1. Main `DICE_NOTATION_REGEX` (`NdM+K`): `shared/src/utils/dice-notation.ts:21`
2. `{{roll:}}` macro (`NdM`, no modifier): `shared/src/utils/macro-engine.ts:3457`
3. Placeholder head: `shared/src/utils/dice-placeholder.ts:247`
4. Narration formatter (`[dice: NdM+K = T]`): `client/src/components/game/game-narration-format.ts:49`
5. Animated dice (sides only): `client/src/components/dice/AnimatedDiceRoll.tsx:33`
6. Ruleset schema (sides only): `shared/src/schemas/ruleset.schema.ts:4748, 14346, 15055, 16248`
7. Ruleset live state (bare `dM`): `shared/src/features/rulesets/live-state.ts:1793`
[Discord message: https://discord.com/channels/1417099416812392641/1565533348716089424/1565533348716089424]

[2026-10-30T01:10:37.537Z] Professor Mari [bot]: Howdy!

Thank you for submitting a new bug report/feature request, your support means a lot. A member of the team will look into it as soon as possible, and if they find it valid, they will open a GitHub Issue out of it. You can check whether it was addressed on the staging build of Marinara Engine. **Do not ping any of the developers, this will not speed up the process.**

**Please include a Support Diagnostic in the post if you haven't done it yet.** Otherwise, ignore this step. To grab it, go to the Marinara Engine tab:

`Settings -> Advanced -> Support Diagnostics -> Copy Support Diagnostics.`

If your report is about any of the **Agents**, please provide **the Agent's version** which you can grab from here:

`Agents -> Download Agents -> Installed Agents -> Selected Agent -> Installed Version`

Thank you, this will help tremendously.

This is an automated message, but if you reply to it, I will do my best to assist you using my LLM-powered brain. That said, it's best for a human to handle this case.

Cheers!
[Discord message: https://discord.com/channels/1417099416812392641/1565533348716089424/1565533352910524518]

[2026-10-30T01:57:31.390Z] ylliselmanii: Sure does seem like a mess
[Discord message: https://discord.com/channels/1417099416812392641/1565533348716089424/1565545155065552997]

[2026-10-30T01:59:25.705Z] ylliselmanii: I'd also suggest pulling as much as possible of the dice rolling stuff into a shared helper/service/whatever, as that seems like the standard case of ai rebuilding instead of reusing when not pointed correctly
[Discord message: https://discord.com/channels/1417099416812392641/1565533348716089424/1565545634537144392]

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @SpicyMarinara on GitHub (Oct 30, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/8489 Originally assigned to: @kolacheee on GitHub. ## Summary Dice rolling and notation parsing are currently distributed across multiple generators, RNG implementations, and parsers. Consolidating them into a shared service could improve maintainability, consistency, and support for extension-defined dice formats such as those needed by a Genesys extension. ## Expected behavior A shared dice service should consistently handle supported dice rolls and parsing across the application, while allowing extensions to register or provide custom dice formats and rolling behavior. ## Actual behavior Dice rolls currently use multiple implementations, including Math.random(), crypto.randomInt, seeded mulberry32, inline random logic, and macro-specific behavior. Dice notation is handled by at least seven separate parsers or formatters, resulting in duplicated and potentially inconsistent behavior. ## Steps to reproduce 1. Review the dice-related code paths under packages/. 2. Compare roll implementations used by composer dice, combat, encounters, skill checks, multiplayer commands, tools, ruleset combat, markets, loot, and macros. 3. Compare the separate parsers and formatters for standard notation, roll macros, placeholders, narration, animated dice, ruleset schemas, and live state. 4. Attempt to implement an extension with a custom dice format, such as a Genesys system extension, and determine how many separate rolling and parsing integrations are required. ## Environment - Marinara Engine version: Not provided - Install type: Not provided - OS + version: Not provided - Browser or app shell: Not provided ## Logs, screenshots, or video No logs, screenshots, or video provided. The Discord context includes a code-path inventory for the current dice rolling and parsing implementations. ## Additional context Current rolling mechanisms include rollParsedDice using Math.random(), an independent skill-check d20 implementation, inline Math.random() calls, crypto.randomInt through rollDieSecurely, seeded mulberry32 for ruleset combat, and macro-specific seeded or Math.random() behavior. The reported paths include shared/src/utils/dice-notation.ts, shared/src/utils/macro-engine.ts, server/src/services/game/, server/src/services/multiplayer/player-commands.ts, server/src/services/tools/tool-executor.ts, shared/src/features/ruleset-combat/, and related client parsers and formatters. The request is an architectural feature proposal rather than a report of a specific runtime error. Discord thread: Unified Dice Rolling Service Discord thread link: https://discord.com/channels/1417099416812392641/1565533348716089424 Requested by: ylliselmanii (ylliselmanii, Discord ID 177503556272783360) <details> <summary>Discord thread transcript (4 messages)</summary> ```text [2026-10-30T01:10:36.537Z] TotallyNotTheCIASurroundsIt: From taking a look at the code, it looks like there are multiple generators and places dice are rolled, in addition to multiple parsers. Is there a reason it's done like this today, and if no, would the dev team be open to PR's being done which unified those rolls and parsers into a single service which consistently handled dice rolls and allowed for extensions to provide their own dice formats? This would improve maintainability and extensibility. This came up when I was looking at making a personal extension for the Genesys system, and is something I would be able to implement. Is the proposed change to a unified rolling service the dev-team would be willing to accept, or is it fine the way it is currently? **How dice are rolled today** (paths under `packages/`) **1. `Math.random()` via `rollParsedDice`**: `shared/src/utils/dice-notation.ts:156` - Composer dice: `POST /game/dice/roll` (`server/src/routes/game.routes.ts:10003`) → `rollDice` (`server/src/services/game/dice.service.ts:41`) - Legacy combat: `server/src/services/game/combat.service.ts:453, 478, 483, 497` - Encounters: `server/src/services/game/encounter.service.ts:153, 175` - `/roll` slash command: `client/src/lib/slash-commands.ts:708` - Multiplayer commands: `server/src/services/multiplayer/player-commands.ts:17` - `roll_dice` tool: `server/src/services/tools/tool-executor.ts:588` **2. `Math.random()`, its own d20**: `server/src/services/game/skill-check.service.ts:63` - Skill checks when no `rollD20` is passed **3. `Math.random()` inline** - Combat AI target picks: `server/src/services/game/combat.service.ts:436–917` - Encounter tables: `server/src/services/game/encounter.service.ts:115, 121` **4. `crypto.randomInt` via `rollDieSecurely`**: `server/src/services/game/dice-rng.ts:23` (files in `server/src/services/game/` unless noted) - Dice pool: `dice-pool.service.ts:266` - One-request dice: `one-request-dice.ts:177` - Skill-check resolution: `skill-check-resolution.service.ts:1112` - Multiplayer commands: `server/src/services/multiplayer/player-commands.ts:17` - `roll_dice` tool: `server/src/services/tools/tool-executor.ts:588` **5. Seeded `mulberry32`**: `shared/src/features/tactical-combat/rng.ts:13`, wrapped by `rulesetCombatRoller` (`shared/src/features/ruleset-combat/dice.ts:12`) - Ruleset fights: `server/src/services/game/ruleset-combat-director.service.ts:580`, `shared/src/features/ruleset-combat/encounter.ts:4185` - Markets: `shared/src/features/rulesets/market.ts:211, 237` - Item use, loot: `game-item-use.service.ts:77, 169`, `game-loot.service.ts:141` **6. `{{roll:NdM}}` macro**: `shared/src/utils/macro-engine.ts:3457` - Seeded hash when `randomSeed` is set, otherwise `Math.random()` (`macro-engine.ts:376`) **Seeds are mixed** - `Math.random()`: `server/src/routes/combat-director.routes.ts:780`, `server/src/services/turn-games/turn-game-runner.service.ts:265` - `crypto.randomInt`: `server/src/routes/game.routes.ts:10957`, `server/src/routes/generate.routes.ts:9867` **Seven notation parsers** 1. Main `DICE_NOTATION_REGEX` (`NdM+K`): `shared/src/utils/dice-notation.ts:21` 2. `{{roll:}}` macro (`NdM`, no modifier): `shared/src/utils/macro-engine.ts:3457` 3. Placeholder head: `shared/src/utils/dice-placeholder.ts:247` 4. Narration formatter (`[dice: NdM+K = T]`): `client/src/components/game/game-narration-format.ts:49` 5. Animated dice (sides only): `client/src/components/dice/AnimatedDiceRoll.tsx:33` 6. Ruleset schema (sides only): `shared/src/schemas/ruleset.schema.ts:4748, 14346, 15055, 16248` 7. Ruleset live state (bare `dM`): `shared/src/features/rulesets/live-state.ts:1793` [Discord message: https://discord.com/channels/1417099416812392641/1565533348716089424/1565533348716089424] [2026-10-30T01:10:37.537Z] Professor Mari [bot]: Howdy! Thank you for submitting a new bug report/feature request, your support means a lot. A member of the team will look into it as soon as possible, and if they find it valid, they will open a GitHub Issue out of it. You can check whether it was addressed on the staging build of Marinara Engine. **Do not ping any of the developers, this will not speed up the process.** **Please include a Support Diagnostic in the post if you haven't done it yet.** Otherwise, ignore this step. To grab it, go to the Marinara Engine tab: `Settings -> Advanced -> Support Diagnostics -> Copy Support Diagnostics.` If your report is about any of the **Agents**, please provide **the Agent's version** which you can grab from here: `Agents -> Download Agents -> Installed Agents -> Selected Agent -> Installed Version` Thank you, this will help tremendously. This is an automated message, but if you reply to it, I will do my best to assist you using my LLM-powered brain. That said, it's best for a human to handle this case. Cheers! [Discord message: https://discord.com/channels/1417099416812392641/1565533348716089424/1565533352910524518] [2026-10-30T01:57:31.390Z] ylliselmanii: Sure does seem like a mess [Discord message: https://discord.com/channels/1417099416812392641/1565533348716089424/1565545155065552997] [2026-10-30T01:59:25.705Z] ylliselmanii: I'd also suggest pulling as much as possible of the dice rolling stuff into a shared helper/service/whatever, as that seems like the standard case of ai rebuilding instead of reusing when not pointed correctly [Discord message: https://discord.com/channels/1417099416812392641/1565533348716089424/1565545634537144392] ``` </details> ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-11-07 16:42:33 +07:00
Author
Owner

@kolacheee commented on GitHub (Oct 30, 2026):

Thanks for mapping all of this out. A shared dice service sounds good to us, and we'd be glad to see extension-defined formats. We have a large Game Mode / ruleset-combat branch in progress that touches several of the listed paths, so a few requests to keep the two from colliding:

  1. Keep ruleset fights seeded and replayable. rulesetCombatRoller(seed, cursor) is deliberate: a fight replays from its seed plus the choices made, and the combat director, item use, loot and markets rely on that cursor. A unified service should accept an injected roller (seeded stream or test sequence) rather than swap those paths to Math.random() or crypto.randomInt.
  2. Keep the secure roller where it is now. Dice pool, one-request dice and skill-check resolution use rollDieSecurely on purpose for player-facing rolls; please keep that behaviour, just routed through the shared service.
  3. Don't change the ruleset schema dice fields. The sides-only fields in ruleset.schema.ts and the bare dM reading in live-state.ts are part of the published ruleset API that installed rulesets depend on. Any new notation should be additive and behind a new API version, with old rulesets reading exactly as before.
  4. Land it in small steps if possible. Starting with the shared parser and RNG interface, then moving callers one area at a time, would make it much easier for us to rebase. If ruleset-combat (shared/src/features/ruleset-combat/, ruleset-combat-director.service.ts) could come last, or be left on its existing roller behind the new interface, that would help a lot.

Happy to review the ruleset-combat part when it's up.

<!-- gh-comment-id:6518263129 --> @kolacheee commented on GitHub (Oct 30, 2026): Thanks for mapping all of this out. A shared dice service sounds good to us, and we'd be glad to see extension-defined formats. We have a large Game Mode / ruleset-combat branch in progress that touches several of the listed paths, so a few requests to keep the two from colliding: 1. **Keep ruleset fights seeded and replayable.** `rulesetCombatRoller(seed, cursor)` is deliberate: a fight replays from its seed plus the choices made, and the combat director, item use, loot and markets rely on that cursor. A unified service should accept an injected roller (seeded stream or test sequence) rather than swap those paths to `Math.random()` or `crypto.randomInt`. 2. **Keep the secure roller where it is now.** Dice pool, one-request dice and skill-check resolution use `rollDieSecurely` on purpose for player-facing rolls; please keep that behaviour, just routed through the shared service. 3. **Don't change the ruleset schema dice fields.** The `sides`-only fields in `ruleset.schema.ts` and the bare `dM` reading in `live-state.ts` are part of the published ruleset API that installed rulesets depend on. Any new notation should be additive and behind a new API version, with old rulesets reading exactly as before. 4. **Land it in small steps if possible.** Starting with the shared parser and RNG interface, then moving callers one area at a time, would make it much easier for us to rebase. If ruleset-combat (`shared/src/features/ruleset-combat/`, `ruleset-combat-director.service.ts`) could come last, or be left on its existing roller behind the new interface, that would help a lot. Happy to review the ruleset-combat part when it's up.
Author
Owner

@kolacheee commented on GitHub (Oct 30, 2026):

#8495 covers the first part (one roller shape, secure plain rolls, {{roll:}} and the narration badge on the shared grammar). Before building the extension-format part, here's the plan, so the Genesys use case lands where it works best:

  1. Genesys dice already roll natively through a ruleset. A symbol-pool ruleset (Capability API 1.141, fights since #8220) declares its own dice, faces and symbols; the Engine throws them for checks and fights, cancels symbols and spends leftovers. So a Genesys system is best shipped as a ruleset, with no extension needed for the rules themselves.
  2. Next PR: named dice in the plain rollers. When the active game's ruleset is symbol-pool, /roll, composer dice and the roll_dice tool also accept its dice by name (for example 2 ability 1 proficiency vs 2 difficulty) and report the symbols, rolled with the shared secure roller. No ruleset schema change.
  3. Then: a small extension hook for custom notation. marinara.dice.registerFormat({ id, label, parse, roll }) for Personal Extensions, where roll receives the host's die roller so an extension can't choose its own faces. Personal Extensions run in the browser, so this covers /roll and the composer; server-side rolls (the GM's tool) stay on the shared grammar and ruleset dice. Does that boundary sound right to you?

Ruleset combat moves onto the shared roller interface last, keeping its seed + cursor stream.

<!-- gh-comment-id:6518663749 --> @kolacheee commented on GitHub (Oct 30, 2026): #8495 covers the first part (one roller shape, secure plain rolls, `{{roll:}}` and the narration badge on the shared grammar). Before building the extension-format part, here's the plan, so the Genesys use case lands where it works best: 1. **Genesys dice already roll natively through a ruleset.** A `symbol-pool` ruleset (Capability API 1.141, fights since #8220) declares its own dice, faces and symbols; the Engine throws them for checks and fights, cancels symbols and spends leftovers. So a Genesys *system* is best shipped as a ruleset, with no extension needed for the rules themselves. 2. **Next PR: named dice in the plain rollers.** When the active game's ruleset is `symbol-pool`, `/roll`, composer dice and the `roll_dice` tool also accept its dice by name (for example `2 ability 1 proficiency vs 2 difficulty`) and report the symbols, rolled with the shared secure roller. No ruleset schema change. 3. **Then: a small extension hook for custom notation.** `marinara.dice.registerFormat({ id, label, parse, roll })` for Personal Extensions, where `roll` receives the host's die roller so an extension can't choose its own faces. Personal Extensions run in the browser, so this covers `/roll` and the composer; server-side rolls (the GM's tool) stay on the shared grammar and ruleset dice. Does that boundary sound right to you? Ruleset combat moves onto the shared roller interface last, keeping its seed + cursor stream.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#8375
No description provided.