@dicelette/core 1.1.0 → 1.1.2

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
@@ -63,8 +63,8 @@ declare function evalOneCombinaison(combinaison: string, stats: {
63
63
  [name: string]: string | number;
64
64
  }): any;
65
65
  declare function verifyTemplateValue(template: any): StatisticalTemplate;
66
- declare function testDamageRoll(template: StatisticalTemplate): void;
67
- declare function testCombinaison(template: StatisticalTemplate): void;
66
+ declare function testDiceRegistered(template: StatisticalTemplate): void;
67
+ declare function testStatCombinaison(template: StatisticalTemplate): void;
68
68
  declare function generateRandomStat(total?: number | undefined, max?: number, min?: number): number;
69
69
 
70
- export { COMMENT_REGEX, type Compare, type Critical, type Modifier, type Resultat, type Sign, type Statistic, type StatisticalTemplate, calculator, cleanedDice, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, generateRandomStat, generateStatsDice, replaceFormulaInDice, roll, testCombinaison, testDamageRoll, verifyTemplateValue };
70
+ export { COMMENT_REGEX, type Compare, type Critical, type Modifier, type Resultat, type Sign, type Statistic, type StatisticalTemplate, calculator, cleanedDice, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, generateRandomStat, generateStatsDice, replaceFormulaInDice, roll, testDiceRegistered, testStatCombinaison, verifyTemplateValue };
package/dist/index.d.ts CHANGED
@@ -63,8 +63,8 @@ declare function evalOneCombinaison(combinaison: string, stats: {
63
63
  [name: string]: string | number;
64
64
  }): any;
65
65
  declare function verifyTemplateValue(template: any): StatisticalTemplate;
66
- declare function testDamageRoll(template: StatisticalTemplate): void;
67
- declare function testCombinaison(template: StatisticalTemplate): void;
66
+ declare function testDiceRegistered(template: StatisticalTemplate): void;
67
+ declare function testStatCombinaison(template: StatisticalTemplate): void;
68
68
  declare function generateRandomStat(total?: number | undefined, max?: number, min?: number): number;
69
69
 
70
- export { COMMENT_REGEX, type Compare, type Critical, type Modifier, type Resultat, type Sign, type Statistic, type StatisticalTemplate, calculator, cleanedDice, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, generateRandomStat, generateStatsDice, replaceFormulaInDice, roll, testCombinaison, testDamageRoll, verifyTemplateValue };
70
+ export { COMMENT_REGEX, type Compare, type Critical, type Modifier, type Resultat, type Sign, type Statistic, type StatisticalTemplate, calculator, cleanedDice, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, generateRandomStat, generateStatsDice, replaceFormulaInDice, roll, testDiceRegistered, testStatCombinaison, verifyTemplateValue };
package/dist/index.js CHANGED
@@ -43,8 +43,8 @@ __export(src_exports, {
43
43
  generateStatsDice: () => generateStatsDice,
44
44
  replaceFormulaInDice: () => replaceFormulaInDice,
45
45
  roll: () => roll,
46
- testCombinaison: () => testCombinaison,
47
- testDamageRoll: () => testDamageRoll,
46
+ testDiceRegistered: () => testDiceRegistered,
47
+ testStatCombinaison: () => testStatCombinaison,
48
48
  verifyTemplateValue: () => verifyTemplateValue
49
49
  });
50
50
  module.exports = __toCommonJS(src_exports);
@@ -293,7 +293,10 @@ function verifyTemplateValue(template) {
293
293
  if (template.diceType) {
294
294
  try {
295
295
  statistiqueTemplate.diceType = template.diceType;
296
- diceTypeRandomParse(template.diceType, statistiqueTemplate);
296
+ const cleanedDice2 = diceTypeRandomParse(template.diceType, statistiqueTemplate);
297
+ const rolled = roll(cleanedDice2);
298
+ if (!rolled)
299
+ throw new Error("[error.invalidDice.withoutDice]");
297
300
  } catch (e) {
298
301
  throw new Error(e.message);
299
302
  }
@@ -314,14 +317,14 @@ function verifyTemplateValue(template) {
314
317
  if (template.damage)
315
318
  statistiqueTemplate.damage = template.damage;
316
319
  try {
317
- testDamageRoll(statistiqueTemplate);
318
- testCombinaison(statistiqueTemplate);
320
+ testDiceRegistered(statistiqueTemplate);
321
+ testStatCombinaison(statistiqueTemplate);
319
322
  } catch (error) {
320
323
  throw new Error(error.message);
321
324
  }
322
325
  return statistiqueTemplate;
323
326
  }
324
- function testDamageRoll(template) {
327
+ function testDiceRegistered(template) {
325
328
  if (!template.damage)
326
329
  return;
327
330
  if (Object.keys(template.damage).length === 0)
@@ -333,13 +336,15 @@ function testDamageRoll(template) {
333
336
  continue;
334
337
  const randomDiceParsed = diceRandomParse(dice, template);
335
338
  try {
336
- roll(randomDiceParsed);
339
+ const rolled = roll(randomDiceParsed);
340
+ if (!rolled)
341
+ throw new Error(`[error.invalidDice, common.space] ${name}`);
337
342
  } catch (error) {
338
343
  throw new Error(`[error.invalidDice, common.space] ${name}`);
339
344
  }
340
345
  }
341
346
  }
342
- function testCombinaison(template) {
347
+ function testStatCombinaison(template) {
343
348
  if (!template.statistics)
344
349
  return;
345
350
  const onlyCombinaisonStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => value.combinaison !== void 0));
@@ -399,8 +404,8 @@ function generateRandomStat(total = 100, max, min) {
399
404
  generateStatsDice,
400
405
  replaceFormulaInDice,
401
406
  roll,
402
- testCombinaison,
403
- testDamageRoll,
407
+ testDiceRegistered,
408
+ testStatCombinaison,
404
409
  verifyTemplateValue
405
410
  });
406
411
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/dice.ts","../src/utils.ts","../src/verify_template.ts"],"sourcesContent":["export * from \"./dice\";\r\nexport * from \"./interface\";\r\nexport * from \"./utils\";\r\nexport * from \"./verify_template\";","/* eslint-disable no-useless-escape */\r\nimport { DiceRoller } from \"@dice-roller/rpg-dice-roller\";\r\nimport { evaluate } from \"mathjs\";\r\n\r\nimport { Compare, Modifier, Resultat, Sign } from \".\";\r\n\r\nexport const COMMENT_REGEX = /\\s+(#|\\/{2}|\\[|\\/\\*)(.*)/;\r\nconst SIGN_REGEX =/[><=!]+/;\r\nconst SIGN_REGEX_SPACE = /[><=!]+(\\S+)/;\r\n\r\n/**\r\n * Parse the string provided and turn it as a readable dice for dice parser\r\n * @param dice {string}\r\n */\r\nexport function roll(dice: string): Resultat | undefined{\r\n\t//parse dice string\r\n\tif (!dice.includes(\"d\")) return undefined;\r\n\tconst compareRegex = dice.match(SIGN_REGEX_SPACE);\r\n\tlet compare : Compare | undefined;\r\n\tif (compareRegex) {\r\n\t\tdice = dice.replace(SIGN_REGEX_SPACE, \"\");\r\n\t\tconst calc = compareRegex[1];\r\n\t\tconst sign = calc.match(/[+-\\/\\*\\^]/)?.[0];\r\n\t\tconst compareSign = compareRegex[0].match(SIGN_REGEX)?.[0];\r\n\t\tif (sign) {\r\n\t\t\tconst toCalc = calc.replace(SIGN_REGEX, \"\").replace(/\\s/g, \"\");\r\n\t\t\tconst total = evaluate(toCalc);\r\n\t\t\tdice = dice.replace(SIGN_REGEX_SPACE, `${compareSign}${total}`);\r\n\t\t\tcompare = {\r\n\t\t\t\tsign: compareSign as \"<\" | \">\" | \">=\" | \"<=\" | \"=\" | \"!=\" | \"==\",\r\n\t\t\t\tvalue: total,\r\n\t\t\t};\r\n\t\t} else compare = {\r\n\t\t\tsign: compareSign as \"<\" | \">\" | \">=\" | \"<=\" | \"=\" | \"!=\" | \"==\",\r\n\t\t\tvalue: parseInt(calc, 10),\r\n\t\t};\r\n\t}\r\n\tconst modifier = dice.matchAll(/(\\+|\\-|%|\\/|\\^|\\*|\\*{2})(\\d+)/gi);\r\n\tlet modificator : Modifier | undefined;\r\n\tfor (const mod of modifier) {\r\n\t\t//calculate the modifier if multiple\r\n\t\tif (modificator) {\r\n\t\t\tconst sign = modificator.sign;\r\n\t\t\tlet value = modificator.value;\r\n\t\t\tif (sign)\r\n\t\t\t\tvalue = calculator(sign, value, parseInt(mod[2], 10));\r\n\t\t\tmodificator = {\r\n\t\t\t\tsign: mod[1] as Sign,\r\n\t\t\t\tvalue,\r\n\t\t\t};\r\n\t\t} else {\r\n\t\t\tmodificator = {\r\n\t\t\t\tsign: mod[1] as Sign,\r\n\t\t\t\tvalue: parseInt(mod[2], 10),\r\n\t\t\t};\r\n\t\t}\r\n\t} \r\n\t\r\n\tif (dice.match(/\\d+?#(.*)/)) {\r\n\t\tconst diceArray = dice.split(\"#\");\r\n\t\tconst numberOfDice = parseInt(diceArray[0], 10);\r\n\t\tconst diceToRoll = diceArray[1].replace(COMMENT_REGEX, \"\");\r\n\t\tconst commentsMatch = diceArray[1].match(COMMENT_REGEX);\r\n\t\tconst comments = commentsMatch ? commentsMatch[2] : undefined;\r\n\t\tconst roller = new DiceRoller();\r\n\t\t//remove comments if any\r\n\t\tfor (let i = 0; i < numberOfDice; i++) {\r\n\t\t\troller.roll(diceToRoll);\r\n\t\t}\r\n\t\treturn {\r\n\t\t\tdice: diceToRoll,\r\n\t\t\tresult: roller.output,\r\n\t\t\tcomment: comments,\r\n\t\t\tcompare: compare ? compare : undefined,\r\n\t\t\tmodifier: modificator,\r\n\t\t};\r\n\t}\r\n\tconst roller = new DiceRoller();\r\n\tconst diceWithoutComment = dice.replace(COMMENT_REGEX, \"\");\r\n\troller.roll(diceWithoutComment);\r\n\tconst commentMatch = dice.match(COMMENT_REGEX);\r\n\tconst comment = commentMatch ? commentMatch[2] : undefined;\r\n\treturn {\r\n\t\tdice,\r\n\t\tresult: roller.output,\r\n\t\tcomment,\r\n\t\tcompare: compare ? compare : undefined,\r\n\t\tmodifier: modificator,\r\n\t};\r\n}\r\n/**\r\n * Evaluate a formula and replace \"^\" by \"**\" if any\r\n * @param {Sign} sign\r\n * @param {number} value \r\n * @param {number} total \r\n * @returns \r\n */\r\nexport function calculator(sign: Sign, value: number, total: number): number {\r\n\tif (sign === \"^\") sign = \"**\";\r\n\treturn evaluate(`${total} ${sign} ${value}`);\r\n}\r\n\r\n\r\n\r\n","import { evaluate } from \"mathjs\";\r\nimport removeAccents from \"remove-accents\";\r\n\r\n/**\r\n * Escape regex string\r\n * @param string {string}\r\n */\r\nexport function escapeRegex(string: string) {\r\n\treturn string.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\r\n}\r\n\r\n\r\n/**\r\n * Replace the stat name by their value using stat and after evaluate any formula using `replaceFormulaInDice`\r\n * @param originalDice {dice}\r\n * @param stats {[name: string]: number}\r\n */\r\nexport function generateStatsDice(originalDice: string, stats?: {[name: string]: number}) {\r\n\tlet dice = originalDice;\r\n\tif (stats && Object.keys(stats).length > 0) {\r\n\t\t//damage field support adding statistic, like : 1d6 + strength\r\n\t\t//check if the value contains a statistic & calculate if it's okay\r\n\t\t//the dice will be converted before roll \r\n\t\tconst allStats = Object.keys(stats);\r\n\t\tfor (const stat of allStats) {\r\n\t\t\tconst regex = new RegExp(escapeRegex(removeAccents(stat)), \"gi\");\r\n\t\t\tif (dice.match(regex)) {\r\n\t\t\t\tconst statValue = stats[stat];\r\n\t\t\t\tdice = dice.replace(regex, statValue.toString());\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn replaceFormulaInDice(dice);\r\n\t\r\n}\r\n\r\n/**\r\n * Replace the {{}} in the dice string and evaluate the interior if any\r\n * @param dice {string}\r\n */\r\nexport function replaceFormulaInDice(dice: string) {\r\n\tconst formula = /(?<formula>\\{{2}(.+?)\\}{2})/gmi;\r\n\tconst formulaMatch = formula.exec(dice);\r\n\tif (formulaMatch?.groups?.formula) {\r\n\t\tconst formula = formulaMatch.groups.formula.replaceAll(\"{{\", \"\").replaceAll(\"}}\", \"\");\r\n\t\ttry {\r\n\t\t\tconst result = evaluate(formula);\r\n\t\t\treturn cleanedDice(dice.replace(formulaMatch.groups.formula, result.toString()));\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidFormula, common.space]: ${formulaMatch.groups.formula}`);\r\n\t\t}\r\n\t}\r\n\treturn cleanedDice(dice);\r\n}\r\n\r\n/**\r\n * Replace the ++ +- -- by their proper value:\r\n * - `++` = `+`\r\n * - `+-` = `-`\r\n * - `--` = `+`\r\n * @param dice {string}\r\n */\r\nexport function cleanedDice(dice: string) {\r\n\treturn dice.replaceAll(\"+-\", \"-\").replaceAll(\"--\", \"+\").replaceAll(\"++\", \"+\");\r\n}","/* eslint-disable @typescript-eslint/no-unused-vars */\r\nimport { evaluate } from \"mathjs\";\r\nimport {Random } from \"random-js\";\r\nimport removeAccents from \"remove-accents\";\r\n\r\nimport { Statistic, StatisticalTemplate } from \".\";\r\nimport { roll } from \"./dice\";\r\nimport { escapeRegex, replaceFormulaInDice } from \"./utils\";\r\n\r\n/**\r\n * Verify if the provided dice work with random value\r\n * @param testDice {string}\r\n * @param stats {[name: string]: number}\r\n */\r\nexport function evalStatsDice(testDice: string, stats?: {[name: string]: number}) {\r\n\tlet dice = testDice;\r\n\tif (stats && Object.keys(stats).length > 0) {\r\n\t\tconst allStats = Object.keys(stats);\r\n\t\tfor (const stat of allStats) {\r\n\t\t\tconst regex = new RegExp(escapeRegex(removeAccents(stat)), \"gi\");\r\n\t\t\tif (testDice.match(regex)) {\r\n\t\t\t\tconst statValue = stats[stat];\r\n\t\t\t\tdice = testDice.replace(regex, statValue.toString());\r\n\t\t\t}\r\n\t\t}\r\n\t}\t\r\n\ttry {\r\n\t\tif (!roll(replaceFormulaInDice(dice))) throw new Error(`[error.invalidDice.withoutDice, common.space] ${dice}`);\r\n\t\treturn testDice;\r\n\t} catch (error) {\r\n\t\tthrow new Error(`[error.invalidDice.withoutDice, common.space]: ${testDice}\\n${(error as Error).message}`);\r\n\t}\r\n}\r\n\r\n/**\r\n * Generate a random dice and remove the formula (+ evaluate it)\r\n * Used for diceDamage only\r\n * @param value {string}\r\n * @param template {StatisticalTemplate}\r\n * @returns \r\n */\r\nexport function diceRandomParse(value: string, template: StatisticalTemplate) {\r\n\tif (!template.statistics) return value;\r\n\tvalue = removeAccents(value);\r\n\tconst allStats = Object.keys(template.statistics).map(stat => removeAccents(stat).toLowerCase());\r\n\tlet newDice = value;\r\n\tfor (const stat of allStats) {\r\n\t\tconst regex = new RegExp(escapeRegex(stat), \"gi\");\r\n\t\tif (value.match(regex)) {\r\n\t\t\tlet max: undefined | number = undefined;\r\n\t\t\tlet min: undefined | number = undefined;\r\n\t\t\tconst stats = template.statistics?.[stat];\r\n\t\t\tif (stats) {\r\n\t\t\t\tmax = template.statistics[removeAccents(stat).toLowerCase()].max;\r\n\t\t\t\tmin = template.statistics[removeAccents(stat).toLowerCase()].min;\r\n\t\t\t}\r\n\t\t\tconst total = template.total || 100;\r\n\t\t\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\t\t\tnewDice = value.replace(regex, randomStatValue.toString());\r\n\t\t}\r\n\t}\r\n\treturn replaceFormulaInDice(newDice);\r\n}\r\n\r\n/**\r\n * Same as damageDice but for DiceType\r\n * @param dice {string}\r\n * @param template {StatisticalTemplate}\r\n */\r\nexport function diceTypeRandomParse(dice: string, template: StatisticalTemplate) {\r\n\tif (!template.statistics) return dice;\r\n\tconst firstStatNotCombinaison = Object.keys(template.statistics).find(stat => !template.statistics?.[stat].combinaison);\r\n\tif (!firstStatNotCombinaison) return dice;\r\n\tconst stats = template.statistics[firstStatNotCombinaison];\r\n\tconst {min, max} = stats;\r\n\tconst total = template.total || 100;\r\n\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\treturn replaceFormulaInDice(dice.replace(\"$\", randomStatValue.toString()));\r\n}\r\n\r\n/**\r\n * Random the combinaison and evaluate it to check if everything is valid\r\n * @param combinaison {[name: string]: string}\r\n * @param stats {[name: string]: string|number}\r\n */\r\nexport function evalCombinaison(combinaison: {[name: string]: string}, stats: {[name: string]: string | number}) {\r\n\tconst newStats: {[name: string]: number} = {};\r\n\tfor (const [stat, combin] of Object.entries(combinaison)) {\r\n\t\t//replace the stats in formula\r\n\t\tlet formula = removeAccents(combin);\r\n\t\tfor (const [statName, value] of Object.entries(stats)) {\r\n\t\t\tconst regex = new RegExp(removeAccents(statName), \"gi\");\r\n\t\t\tformula = formula.replace(regex, value.toString());\r\n\t\t}\r\n\t\ttry {\r\n\t\t\tconst result = evaluate(formula);\r\n\t\t\tnewStats[stat] = result;\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidFormula, common.space]: ${stat}`);\r\n\t\t}\r\n\t}\r\n\treturn newStats;\r\n}\r\n\r\n/**\r\n * Evaluate one selected combinaison\r\n * @param combinaison {string}\r\n * @param stats {[name: string]: string|number}\r\n */\r\nexport function evalOneCombinaison(combinaison: string, stats: {[name: string]: string | number}) {\r\n\tlet formula = removeAccents(combinaison);\r\n\tfor (const [statName, value] of Object.entries(stats)) {\r\n\t\tconst regex = new RegExp(removeAccents(statName), \"gi\");\r\n\t\tformula = formula.replace(regex, value.toString());\r\n\t}\r\n\ttry {\r\n\t\treturn evaluate(formula);\r\n\t} catch (error) {\r\n\t\tthrow new Error(`[error.invalidFormula, common.space]: ${combinaison}`);\r\n\t}\r\n}\r\n\r\n/**\r\n * Parse the provided JSON and verify each field to check if everything could work when rolling\r\n * @param {any} template \r\n * @returns {StatisticalTemplate}\r\n */\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport function verifyTemplateValue(template: any): StatisticalTemplate {\r\n\tconst statistiqueTemplate: StatisticalTemplate = {\r\n\t\tdiceType: \"\",\r\n\t\tstatistics: {} as Statistic\r\n\t};\r\n\tif (!template.statistics) statistiqueTemplate.statistics = undefined;\r\n\telse if (template.statistics && Object.keys(template.statistics).length > 0) {\r\n\t\tfor (const [key, value] of Object.entries(template.statistics)) {\r\n\t\t\tconst dataValue = value as { max?: number, min?: number, combinaison?: string };\r\n\t\t\tif (dataValue.max && dataValue.min && dataValue.max <= dataValue.min)\r\n\t\t\t\tthrow new Error(\"[error.maxGreater]\");\t\t\t\t\r\n\t\t\tif (dataValue.max && dataValue.max <= 0 ) dataValue.max = undefined;\r\n\t\t\tif (dataValue.min && dataValue.min <= 0 ) dataValue.min = undefined;\r\n\t\t\tlet formula = dataValue.combinaison ? removeAccents(dataValue.combinaison).toLowerCase() : undefined;\r\n\t\t\tformula = formula && formula.trim().length > 0 ? formula : undefined;\r\n\t\t\tif (!statistiqueTemplate.statistics) {\r\n\t\t\t\tstatistiqueTemplate.statistics = {} as Statistic;\r\n\t\t\t}\r\n\t\t\tstatistiqueTemplate.statistics[key] = {\r\n\t\t\t\tmax: dataValue.max,\r\n\t\t\t\tmin: dataValue.min,\r\n\t\t\t\tcombinaison: formula || undefined,\r\n\t\t\t};\r\n\t\t}\r\n\t}\r\n\tif (template.diceType) {\r\n\t\ttry {\r\n\t\t\tstatistiqueTemplate.diceType = template.diceType;\r\n\t\t\tdiceTypeRandomParse(template.diceType, statistiqueTemplate);\r\n\t\t} catch (e) {\r\n\t\t\tthrow new Error((e as Error).message);\r\n\t\t}\r\n\t}\r\n\r\n\t\r\n\tif (template.critical && Object.keys(template.critical).length > 0){\r\n\t\tstatistiqueTemplate.critical = {\r\n\t\t\tfailure: template.critical.failure ?? undefined,\r\n\t\t\tsuccess: template.critical.success ?? undefined\r\n\t\t};\r\n\r\n\t}\r\n\tif (template.total) {\r\n\t\tif (template.total <= 0)\r\n\t\t\ttemplate.total = undefined;\r\n\t\tstatistiqueTemplate.total = template.total;\r\n\t}\r\n\tif (template.charName) statistiqueTemplate.charName = template.charName;\r\n\tif (template.damage) statistiqueTemplate.damage = template.damage;\r\n\ttry {\r\n\t\ttestDamageRoll(statistiqueTemplate);\r\n\t\ttestCombinaison(statistiqueTemplate);\r\n\t} catch (error) {\r\n\t\tthrow new Error((error as Error).message);\r\n\t}\r\n\treturn statistiqueTemplate;\r\n}\r\n\r\n/**\r\n * Test each damage roll from the template.damage\r\n * @param {StatisticalTemplate} template \r\n */\r\nexport function testDamageRoll(template: StatisticalTemplate) {\r\n\tif (!template.damage) return;\r\n\tif (Object.keys(template.damage).length === 0) throw new Error(\"[error.emptyObject]\");\r\n\tif (Object.keys(template.damage).length > 25) throw new Error(\"[error.tooManyDice]\");\r\n\tfor (const [name, dice] of Object.entries(template.damage)) {\r\n\t\tif (!dice) continue;\r\n\t\tconst randomDiceParsed = diceRandomParse(dice, template);\r\n\t\ttry {\r\n\t\t\troll(randomDiceParsed);\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidDice, common.space] ${name}`);\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\r\n\r\n/**\r\n * Test all combinaison with generated random value\r\n * @param {StatisticalTemplate} template \r\n */\r\nexport function testCombinaison(template: StatisticalTemplate) {\r\n\tif (!template.statistics) return;\r\n\tconst onlyCombinaisonStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => value.combinaison !== undefined));\r\n\tconst allOtherStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => !value.combinaison));\t\r\n\tif (Object.keys(onlyCombinaisonStats).length===0) return;\r\n\tconst allStats = Object.keys(template.statistics).filter(stat => !template.statistics![stat].combinaison);\r\n\tif (allStats.length === 0) \r\n\t\tthrow new Error(\"[error.noStat]\");\r\n\tconst error= [];\r\n\tfor (const [stat, value] of Object.entries(onlyCombinaisonStats)) {\r\n\t\tlet formula = value.combinaison as string;\r\n\t\tfor (const [other, data] of Object.entries(allOtherStats)) {\r\n\t\t\tconst {max, min} = data;\r\n\t\t\tconst total = template.total || 100;\r\n\t\t\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\t\t\tconst regex = new RegExp(other, \"gi\");\r\n\t\t\tformula = formula.replace(regex, randomStatValue.toString());\r\n\t\t}\r\n\t\ttry {\r\n\t\t\tevaluate(formula);\r\n\t\t} catch (e) {\r\n\t\t\terror.push(stat);\r\n\t\t}\r\n\t}\r\n\tif (error.length > 0) \r\n\t\tthrow new Error(`[error.invalidFormula, common.space] ${error.join(\", \")}`);\r\n\treturn;\r\n}\r\n\r\n/**\r\n * Generate a random stat based on the template and the statistical min and max\r\n * @param {number|undefined} total\r\n * @param {number | undefined} max \r\n * @param {number | undefined} min \r\n * @returns \r\n */\r\nexport function generateRandomStat(total: number | undefined = 100, max?: number, min?: number) {\r\n\tlet randomStatValue = total + 1;\r\n\twhile (randomStatValue >= total) {\r\n\t\tconst random = new Random();\r\n\t\tif (max && min)\r\n\t\t\trandomStatValue = random.integer(min, max);\r\n\t\telse if (max)\r\n\t\t\trandomStatValue = random.integer(0, max);\r\n\t\telse if (min)\r\n\t\t\trandomStatValue = random.integer(min, total);\r\n\t\telse\r\n\t\t\trandomStatValue = random.integer(0, total);\r\n\t}\r\n\treturn randomStatValue;\r\n}\r\n\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,6BAA2B;AAC3B,oBAAyB;AAIlB,IAAM,gBAAgB;AAC7B,IAAM,aAAY;AAClB,IAAM,mBAAmB;AAMlB,SAAS,KAAK,MAAmC;AAEvD,MAAI,CAAC,KAAK,SAAS,GAAG;AAAG,WAAO;AAChC,QAAM,eAAe,KAAK,MAAM,gBAAgB;AAChD,MAAI;AACJ,MAAI,cAAc;AACjB,WAAO,KAAK,QAAQ,kBAAkB,EAAE;AACxC,UAAM,OAAO,aAAa,CAAC;AAC3B,UAAM,OAAO,KAAK,MAAM,YAAY,IAAI,CAAC;AACzC,UAAM,cAAc,aAAa,CAAC,EAAE,MAAM,UAAU,IAAI,CAAC;AACzD,QAAI,MAAM;AACT,YAAM,SAAS,KAAK,QAAQ,YAAY,EAAE,EAAE,QAAQ,OAAO,EAAE;AAC7D,YAAM,YAAQ,wBAAS,MAAM;AAC7B,aAAO,KAAK,QAAQ,kBAAkB,GAAG,WAAW,GAAG,KAAK,EAAE;AAC9D,gBAAU;AAAA,QACT,MAAM;AAAA,QACN,OAAO;AAAA,MACR;AAAA,IACD;AAAO,gBAAU;AAAA,QAChB,MAAM;AAAA,QACN,OAAO,SAAS,MAAM,EAAE;AAAA,MACzB;AAAA,EACD;AACA,QAAM,WAAW,KAAK,SAAS,iCAAiC;AAChE,MAAI;AACJ,aAAW,OAAO,UAAU;AAE3B,QAAI,aAAa;AAChB,YAAM,OAAO,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OAAO,QAAQ,GAAG,KAAK;AAAA,EAC3C;AACA,SAAO;AACR;","names":["roller","import_mathjs","removeAccents","formula","import_mathjs","import_remove_accents","removeAccents"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/dice.ts","../src/utils.ts","../src/verify_template.ts"],"sourcesContent":["export * from \"./dice\";\r\nexport * from \"./interface\";\r\nexport * from \"./utils\";\r\nexport * from \"./verify_template\";","/* eslint-disable no-useless-escape */\r\nimport { DiceRoller } from \"@dice-roller/rpg-dice-roller\";\r\nimport { evaluate } from \"mathjs\";\r\n\r\nimport { Compare, Modifier, Resultat, Sign } from \".\";\r\n\r\nexport const COMMENT_REGEX = /\\s+(#|\\/{2}|\\[|\\/\\*)(.*)/;\r\nconst SIGN_REGEX =/[><=!]+/;\r\nconst SIGN_REGEX_SPACE = /[><=!]+(\\S+)/;\r\n\r\n/**\r\n * Parse the string provided and turn it as a readable dice for dice parser\r\n * @param dice {string}\r\n */\r\nexport function roll(dice: string): Resultat | undefined{\r\n\t//parse dice string\r\n\tif (!dice.includes(\"d\")) return undefined;\r\n\tconst compareRegex = dice.match(SIGN_REGEX_SPACE);\r\n\tlet compare : Compare | undefined;\r\n\tif (compareRegex) {\r\n\t\tdice = dice.replace(SIGN_REGEX_SPACE, \"\");\r\n\t\tconst calc = compareRegex[1];\r\n\t\tconst sign = calc.match(/[+-\\/\\*\\^]/)?.[0];\r\n\t\tconst compareSign = compareRegex[0].match(SIGN_REGEX)?.[0];\r\n\t\tif (sign) {\r\n\t\t\tconst toCalc = calc.replace(SIGN_REGEX, \"\").replace(/\\s/g, \"\");\r\n\t\t\tconst total = evaluate(toCalc);\r\n\t\t\tdice = dice.replace(SIGN_REGEX_SPACE, `${compareSign}${total}`);\r\n\t\t\tcompare = {\r\n\t\t\t\tsign: compareSign as \"<\" | \">\" | \">=\" | \"<=\" | \"=\" | \"!=\" | \"==\",\r\n\t\t\t\tvalue: total,\r\n\t\t\t};\r\n\t\t} else compare = {\r\n\t\t\tsign: compareSign as \"<\" | \">\" | \">=\" | \"<=\" | \"=\" | \"!=\" | \"==\",\r\n\t\t\tvalue: parseInt(calc, 10),\r\n\t\t};\r\n\t}\r\n\tconst modifier = dice.matchAll(/(\\+|\\-|%|\\/|\\^|\\*|\\*{2})(\\d+)/gi);\r\n\tlet modificator : Modifier | undefined;\r\n\tfor (const mod of modifier) {\r\n\t\t//calculate the modifier if multiple\r\n\t\tif (modificator) {\r\n\t\t\tconst sign = modificator.sign;\r\n\t\t\tlet value = modificator.value;\r\n\t\t\tif (sign)\r\n\t\t\t\tvalue = calculator(sign, value, parseInt(mod[2], 10));\r\n\t\t\tmodificator = {\r\n\t\t\t\tsign: mod[1] as Sign,\r\n\t\t\t\tvalue,\r\n\t\t\t};\r\n\t\t} else {\r\n\t\t\tmodificator = {\r\n\t\t\t\tsign: mod[1] as Sign,\r\n\t\t\t\tvalue: parseInt(mod[2], 10),\r\n\t\t\t};\r\n\t\t}\r\n\t} \r\n\t\r\n\tif (dice.match(/\\d+?#(.*)/)) {\r\n\t\tconst diceArray = dice.split(\"#\");\r\n\t\tconst numberOfDice = parseInt(diceArray[0], 10);\r\n\t\tconst diceToRoll = diceArray[1].replace(COMMENT_REGEX, \"\");\r\n\t\tconst commentsMatch = diceArray[1].match(COMMENT_REGEX);\r\n\t\tconst comments = commentsMatch ? commentsMatch[2] : undefined;\r\n\t\tconst roller = new DiceRoller();\r\n\t\t//remove comments if any\r\n\t\tfor (let i = 0; i < numberOfDice; i++) {\r\n\t\t\troller.roll(diceToRoll);\r\n\t\t}\r\n\t\treturn {\r\n\t\t\tdice: diceToRoll,\r\n\t\t\tresult: roller.output,\r\n\t\t\tcomment: comments,\r\n\t\t\tcompare: compare ? compare : undefined,\r\n\t\t\tmodifier: modificator,\r\n\t\t};\r\n\t}\r\n\tconst roller = new DiceRoller();\r\n\tconst diceWithoutComment = dice.replace(COMMENT_REGEX, \"\");\r\n\troller.roll(diceWithoutComment);\r\n\tconst commentMatch = dice.match(COMMENT_REGEX);\r\n\tconst comment = commentMatch ? commentMatch[2] : undefined;\r\n\treturn {\r\n\t\tdice,\r\n\t\tresult: roller.output,\r\n\t\tcomment,\r\n\t\tcompare: compare ? compare : undefined,\r\n\t\tmodifier: modificator,\r\n\t};\r\n}\r\n/**\r\n * Evaluate a formula and replace \"^\" by \"**\" if any\r\n * @param {Sign} sign\r\n * @param {number} value \r\n * @param {number} total \r\n * @returns \r\n */\r\nexport function calculator(sign: Sign, value: number, total: number): number {\r\n\tif (sign === \"^\") sign = \"**\";\r\n\treturn evaluate(`${total} ${sign} ${value}`);\r\n}\r\n\r\n\r\n\r\n","import { evaluate } from \"mathjs\";\r\nimport removeAccents from \"remove-accents\";\r\n\r\n/**\r\n * Escape regex string\r\n * @param string {string}\r\n */\r\nexport function escapeRegex(string: string) {\r\n\treturn string.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\r\n}\r\n\r\n\r\n/**\r\n * Replace the stat name by their value using stat and after evaluate any formula using `replaceFormulaInDice`\r\n * @param originalDice {dice}\r\n * @param stats {[name: string]: number}\r\n */\r\nexport function generateStatsDice(originalDice: string, stats?: {[name: string]: number}) {\r\n\tlet dice = originalDice;\r\n\tif (stats && Object.keys(stats).length > 0) {\r\n\t\t//damage field support adding statistic, like : 1d6 + strength\r\n\t\t//check if the value contains a statistic & calculate if it's okay\r\n\t\t//the dice will be converted before roll \r\n\t\tconst allStats = Object.keys(stats);\r\n\t\tfor (const stat of allStats) {\r\n\t\t\tconst regex = new RegExp(escapeRegex(removeAccents(stat)), \"gi\");\r\n\t\t\tif (dice.match(regex)) {\r\n\t\t\t\tconst statValue = stats[stat];\r\n\t\t\t\tdice = dice.replace(regex, statValue.toString());\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn replaceFormulaInDice(dice);\r\n\t\r\n}\r\n\r\n/**\r\n * Replace the {{}} in the dice string and evaluate the interior if any\r\n * @param dice {string}\r\n */\r\nexport function replaceFormulaInDice(dice: string) {\r\n\tconst formula = /(?<formula>\\{{2}(.+?)\\}{2})/gmi;\r\n\tconst formulaMatch = formula.exec(dice);\r\n\tif (formulaMatch?.groups?.formula) {\r\n\t\tconst formula = formulaMatch.groups.formula.replaceAll(\"{{\", \"\").replaceAll(\"}}\", \"\");\r\n\t\ttry {\r\n\t\t\tconst result = evaluate(formula);\r\n\t\t\treturn cleanedDice(dice.replace(formulaMatch.groups.formula, result.toString()));\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidFormula, common.space]: ${formulaMatch.groups.formula}`);\r\n\t\t}\r\n\t}\r\n\treturn cleanedDice(dice);\r\n}\r\n\r\n/**\r\n * Replace the ++ +- -- by their proper value:\r\n * - `++` = `+`\r\n * - `+-` = `-`\r\n * - `--` = `+`\r\n * @param dice {string}\r\n */\r\nexport function cleanedDice(dice: string) {\r\n\treturn dice.replaceAll(\"+-\", \"-\").replaceAll(\"--\", \"+\").replaceAll(\"++\", \"+\");\r\n}","/* eslint-disable @typescript-eslint/no-unused-vars */\r\nimport { evaluate } from \"mathjs\";\r\nimport {Random } from \"random-js\";\r\nimport removeAccents from \"remove-accents\";\r\n\r\nimport { Statistic, StatisticalTemplate } from \".\";\r\nimport { roll } from \"./dice\";\r\nimport { escapeRegex, replaceFormulaInDice } from \"./utils\";\r\n\r\n/**\r\n * Verify if the provided dice work with random value\r\n * @param testDice {string}\r\n * @param stats {[name: string]: number}\r\n */\r\nexport function evalStatsDice(testDice: string, stats?: {[name: string]: number}) {\r\n\tlet dice = testDice;\r\n\tif (stats && Object.keys(stats).length > 0) {\r\n\t\tconst allStats = Object.keys(stats);\r\n\t\tfor (const stat of allStats) {\r\n\t\t\tconst regex = new RegExp(escapeRegex(removeAccents(stat)), \"gi\");\r\n\t\t\tif (testDice.match(regex)) {\r\n\t\t\t\tconst statValue = stats[stat];\r\n\t\t\t\tdice = testDice.replace(regex, statValue.toString());\r\n\t\t\t}\r\n\t\t}\r\n\t}\t\r\n\ttry {\r\n\t\tif (!roll(replaceFormulaInDice(dice))) throw new Error(`[error.invalidDice.withoutDice, common.space] ${dice}`);\r\n\t\treturn testDice;\r\n\t} catch (error) {\r\n\t\tthrow new Error(`[error.invalidDice.withoutDice, common.space]: ${testDice}\\n${(error as Error).message}`);\r\n\t}\r\n}\r\n\r\n/**\r\n * Generate a random dice and remove the formula (+ evaluate it)\r\n * Used for diceDamage only\r\n * @param value {string}\r\n * @param template {StatisticalTemplate}\r\n * @returns \r\n */\r\nexport function diceRandomParse(value: string, template: StatisticalTemplate) {\r\n\tif (!template.statistics) return value;\r\n\tvalue = removeAccents(value);\r\n\tconst allStats = Object.keys(template.statistics).map(stat => removeAccents(stat).toLowerCase());\r\n\tlet newDice = value;\r\n\tfor (const stat of allStats) {\r\n\t\tconst regex = new RegExp(escapeRegex(stat), \"gi\");\r\n\t\tif (value.match(regex)) {\r\n\t\t\tlet max: undefined | number = undefined;\r\n\t\t\tlet min: undefined | number = undefined;\r\n\t\t\tconst stats = template.statistics?.[stat];\r\n\t\t\tif (stats) {\r\n\t\t\t\tmax = template.statistics[removeAccents(stat).toLowerCase()].max;\r\n\t\t\t\tmin = template.statistics[removeAccents(stat).toLowerCase()].min;\r\n\t\t\t}\r\n\t\t\tconst total = template.total || 100;\r\n\t\t\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\t\t\tnewDice = value.replace(regex, randomStatValue.toString());\r\n\t\t}\r\n\t}\r\n\treturn replaceFormulaInDice(newDice);\r\n}\r\n\r\n/**\r\n * Same as damageDice but for DiceType\r\n * @param dice {string}\r\n * @param template {StatisticalTemplate}\r\n */\r\nexport function diceTypeRandomParse(dice: string, template: StatisticalTemplate) {\r\n\tif (!template.statistics) return dice;\r\n\tconst firstStatNotCombinaison = Object.keys(template.statistics).find(stat => !template.statistics?.[stat].combinaison);\r\n\tif (!firstStatNotCombinaison) return dice;\r\n\tconst stats = template.statistics[firstStatNotCombinaison];\r\n\tconst {min, max} = stats;\r\n\tconst total = template.total || 100;\r\n\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\treturn replaceFormulaInDice(dice.replace(\"$\", randomStatValue.toString()));\r\n}\r\n\r\n/**\r\n * Random the combinaison and evaluate it to check if everything is valid\r\n * @param combinaison {[name: string]: string}\r\n * @param stats {[name: string]: string|number}\r\n */\r\nexport function evalCombinaison(combinaison: {[name: string]: string}, stats: {[name: string]: string | number}) {\r\n\tconst newStats: {[name: string]: number} = {};\r\n\tfor (const [stat, combin] of Object.entries(combinaison)) {\r\n\t\t//replace the stats in formula\r\n\t\tlet formula = removeAccents(combin);\r\n\t\tfor (const [statName, value] of Object.entries(stats)) {\r\n\t\t\tconst regex = new RegExp(removeAccents(statName), \"gi\");\r\n\t\t\tformula = formula.replace(regex, value.toString());\r\n\t\t}\r\n\t\ttry {\r\n\t\t\tconst result = evaluate(formula);\r\n\t\t\tnewStats[stat] = result;\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidFormula, common.space]: ${stat}`);\r\n\t\t}\r\n\t}\r\n\treturn newStats;\r\n}\r\n\r\n/**\r\n * Evaluate one selected combinaison\r\n * @param combinaison {string}\r\n * @param stats {[name: string]: string|number}\r\n */\r\nexport function evalOneCombinaison(combinaison: string, stats: {[name: string]: string | number}) {\r\n\tlet formula = removeAccents(combinaison);\r\n\tfor (const [statName, value] of Object.entries(stats)) {\r\n\t\tconst regex = new RegExp(removeAccents(statName), \"gi\");\r\n\t\tformula = formula.replace(regex, value.toString());\r\n\t}\r\n\ttry {\r\n\t\treturn evaluate(formula);\r\n\t} catch (error) {\r\n\t\tthrow new Error(`[error.invalidFormula, common.space]: ${combinaison}`);\r\n\t}\r\n}\r\n\r\n/**\r\n * Parse the provided JSON and verify each field to check if everything could work when rolling\r\n * @param {any} template \r\n * @returns {StatisticalTemplate}\r\n */\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport function verifyTemplateValue(template: any): StatisticalTemplate {\r\n\tconst statistiqueTemplate: StatisticalTemplate = {\r\n\t\tdiceType: \"\",\r\n\t\tstatistics: {} as Statistic\r\n\t};\r\n\tif (!template.statistics) statistiqueTemplate.statistics = undefined;\r\n\telse if (template.statistics && Object.keys(template.statistics).length > 0) {\r\n\t\tfor (const [key, value] of Object.entries(template.statistics)) {\r\n\t\t\tconst dataValue = value as { max?: number, min?: number, combinaison?: string };\r\n\t\t\tif (dataValue.max && dataValue.min && dataValue.max <= dataValue.min)\r\n\t\t\t\tthrow new Error(\"[error.maxGreater]\");\t\t\t\t\r\n\t\t\tif (dataValue.max && dataValue.max <= 0 ) dataValue.max = undefined;\r\n\t\t\tif (dataValue.min && dataValue.min <= 0 ) dataValue.min = undefined;\r\n\t\t\tlet formula = dataValue.combinaison ? removeAccents(dataValue.combinaison).toLowerCase() : undefined;\r\n\t\t\tformula = formula && formula.trim().length > 0 ? formula : undefined;\r\n\t\t\tif (!statistiqueTemplate.statistics) {\r\n\t\t\t\tstatistiqueTemplate.statistics = {} as Statistic;\r\n\t\t\t}\r\n\t\t\tstatistiqueTemplate.statistics[key] = {\r\n\t\t\t\tmax: dataValue.max,\r\n\t\t\t\tmin: dataValue.min,\r\n\t\t\t\tcombinaison: formula || undefined,\r\n\t\t\t};\r\n\t\t}\r\n\t}\r\n\tif (template.diceType) {\r\n\t\ttry {\r\n\t\t\tstatistiqueTemplate.diceType = template.diceType;\r\n\t\t\tconst cleanedDice = diceTypeRandomParse(template.diceType, statistiqueTemplate);\r\n\t\t\tconst rolled = roll(cleanedDice);\r\n\t\t\tif (!rolled) throw new Error(\"[error.invalidDice.withoutDice]\");\r\n\t\t} catch (e) {\r\n\t\t\tthrow new Error((e as Error).message);\r\n\t\t}\r\n\t}\r\n\r\n\t\r\n\tif (template.critical && Object.keys(template.critical).length > 0){\r\n\t\tstatistiqueTemplate.critical = {\r\n\t\t\tfailure: template.critical.failure ?? undefined,\r\n\t\t\tsuccess: template.critical.success ?? undefined\r\n\t\t};\r\n\r\n\t}\r\n\tif (template.total) {\r\n\t\tif (template.total <= 0)\r\n\t\t\ttemplate.total = undefined;\r\n\t\tstatistiqueTemplate.total = template.total;\r\n\t}\r\n\tif (template.charName) statistiqueTemplate.charName = template.charName;\r\n\tif (template.damage) statistiqueTemplate.damage = template.damage;\r\n\ttry {\r\n\t\ttestDiceRegistered(statistiqueTemplate);\r\n\t\ttestStatCombinaison(statistiqueTemplate);\r\n\t} catch (error) {\r\n\t\tthrow new Error((error as Error).message);\r\n\t}\r\n\treturn statistiqueTemplate;\r\n}\r\n\r\n/**\r\n * Test each damage roll from the template.damage\r\n * @param {StatisticalTemplate} template \r\n */\r\nexport function testDiceRegistered(template: StatisticalTemplate) {\r\n\tif (!template.damage) return;\r\n\tif (Object.keys(template.damage).length === 0) throw new Error(\"[error.emptyObject]\");\r\n\tif (Object.keys(template.damage).length > 25) throw new Error(\"[error.tooManyDice]\");\r\n\tfor (const [name, dice] of Object.entries(template.damage)) {\r\n\t\tif (!dice) continue;\r\n\t\tconst randomDiceParsed = diceRandomParse(dice, template);\r\n\t\ttry {\r\n\t\t\tconst rolled = roll(randomDiceParsed);\r\n\t\t\tif (!rolled) throw new Error(`[error.invalidDice, common.space] ${name}`);\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidDice, common.space] ${name}`);\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\r\n\r\n/**\r\n * Test all combinaison with generated random value\r\n * @param {StatisticalTemplate} template \r\n */\r\nexport function testStatCombinaison(template: StatisticalTemplate) {\r\n\tif (!template.statistics) return;\r\n\tconst onlyCombinaisonStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => value.combinaison !== undefined));\r\n\tconst allOtherStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => !value.combinaison));\t\r\n\tif (Object.keys(onlyCombinaisonStats).length===0) return;\r\n\tconst allStats = Object.keys(template.statistics).filter(stat => !template.statistics![stat].combinaison);\r\n\tif (allStats.length === 0) \r\n\t\tthrow new Error(\"[error.noStat]\");\r\n\tconst error= [];\r\n\tfor (const [stat, value] of Object.entries(onlyCombinaisonStats)) {\r\n\t\tlet formula = value.combinaison as string;\r\n\t\tfor (const [other, data] of Object.entries(allOtherStats)) {\r\n\t\t\tconst {max, min} = data;\r\n\t\t\tconst total = template.total || 100;\r\n\t\t\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\t\t\tconst regex = new RegExp(other, \"gi\");\r\n\t\t\tformula = formula.replace(regex, randomStatValue.toString());\r\n\t\t}\r\n\t\ttry {\r\n\t\t\tevaluate(formula);\r\n\t\t} catch (e) {\r\n\t\t\terror.push(stat);\r\n\t\t}\r\n\t}\r\n\tif (error.length > 0) \r\n\t\tthrow new Error(`[error.invalidFormula, common.space] ${error.join(\", \")}`);\r\n\treturn;\r\n}\r\n\r\n/**\r\n * Generate a random stat based on the template and the statistical min and max\r\n * @param {number|undefined} total\r\n * @param {number | undefined} max \r\n * @param {number | undefined} min \r\n * @returns \r\n */\r\nexport function generateRandomStat(total: number | undefined = 100, max?: number, min?: number) {\r\n\tlet randomStatValue = total + 1;\r\n\twhile (randomStatValue >= total) {\r\n\t\tconst random = new Random();\r\n\t\tif (max && min)\r\n\t\t\trandomStatValue = random.integer(min, max);\r\n\t\telse if (max)\r\n\t\t\trandomStatValue = random.integer(0, max);\r\n\t\telse if (min)\r\n\t\t\trandomStatValue = random.integer(min, total);\r\n\t\telse\r\n\t\t\trandomStatValue = random.integer(0, total);\r\n\t}\r\n\treturn randomStatValue;\r\n}\r\n\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,6BAA2B;AAC3B,oBAAyB;AAIlB,IAAM,gBAAgB;AAC7B,IAAM,aAAY;AAClB,IAAM,mBAAmB;AAMlB,SAAS,KAAK,MAAmC;AAEvD,MAAI,CAAC,KAAK,SAAS,GAAG;AAAG,WAAO;AAChC,QAAM,eAAe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AK,KAAK,OAAO,QAAQ,oBAAoB,GAAG;AACjE,QAAI,UAAU,MAAM;AACpB,eAAW,CAAC,OAAO,IAAI,KAAK,OAAO,QAAQ,aAAa,GAAG;AAC1D,YAAM,EAAC,KAAK,IAAG,IAAI;AACnB,YAAM,QAAQ,SAAS,SAAS;AAChC,YAAM,kBAAkB,mBAAmB,OAAO,KAAK,GAAG;AAC1D,YAAM,QAAQ,IAAI,OAAO,OAAO,IAAI;AACpC,gBAAU,QAAQ,QAAQ,OAAO,gBAAgB,SAAS,CAAC;AAAA,IAC5D;AACA,QAAI;AACH,mCAAS,OAAO;AAAA,IACjB,SAAS,GAAG;AACX,YAAM,KAAK,IAAI;AAAA,IAChB;AAAA,EACD;AACA,MAAI,MAAM,SAAS;AAClB,UAAM,IAAI,MAAM,wCAAwC,MAAM,KAAK,IAAI,CAAC,EAAE;AAC3E;AACD;AASO,SAAS,mBAAmB,QAA4B,KAAK,KAAc,KAAc;AAC/F,MAAI,kBAAkB,QAAQ;AAC9B,SAAO,mBAAmB,OAAO;AAChC,UAAM,SAAS,IAAI,wBAAO;AAC1B,QAAI,OAAO;AACV,wBAAkB,OAAO,QAAQ,KAAK,GAAG;AAAA,aACjC;AACR,wBAAkB,OAAO,QAAQ,GAAG,GAAG;AAAA,aAC/B;AACR,wBAAkB,OAAO,QAAQ,KAAK,KAAK;AAAA;AAE3C,wBAAkB,OAAO,QAAQ,GAAG,KAAK;AAAA,EAC3C;AACA,SAAO;AACR;","names":["roller","import_mathjs","removeAccents","formula","import_mathjs","import_remove_accents","removeAccents","cleanedDice"]}
package/dist/index.mjs CHANGED
@@ -242,7 +242,10 @@ function verifyTemplateValue(template) {
242
242
  if (template.diceType) {
243
243
  try {
244
244
  statistiqueTemplate.diceType = template.diceType;
245
- diceTypeRandomParse(template.diceType, statistiqueTemplate);
245
+ const cleanedDice2 = diceTypeRandomParse(template.diceType, statistiqueTemplate);
246
+ const rolled = roll(cleanedDice2);
247
+ if (!rolled)
248
+ throw new Error("[error.invalidDice.withoutDice]");
246
249
  } catch (e) {
247
250
  throw new Error(e.message);
248
251
  }
@@ -263,14 +266,14 @@ function verifyTemplateValue(template) {
263
266
  if (template.damage)
264
267
  statistiqueTemplate.damage = template.damage;
265
268
  try {
266
- testDamageRoll(statistiqueTemplate);
267
- testCombinaison(statistiqueTemplate);
269
+ testDiceRegistered(statistiqueTemplate);
270
+ testStatCombinaison(statistiqueTemplate);
268
271
  } catch (error) {
269
272
  throw new Error(error.message);
270
273
  }
271
274
  return statistiqueTemplate;
272
275
  }
273
- function testDamageRoll(template) {
276
+ function testDiceRegistered(template) {
274
277
  if (!template.damage)
275
278
  return;
276
279
  if (Object.keys(template.damage).length === 0)
@@ -282,13 +285,15 @@ function testDamageRoll(template) {
282
285
  continue;
283
286
  const randomDiceParsed = diceRandomParse(dice, template);
284
287
  try {
285
- roll(randomDiceParsed);
288
+ const rolled = roll(randomDiceParsed);
289
+ if (!rolled)
290
+ throw new Error(`[error.invalidDice, common.space] ${name}`);
286
291
  } catch (error) {
287
292
  throw new Error(`[error.invalidDice, common.space] ${name}`);
288
293
  }
289
294
  }
290
295
  }
291
- function testCombinaison(template) {
296
+ function testStatCombinaison(template) {
292
297
  if (!template.statistics)
293
298
  return;
294
299
  const onlyCombinaisonStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => value.combinaison !== void 0));
@@ -347,8 +352,8 @@ export {
347
352
  generateStatsDice,
348
353
  replaceFormulaInDice,
349
354
  roll,
350
- testCombinaison,
351
- testDamageRoll,
355
+ testDiceRegistered,
356
+ testStatCombinaison,
352
357
  verifyTemplateValue
353
358
  };
354
359
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/dice.ts","../src/utils.ts","../src/verify_template.ts"],"sourcesContent":["/* eslint-disable no-useless-escape */\r\nimport { DiceRoller } from \"@dice-roller/rpg-dice-roller\";\r\nimport { evaluate } from \"mathjs\";\r\n\r\nimport { Compare, Modifier, Resultat, Sign } from \".\";\r\n\r\nexport const COMMENT_REGEX = /\\s+(#|\\/{2}|\\[|\\/\\*)(.*)/;\r\nconst SIGN_REGEX =/[><=!]+/;\r\nconst SIGN_REGEX_SPACE = /[><=!]+(\\S+)/;\r\n\r\n/**\r\n * Parse the string provided and turn it as a readable dice for dice parser\r\n * @param dice {string}\r\n */\r\nexport function roll(dice: string): Resultat | undefined{\r\n\t//parse dice string\r\n\tif (!dice.includes(\"d\")) return undefined;\r\n\tconst compareRegex = dice.match(SIGN_REGEX_SPACE);\r\n\tlet compare : Compare | undefined;\r\n\tif (compareRegex) {\r\n\t\tdice = dice.replace(SIGN_REGEX_SPACE, \"\");\r\n\t\tconst calc = compareRegex[1];\r\n\t\tconst sign = calc.match(/[+-\\/\\*\\^]/)?.[0];\r\n\t\tconst compareSign = compareRegex[0].match(SIGN_REGEX)?.[0];\r\n\t\tif (sign) {\r\n\t\t\tconst toCalc = calc.replace(SIGN_REGEX, \"\").replace(/\\s/g, \"\");\r\n\t\t\tconst total = evaluate(toCalc);\r\n\t\t\tdice = dice.replace(SIGN_REGEX_SPACE, `${compareSign}${total}`);\r\n\t\t\tcompare = {\r\n\t\t\t\tsign: compareSign as \"<\" | \">\" | \">=\" | \"<=\" | \"=\" | \"!=\" | \"==\",\r\n\t\t\t\tvalue: total,\r\n\t\t\t};\r\n\t\t} else compare = {\r\n\t\t\tsign: compareSign as \"<\" | \">\" | \">=\" | \"<=\" | \"=\" | \"!=\" | \"==\",\r\n\t\t\tvalue: parseInt(calc, 10),\r\n\t\t};\r\n\t}\r\n\tconst modifier = dice.matchAll(/(\\+|\\-|%|\\/|\\^|\\*|\\*{2})(\\d+)/gi);\r\n\tlet modificator : Modifier | undefined;\r\n\tfor (const mod of modifier) {\r\n\t\t//calculate the modifier if multiple\r\n\t\tif (modificator) {\r\n\t\t\tconst sign = modificator.sign;\r\n\t\t\tlet value = modificator.value;\r\n\t\t\tif (sign)\r\n\t\t\t\tvalue = calculator(sign, value, parseInt(mod[2], 10));\r\n\t\t\tmodificator = {\r\n\t\t\t\tsign: mod[1] as Sign,\r\n\t\t\t\tvalue,\r\n\t\t\t};\r\n\t\t} else {\r\n\t\t\tmodificator = {\r\n\t\t\t\tsign: mod[1] as Sign,\r\n\t\t\t\tvalue: parseInt(mod[2], 10),\r\n\t\t\t};\r\n\t\t}\r\n\t} \r\n\t\r\n\tif (dice.match(/\\d+?#(.*)/)) {\r\n\t\tconst diceArray = dice.split(\"#\");\r\n\t\tconst numberOfDice = parseInt(diceArray[0], 10);\r\n\t\tconst diceToRoll = diceArray[1].replace(COMMENT_REGEX, \"\");\r\n\t\tconst commentsMatch = diceArray[1].match(COMMENT_REGEX);\r\n\t\tconst comments = commentsMatch ? commentsMatch[2] : undefined;\r\n\t\tconst roller = new DiceRoller();\r\n\t\t//remove comments if any\r\n\t\tfor (let i = 0; i < numberOfDice; i++) {\r\n\t\t\troller.roll(diceToRoll);\r\n\t\t}\r\n\t\treturn {\r\n\t\t\tdice: diceToRoll,\r\n\t\t\tresult: roller.output,\r\n\t\t\tcomment: comments,\r\n\t\t\tcompare: compare ? compare : undefined,\r\n\t\t\tmodifier: modificator,\r\n\t\t};\r\n\t}\r\n\tconst roller = new DiceRoller();\r\n\tconst diceWithoutComment = dice.replace(COMMENT_REGEX, \"\");\r\n\troller.roll(diceWithoutComment);\r\n\tconst commentMatch = dice.match(COMMENT_REGEX);\r\n\tconst comment = commentMatch ? commentMatch[2] : undefined;\r\n\treturn {\r\n\t\tdice,\r\n\t\tresult: roller.output,\r\n\t\tcomment,\r\n\t\tcompare: compare ? compare : undefined,\r\n\t\tmodifier: modificator,\r\n\t};\r\n}\r\n/**\r\n * Evaluate a formula and replace \"^\" by \"**\" if any\r\n * @param {Sign} sign\r\n * @param {number} value \r\n * @param {number} total \r\n * @returns \r\n */\r\nexport function calculator(sign: Sign, value: number, total: number): number {\r\n\tif (sign === \"^\") sign = \"**\";\r\n\treturn evaluate(`${total} ${sign} ${value}`);\r\n}\r\n\r\n\r\n\r\n","import { evaluate } from \"mathjs\";\r\nimport removeAccents from \"remove-accents\";\r\n\r\n/**\r\n * Escape regex string\r\n * @param string {string}\r\n */\r\nexport function escapeRegex(string: string) {\r\n\treturn string.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\r\n}\r\n\r\n\r\n/**\r\n * Replace the stat name by their value using stat and after evaluate any formula using `replaceFormulaInDice`\r\n * @param originalDice {dice}\r\n * @param stats {[name: string]: number}\r\n */\r\nexport function generateStatsDice(originalDice: string, stats?: {[name: string]: number}) {\r\n\tlet dice = originalDice;\r\n\tif (stats && Object.keys(stats).length > 0) {\r\n\t\t//damage field support adding statistic, like : 1d6 + strength\r\n\t\t//check if the value contains a statistic & calculate if it's okay\r\n\t\t//the dice will be converted before roll \r\n\t\tconst allStats = Object.keys(stats);\r\n\t\tfor (const stat of allStats) {\r\n\t\t\tconst regex = new RegExp(escapeRegex(removeAccents(stat)), \"gi\");\r\n\t\t\tif (dice.match(regex)) {\r\n\t\t\t\tconst statValue = stats[stat];\r\n\t\t\t\tdice = dice.replace(regex, statValue.toString());\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn replaceFormulaInDice(dice);\r\n\t\r\n}\r\n\r\n/**\r\n * Replace the {{}} in the dice string and evaluate the interior if any\r\n * @param dice {string}\r\n */\r\nexport function replaceFormulaInDice(dice: string) {\r\n\tconst formula = /(?<formula>\\{{2}(.+?)\\}{2})/gmi;\r\n\tconst formulaMatch = formula.exec(dice);\r\n\tif (formulaMatch?.groups?.formula) {\r\n\t\tconst formula = formulaMatch.groups.formula.replaceAll(\"{{\", \"\").replaceAll(\"}}\", \"\");\r\n\t\ttry {\r\n\t\t\tconst result = evaluate(formula);\r\n\t\t\treturn cleanedDice(dice.replace(formulaMatch.groups.formula, result.toString()));\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidFormula, common.space]: ${formulaMatch.groups.formula}`);\r\n\t\t}\r\n\t}\r\n\treturn cleanedDice(dice);\r\n}\r\n\r\n/**\r\n * Replace the ++ +- -- by their proper value:\r\n * - `++` = `+`\r\n * - `+-` = `-`\r\n * - `--` = `+`\r\n * @param dice {string}\r\n */\r\nexport function cleanedDice(dice: string) {\r\n\treturn dice.replaceAll(\"+-\", \"-\").replaceAll(\"--\", \"+\").replaceAll(\"++\", \"+\");\r\n}","/* eslint-disable @typescript-eslint/no-unused-vars */\r\nimport { evaluate } from \"mathjs\";\r\nimport {Random } from \"random-js\";\r\nimport removeAccents from \"remove-accents\";\r\n\r\nimport { Statistic, StatisticalTemplate } from \".\";\r\nimport { roll } from \"./dice\";\r\nimport { escapeRegex, replaceFormulaInDice } from \"./utils\";\r\n\r\n/**\r\n * Verify if the provided dice work with random value\r\n * @param testDice {string}\r\n * @param stats {[name: string]: number}\r\n */\r\nexport function evalStatsDice(testDice: string, stats?: {[name: string]: number}) {\r\n\tlet dice = testDice;\r\n\tif (stats && Object.keys(stats).length > 0) {\r\n\t\tconst allStats = Object.keys(stats);\r\n\t\tfor (const stat of allStats) {\r\n\t\t\tconst regex = new RegExp(escapeRegex(removeAccents(stat)), \"gi\");\r\n\t\t\tif (testDice.match(regex)) {\r\n\t\t\t\tconst statValue = stats[stat];\r\n\t\t\t\tdice = testDice.replace(regex, statValue.toString());\r\n\t\t\t}\r\n\t\t}\r\n\t}\t\r\n\ttry {\r\n\t\tif (!roll(replaceFormulaInDice(dice))) throw new Error(`[error.invalidDice.withoutDice, common.space] ${dice}`);\r\n\t\treturn testDice;\r\n\t} catch (error) {\r\n\t\tthrow new Error(`[error.invalidDice.withoutDice, common.space]: ${testDice}\\n${(error as Error).message}`);\r\n\t}\r\n}\r\n\r\n/**\r\n * Generate a random dice and remove the formula (+ evaluate it)\r\n * Used for diceDamage only\r\n * @param value {string}\r\n * @param template {StatisticalTemplate}\r\n * @returns \r\n */\r\nexport function diceRandomParse(value: string, template: StatisticalTemplate) {\r\n\tif (!template.statistics) return value;\r\n\tvalue = removeAccents(value);\r\n\tconst allStats = Object.keys(template.statistics).map(stat => removeAccents(stat).toLowerCase());\r\n\tlet newDice = value;\r\n\tfor (const stat of allStats) {\r\n\t\tconst regex = new RegExp(escapeRegex(stat), \"gi\");\r\n\t\tif (value.match(regex)) {\r\n\t\t\tlet max: undefined | number = undefined;\r\n\t\t\tlet min: undefined | number = undefined;\r\n\t\t\tconst stats = template.statistics?.[stat];\r\n\t\t\tif (stats) {\r\n\t\t\t\tmax = template.statistics[removeAccents(stat).toLowerCase()].max;\r\n\t\t\t\tmin = template.statistics[removeAccents(stat).toLowerCase()].min;\r\n\t\t\t}\r\n\t\t\tconst total = template.total || 100;\r\n\t\t\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\t\t\tnewDice = value.replace(regex, randomStatValue.toString());\r\n\t\t}\r\n\t}\r\n\treturn replaceFormulaInDice(newDice);\r\n}\r\n\r\n/**\r\n * Same as damageDice but for DiceType\r\n * @param dice {string}\r\n * @param template {StatisticalTemplate}\r\n */\r\nexport function diceTypeRandomParse(dice: string, template: StatisticalTemplate) {\r\n\tif (!template.statistics) return dice;\r\n\tconst firstStatNotCombinaison = Object.keys(template.statistics).find(stat => !template.statistics?.[stat].combinaison);\r\n\tif (!firstStatNotCombinaison) return dice;\r\n\tconst stats = template.statistics[firstStatNotCombinaison];\r\n\tconst {min, max} = stats;\r\n\tconst total = template.total || 100;\r\n\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\treturn replaceFormulaInDice(dice.replace(\"$\", randomStatValue.toString()));\r\n}\r\n\r\n/**\r\n * Random the combinaison and evaluate it to check if everything is valid\r\n * @param combinaison {[name: string]: string}\r\n * @param stats {[name: string]: string|number}\r\n */\r\nexport function evalCombinaison(combinaison: {[name: string]: string}, stats: {[name: string]: string | number}) {\r\n\tconst newStats: {[name: string]: number} = {};\r\n\tfor (const [stat, combin] of Object.entries(combinaison)) {\r\n\t\t//replace the stats in formula\r\n\t\tlet formula = removeAccents(combin);\r\n\t\tfor (const [statName, value] of Object.entries(stats)) {\r\n\t\t\tconst regex = new RegExp(removeAccents(statName), \"gi\");\r\n\t\t\tformula = formula.replace(regex, value.toString());\r\n\t\t}\r\n\t\ttry {\r\n\t\t\tconst result = evaluate(formula);\r\n\t\t\tnewStats[stat] = result;\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidFormula, common.space]: ${stat}`);\r\n\t\t}\r\n\t}\r\n\treturn newStats;\r\n}\r\n\r\n/**\r\n * Evaluate one selected combinaison\r\n * @param combinaison {string}\r\n * @param stats {[name: string]: string|number}\r\n */\r\nexport function evalOneCombinaison(combinaison: string, stats: {[name: string]: string | number}) {\r\n\tlet formula = removeAccents(combinaison);\r\n\tfor (const [statName, value] of Object.entries(stats)) {\r\n\t\tconst regex = new RegExp(removeAccents(statName), \"gi\");\r\n\t\tformula = formula.replace(regex, value.toString());\r\n\t}\r\n\ttry {\r\n\t\treturn evaluate(formula);\r\n\t} catch (error) {\r\n\t\tthrow new Error(`[error.invalidFormula, common.space]: ${combinaison}`);\r\n\t}\r\n}\r\n\r\n/**\r\n * Parse the provided JSON and verify each field to check if everything could work when rolling\r\n * @param {any} template \r\n * @returns {StatisticalTemplate}\r\n */\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport function verifyTemplateValue(template: any): StatisticalTemplate {\r\n\tconst statistiqueTemplate: StatisticalTemplate = {\r\n\t\tdiceType: \"\",\r\n\t\tstatistics: {} as Statistic\r\n\t};\r\n\tif (!template.statistics) statistiqueTemplate.statistics = undefined;\r\n\telse if (template.statistics && Object.keys(template.statistics).length > 0) {\r\n\t\tfor (const [key, value] of Object.entries(template.statistics)) {\r\n\t\t\tconst dataValue = value as { max?: number, min?: number, combinaison?: string };\r\n\t\t\tif (dataValue.max && dataValue.min && dataValue.max <= dataValue.min)\r\n\t\t\t\tthrow new Error(\"[error.maxGreater]\");\t\t\t\t\r\n\t\t\tif (dataValue.max && dataValue.max <= 0 ) dataValue.max = undefined;\r\n\t\t\tif (dataValue.min && dataValue.min <= 0 ) dataValue.min = undefined;\r\n\t\t\tlet formula = dataValue.combinaison ? removeAccents(dataValue.combinaison).toLowerCase() : undefined;\r\n\t\t\tformula = formula && formula.trim().length > 0 ? formula : undefined;\r\n\t\t\tif (!statistiqueTemplate.statistics) {\r\n\t\t\t\tstatistiqueTemplate.statistics = {} as Statistic;\r\n\t\t\t}\r\n\t\t\tstatistiqueTemplate.statistics[key] = {\r\n\t\t\t\tmax: dataValue.max,\r\n\t\t\t\tmin: dataValue.min,\r\n\t\t\t\tcombinaison: formula || undefined,\r\n\t\t\t};\r\n\t\t}\r\n\t}\r\n\tif (template.diceType) {\r\n\t\ttry {\r\n\t\t\tstatistiqueTemplate.diceType = template.diceType;\r\n\t\t\tdiceTypeRandomParse(template.diceType, statistiqueTemplate);\r\n\t\t} catch (e) {\r\n\t\t\tthrow new Error((e as Error).message);\r\n\t\t}\r\n\t}\r\n\r\n\t\r\n\tif (template.critical && Object.keys(template.critical).length > 0){\r\n\t\tstatistiqueTemplate.critical = {\r\n\t\t\tfailure: template.critical.failure ?? undefined,\r\n\t\t\tsuccess: template.critical.success ?? undefined\r\n\t\t};\r\n\r\n\t}\r\n\tif (template.total) {\r\n\t\tif (template.total <= 0)\r\n\t\t\ttemplate.total = undefined;\r\n\t\tstatistiqueTemplate.total = template.total;\r\n\t}\r\n\tif (template.charName) statistiqueTemplate.charName = template.charName;\r\n\tif (template.damage) statistiqueTemplate.damage = template.damage;\r\n\ttry {\r\n\t\ttestDamageRoll(statistiqueTemplate);\r\n\t\ttestCombinaison(statistiqueTemplate);\r\n\t} catch (error) {\r\n\t\tthrow new Error((error as Error).message);\r\n\t}\r\n\treturn statistiqueTemplate;\r\n}\r\n\r\n/**\r\n * Test each damage roll from the template.damage\r\n * @param {StatisticalTemplate} template \r\n */\r\nexport function testDamageRoll(template: StatisticalTemplate) {\r\n\tif (!template.damage) return;\r\n\tif (Object.keys(template.damage).length === 0) throw new Error(\"[error.emptyObject]\");\r\n\tif (Object.keys(template.damage).length > 25) throw new Error(\"[error.tooManyDice]\");\r\n\tfor (const [name, dice] of Object.entries(template.damage)) {\r\n\t\tif (!dice) continue;\r\n\t\tconst randomDiceParsed = diceRandomParse(dice, template);\r\n\t\ttry {\r\n\t\t\troll(randomDiceParsed);\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidDice, common.space] ${name}`);\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\r\n\r\n/**\r\n * Test all combinaison with generated random value\r\n * @param {StatisticalTemplate} template \r\n */\r\nexport function testCombinaison(template: StatisticalTemplate) {\r\n\tif (!template.statistics) return;\r\n\tconst onlyCombinaisonStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => value.combinaison !== undefined));\r\n\tconst allOtherStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => !value.combinaison));\t\r\n\tif (Object.keys(onlyCombinaisonStats).length===0) return;\r\n\tconst allStats = Object.keys(template.statistics).filter(stat => !template.statistics![stat].combinaison);\r\n\tif (allStats.length === 0) \r\n\t\tthrow new Error(\"[error.noStat]\");\r\n\tconst error= [];\r\n\tfor (const [stat, value] of Object.entries(onlyCombinaisonStats)) {\r\n\t\tlet formula = value.combinaison as string;\r\n\t\tfor (const [other, data] of Object.entries(allOtherStats)) {\r\n\t\t\tconst {max, min} = data;\r\n\t\t\tconst total = template.total || 100;\r\n\t\t\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\t\t\tconst regex = new RegExp(other, \"gi\");\r\n\t\t\tformula = formula.replace(regex, randomStatValue.toString());\r\n\t\t}\r\n\t\ttry {\r\n\t\t\tevaluate(formula);\r\n\t\t} catch (e) {\r\n\t\t\terror.push(stat);\r\n\t\t}\r\n\t}\r\n\tif (error.length > 0) \r\n\t\tthrow new Error(`[error.invalidFormula, common.space] ${error.join(\", \")}`);\r\n\treturn;\r\n}\r\n\r\n/**\r\n * Generate a random stat based on the template and the statistical min and max\r\n * @param {number|undefined} total\r\n * @param {number | undefined} max \r\n * @param {number | undefined} min \r\n * @returns \r\n */\r\nexport function generateRandomStat(total: number | undefined = 100, max?: number, min?: number) {\r\n\tlet randomStatValue = total + 1;\r\n\twhile (randomStatValue >= total) {\r\n\t\tconst random = new Random();\r\n\t\tif (max && min)\r\n\t\t\trandomStatValue = random.integer(min, max);\r\n\t\telse if (max)\r\n\t\t\trandomStatValue = random.integer(0, max);\r\n\t\telse if (min)\r\n\t\t\trandomStatValue = random.integer(min, total);\r\n\t\telse\r\n\t\t\trandomStatValue = random.integer(0, total);\r\n\t}\r\n\treturn randomStatValue;\r\n}\r\n\r\n"],"mappings":";AACA,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB;AAIlB,IAAM,gBAAgB;AAC7B,IAAM,aAAY;AAClB,IAAM,mBAAmB;AAMlB,SAAS,KAAK,MAAmC;AAEvD,MAAI,CAAC,KAAK,SAAS,GAAG;AAAG,WAAO;AAChC,QAAM,eAAe,KAAK,MAAM,gBAAgB;AAChD,MAAI;AACJ,MAAI,cAAc;AACjB,WAAO,KAAK,QAAQ,kBAAkB,EAAE;AACxC,UAAM,OAAO,aAAa,CAAC;AAC3B,UAAM,OAAO,KAAK,MAAM,YAAY,IAAI,CAAC;AACzC,UAAM,cAAc,aAAa,CAAC,EAAE,MAAM,UAAU,IAAI,CAAC;AACzD,QAAI,MAAM;AACT,YAAM,SAAS,KAAK,QAAQ,YAAY,EAAE,EAAE,QAAQ,OAAO,EAAE;AAC7D,YAAM,QAAQ,SAAS,MAAM;AAC7B,aAAO,KAAK,QAAQ,kBAAkB,GAAG,WAAW,GAAG,KAAK,EAAE;AAC9D,gBAAU;AAAA,QACT,MAAM;AAAA,QACN,OAAO;AAAA,MACR;AAAA,IACD;AAAO,gBAAU;AAAA,QAChB,MAAM;AAAA,QACN,OAAO,SAAS,MAAM,EAAE;AAAA,MACzB;AAAA,EACD;AACA,QAAM,WAAW,KAAK,SAAS,iCAAiC;AAChE,MAAI;AACJ,aAAW,OAAO,UAAU;AAE3B,QAAI,aAAa;AAChB,YAAM,OAAO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aluate","formula","evaluate","removeAccents","removeAccents","evaluate"]}
1
+ {"version":3,"sources":["../src/dice.ts","../src/utils.ts","../src/verify_template.ts"],"sourcesContent":["/* eslint-disable no-useless-escape */\r\nimport { DiceRoller } from \"@dice-roller/rpg-dice-roller\";\r\nimport { evaluate } from \"mathjs\";\r\n\r\nimport { Compare, Modifier, Resultat, Sign } from \".\";\r\n\r\nexport const COMMENT_REGEX = /\\s+(#|\\/{2}|\\[|\\/\\*)(.*)/;\r\nconst SIGN_REGEX =/[><=!]+/;\r\nconst SIGN_REGEX_SPACE = /[><=!]+(\\S+)/;\r\n\r\n/**\r\n * Parse the string provided and turn it as a readable dice for dice parser\r\n * @param dice {string}\r\n */\r\nexport function roll(dice: string): Resultat | undefined{\r\n\t//parse dice string\r\n\tif (!dice.includes(\"d\")) return undefined;\r\n\tconst compareRegex = dice.match(SIGN_REGEX_SPACE);\r\n\tlet compare : Compare | undefined;\r\n\tif (compareRegex) {\r\n\t\tdice = dice.replace(SIGN_REGEX_SPACE, \"\");\r\n\t\tconst calc = compareRegex[1];\r\n\t\tconst sign = calc.match(/[+-\\/\\*\\^]/)?.[0];\r\n\t\tconst compareSign = compareRegex[0].match(SIGN_REGEX)?.[0];\r\n\t\tif (sign) {\r\n\t\t\tconst toCalc = calc.replace(SIGN_REGEX, \"\").replace(/\\s/g, \"\");\r\n\t\t\tconst total = evaluate(toCalc);\r\n\t\t\tdice = dice.replace(SIGN_REGEX_SPACE, `${compareSign}${total}`);\r\n\t\t\tcompare = {\r\n\t\t\t\tsign: compareSign as \"<\" | \">\" | \">=\" | \"<=\" | \"=\" | \"!=\" | \"==\",\r\n\t\t\t\tvalue: total,\r\n\t\t\t};\r\n\t\t} else compare = {\r\n\t\t\tsign: compareSign as \"<\" | \">\" | \">=\" | \"<=\" | \"=\" | \"!=\" | \"==\",\r\n\t\t\tvalue: parseInt(calc, 10),\r\n\t\t};\r\n\t}\r\n\tconst modifier = dice.matchAll(/(\\+|\\-|%|\\/|\\^|\\*|\\*{2})(\\d+)/gi);\r\n\tlet modificator : Modifier | undefined;\r\n\tfor (const mod of modifier) {\r\n\t\t//calculate the modifier if multiple\r\n\t\tif (modificator) {\r\n\t\t\tconst sign = modificator.sign;\r\n\t\t\tlet value = modificator.value;\r\n\t\t\tif (sign)\r\n\t\t\t\tvalue = calculator(sign, value, parseInt(mod[2], 10));\r\n\t\t\tmodificator = {\r\n\t\t\t\tsign: mod[1] as Sign,\r\n\t\t\t\tvalue,\r\n\t\t\t};\r\n\t\t} else {\r\n\t\t\tmodificator = {\r\n\t\t\t\tsign: mod[1] as Sign,\r\n\t\t\t\tvalue: parseInt(mod[2], 10),\r\n\t\t\t};\r\n\t\t}\r\n\t} \r\n\t\r\n\tif (dice.match(/\\d+?#(.*)/)) {\r\n\t\tconst diceArray = dice.split(\"#\");\r\n\t\tconst numberOfDice = parseInt(diceArray[0], 10);\r\n\t\tconst diceToRoll = diceArray[1].replace(COMMENT_REGEX, \"\");\r\n\t\tconst commentsMatch = diceArray[1].match(COMMENT_REGEX);\r\n\t\tconst comments = commentsMatch ? commentsMatch[2] : undefined;\r\n\t\tconst roller = new DiceRoller();\r\n\t\t//remove comments if any\r\n\t\tfor (let i = 0; i < numberOfDice; i++) {\r\n\t\t\troller.roll(diceToRoll);\r\n\t\t}\r\n\t\treturn {\r\n\t\t\tdice: diceToRoll,\r\n\t\t\tresult: roller.output,\r\n\t\t\tcomment: comments,\r\n\t\t\tcompare: compare ? compare : undefined,\r\n\t\t\tmodifier: modificator,\r\n\t\t};\r\n\t}\r\n\tconst roller = new DiceRoller();\r\n\tconst diceWithoutComment = dice.replace(COMMENT_REGEX, \"\");\r\n\troller.roll(diceWithoutComment);\r\n\tconst commentMatch = dice.match(COMMENT_REGEX);\r\n\tconst comment = commentMatch ? commentMatch[2] : undefined;\r\n\treturn {\r\n\t\tdice,\r\n\t\tresult: roller.output,\r\n\t\tcomment,\r\n\t\tcompare: compare ? compare : undefined,\r\n\t\tmodifier: modificator,\r\n\t};\r\n}\r\n/**\r\n * Evaluate a formula and replace \"^\" by \"**\" if any\r\n * @param {Sign} sign\r\n * @param {number} value \r\n * @param {number} total \r\n * @returns \r\n */\r\nexport function calculator(sign: Sign, value: number, total: number): number {\r\n\tif (sign === \"^\") sign = \"**\";\r\n\treturn evaluate(`${total} ${sign} ${value}`);\r\n}\r\n\r\n\r\n\r\n","import { evaluate } from \"mathjs\";\r\nimport removeAccents from \"remove-accents\";\r\n\r\n/**\r\n * Escape regex string\r\n * @param string {string}\r\n */\r\nexport function escapeRegex(string: string) {\r\n\treturn string.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\r\n}\r\n\r\n\r\n/**\r\n * Replace the stat name by their value using stat and after evaluate any formula using `replaceFormulaInDice`\r\n * @param originalDice {dice}\r\n * @param stats {[name: string]: number}\r\n */\r\nexport function generateStatsDice(originalDice: string, stats?: {[name: string]: number}) {\r\n\tlet dice = originalDice;\r\n\tif (stats && Object.keys(stats).length > 0) {\r\n\t\t//damage field support adding statistic, like : 1d6 + strength\r\n\t\t//check if the value contains a statistic & calculate if it's okay\r\n\t\t//the dice will be converted before roll \r\n\t\tconst allStats = Object.keys(stats);\r\n\t\tfor (const stat of allStats) {\r\n\t\t\tconst regex = new RegExp(escapeRegex(removeAccents(stat)), \"gi\");\r\n\t\t\tif (dice.match(regex)) {\r\n\t\t\t\tconst statValue = stats[stat];\r\n\t\t\t\tdice = dice.replace(regex, statValue.toString());\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn replaceFormulaInDice(dice);\r\n\t\r\n}\r\n\r\n/**\r\n * Replace the {{}} in the dice string and evaluate the interior if any\r\n * @param dice {string}\r\n */\r\nexport function replaceFormulaInDice(dice: string) {\r\n\tconst formula = /(?<formula>\\{{2}(.+?)\\}{2})/gmi;\r\n\tconst formulaMatch = formula.exec(dice);\r\n\tif (formulaMatch?.groups?.formula) {\r\n\t\tconst formula = formulaMatch.groups.formula.replaceAll(\"{{\", \"\").replaceAll(\"}}\", \"\");\r\n\t\ttry {\r\n\t\t\tconst result = evaluate(formula);\r\n\t\t\treturn cleanedDice(dice.replace(formulaMatch.groups.formula, result.toString()));\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidFormula, common.space]: ${formulaMatch.groups.formula}`);\r\n\t\t}\r\n\t}\r\n\treturn cleanedDice(dice);\r\n}\r\n\r\n/**\r\n * Replace the ++ +- -- by their proper value:\r\n * - `++` = `+`\r\n * - `+-` = `-`\r\n * - `--` = `+`\r\n * @param dice {string}\r\n */\r\nexport function cleanedDice(dice: string) {\r\n\treturn dice.replaceAll(\"+-\", \"-\").replaceAll(\"--\", \"+\").replaceAll(\"++\", \"+\");\r\n}","/* eslint-disable @typescript-eslint/no-unused-vars */\r\nimport { evaluate } from \"mathjs\";\r\nimport {Random } from \"random-js\";\r\nimport removeAccents from \"remove-accents\";\r\n\r\nimport { Statistic, StatisticalTemplate } from \".\";\r\nimport { roll } from \"./dice\";\r\nimport { escapeRegex, replaceFormulaInDice } from \"./utils\";\r\n\r\n/**\r\n * Verify if the provided dice work with random value\r\n * @param testDice {string}\r\n * @param stats {[name: string]: number}\r\n */\r\nexport function evalStatsDice(testDice: string, stats?: {[name: string]: number}) {\r\n\tlet dice = testDice;\r\n\tif (stats && Object.keys(stats).length > 0) {\r\n\t\tconst allStats = Object.keys(stats);\r\n\t\tfor (const stat of allStats) {\r\n\t\t\tconst regex = new RegExp(escapeRegex(removeAccents(stat)), \"gi\");\r\n\t\t\tif (testDice.match(regex)) {\r\n\t\t\t\tconst statValue = stats[stat];\r\n\t\t\t\tdice = testDice.replace(regex, statValue.toString());\r\n\t\t\t}\r\n\t\t}\r\n\t}\t\r\n\ttry {\r\n\t\tif (!roll(replaceFormulaInDice(dice))) throw new Error(`[error.invalidDice.withoutDice, common.space] ${dice}`);\r\n\t\treturn testDice;\r\n\t} catch (error) {\r\n\t\tthrow new Error(`[error.invalidDice.withoutDice, common.space]: ${testDice}\\n${(error as Error).message}`);\r\n\t}\r\n}\r\n\r\n/**\r\n * Generate a random dice and remove the formula (+ evaluate it)\r\n * Used for diceDamage only\r\n * @param value {string}\r\n * @param template {StatisticalTemplate}\r\n * @returns \r\n */\r\nexport function diceRandomParse(value: string, template: StatisticalTemplate) {\r\n\tif (!template.statistics) return value;\r\n\tvalue = removeAccents(value);\r\n\tconst allStats = Object.keys(template.statistics).map(stat => removeAccents(stat).toLowerCase());\r\n\tlet newDice = value;\r\n\tfor (const stat of allStats) {\r\n\t\tconst regex = new RegExp(escapeRegex(stat), \"gi\");\r\n\t\tif (value.match(regex)) {\r\n\t\t\tlet max: undefined | number = undefined;\r\n\t\t\tlet min: undefined | number = undefined;\r\n\t\t\tconst stats = template.statistics?.[stat];\r\n\t\t\tif (stats) {\r\n\t\t\t\tmax = template.statistics[removeAccents(stat).toLowerCase()].max;\r\n\t\t\t\tmin = template.statistics[removeAccents(stat).toLowerCase()].min;\r\n\t\t\t}\r\n\t\t\tconst total = template.total || 100;\r\n\t\t\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\t\t\tnewDice = value.replace(regex, randomStatValue.toString());\r\n\t\t}\r\n\t}\r\n\treturn replaceFormulaInDice(newDice);\r\n}\r\n\r\n/**\r\n * Same as damageDice but for DiceType\r\n * @param dice {string}\r\n * @param template {StatisticalTemplate}\r\n */\r\nexport function diceTypeRandomParse(dice: string, template: StatisticalTemplate) {\r\n\tif (!template.statistics) return dice;\r\n\tconst firstStatNotCombinaison = Object.keys(template.statistics).find(stat => !template.statistics?.[stat].combinaison);\r\n\tif (!firstStatNotCombinaison) return dice;\r\n\tconst stats = template.statistics[firstStatNotCombinaison];\r\n\tconst {min, max} = stats;\r\n\tconst total = template.total || 100;\r\n\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\treturn replaceFormulaInDice(dice.replace(\"$\", randomStatValue.toString()));\r\n}\r\n\r\n/**\r\n * Random the combinaison and evaluate it to check if everything is valid\r\n * @param combinaison {[name: string]: string}\r\n * @param stats {[name: string]: string|number}\r\n */\r\nexport function evalCombinaison(combinaison: {[name: string]: string}, stats: {[name: string]: string | number}) {\r\n\tconst newStats: {[name: string]: number} = {};\r\n\tfor (const [stat, combin] of Object.entries(combinaison)) {\r\n\t\t//replace the stats in formula\r\n\t\tlet formula = removeAccents(combin);\r\n\t\tfor (const [statName, value] of Object.entries(stats)) {\r\n\t\t\tconst regex = new RegExp(removeAccents(statName), \"gi\");\r\n\t\t\tformula = formula.replace(regex, value.toString());\r\n\t\t}\r\n\t\ttry {\r\n\t\t\tconst result = evaluate(formula);\r\n\t\t\tnewStats[stat] = result;\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidFormula, common.space]: ${stat}`);\r\n\t\t}\r\n\t}\r\n\treturn newStats;\r\n}\r\n\r\n/**\r\n * Evaluate one selected combinaison\r\n * @param combinaison {string}\r\n * @param stats {[name: string]: string|number}\r\n */\r\nexport function evalOneCombinaison(combinaison: string, stats: {[name: string]: string | number}) {\r\n\tlet formula = removeAccents(combinaison);\r\n\tfor (const [statName, value] of Object.entries(stats)) {\r\n\t\tconst regex = new RegExp(removeAccents(statName), \"gi\");\r\n\t\tformula = formula.replace(regex, value.toString());\r\n\t}\r\n\ttry {\r\n\t\treturn evaluate(formula);\r\n\t} catch (error) {\r\n\t\tthrow new Error(`[error.invalidFormula, common.space]: ${combinaison}`);\r\n\t}\r\n}\r\n\r\n/**\r\n * Parse the provided JSON and verify each field to check if everything could work when rolling\r\n * @param {any} template \r\n * @returns {StatisticalTemplate}\r\n */\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport function verifyTemplateValue(template: any): StatisticalTemplate {\r\n\tconst statistiqueTemplate: StatisticalTemplate = {\r\n\t\tdiceType: \"\",\r\n\t\tstatistics: {} as Statistic\r\n\t};\r\n\tif (!template.statistics) statistiqueTemplate.statistics = undefined;\r\n\telse if (template.statistics && Object.keys(template.statistics).length > 0) {\r\n\t\tfor (const [key, value] of Object.entries(template.statistics)) {\r\n\t\t\tconst dataValue = value as { max?: number, min?: number, combinaison?: string };\r\n\t\t\tif (dataValue.max && dataValue.min && dataValue.max <= dataValue.min)\r\n\t\t\t\tthrow new Error(\"[error.maxGreater]\");\t\t\t\t\r\n\t\t\tif (dataValue.max && dataValue.max <= 0 ) dataValue.max = undefined;\r\n\t\t\tif (dataValue.min && dataValue.min <= 0 ) dataValue.min = undefined;\r\n\t\t\tlet formula = dataValue.combinaison ? removeAccents(dataValue.combinaison).toLowerCase() : undefined;\r\n\t\t\tformula = formula && formula.trim().length > 0 ? formula : undefined;\r\n\t\t\tif (!statistiqueTemplate.statistics) {\r\n\t\t\t\tstatistiqueTemplate.statistics = {} as Statistic;\r\n\t\t\t}\r\n\t\t\tstatistiqueTemplate.statistics[key] = {\r\n\t\t\t\tmax: dataValue.max,\r\n\t\t\t\tmin: dataValue.min,\r\n\t\t\t\tcombinaison: formula || undefined,\r\n\t\t\t};\r\n\t\t}\r\n\t}\r\n\tif (template.diceType) {\r\n\t\ttry {\r\n\t\t\tstatistiqueTemplate.diceType = template.diceType;\r\n\t\t\tconst cleanedDice = diceTypeRandomParse(template.diceType, statistiqueTemplate);\r\n\t\t\tconst rolled = roll(cleanedDice);\r\n\t\t\tif (!rolled) throw new Error(\"[error.invalidDice.withoutDice]\");\r\n\t\t} catch (e) {\r\n\t\t\tthrow new Error((e as Error).message);\r\n\t\t}\r\n\t}\r\n\r\n\t\r\n\tif (template.critical && Object.keys(template.critical).length > 0){\r\n\t\tstatistiqueTemplate.critical = {\r\n\t\t\tfailure: template.critical.failure ?? undefined,\r\n\t\t\tsuccess: template.critical.success ?? undefined\r\n\t\t};\r\n\r\n\t}\r\n\tif (template.total) {\r\n\t\tif (template.total <= 0)\r\n\t\t\ttemplate.total = undefined;\r\n\t\tstatistiqueTemplate.total = template.total;\r\n\t}\r\n\tif (template.charName) statistiqueTemplate.charName = template.charName;\r\n\tif (template.damage) statistiqueTemplate.damage = template.damage;\r\n\ttry {\r\n\t\ttestDiceRegistered(statistiqueTemplate);\r\n\t\ttestStatCombinaison(statistiqueTemplate);\r\n\t} catch (error) {\r\n\t\tthrow new Error((error as Error).message);\r\n\t}\r\n\treturn statistiqueTemplate;\r\n}\r\n\r\n/**\r\n * Test each damage roll from the template.damage\r\n * @param {StatisticalTemplate} template \r\n */\r\nexport function testDiceRegistered(template: StatisticalTemplate) {\r\n\tif (!template.damage) return;\r\n\tif (Object.keys(template.damage).length === 0) throw new Error(\"[error.emptyObject]\");\r\n\tif (Object.keys(template.damage).length > 25) throw new Error(\"[error.tooManyDice]\");\r\n\tfor (const [name, dice] of Object.entries(template.damage)) {\r\n\t\tif (!dice) continue;\r\n\t\tconst randomDiceParsed = diceRandomParse(dice, template);\r\n\t\ttry {\r\n\t\t\tconst rolled = roll(randomDiceParsed);\r\n\t\t\tif (!rolled) throw new Error(`[error.invalidDice, common.space] ${name}`);\r\n\t\t} catch (error) {\r\n\t\t\tthrow new Error(`[error.invalidDice, common.space] ${name}`);\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\r\n\r\n/**\r\n * Test all combinaison with generated random value\r\n * @param {StatisticalTemplate} template \r\n */\r\nexport function testStatCombinaison(template: StatisticalTemplate) {\r\n\tif (!template.statistics) return;\r\n\tconst onlyCombinaisonStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => value.combinaison !== undefined));\r\n\tconst allOtherStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => !value.combinaison));\t\r\n\tif (Object.keys(onlyCombinaisonStats).length===0) return;\r\n\tconst allStats = Object.keys(template.statistics).filter(stat => !template.statistics![stat].combinaison);\r\n\tif (allStats.length === 0) \r\n\t\tthrow new Error(\"[error.noStat]\");\r\n\tconst error= [];\r\n\tfor (const [stat, value] of Object.entries(onlyCombinaisonStats)) {\r\n\t\tlet formula = value.combinaison as string;\r\n\t\tfor (const [other, data] of Object.entries(allOtherStats)) {\r\n\t\t\tconst {max, min} = data;\r\n\t\t\tconst total = template.total || 100;\r\n\t\t\tconst randomStatValue = generateRandomStat(total, max, min);\r\n\t\t\tconst regex = new RegExp(other, \"gi\");\r\n\t\t\tformula = formula.replace(regex, randomStatValue.toString());\r\n\t\t}\r\n\t\ttry {\r\n\t\t\tevaluate(formula);\r\n\t\t} catch (e) {\r\n\t\t\terror.push(stat);\r\n\t\t}\r\n\t}\r\n\tif (error.length > 0) \r\n\t\tthrow new Error(`[error.invalidFormula, common.space] ${error.join(\", \")}`);\r\n\treturn;\r\n}\r\n\r\n/**\r\n * Generate a random stat based on the template and the statistical min and max\r\n * @param {number|undefined} total\r\n * @param {number | undefined} max \r\n * @param {number | undefined} min \r\n * @returns \r\n */\r\nexport function generateRandomStat(total: number | undefined = 100, max?: number, min?: number) {\r\n\tlet randomStatValue = total + 1;\r\n\twhile (randomStatValue >= total) {\r\n\t\tconst random = new Random();\r\n\t\tif (max && min)\r\n\t\t\trandomStatValue = random.integer(min, max);\r\n\t\telse if (max)\r\n\t\t\trandomStatValue = random.integer(0, max);\r\n\t\telse if (min)\r\n\t\t\trandomStatValue = random.integer(min, total);\r\n\t\telse\r\n\t\t\trandomStatValue = random.integer(0, total);\r\n\t}\r\n\treturn randomStatValue;\r\n}\r\n\r\n"],"mappings":";AACA,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB;AAIlB,IAAM,gBAAgB;AAC7B,IAAM,aAAY;AAClB,IAAM,mBAAmB;AAMlB,SAAS,KAAK,MAAmC;AAEvD,MAAI,CAAC,KAAK,SAAS,GAAG;AAAG,WAAO;AAChC,QAAM,eAAe,KAAK,MAAM,gBAAgB;AAChD,MAAI;AACJ,MAAI,cAAc;AACjB,WAAO,KAAK,QAAQ,kBAAkB,EAAE;AACxC,UAAM,OAAO,aAAa,CAAC;AAC3B,UAAM,OAAO,KAAK,MAAM,YAAY,IAAI,CAAC;AACzC,UAAM,cAAc,aAAa,CAAC,EAAE,MAAM,UAAU,IAAI,CAAC;AACzD,QAAI,MAAM;AACT,YAAM,SAAS,KAAK,QAAQ,YAAY,EAAE,EAAE,Q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D,UAAS,OAAO;AAAA,IACjB,SAAS,GAAG;AACX,YAAM,KAAK,IAAI;AAAA,IAChB;AAAA,EACD;AACA,MAAI,MAAM,SAAS;AAClB,UAAM,IAAI,MAAM,wCAAwC,MAAM,KAAK,IAAI,CAAC,EAAE;AAC3E;AACD;AASO,SAAS,mBAAmB,QAA4B,KAAK,KAAc,KAAc;AAC/F,MAAI,kBAAkB,QAAQ;AAC9B,SAAO,mBAAmB,OAAO;AAChC,UAAM,SAAS,IAAI,OAAO;AAC1B,QAAI,OAAO;AACV,wBAAkB,OAAO,QAAQ,KAAK,GAAG;AAAA,aACjC;AACR,wBAAkB,OAAO,QAAQ,GAAG,GAAG;AAAA,aAC/B;AACR,wBAAkB,OAAO,QAAQ,KAAK,KAAK;AAAA;AAE3C,wBAAkB,OAAO,QAAQ,GAAG,KAAK;AAAA,EAC3C;AACA,SAAO;AACR;","names":["roller","evaluate","formula","evaluate","removeAccents","removeAccents","evaluate","cleanedDice"]}
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@dicelette/core",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Core library for the Dicelette Discord bot",
5
5
  "repository": {
6
6
  "type": "git",
7
- "url": "https://github.com/Dicelette/core.git"
7
+ "url": "git+https://github.com/Dicelette/core.git"
8
8
  },
9
9
  "type": "commonjs",
10
10
  "main": "./dist/index.js",
@@ -13,12 +13,6 @@
13
13
  "files": [
14
14
  "dist"
15
15
  ],
16
- "scripts": {
17
- "test": "jest",
18
- "build": "tsup",
19
- "prepublish": "npm run build",
20
- "publish": "npm publish"
21
- },
22
16
  "keywords": [
23
17
  "discord",
24
18
  "roll",
@@ -31,7 +25,6 @@
31
25
  "dependencies": {
32
26
  "@dice-roller/rpg-dice-roller": "^5.5.0",
33
27
  "@lisandra-dev/eslint-config": "^1.1.4",
34
- "@types/jest": "^29.5.12",
35
28
  "eslint": "^8.57.0",
36
29
  "mathjs": "^12.4.1",
37
30
  "moment": "^2.30.1",
@@ -40,8 +33,16 @@
40
33
  "ts-dedent": "^2.2.0"
41
34
  },
42
35
  "devDependencies": {
36
+ "@types/jest": "^29.5.12",
37
+ "jest": "^29.7.0",
38
+ "ts-jest": "^29.1.2",
43
39
  "tslib": "^2.6.2",
44
40
  "tsup": "^8.0.2",
45
41
  "typescript": "^5.4.3"
42
+ },
43
+ "scripts": {
44
+ "test": "jest",
45
+ "build": "tsup",
46
+ "prepublish": "npm run build"
46
47
  }
47
- }
48
+ }