@dicelette/core 1.19.0 → 1.20.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/dist/index.d.mts CHANGED
@@ -9,7 +9,7 @@ import { z } from 'zod';
9
9
  * Random stats = 6
10
10
  * result = "1d20>3"
11
11
  */
12
- declare function createCriticalCustom(dice: string, customCritical: CustomCritical, template: StatisticalTemplate): string;
12
+ declare function createCriticalCustom(dice: string, customCritical: CustomCritical, template: StatisticalTemplate, engine?: Engine | null): string;
13
13
  /**
14
14
  * Parse the string provided and turn it as a readable dice for dice parser
15
15
  * @param dice {string}
@@ -232,16 +232,25 @@ declare function isNumber(value: unknown): boolean;
232
232
  * Replace the `{exp}` in the dice.
233
233
  * If the `{exp}` has a default value in the form of `{exp || defaultValue}`, it will be replaced by the default value.
234
234
  * @param {string} dice
235
+ * @param engine
235
236
  * @returns {string} the dice with the {exp} replaced by a random value
236
237
  */
237
- declare function replaceExpByRandom(dice: string): string;
238
+ declare function replaceExpByRandom(dice: string, engine?: Engine | null): string;
239
+ /**
240
+ * Utility function to get the engine from its name
241
+ * @param engine {"nativeMath" | "browserCrypto" | "nodeCrypto"} The engine name
242
+ * @returns {Engine} The engine
243
+ * @public
244
+ */
245
+ declare function getEngine(engine: "nativeMath" | "browserCrypto" | "nodeCrypto"): Engine;
246
+ declare function randomInt(min: number, max: number, engine?: Engine | null): number;
238
247
 
239
248
  /**
240
249
  * Verify if the provided dice work with random value
241
250
  * @param testDice {string}
242
251
  * @param allStats {Record<string,number>}
243
252
  */
244
- declare function evalStatsDice(testDice: string, allStats?: Record<string, number>): string;
253
+ declare function evalStatsDice(testDice: string, allStats?: Record<string, number>, engine?: Engine | null): string;
245
254
  /**
246
255
  * Generate a random dice and remove the formula (+ evaluate it)
247
256
  * Used for diceDamage only
@@ -249,13 +258,13 @@ declare function evalStatsDice(testDice: string, allStats?: Record<string, numbe
249
258
  * @param template {StatisticalTemplate}
250
259
  * @returns
251
260
  */
252
- declare function diceRandomParse(value: string, template: StatisticalTemplate): string;
261
+ declare function diceRandomParse(value: string, template: StatisticalTemplate, engine?: Engine | null): string;
253
262
  /**
254
263
  * Same as damageDice but for DiceType
255
264
  * @param dice {string}
256
265
  * @param template {StatisticalTemplate}
257
266
  */
258
- declare function diceTypeRandomParse(dice: string, template: StatisticalTemplate): string;
267
+ declare function diceTypeRandomParse(dice: string, template: StatisticalTemplate, engine?: Engine | null): string;
259
268
  /**
260
269
  * Random the combinaison and evaluate it to check if everything is valid
261
270
  * @param combinaison {Record<string,string>}
@@ -274,17 +283,17 @@ declare function evalOneCombinaison(combinaison: string, stats: Record<string, n
274
283
  * @param {boolean} verify - If true, will roll the dices to check if everything is valid
275
284
  * @returns {StatisticalTemplate}
276
285
  */
277
- declare function verifyTemplateValue(template: unknown, verify?: boolean): StatisticalTemplate;
286
+ declare function verifyTemplateValue(template: unknown, verify?: boolean, engine?: Engine | null): StatisticalTemplate;
278
287
  /**
279
288
  * Test each damage roll from the template.damage
280
289
  * @param {StatisticalTemplate} template
281
290
  */
282
- declare function testDiceRegistered(template: StatisticalTemplate): void;
291
+ declare function testDiceRegistered(template: StatisticalTemplate, engine?: Engine | null): void;
283
292
  /**
284
293
  * Test all combinaison with generated random value
285
294
  * @param {StatisticalTemplate} template
286
295
  */
287
- declare function testStatCombinaison(template: StatisticalTemplate): void;
296
+ declare function testStatCombinaison(template: StatisticalTemplate, engine?: Engine | null): void;
288
297
  /**
289
298
  * Generate a random stat based on the template and the statistical min and max
290
299
  * @param {number|undefined} total
@@ -292,7 +301,7 @@ declare function testStatCombinaison(template: StatisticalTemplate): void;
292
301
  * @param {number | undefined} min
293
302
  * @returns
294
303
  */
295
- declare function generateRandomStat(total?: number | undefined, max?: number, min?: number): number;
304
+ declare function generateRandomStat(total?: number | undefined, max?: number, min?: number, engine?: Engine | null): number;
296
305
 
297
306
  declare class DiceTypeError extends Error {
298
307
  readonly dice: string;
@@ -384,4 +393,4 @@ interface StatisticalSchema extends StatisticalTemplate {
384
393
  $schema?: string;
385
394
  }
386
395
 
387
- export { COMMENT_REGEX, type Compare, type ComparedValue, type Critical, type CustomCritical, type CustomCriticalMap, DETECT_CRITICAL, DiceTypeError, EmptyObjectError, FormulaError, MaxGreater, type Modifier, NoStatisticsError, type Resultat, SIGN_REGEX, SIGN_REGEX_SPACE, SYMBOL_DICE, type Sign, type Statistic, type StatisticalSchema, type StatisticalTemplate, TooManyDice, TooManyStats, calculator, createCriticalCustom, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, generateRandomStat, generateStatsDice, isNumber, replaceExpByRandom, replaceFormulaInDice, roll, standardizeDice, templateSchema, testDiceRegistered, testStatCombinaison, verifyTemplateValue };
396
+ export { COMMENT_REGEX, type Compare, type ComparedValue, type Critical, type CustomCritical, type CustomCriticalMap, DETECT_CRITICAL, DiceTypeError, EmptyObjectError, FormulaError, MaxGreater, type Modifier, NoStatisticsError, type Resultat, SIGN_REGEX, SIGN_REGEX_SPACE, SYMBOL_DICE, type Sign, type Statistic, type StatisticalSchema, type StatisticalTemplate, TooManyDice, TooManyStats, calculator, createCriticalCustom, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, generateRandomStat, generateStatsDice, getEngine, isNumber, randomInt, replaceExpByRandom, replaceFormulaInDice, roll, standardizeDice, templateSchema, testDiceRegistered, testStatCombinaison, verifyTemplateValue };
package/dist/index.d.ts CHANGED
@@ -9,7 +9,7 @@ import { z } from 'zod';
9
9
  * Random stats = 6
10
10
  * result = "1d20>3"
11
11
  */
12
- declare function createCriticalCustom(dice: string, customCritical: CustomCritical, template: StatisticalTemplate): string;
12
+ declare function createCriticalCustom(dice: string, customCritical: CustomCritical, template: StatisticalTemplate, engine?: Engine | null): string;
13
13
  /**
14
14
  * Parse the string provided and turn it as a readable dice for dice parser
15
15
  * @param dice {string}
@@ -232,16 +232,25 @@ declare function isNumber(value: unknown): boolean;
232
232
  * Replace the `{exp}` in the dice.
233
233
  * If the `{exp}` has a default value in the form of `{exp || defaultValue}`, it will be replaced by the default value.
234
234
  * @param {string} dice
235
+ * @param engine
235
236
  * @returns {string} the dice with the {exp} replaced by a random value
236
237
  */
237
- declare function replaceExpByRandom(dice: string): string;
238
+ declare function replaceExpByRandom(dice: string, engine?: Engine | null): string;
239
+ /**
240
+ * Utility function to get the engine from its name
241
+ * @param engine {"nativeMath" | "browserCrypto" | "nodeCrypto"} The engine name
242
+ * @returns {Engine} The engine
243
+ * @public
244
+ */
245
+ declare function getEngine(engine: "nativeMath" | "browserCrypto" | "nodeCrypto"): Engine;
246
+ declare function randomInt(min: number, max: number, engine?: Engine | null): number;
238
247
 
239
248
  /**
240
249
  * Verify if the provided dice work with random value
241
250
  * @param testDice {string}
242
251
  * @param allStats {Record<string,number>}
243
252
  */
244
- declare function evalStatsDice(testDice: string, allStats?: Record<string, number>): string;
253
+ declare function evalStatsDice(testDice: string, allStats?: Record<string, number>, engine?: Engine | null): string;
245
254
  /**
246
255
  * Generate a random dice and remove the formula (+ evaluate it)
247
256
  * Used for diceDamage only
@@ -249,13 +258,13 @@ declare function evalStatsDice(testDice: string, allStats?: Record<string, numbe
249
258
  * @param template {StatisticalTemplate}
250
259
  * @returns
251
260
  */
252
- declare function diceRandomParse(value: string, template: StatisticalTemplate): string;
261
+ declare function diceRandomParse(value: string, template: StatisticalTemplate, engine?: Engine | null): string;
253
262
  /**
254
263
  * Same as damageDice but for DiceType
255
264
  * @param dice {string}
256
265
  * @param template {StatisticalTemplate}
257
266
  */
258
- declare function diceTypeRandomParse(dice: string, template: StatisticalTemplate): string;
267
+ declare function diceTypeRandomParse(dice: string, template: StatisticalTemplate, engine?: Engine | null): string;
259
268
  /**
260
269
  * Random the combinaison and evaluate it to check if everything is valid
261
270
  * @param combinaison {Record<string,string>}
@@ -274,17 +283,17 @@ declare function evalOneCombinaison(combinaison: string, stats: Record<string, n
274
283
  * @param {boolean} verify - If true, will roll the dices to check if everything is valid
275
284
  * @returns {StatisticalTemplate}
276
285
  */
277
- declare function verifyTemplateValue(template: unknown, verify?: boolean): StatisticalTemplate;
286
+ declare function verifyTemplateValue(template: unknown, verify?: boolean, engine?: Engine | null): StatisticalTemplate;
278
287
  /**
279
288
  * Test each damage roll from the template.damage
280
289
  * @param {StatisticalTemplate} template
281
290
  */
282
- declare function testDiceRegistered(template: StatisticalTemplate): void;
291
+ declare function testDiceRegistered(template: StatisticalTemplate, engine?: Engine | null): void;
283
292
  /**
284
293
  * Test all combinaison with generated random value
285
294
  * @param {StatisticalTemplate} template
286
295
  */
287
- declare function testStatCombinaison(template: StatisticalTemplate): void;
296
+ declare function testStatCombinaison(template: StatisticalTemplate, engine?: Engine | null): void;
288
297
  /**
289
298
  * Generate a random stat based on the template and the statistical min and max
290
299
  * @param {number|undefined} total
@@ -292,7 +301,7 @@ declare function testStatCombinaison(template: StatisticalTemplate): void;
292
301
  * @param {number | undefined} min
293
302
  * @returns
294
303
  */
295
- declare function generateRandomStat(total?: number | undefined, max?: number, min?: number): number;
304
+ declare function generateRandomStat(total?: number | undefined, max?: number, min?: number, engine?: Engine | null): number;
296
305
 
297
306
  declare class DiceTypeError extends Error {
298
307
  readonly dice: string;
@@ -384,4 +393,4 @@ interface StatisticalSchema extends StatisticalTemplate {
384
393
  $schema?: string;
385
394
  }
386
395
 
387
- export { COMMENT_REGEX, type Compare, type ComparedValue, type Critical, type CustomCritical, type CustomCriticalMap, DETECT_CRITICAL, DiceTypeError, EmptyObjectError, FormulaError, MaxGreater, type Modifier, NoStatisticsError, type Resultat, SIGN_REGEX, SIGN_REGEX_SPACE, SYMBOL_DICE, type Sign, type Statistic, type StatisticalSchema, type StatisticalTemplate, TooManyDice, TooManyStats, calculator, createCriticalCustom, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, generateRandomStat, generateStatsDice, isNumber, replaceExpByRandom, replaceFormulaInDice, roll, standardizeDice, templateSchema, testDiceRegistered, testStatCombinaison, verifyTemplateValue };
396
+ export { COMMENT_REGEX, type Compare, type ComparedValue, type Critical, type CustomCritical, type CustomCriticalMap, DETECT_CRITICAL, DiceTypeError, EmptyObjectError, FormulaError, MaxGreater, type Modifier, NoStatisticsError, type Resultat, SIGN_REGEX, SIGN_REGEX_SPACE, SYMBOL_DICE, type Sign, type Statistic, type StatisticalSchema, type StatisticalTemplate, TooManyDice, TooManyStats, calculator, createCriticalCustom, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, generateRandomStat, generateStatsDice, getEngine, isNumber, randomInt, replaceExpByRandom, replaceFormulaInDice, roll, standardizeDice, templateSchema, testDiceRegistered, testStatCombinaison, verifyTemplateValue };
package/dist/index.js CHANGED
@@ -42,7 +42,9 @@ __export(index_exports, {
42
42
  evalStatsDice: () => evalStatsDice,
43
43
  generateRandomStat: () => generateRandomStat,
44
44
  generateStatsDice: () => generateStatsDice,
45
+ getEngine: () => getEngine,
45
46
  isNumber: () => isNumber,
47
+ randomInt: () => randomInt,
46
48
  replaceExpByRandom: () => replaceExpByRandom,
47
49
  replaceFormulaInDice: () => replaceFormulaInDice,
48
50
  roll: () => roll,
@@ -55,12 +57,14 @@ __export(index_exports, {
55
57
  module.exports = __toCommonJS(index_exports);
56
58
 
57
59
  // src/dice.ts
58
- var import_rpg_dice_roller = require("@dice-roller/rpg-dice-roller");
60
+ var import_rpg_dice_roller2 = require("@dice-roller/rpg-dice-roller");
59
61
  var import_mathjs2 = require("mathjs");
60
62
 
61
63
  // src/utils.ts
62
64
  var import_mathjs = require("mathjs");
63
65
  var import_uniformize = require("uniformize");
66
+ var import_rpg_dice_roller = require("@dice-roller/rpg-dice-roller");
67
+ var import_random_js = require("random-js");
64
68
  function escapeRegex(string) {
65
69
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
66
70
  }
@@ -108,16 +112,32 @@ function cleanedDice(dice) {
108
112
  function isNumber(value) {
109
113
  return value !== void 0 && (typeof value === "number" || !Number.isNaN(Number(value)) && typeof value === "string" && value.trim().length > 0);
110
114
  }
111
- function replaceExpByRandom(dice) {
115
+ function replaceExpByRandom(dice, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
112
116
  const diceRegex = /\{exp( ?\|\| ?(?<default>\d+))?}/gi;
113
117
  return dice.replace(diceRegex, (_match, _p1, _p2, _offset, _string, groups) => {
114
118
  const defaultValue = groups?.default;
115
- return defaultValue ?? (0, import_mathjs.randomInt)(1, 999).toString();
119
+ return defaultValue ?? randomInt(1, 999, engine).toString();
116
120
  });
117
121
  }
122
+ function getEngine(engine) {
123
+ switch (engine) {
124
+ case "nativeMath":
125
+ return import_rpg_dice_roller.NumberGenerator.engines.nativeMath;
126
+ case "browserCrypto":
127
+ return import_rpg_dice_roller.NumberGenerator.engines.browserCrypto;
128
+ case "nodeCrypto":
129
+ return import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto;
130
+ default:
131
+ return import_rpg_dice_roller.NumberGenerator.engines.nativeMath;
132
+ }
133
+ }
134
+ function randomInt(min, max, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
135
+ const rng = new import_random_js.Random(engine || void 0);
136
+ return rng.integer(min, max);
137
+ }
118
138
 
119
139
  // src/dice.ts
120
- function getCompare(dice, compareRegex) {
140
+ function getCompare(dice, compareRegex, engine = import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto) {
121
141
  if (dice.match(/((\{.*,(.*)+\}|([><=!]+\d+f))[><=!]+\d+\}?)|\{(.*)([><=!]+).*\}/))
122
142
  return { dice, compare: void 0 };
123
143
  dice = dice.replace(SIGN_REGEX_SPACE, "");
@@ -127,7 +147,7 @@ function getCompare(dice, compareRegex) {
127
147
  const compareSign = compareRegex[0].match(SIGN_REGEX)?.[0];
128
148
  if (sign) {
129
149
  const toCalc = calc.replace(SIGN_REGEX, "").replace(/\s/g, "").replace(/;(.*)/, "");
130
- const rCompare = rollCompare(toCalc);
150
+ const rCompare = rollCompare(toCalc, engine);
131
151
  const total = (0, import_mathjs2.evaluate)(rCompare.value.toString());
132
152
  dice = dice.replace(SIGN_REGEX_SPACE, `${compareSign}${total}`);
133
153
  compare = {
@@ -137,7 +157,7 @@ function getCompare(dice, compareRegex) {
137
157
  rollValue: rCompare.diceResult
138
158
  };
139
159
  } else {
140
- const rcompare = rollCompare(calc);
160
+ const rcompare = rollCompare(calc, engine);
141
161
  compare = {
142
162
  sign: compareSign,
143
163
  value: rcompare.value,
@@ -147,9 +167,9 @@ function getCompare(dice, compareRegex) {
147
167
  }
148
168
  return { dice, compare };
149
169
  }
150
- function rollCompare(value) {
170
+ function rollCompare(value, engine = import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto) {
151
171
  if (isNumber(value)) return { value: Number.parseInt(value, 10) };
152
- const rollComp = roll(value);
172
+ const rollComp = roll(value, engine);
153
173
  if (!rollComp?.total)
154
174
  return { value: (0, import_mathjs2.evaluate)(value), diceResult: value };
155
175
  return {
@@ -158,16 +178,16 @@ function rollCompare(value) {
158
178
  diceResult: rollComp?.result
159
179
  };
160
180
  }
161
- function createCriticalCustom(dice, customCritical, template) {
181
+ function createCriticalCustom(dice, customCritical, template, engine = import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto) {
162
182
  const compareRegex = dice.match(SIGN_REGEX_SPACE);
163
183
  let customDice = dice;
164
- const compareValue = diceTypeRandomParse(customCritical.value, template);
184
+ const compareValue = diceTypeRandomParse(customCritical.value, template, engine);
165
185
  if (compareValue.includes("$"))
166
186
  throw new DiceTypeError(compareValue, "createCriticalCustom");
167
187
  const comparaison = `${customCritical.sign}${compareValue}`;
168
188
  if (compareRegex) customDice = customDice.replace(SIGN_REGEX_SPACE, comparaison);
169
189
  else customDice += comparaison;
170
- return diceTypeRandomParse(customDice, template);
190
+ return diceTypeRandomParse(customDice, template, engine);
171
191
  }
172
192
  function getModifier(dice) {
173
193
  const modifier = dice.matchAll(/(\+|-|%|\/|\^|\*|\*{2})(\d+)/gi);
@@ -190,15 +210,15 @@ function getModifier(dice) {
190
210
  }
191
211
  return modificator;
192
212
  }
193
- function roll(dice, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
213
+ function roll(dice, engine = import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto) {
194
214
  dice = standardizeDice(dice).replace(/^\+/, "").replaceAll("=>", ">=").replaceAll("=<", "<=").trimStart();
195
215
  if (!dice.includes("d")) return void 0;
196
216
  dice = dice.replaceAll(DETECT_CRITICAL, "").trimEnd();
197
217
  const compareRegex = dice.match(SIGN_REGEX_SPACE);
198
218
  let compare;
199
- if (dice.includes(";")) return sharedRolls(dice);
219
+ if (dice.includes(";")) return sharedRolls(dice, engine);
200
220
  if (compareRegex) {
201
- const compareResult = getCompare(dice, compareRegex);
221
+ const compareResult = getCompare(dice, compareRegex, engine);
202
222
  dice = compareResult.dice;
203
223
  compare = compareResult.compare;
204
224
  }
@@ -209,8 +229,8 @@ function roll(dice, engine = import_rpg_dice_roller.NumberGenerator.engines.node
209
229
  let diceToRoll = diceArray[1].replace(COMMENT_REGEX, "");
210
230
  const commentsMatch = diceArray[1].match(COMMENT_REGEX);
211
231
  const comments = commentsMatch ? commentsMatch[2] : void 0;
212
- const roller2 = new import_rpg_dice_roller.DiceRoller();
213
- import_rpg_dice_roller.NumberGenerator.generator.engine = engine;
232
+ const roller2 = new import_rpg_dice_roller2.DiceRoller();
233
+ import_rpg_dice_roller2.NumberGenerator.generator.engine = engine;
214
234
  for (let i = 0; i < numberOfDice; i++) {
215
235
  try {
216
236
  roller2.roll(diceToRoll);
@@ -227,8 +247,8 @@ function roll(dice, engine = import_rpg_dice_roller.NumberGenerator.engines.node
227
247
  total: roller2.total
228
248
  };
229
249
  }
230
- const roller = new import_rpg_dice_roller.DiceRoller();
231
- import_rpg_dice_roller.NumberGenerator.generator.engine = engine;
250
+ const roller = new import_rpg_dice_roller2.DiceRoller();
251
+ import_rpg_dice_roller2.NumberGenerator.generator.engine = engine;
232
252
  const diceWithoutComment = dice.replace(COMMENT_REGEX, "").trimEnd();
233
253
  try {
234
254
  roller.roll(diceWithoutComment);
@@ -268,7 +288,7 @@ function inverseSign(sign) {
268
288
  return "==";
269
289
  }
270
290
  }
271
- function replaceInFormula(element, diceResult, compareResult, res) {
291
+ function replaceInFormula(element, diceResult, compareResult, res, engine = import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto) {
272
292
  const { formule, diceAll } = replaceText(
273
293
  element,
274
294
  diceResult.total ?? 0,
@@ -281,24 +301,24 @@ function replaceInFormula(element, diceResult, compareResult, res) {
281
301
  evaluateRoll = (0, import_mathjs2.evaluate)(compareResult.dice);
282
302
  return `${validSign} ${diceAll}: ${formule} = ${evaluateRoll}${invertedSign}${compareResult.compare?.value}`;
283
303
  } catch (error) {
284
- const evaluateRoll2 = roll(compareResult.dice);
304
+ const evaluateRoll2 = roll(compareResult.dice, engine);
285
305
  if (evaluateRoll2)
286
306
  return `${validSign} ${diceAll}: ${evaluateRoll2.result.split(":").splice(1).join(":")}`;
287
307
  return `${validSign} ${diceAll}: ${formule} = ${evaluateRoll2}${invertedSign}${compareResult.compare?.value}`;
288
308
  }
289
309
  }
290
- function compareSignFormule(toRoll, compareRegex, element, diceResult) {
310
+ function compareSignFormule(toRoll, compareRegex, element, diceResult, engine = import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto) {
291
311
  let results = "";
292
- const compareResult = getCompare(toRoll, compareRegex);
312
+ const compareResult = getCompare(toRoll, compareRegex, engine);
293
313
  const toCompare = `${compareResult.dice}${compareResult.compare?.sign}${compareResult.compare?.value}`;
294
314
  let res;
295
315
  try {
296
316
  res = (0, import_mathjs2.evaluate)(toCompare);
297
317
  } catch (error) {
298
- res = roll(toCompare);
318
+ res = roll(toCompare, engine);
299
319
  }
300
320
  if (typeof res === "boolean") {
301
- results = replaceInFormula(element, diceResult, compareResult, res);
321
+ results = replaceInFormula(element, diceResult, compareResult, res, engine);
302
322
  } else if (res instanceof Object) {
303
323
  const diceResult2 = res;
304
324
  if (diceResult2.compare) {
@@ -324,7 +344,7 @@ function formatComment(dice) {
324
344
  const commentsMatch = commentsRegex.exec(dice);
325
345
  return commentsMatch?.groups?.comments ? `__${commentsMatch.groups.comments}__ \u2014 ` : "";
326
346
  }
327
- function sharedRolls(dice) {
347
+ function sharedRolls(dice, engine = import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto) {
328
348
  if (dice.match(/\d+?#(.*?)/))
329
349
  throw new DiceTypeError(
330
350
  dice,
@@ -348,7 +368,7 @@ function sharedRolls(dice) {
348
368
  const commentsRegex = /\[(?<comments>.*?)\]/gi;
349
369
  const comments = formatComment(diceMain);
350
370
  diceMain = diceMain.replaceAll(commentsRegex, "").trim();
351
- const diceResult = roll(diceMain);
371
+ const diceResult = roll(diceMain, engine);
352
372
  if (!diceResult || !diceResult.total) return void 0;
353
373
  results.push(`\u203B ${comments}${diceResult.result}`);
354
374
  let total = diceResult.total;
@@ -360,7 +380,7 @@ function sharedRolls(dice) {
360
380
  let toRoll = element.replace(SYMBOL_DICE, `${diceResult.total}`);
361
381
  const compareRegex = toRoll.match(SIGN_REGEX_SPACE);
362
382
  if (compareRegex) {
363
- const compareResult = compareSignFormule(toRoll, compareRegex, element, diceResult);
383
+ const compareResult = compareSignFormule(toRoll, compareRegex, element, diceResult, engine);
364
384
  toRoll = compareResult.dice;
365
385
  results.push(compareResult.results);
366
386
  } else {
@@ -374,7 +394,7 @@ function sharedRolls(dice) {
374
394
  results.push(`\u25C8 ${comment}${diceAll}: ${formule} = ${evaluated}`);
375
395
  total += Number.parseInt(evaluated, 10);
376
396
  } catch (error) {
377
- const evaluated = roll(toRoll);
397
+ const evaluated = roll(toRoll, engine);
378
398
  if (evaluated)
379
399
  results.push(
380
400
  `\u25C8 ${comment}${diceAll}: ${evaluated.result.split(":").slice(1).join(":")}`
@@ -398,9 +418,10 @@ function sharedRolls(dice) {
398
418
 
399
419
  // src/verify_template.ts
400
420
  var import_mathjs3 = require("mathjs");
401
- var import_random_js = require("random-js");
421
+ var import_random_js2 = require("random-js");
402
422
  var import_uniformize2 = require("uniformize");
403
- function evalStatsDice(testDice, allStats) {
423
+ var import_rpg_dice_roller3 = require("@dice-roller/rpg-dice-roller");
424
+ function evalStatsDice(testDice, allStats, engine = import_rpg_dice_roller3.NumberGenerator.engines.nodeCrypto) {
404
425
  let dice = testDice.trimEnd();
405
426
  if (allStats && Object.keys(allStats).length > 0) {
406
427
  const names = Object.keys(allStats);
@@ -413,14 +434,14 @@ function evalStatsDice(testDice, allStats) {
413
434
  }
414
435
  }
415
436
  try {
416
- if (!roll(replaceFormulaInDice(replaceExpByRandom(dice))))
437
+ if (!roll(replaceFormulaInDice(replaceExpByRandom(dice)), engine))
417
438
  throw new DiceTypeError(dice, "evalStatsDice", "no roll result");
418
439
  return testDice;
419
440
  } catch (error) {
420
441
  throw new DiceTypeError(dice, "evalStatsDice", error);
421
442
  }
422
443
  }
423
- function diceRandomParse(value, template) {
444
+ function diceRandomParse(value, template, engine = import_rpg_dice_roller3.NumberGenerator.engines.nodeCrypto) {
424
445
  if (!template.statistics) return replaceFormulaInDice(value.standardize());
425
446
  value = value.standardize();
426
447
  const statNames = Object.keys(template.statistics);
@@ -436,13 +457,13 @@ function diceRandomParse(value, template) {
436
457
  min = foundStat.min;
437
458
  }
438
459
  const total = template.total || 100;
439
- const randomStatValue = generateRandomStat(total, max, min);
460
+ const randomStatValue = generateRandomStat(total, max, min, engine);
440
461
  newDice = value.replace(regex, randomStatValue.toString());
441
462
  }
442
463
  }
443
464
  return replaceFormulaInDice(newDice);
444
465
  }
445
- function diceTypeRandomParse(dice, template) {
466
+ function diceTypeRandomParse(dice, template, engine = import_rpg_dice_roller3.NumberGenerator.engines.nodeCrypto) {
446
467
  dice = replaceExpByRandom(dice);
447
468
  if (!template.statistics) return dice;
448
469
  const firstStatNotcombinaison = Object.keys(template.statistics).find(
@@ -452,7 +473,7 @@ function diceTypeRandomParse(dice, template) {
452
473
  const stats = template.statistics[firstStatNotcombinaison];
453
474
  const { min, max } = stats;
454
475
  const total = template.total || 100;
455
- const randomStatValue = generateRandomStat(total, max, min);
476
+ const randomStatValue = generateRandomStat(total, max, min, engine);
456
477
  return replaceFormulaInDice(dice.replaceAll("$", randomStatValue.toString()));
457
478
  }
458
479
  function evalCombinaison(combinaison, stats) {
@@ -489,7 +510,7 @@ function convertNumber(number) {
489
510
  if (isNumber(number)) return Number.parseInt(number.toString(), 10);
490
511
  return void 0;
491
512
  }
492
- function verifyTemplateValue(template, verify = true) {
513
+ function verifyTemplateValue(template, verify = true, engine = import_rpg_dice_roller3.NumberGenerator.engines.nodeCrypto) {
493
514
  const parsedTemplate = templateSchema.parse(template);
494
515
  const { success, failure } = parsedTemplate.critical ?? {};
495
516
  const criticicalVal = {
@@ -517,9 +538,10 @@ function verifyTemplateValue(template, verify = true) {
517
538
  }
518
539
  const cleanedDice2 = diceTypeRandomParse(
519
540
  statistiqueTemplate.diceType,
520
- statistiqueTemplate
541
+ statistiqueTemplate,
542
+ engine
521
543
  );
522
- const rolled = roll(cleanedDice2);
544
+ const rolled = roll(cleanedDice2, engine);
523
545
  if (!rolled)
524
546
  throw new DiceTypeError(cleanedDice2, "no_roll_result", "no roll result");
525
547
  }
@@ -532,34 +554,35 @@ function verifyTemplateValue(template, verify = true) {
532
554
  const cleanedDice2 = createCriticalCustom(
533
555
  statistiqueTemplate.diceType,
534
556
  custom,
535
- statistiqueTemplate
557
+ statistiqueTemplate,
558
+ engine
536
559
  );
537
- const rolled = roll(cleanedDice2);
560
+ const rolled = roll(cleanedDice2, engine);
538
561
  if (!rolled)
539
562
  throw new DiceTypeError(cleanedDice2, "verifyTemplateValue", "no roll result");
540
563
  }
541
564
  }
542
- testDiceRegistered(statistiqueTemplate);
543
- testStatCombinaison(statistiqueTemplate);
565
+ testDiceRegistered(statistiqueTemplate, engine);
566
+ testStatCombinaison(statistiqueTemplate, engine);
544
567
  return statistiqueTemplate;
545
568
  }
546
- function testDiceRegistered(template) {
569
+ function testDiceRegistered(template, engine = import_rpg_dice_roller3.NumberGenerator.engines.nodeCrypto) {
547
570
  if (!template.damage) return;
548
571
  if (Object.keys(template.damage).length === 0) throw new EmptyObjectError();
549
572
  if (Object.keys(template.damage).length > 25) throw new TooManyDice();
550
573
  for (const [name, dice] of Object.entries(template.damage)) {
551
574
  if (!dice) continue;
552
575
  const diceReplaced = replaceExpByRandom(dice);
553
- const randomDiceParsed = diceRandomParse(diceReplaced, template);
576
+ const randomDiceParsed = diceRandomParse(diceReplaced, template, engine);
554
577
  try {
555
- const rolled = roll(randomDiceParsed);
578
+ const rolled = roll(randomDiceParsed, engine);
556
579
  if (!rolled) throw new DiceTypeError(name, "no_roll_result", dice);
557
580
  } catch (error) {
558
581
  throw new DiceTypeError(name, "testDiceRegistered", error);
559
582
  }
560
583
  }
561
584
  }
562
- function testStatCombinaison(template) {
585
+ function testStatCombinaison(template, engine = import_rpg_dice_roller3.NumberGenerator.engines.nodeCrypto) {
563
586
  if (!template.statistics) return;
564
587
  const onlycombinaisonStats = Object.fromEntries(
565
588
  Object.entries(template.statistics).filter(
@@ -580,7 +603,7 @@ function testStatCombinaison(template) {
580
603
  for (const [other, data] of Object.entries(allOtherStats)) {
581
604
  const { max, min } = data;
582
605
  const total = template.total || 100;
583
- const randomStatValue = generateRandomStat(total, max, min);
606
+ const randomStatValue = generateRandomStat(total, max, min, engine);
584
607
  const regex = new RegExp(other, "gi");
585
608
  formula = formula.replace(regex, randomStatValue.toString());
586
609
  }
@@ -593,10 +616,10 @@ function testStatCombinaison(template) {
593
616
  if (error.length > 0) throw new FormulaError(error.join(", "), "testStatCombinaison");
594
617
  return;
595
618
  }
596
- function generateRandomStat(total = 100, max, min) {
619
+ function generateRandomStat(total = 100, max, min, engine = import_rpg_dice_roller3.NumberGenerator.engines.nodeCrypto) {
597
620
  let randomStatValue = total + 1;
598
621
  while (randomStatValue >= total || randomStatValue === 0) {
599
- const random2 = new import_random_js.Random();
622
+ const random2 = new import_random_js2.Random(engine || void 0);
600
623
  if (max && min) randomStatValue = random2.integer(min, max);
601
624
  else if (max) randomStatValue = random2.integer(1, max);
602
625
  else if (min) randomStatValue = random2.integer(min, total);
@@ -754,7 +777,9 @@ var templateSchema = import_zod.z.object({
754
777
  evalStatsDice,
755
778
  generateRandomStat,
756
779
  generateStatsDice,
780
+ getEngine,
757
781
  isNumber,
782
+ randomInt,
758
783
  replaceExpByRandom,
759
784
  replaceFormulaInDice,
760
785
  roll,