@dicelette/core 1.1.2 → 1.1.3

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.js CHANGED
@@ -106,7 +106,11 @@ function roll(dice) {
106
106
  const comments = commentsMatch ? commentsMatch[2] : void 0;
107
107
  const roller2 = new import_rpg_dice_roller.DiceRoller();
108
108
  for (let i = 0; i < numberOfDice; i++) {
109
- roller2.roll(diceToRoll);
109
+ try {
110
+ roller2.roll(diceToRoll);
111
+ } catch (error) {
112
+ throw new Error(`[error.invalidDice.withoutDice, common.space]: ${diceToRoll}`);
113
+ }
110
114
  }
111
115
  return {
112
116
  dice: diceToRoll,
@@ -118,7 +122,11 @@ function roll(dice) {
118
122
  }
119
123
  const roller = new import_rpg_dice_roller.DiceRoller();
120
124
  const diceWithoutComment = dice.replace(COMMENT_REGEX, "");
121
- roller.roll(diceWithoutComment);
125
+ try {
126
+ roller.roll(diceWithoutComment);
127
+ } catch (error) {
128
+ throw new Error(`[error.invalidDice.withoutDice, common.space]: ${diceWithoutComment}`);
129
+ }
122
130
  const commentMatch = dice.match(COMMENT_REGEX);
123
131
  const comment = commentMatch ? commentMatch[2] : void 0;
124
132
  return {
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\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,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;A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AAS,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"]}
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\ttry {\r\n\t\t\t\troller.roll(diceToRoll);\r\n\t\t\t} catch (error) {\r\n\t\t\t\tthrow new Error(`[error.invalidDice.withoutDice, common.space]: ${diceToRoll}`);\r\n\t\t\t}\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\ttry {\t\r\n\t\troller.roll(diceWithoutComment);\r\n\t} catch (error) {\r\n\t\tthrow new Error(`[error.invalidDice.withoutDice, common.space]: ${diceWithoutComment}`);\r\n\t}\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,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,YAAY;AACzB,UAAI,Q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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
@@ -55,7 +55,11 @@ function roll(dice) {
55
55
  const comments = commentsMatch ? commentsMatch[2] : void 0;
56
56
  const roller2 = new DiceRoller();
57
57
  for (let i = 0; i < numberOfDice; i++) {
58
- roller2.roll(diceToRoll);
58
+ try {
59
+ roller2.roll(diceToRoll);
60
+ } catch (error) {
61
+ throw new Error(`[error.invalidDice.withoutDice, common.space]: ${diceToRoll}`);
62
+ }
59
63
  }
60
64
  return {
61
65
  dice: diceToRoll,
@@ -67,7 +71,11 @@ function roll(dice) {
67
71
  }
68
72
  const roller = new DiceRoller();
69
73
  const diceWithoutComment = dice.replace(COMMENT_REGEX, "");
70
- roller.roll(diceWithoutComment);
74
+ try {
75
+ roller.roll(diceWithoutComment);
76
+ } catch (error) {
77
+ throw new Error(`[error.invalidDice.withoutDice, common.space]: ${diceWithoutComment}`);
78
+ }
71
79
  const commentMatch = dice.match(COMMENT_REGEX);
72
80
  const comment = commentMatch ? commentMatch[2] : void 0;
73
81
  return {
@@ -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\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,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;AAA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ACD;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"]}
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\ttry {\r\n\t\t\t\troller.roll(diceToRoll);\r\n\t\t\t} catch (error) {\r\n\t\t\t\tthrow new Error(`[error.invalidDice.withoutDice, common.space]: ${diceToRoll}`);\r\n\t\t\t}\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\ttry {\t\r\n\t\troller.roll(diceWithoutComment);\r\n\t} catch (error) {\r\n\t\tthrow new Error(`[error.invalidDice.withoutDice, common.space]: ${diceWithoutComment}`);\r\n\t}\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\trandomStatVal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AO,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,6 +1,6 @@
1
1
  {
2
2
  "name": "@dicelette/core",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Core library for the Dicelette Discord bot",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,6 +25,7 @@
25
25
  "dependencies": {
26
26
  "@dice-roller/rpg-dice-roller": "^5.5.0",
27
27
  "@lisandra-dev/eslint-config": "^1.1.4",
28
+ "dicelette-docs": "link:E:/Documents/Github/dicelette/dicelette-docs",
28
29
  "eslint": "^8.57.0",
29
30
  "mathjs": "^12.4.1",
30
31
  "moment": "^2.30.1",