@yipe/dice 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Michael Margolis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,285 @@
1
+ <!-- [![npm version](https://img.shields.io/npm/v/@dpr/dice.svg)](https://www.npmjs.com/package/@dpr/dice)
2
+ [![Build Status](https://github.com/yipe/dpr/actions/workflows/ci.yml/badge.svg)](https://github.com/yipe/dpr/actions)
3
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE) -->
4
+
5
+ # 🎲 @dpr/dice
6
+
7
+ A TypeScript library for **D&D 5e damage-per-round (DPR) calculations**, designed for players, Dungeon Masters, and developers who want to analyze combat mathematically.
8
+
9
+ This library powers [dprcalc.com](https://dprcalc.com) and provides a precise, composable way to model dice rolls, attacks, and outcomes with probability mass functions (PMFs) — not just averages. This allows for rich charting and statistics with full outcome attribution.
10
+
11
+ ```ts
12
+ import { parse } from "@dpr/dice";
13
+
14
+ const attack = parse("(d20 + 8 AC 16) * (1d8 + 4) crit (2d8 + 4)");
15
+ console.log("DPR:", attack.mean());
16
+ ```
17
+
18
+ ## ✨ Features
19
+
20
+ - **D&D 5e Focused**: Designed around 5e rules (2014 and 2024).
21
+ - **Probability Mass Functions (PMF)**: Precise modeling of dice rolls and outcomes, not just averages.
22
+ - **Complex Attack Expressions**: Supports crit ranges, advantage/disadvantage, conditional damage, rerolls, minimum damage, and more.
23
+ - **Composable API**: Build dice expressions, run queries, and analyze results in just a few lines.
24
+ - **TypeScript First**: Full type safety and developer experience.
25
+
26
+ ## 🚀 Installation
27
+
28
+ (COMING SOON!)
29
+
30
+ ```bash
31
+ # Install with npm or yarn
32
+ npm install @dpr/dice
33
+ # or
34
+ yarn add @dpr/dice
35
+ ```
36
+
37
+ ## 📦 Core Concepts
38
+
39
+ | Concept | Description |
40
+ | ---------- | ---------------------------------------------------------------------------- |
41
+ | **PMF** | Probability Mass Function. The core mathematical representation of outcomes. |
42
+ | **Query** | Runs calculations and scenarios over one or more PMFs. |
43
+ | **Parser** | Parses text-based dice expressions like `(d20 + 8 AC 16) * (1d4 + 4)`. |
44
+ | **Dice** | Represents the outcome of a single dice expression |
45
+
46
+ ## 🧙 Basic Usage
47
+
48
+ Here's a simple example of calculating damage for a basic attack:
49
+
50
+ ```ts
51
+ import { parse, DiceQuery } from "@dpr/dice";
52
+
53
+ const query = parse("(d20 + 8 AC 16) * (1d4 + 4) crit (2d4 + 4)").toQuery();
54
+
55
+ console.log("Hit chance:", query.probAtLeastOne(["hit", "crit"]));
56
+ console.log("Crit chance:", query.probAtLeastOne(["crit"]));
57
+ console.log("DPR:", query.mean());
58
+ ```
59
+
60
+ **Output:**
61
+
62
+ ```
63
+ Hit chance: 0.65
64
+ Crit chance: 0.05
65
+ DPR: 4.35
66
+ ```
67
+
68
+ ## ⚔️ Sneak Attack Example
69
+
70
+ Conditional damage ("once-per-turn damage riders") like Sneak Attack can be modeled easily:
71
+
72
+ ```ts
73
+ import { parse, DiceQuery } from "@dpr/dice";
74
+
75
+ function damageRiderExample() {
76
+ const attack = parse("(d20 + 8 AC 16) * (1d4 + 4) crit (2d4 + 4)");
77
+ const sneakAttack = parse("3d6");
78
+ const sneakAttackCrit = parse("6d6");
79
+
80
+ // Determine the probability of a success (hit or crit) and a crit
81
+ const attacks = new DiceQuery([attack, attack]);
82
+ const [pSuccess, pCrit] = attacks.firstSuccessSplit(
83
+ ["hit", "crit"],
84
+ ["crit"]
85
+ );
86
+
87
+ // Calculate the expected damage of sneak attack in both scenarios
88
+ const sneak = PMF.exclusive([
89
+ [sneakAttack, pSuccess],
90
+ [sneakAttackCrit, pCrit],
91
+ ]);
92
+
93
+ // Create a full round with all of those damage outcomes
94
+
95
+ const fullRound = new DiceQuery([attack, attack, sneak]);
96
+ const DPR = fullRound.mean();
97
+
98
+ console.log("full round DPR", DPR);
99
+
100
+ // Congrats, you can now chart and query this round!
101
+ return fullRound;
102
+ }
103
+ ```
104
+
105
+ ## 📊 Statistics and Charts
106
+
107
+ You can generate full statistical distributions for visualization or reporting.
108
+
109
+ ```ts
110
+ import { parse, DiceQuery } from "@dpr/dice";
111
+
112
+ const query2 = parse("(d20 + 8 AC 16) * (1d4 + 4) crit (2d4 + 4)").toQuery();
113
+ console.table(query2.toChartSeries());
114
+ ```
115
+
116
+ **Output:**
117
+
118
+ ```
119
+ ┌─────────┬────┬──────────┐
120
+ │ (index) │ x │ y │
121
+ ├─────────┼────┼──────────┤
122
+ │ 0 │ 0 │ 0.35 │
123
+ │ 1 │ 5 │ 0.15 │
124
+ │ 2 │ 6 │ 0.153125 │
125
+ │ 3 │ 7 │ 0.15625 │
126
+ │ 4 │ 8 │ 0.159375 │
127
+ │ 5 │ 9 │ 0.0125 │
128
+ │ 6 │ 10 │ 0.009375 │
129
+ │ 7 │ 11 │ 0.00625 │
130
+ │ 8 │ 12 │ 0.003125 │
131
+ └─────────┴────┴──────────┘
132
+ ```
133
+
134
+ ## 🗂 Project Structure
135
+
136
+ | File | Purpose |
137
+ | ----------- | ------------------------------------- |
138
+ | `src/` | Core Dice class and logic |
139
+ | `examples/` | Example scripts showing library usage |
140
+ | `tests/` | Comprehensive library tests |
141
+
142
+ ## 🧪 Running Examples
143
+
144
+ This repository includes example scripts.
145
+
146
+ ```bash
147
+ yarn example basic
148
+ yarn example stats
149
+ yarn example sneakattack
150
+ yarn example misc
151
+ ```
152
+
153
+ Here is the basic example:
154
+
155
+ ```
156
+ % yarn example basic
157
+
158
+ ┌────────────┐
159
+ │ Summary │
160
+ ├────────────┴─────────────────────────────────────────────┐
161
+ │ Expression: (d20 + 5 AC 15) * (1d6+2) crit (2d6 + 2) │
162
+ │ Success Chance: 0.55 │
163
+ │ Expected DPR: 3.20 │
164
+ └──────────────────────────────────────────────────────────┘
165
+
166
+ ┌────────────┐
167
+ │ PMF () │
168
+ ├────────────┴───────────────────────────────────────────────────────────────────────────┐
169
+ │ 0: █████████████████████████████████████████████████████████████████████████ 45.00% │
170
+ │ 3: █████████████▌ 8.33% │
171
+ │ 4: █████████████▋ 8.47% │
172
+ │ 5: █████████████▉ 8.61% │
173
+ │ 6: ██████████████▏ 8.75% │
174
+ │ 7: ██████████████▍ 8.89% │
175
+ │ 8: ██████████████▋ 9.03% │
176
+ │ 9: █▎ 0.83% │
177
+ │ 10: █▏ 0.69% │
178
+ │ 11: ▉ 0.56% │
179
+ │ 12: ▋ 0.42% │
180
+ │ 13: ▍ 0.28% │
181
+ │ 14: ▏ 0.14% │
182
+ └────────────────────────────────────────────────────────────────────────────────────────┘
183
+
184
+ ┌──────────────────┐
185
+ │ CDF (): P(X ≤ x) │
186
+ ├──────────────────┴─────────────────────────────────────────────────────────────────────┐
187
+ │ 0: ████████████████████████████████▍ 45.00% │
188
+ │ 1: ████████████████████████████████▍ 45.00% │
189
+ │ 2: ████████████████████████████████▍ 45.00% │
190
+ │ 3: ██████████████████████████████████████▍ 53.33% │
191
+ │ 4: ████████████████████████████████████████████▌ 61.81% │
192
+ │ 5: ██████████████████████████████████████████████████▋ 70.42% │
193
+ │ 6: █████████████████████████████████████████████████████████▏ 79.17% │
194
+ │ 7: ███████████████████████████████████████████████████████████████▍ 88.06% │
195
+ │ 8: █████████████████████████████████████████████████████████████████████▉ 97.08% │
196
+ │ 9: ██████████████████████████████████████████████████████████████████████▌ 97.92% │
197
+ │ 10: ███████████████████████████████████████████████████████████████████████ 98.61% │
198
+ │ 11: ███████████████████████████████████████████████████████████████████████▍ 99.17% │
199
+ │ 12: ███████████████████████████████████████████████████████████████████████▋ 99.58% │
200
+ │ 13: ███████████████████████████████████████████████████████████████████████▉ 99.86% │
201
+ │ 14: ████████████████████████████████████████████████████████████████████████ 100.00% │
202
+ └────────────────────────────────────────────────────────────────────────────────────────┘
203
+
204
+ ┌──────────────────┐
205
+ │ Outcome Table () │
206
+ ├──────────────────┴───────────────────────────┐
207
+ │ DAMAGE │ PERCENT │ Crit % │ Hit % │ Miss % │
208
+ ├────────┼─────────┼────────┼────────┼─────────┤
209
+ │ 0 │ 45.000% │ 0.000% │ 0.000% │ 45.000% │
210
+ │ 3 │ 8.333% │ 0.000% │ 8.333% │ 0.000% │
211
+ │ 4 │ 8.472% │ 0.139% │ 8.333% │ 0.000% │
212
+ │ 5 │ 8.611% │ 0.278% │ 8.333% │ 0.000% │
213
+ │ 6 │ 8.750% │ 0.417% │ 8.333% │ 0.000% │
214
+ │ 7 │ 8.889% │ 0.556% │ 8.333% │ 0.000% │
215
+ │ 8 │ 9.028% │ 0.694% │ 8.333% │ 0.000% │
216
+ │ 9 │ 0.833% │ 0.833% │ 0.000% │ 0.000% │
217
+ │ 10 │ 0.694% │ 0.694% │ 0.000% │ 0.000% │
218
+ │ 11 │ 0.556% │ 0.556% │ 0.000% │ 0.000% │
219
+ │ 12 │ 0.417% │ 0.417% │ 0.000% │ 0.000% │
220
+ │ 13 │ 0.278% │ 0.278% │ 0.000% │ 0.000% │
221
+ │ 14 │ 0.139% │ 0.139% │ 0.000% │ 0.000% │
222
+ └────────┴─────────┴────────┴────────┴─────────┘
223
+
224
+ ```
225
+
226
+ ## 🎲 Sample Dice Expression Grammar
227
+
228
+ ## 🧱 Roadmap
229
+
230
+ - [ ] **Publish** to npm as `@dpr/dice`
231
+ - [ ] Create a **web playground** with live examples
232
+ - [ ] Consider creating higher-level APIs: `Turn`, `Attack`, `DamageRider`
233
+ - [ ] Simplify and improve PMF and Query interface ergonomics
234
+ - [ ] Add more comprehensive 5e rule examples
235
+ - [ ] Performance improvements for DPR-only calculations
236
+ - [ ] Multi-round and sustained vs nova simulations
237
+ - [ ] Deeper integration with [dprcalc.com](https://dprcalc.com)
238
+ - [ ] Blog posts and documentation
239
+ - [ ] Grammar refinements and new YACC parsing
240
+
241
+ ## 💬 Discuss
242
+
243
+ Join our [Discord](https://dprcalc.com/discord) to discuss this library and more!
244
+
245
+ ## 🤝 Contributing
246
+
247
+ Clone the repo and install dependencies:
248
+
249
+ ```bash
250
+ git clone https://github.com/yipe/dice.git
251
+ cd dice
252
+ yarn install
253
+ ```
254
+
255
+ Run tests
256
+
257
+ ```bash
258
+ yarn test
259
+ ```
260
+
261
+ Run examples
262
+
263
+ ```bash
264
+ yarn example
265
+ ```
266
+
267
+ ## 📜 License
268
+
269
+ 2025 MIT © [Michael Margolis](https://github.com/yipe)
270
+
271
+ ## ⚖️ Legal / Trademarks
272
+
273
+ Wizards of the Coast, Dungeons & Dragons, and their logos are trademarks of Wizards of the Coast LLC in the United States and other countries.
274
+
275
+ © 2025 Wizards. All Rights Reserved.
276
+
277
+ ## ❤️ Credits
278
+
279
+ Portions of this code are inspired by [dice.clockworkmod.com](https://github.com/koush/dice.clockworkmod.com) by Koushik Dutta (2013), licensed under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
280
+
281
+ Initial [TypeScript port](https://github.com/loginName1/dice-calculator-ts) expertly created by [loginName1](https://github.com/loginName1).
282
+
283
+ ```
284
+
285
+ ```