@dicelette/core 1.0.10 → 1.1.1
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 +70 -0
- package/dist/index.d.ts +70 -0
- package/dist/index.js +406 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +354 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +18 -8
- package/.eslintrc.js +0 -14
- package/core/dice.ts +0 -104
- package/core/interface.ts +0 -63
- package/core/utils.ts +0 -65
- package/core/verify_template.ts +0 -263
- package/index.ts +0 -4
- package/jest.config.js +0 -6
- package/tests/verify_template.test.ts +0 -243
- package/tree.txt +0 -25
- package/tsconfig.json +0 -32
package/dist/index.mjs
ADDED
@@ -0,0 +1,354 @@
|
|
1
|
+
// src/dice.ts
|
2
|
+
import { DiceRoller } from "@dice-roller/rpg-dice-roller";
|
3
|
+
import { evaluate } from "mathjs";
|
4
|
+
var COMMENT_REGEX = /\s+(#|\/{2}|\[|\/\*)(.*)/;
|
5
|
+
var SIGN_REGEX = /[><=!]+/;
|
6
|
+
var SIGN_REGEX_SPACE = /[><=!]+(\S+)/;
|
7
|
+
function roll(dice) {
|
8
|
+
if (!dice.includes("d"))
|
9
|
+
return void 0;
|
10
|
+
const compareRegex = dice.match(SIGN_REGEX_SPACE);
|
11
|
+
let compare;
|
12
|
+
if (compareRegex) {
|
13
|
+
dice = dice.replace(SIGN_REGEX_SPACE, "");
|
14
|
+
const calc = compareRegex[1];
|
15
|
+
const sign = calc.match(/[+-\/\*\^]/)?.[0];
|
16
|
+
const compareSign = compareRegex[0].match(SIGN_REGEX)?.[0];
|
17
|
+
if (sign) {
|
18
|
+
const toCalc = calc.replace(SIGN_REGEX, "").replace(/\s/g, "");
|
19
|
+
const total = evaluate(toCalc);
|
20
|
+
dice = dice.replace(SIGN_REGEX_SPACE, `${compareSign}${total}`);
|
21
|
+
compare = {
|
22
|
+
sign: compareSign,
|
23
|
+
value: total
|
24
|
+
};
|
25
|
+
} else
|
26
|
+
compare = {
|
27
|
+
sign: compareSign,
|
28
|
+
value: parseInt(calc, 10)
|
29
|
+
};
|
30
|
+
}
|
31
|
+
const modifier = dice.matchAll(/(\+|\-|%|\/|\^|\*|\*{2})(\d+)/gi);
|
32
|
+
let modificator;
|
33
|
+
for (const mod of modifier) {
|
34
|
+
if (modificator) {
|
35
|
+
const sign = modificator.sign;
|
36
|
+
let value = modificator.value;
|
37
|
+
if (sign)
|
38
|
+
value = calculator(sign, value, parseInt(mod[2], 10));
|
39
|
+
modificator = {
|
40
|
+
sign: mod[1],
|
41
|
+
value
|
42
|
+
};
|
43
|
+
} else {
|
44
|
+
modificator = {
|
45
|
+
sign: mod[1],
|
46
|
+
value: parseInt(mod[2], 10)
|
47
|
+
};
|
48
|
+
}
|
49
|
+
}
|
50
|
+
if (dice.match(/\d+?#(.*)/)) {
|
51
|
+
const diceArray = dice.split("#");
|
52
|
+
const numberOfDice = parseInt(diceArray[0], 10);
|
53
|
+
const diceToRoll = diceArray[1].replace(COMMENT_REGEX, "");
|
54
|
+
const commentsMatch = diceArray[1].match(COMMENT_REGEX);
|
55
|
+
const comments = commentsMatch ? commentsMatch[2] : void 0;
|
56
|
+
const roller2 = new DiceRoller();
|
57
|
+
for (let i = 0; i < numberOfDice; i++) {
|
58
|
+
roller2.roll(diceToRoll);
|
59
|
+
}
|
60
|
+
return {
|
61
|
+
dice: diceToRoll,
|
62
|
+
result: roller2.output,
|
63
|
+
comment: comments,
|
64
|
+
compare: compare ? compare : void 0,
|
65
|
+
modifier: modificator
|
66
|
+
};
|
67
|
+
}
|
68
|
+
const roller = new DiceRoller();
|
69
|
+
const diceWithoutComment = dice.replace(COMMENT_REGEX, "");
|
70
|
+
roller.roll(diceWithoutComment);
|
71
|
+
const commentMatch = dice.match(COMMENT_REGEX);
|
72
|
+
const comment = commentMatch ? commentMatch[2] : void 0;
|
73
|
+
return {
|
74
|
+
dice,
|
75
|
+
result: roller.output,
|
76
|
+
comment,
|
77
|
+
compare: compare ? compare : void 0,
|
78
|
+
modifier: modificator
|
79
|
+
};
|
80
|
+
}
|
81
|
+
function calculator(sign, value, total) {
|
82
|
+
if (sign === "^")
|
83
|
+
sign = "**";
|
84
|
+
return evaluate(`${total} ${sign} ${value}`);
|
85
|
+
}
|
86
|
+
|
87
|
+
// src/utils.ts
|
88
|
+
import { evaluate as evaluate2 } from "mathjs";
|
89
|
+
import removeAccents from "remove-accents";
|
90
|
+
function escapeRegex(string) {
|
91
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
92
|
+
}
|
93
|
+
function generateStatsDice(originalDice, stats) {
|
94
|
+
let dice = originalDice;
|
95
|
+
if (stats && Object.keys(stats).length > 0) {
|
96
|
+
const allStats = Object.keys(stats);
|
97
|
+
for (const stat of allStats) {
|
98
|
+
const regex = new RegExp(escapeRegex(removeAccents(stat)), "gi");
|
99
|
+
if (dice.match(regex)) {
|
100
|
+
const statValue = stats[stat];
|
101
|
+
dice = dice.replace(regex, statValue.toString());
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
return replaceFormulaInDice(dice);
|
106
|
+
}
|
107
|
+
function replaceFormulaInDice(dice) {
|
108
|
+
const formula = /(?<formula>\{{2}(.+?)\}{2})/gmi;
|
109
|
+
const formulaMatch = formula.exec(dice);
|
110
|
+
if (formulaMatch?.groups?.formula) {
|
111
|
+
const formula2 = formulaMatch.groups.formula.replaceAll("{{", "").replaceAll("}}", "");
|
112
|
+
try {
|
113
|
+
const result = evaluate2(formula2);
|
114
|
+
return cleanedDice(dice.replace(formulaMatch.groups.formula, result.toString()));
|
115
|
+
} catch (error) {
|
116
|
+
throw new Error(`[error.invalidFormula, common.space]: ${formulaMatch.groups.formula}`);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
return cleanedDice(dice);
|
120
|
+
}
|
121
|
+
function cleanedDice(dice) {
|
122
|
+
return dice.replaceAll("+-", "-").replaceAll("--", "+").replaceAll("++", "+");
|
123
|
+
}
|
124
|
+
|
125
|
+
// src/verify_template.ts
|
126
|
+
import { evaluate as evaluate3 } from "mathjs";
|
127
|
+
import { Random } from "random-js";
|
128
|
+
import removeAccents2 from "remove-accents";
|
129
|
+
function evalStatsDice(testDice, stats) {
|
130
|
+
let dice = testDice;
|
131
|
+
if (stats && Object.keys(stats).length > 0) {
|
132
|
+
const allStats = Object.keys(stats);
|
133
|
+
for (const stat of allStats) {
|
134
|
+
const regex = new RegExp(escapeRegex(removeAccents2(stat)), "gi");
|
135
|
+
if (testDice.match(regex)) {
|
136
|
+
const statValue = stats[stat];
|
137
|
+
dice = testDice.replace(regex, statValue.toString());
|
138
|
+
}
|
139
|
+
}
|
140
|
+
}
|
141
|
+
try {
|
142
|
+
if (!roll(replaceFormulaInDice(dice)))
|
143
|
+
throw new Error(`[error.invalidDice.withoutDice, common.space] ${dice}`);
|
144
|
+
return testDice;
|
145
|
+
} catch (error) {
|
146
|
+
throw new Error(`[error.invalidDice.withoutDice, common.space]: ${testDice}
|
147
|
+
${error.message}`);
|
148
|
+
}
|
149
|
+
}
|
150
|
+
function diceRandomParse(value, template) {
|
151
|
+
if (!template.statistics)
|
152
|
+
return value;
|
153
|
+
value = removeAccents2(value);
|
154
|
+
const allStats = Object.keys(template.statistics).map((stat) => removeAccents2(stat).toLowerCase());
|
155
|
+
let newDice = value;
|
156
|
+
for (const stat of allStats) {
|
157
|
+
const regex = new RegExp(escapeRegex(stat), "gi");
|
158
|
+
if (value.match(regex)) {
|
159
|
+
let max = void 0;
|
160
|
+
let min = void 0;
|
161
|
+
const stats = template.statistics?.[stat];
|
162
|
+
if (stats) {
|
163
|
+
max = template.statistics[removeAccents2(stat).toLowerCase()].max;
|
164
|
+
min = template.statistics[removeAccents2(stat).toLowerCase()].min;
|
165
|
+
}
|
166
|
+
const total = template.total || 100;
|
167
|
+
const randomStatValue = generateRandomStat(total, max, min);
|
168
|
+
newDice = value.replace(regex, randomStatValue.toString());
|
169
|
+
}
|
170
|
+
}
|
171
|
+
return replaceFormulaInDice(newDice);
|
172
|
+
}
|
173
|
+
function diceTypeRandomParse(dice, template) {
|
174
|
+
if (!template.statistics)
|
175
|
+
return dice;
|
176
|
+
const firstStatNotCombinaison = Object.keys(template.statistics).find((stat) => !template.statistics?.[stat].combinaison);
|
177
|
+
if (!firstStatNotCombinaison)
|
178
|
+
return dice;
|
179
|
+
const stats = template.statistics[firstStatNotCombinaison];
|
180
|
+
const { min, max } = stats;
|
181
|
+
const total = template.total || 100;
|
182
|
+
const randomStatValue = generateRandomStat(total, max, min);
|
183
|
+
return replaceFormulaInDice(dice.replace("$", randomStatValue.toString()));
|
184
|
+
}
|
185
|
+
function evalCombinaison(combinaison, stats) {
|
186
|
+
const newStats = {};
|
187
|
+
for (const [stat, combin] of Object.entries(combinaison)) {
|
188
|
+
let formula = removeAccents2(combin);
|
189
|
+
for (const [statName, value] of Object.entries(stats)) {
|
190
|
+
const regex = new RegExp(removeAccents2(statName), "gi");
|
191
|
+
formula = formula.replace(regex, value.toString());
|
192
|
+
}
|
193
|
+
try {
|
194
|
+
const result = evaluate3(formula);
|
195
|
+
newStats[stat] = result;
|
196
|
+
} catch (error) {
|
197
|
+
throw new Error(`[error.invalidFormula, common.space]: ${stat}`);
|
198
|
+
}
|
199
|
+
}
|
200
|
+
return newStats;
|
201
|
+
}
|
202
|
+
function evalOneCombinaison(combinaison, stats) {
|
203
|
+
let formula = removeAccents2(combinaison);
|
204
|
+
for (const [statName, value] of Object.entries(stats)) {
|
205
|
+
const regex = new RegExp(removeAccents2(statName), "gi");
|
206
|
+
formula = formula.replace(regex, value.toString());
|
207
|
+
}
|
208
|
+
try {
|
209
|
+
return evaluate3(formula);
|
210
|
+
} catch (error) {
|
211
|
+
throw new Error(`[error.invalidFormula, common.space]: ${combinaison}`);
|
212
|
+
}
|
213
|
+
}
|
214
|
+
function verifyTemplateValue(template) {
|
215
|
+
const statistiqueTemplate = {
|
216
|
+
diceType: "",
|
217
|
+
statistics: {}
|
218
|
+
};
|
219
|
+
if (!template.statistics)
|
220
|
+
statistiqueTemplate.statistics = void 0;
|
221
|
+
else if (template.statistics && Object.keys(template.statistics).length > 0) {
|
222
|
+
for (const [key, value] of Object.entries(template.statistics)) {
|
223
|
+
const dataValue = value;
|
224
|
+
if (dataValue.max && dataValue.min && dataValue.max <= dataValue.min)
|
225
|
+
throw new Error("[error.maxGreater]");
|
226
|
+
if (dataValue.max && dataValue.max <= 0)
|
227
|
+
dataValue.max = void 0;
|
228
|
+
if (dataValue.min && dataValue.min <= 0)
|
229
|
+
dataValue.min = void 0;
|
230
|
+
let formula = dataValue.combinaison ? removeAccents2(dataValue.combinaison).toLowerCase() : void 0;
|
231
|
+
formula = formula && formula.trim().length > 0 ? formula : void 0;
|
232
|
+
if (!statistiqueTemplate.statistics) {
|
233
|
+
statistiqueTemplate.statistics = {};
|
234
|
+
}
|
235
|
+
statistiqueTemplate.statistics[key] = {
|
236
|
+
max: dataValue.max,
|
237
|
+
min: dataValue.min,
|
238
|
+
combinaison: formula || void 0
|
239
|
+
};
|
240
|
+
}
|
241
|
+
}
|
242
|
+
if (template.diceType) {
|
243
|
+
try {
|
244
|
+
statistiqueTemplate.diceType = template.diceType;
|
245
|
+
diceTypeRandomParse(template.diceType, statistiqueTemplate);
|
246
|
+
} catch (e) {
|
247
|
+
throw new Error(e.message);
|
248
|
+
}
|
249
|
+
}
|
250
|
+
if (template.critical && Object.keys(template.critical).length > 0) {
|
251
|
+
statistiqueTemplate.critical = {
|
252
|
+
failure: template.critical.failure ?? void 0,
|
253
|
+
success: template.critical.success ?? void 0
|
254
|
+
};
|
255
|
+
}
|
256
|
+
if (template.total) {
|
257
|
+
if (template.total <= 0)
|
258
|
+
template.total = void 0;
|
259
|
+
statistiqueTemplate.total = template.total;
|
260
|
+
}
|
261
|
+
if (template.charName)
|
262
|
+
statistiqueTemplate.charName = template.charName;
|
263
|
+
if (template.damage)
|
264
|
+
statistiqueTemplate.damage = template.damage;
|
265
|
+
try {
|
266
|
+
testDamageRoll(statistiqueTemplate);
|
267
|
+
testCombinaison(statistiqueTemplate);
|
268
|
+
} catch (error) {
|
269
|
+
throw new Error(error.message);
|
270
|
+
}
|
271
|
+
return statistiqueTemplate;
|
272
|
+
}
|
273
|
+
function testDamageRoll(template) {
|
274
|
+
if (!template.damage)
|
275
|
+
return;
|
276
|
+
if (Object.keys(template.damage).length === 0)
|
277
|
+
throw new Error("[error.emptyObject]");
|
278
|
+
if (Object.keys(template.damage).length > 25)
|
279
|
+
throw new Error("[error.tooManyDice]");
|
280
|
+
for (const [name, dice] of Object.entries(template.damage)) {
|
281
|
+
if (!dice)
|
282
|
+
continue;
|
283
|
+
const randomDiceParsed = diceRandomParse(dice, template);
|
284
|
+
try {
|
285
|
+
roll(randomDiceParsed);
|
286
|
+
} catch (error) {
|
287
|
+
throw new Error(`[error.invalidDice, common.space] ${name}`);
|
288
|
+
}
|
289
|
+
}
|
290
|
+
}
|
291
|
+
function testCombinaison(template) {
|
292
|
+
if (!template.statistics)
|
293
|
+
return;
|
294
|
+
const onlyCombinaisonStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => value.combinaison !== void 0));
|
295
|
+
const allOtherStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => !value.combinaison));
|
296
|
+
if (Object.keys(onlyCombinaisonStats).length === 0)
|
297
|
+
return;
|
298
|
+
const allStats = Object.keys(template.statistics).filter((stat) => !template.statistics[stat].combinaison);
|
299
|
+
if (allStats.length === 0)
|
300
|
+
throw new Error("[error.noStat]");
|
301
|
+
const error = [];
|
302
|
+
for (const [stat, value] of Object.entries(onlyCombinaisonStats)) {
|
303
|
+
let formula = value.combinaison;
|
304
|
+
for (const [other, data] of Object.entries(allOtherStats)) {
|
305
|
+
const { max, min } = data;
|
306
|
+
const total = template.total || 100;
|
307
|
+
const randomStatValue = generateRandomStat(total, max, min);
|
308
|
+
const regex = new RegExp(other, "gi");
|
309
|
+
formula = formula.replace(regex, randomStatValue.toString());
|
310
|
+
}
|
311
|
+
try {
|
312
|
+
evaluate3(formula);
|
313
|
+
} catch (e) {
|
314
|
+
error.push(stat);
|
315
|
+
}
|
316
|
+
}
|
317
|
+
if (error.length > 0)
|
318
|
+
throw new Error(`[error.invalidFormula, common.space] ${error.join(", ")}`);
|
319
|
+
return;
|
320
|
+
}
|
321
|
+
function generateRandomStat(total = 100, max, min) {
|
322
|
+
let randomStatValue = total + 1;
|
323
|
+
while (randomStatValue >= total) {
|
324
|
+
const random = new Random();
|
325
|
+
if (max && min)
|
326
|
+
randomStatValue = random.integer(min, max);
|
327
|
+
else if (max)
|
328
|
+
randomStatValue = random.integer(0, max);
|
329
|
+
else if (min)
|
330
|
+
randomStatValue = random.integer(min, total);
|
331
|
+
else
|
332
|
+
randomStatValue = random.integer(0, total);
|
333
|
+
}
|
334
|
+
return randomStatValue;
|
335
|
+
}
|
336
|
+
export {
|
337
|
+
COMMENT_REGEX,
|
338
|
+
calculator,
|
339
|
+
cleanedDice,
|
340
|
+
diceRandomParse,
|
341
|
+
diceTypeRandomParse,
|
342
|
+
escapeRegex,
|
343
|
+
evalCombinaison,
|
344
|
+
evalOneCombinaison,
|
345
|
+
evalStatsDice,
|
346
|
+
generateRandomStat,
|
347
|
+
generateStatsDice,
|
348
|
+
replaceFormulaInDice,
|
349
|
+
roll,
|
350
|
+
testCombinaison,
|
351
|
+
testDamageRoll,
|
352
|
+
verifyTemplateValue
|
353
|
+
};
|
354
|
+
//# sourceMappingURL=index.mjs.map
|
@@ -0,0 +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,YAAY;AACzB,UAAI,QAAQ,YAAY;AACxB,UAAI;AACH,gBAAQ,WAAW,MAAM,OAAO,SAAS,IAAI,CAAC,GAAG,EAAE,CA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ette/core",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.1.1",
|
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
|
-
"main": "index.
|
10
|
+
"main": "./dist/index.js",
|
11
|
+
"module": "./dist/index.mjs",
|
12
|
+
"types": "./dist/index.d.ts",
|
13
|
+
"files": [
|
14
|
+
"dist"
|
15
|
+
],
|
16
|
+
"scripts": {
|
17
|
+
"test": "jest",
|
18
|
+
"build": "tsup",
|
19
|
+
"prepublish": "npm run build",
|
20
|
+
"publish": "npm publish"
|
21
|
+
},
|
11
22
|
"keywords": [
|
12
23
|
"discord",
|
13
24
|
"roll",
|
@@ -29,9 +40,8 @@
|
|
29
40
|
"ts-dedent": "^2.2.0"
|
30
41
|
},
|
31
42
|
"devDependencies": {
|
32
|
-
"tslib": "^2.6.2"
|
33
|
-
|
34
|
-
|
35
|
-
"test": "jest"
|
43
|
+
"tslib": "^2.6.2",
|
44
|
+
"tsup": "^8.0.2",
|
45
|
+
"typescript": "^5.4.3"
|
36
46
|
}
|
37
|
-
}
|
47
|
+
}
|
package/.eslintrc.js
DELETED
package/core/dice.ts
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
/* eslint-disable no-useless-escape */
|
2
|
-
import { DiceRoller } from "@dice-roller/rpg-dice-roller";
|
3
|
-
import { evaluate } from "mathjs";
|
4
|
-
|
5
|
-
import { Compare, Modifier, Resultat, Sign } from "..";
|
6
|
-
|
7
|
-
export const COMMENT_REGEX = /\s+(#|\/{2}|\[|\/\*)(.*)/;
|
8
|
-
const SIGN_REGEX =/[><=!]+/;
|
9
|
-
const SIGN_REGEX_SPACE = /[><=!]+(\S+)/;
|
10
|
-
|
11
|
-
/**
|
12
|
-
* Parse the string provided and turn it as a readable dice for dice parser
|
13
|
-
* @param dice {string}
|
14
|
-
*/
|
15
|
-
export function roll(dice: string): Resultat | undefined{
|
16
|
-
//parse dice string
|
17
|
-
if (!dice.includes("d")) return undefined;
|
18
|
-
const compareRegex = dice.match(SIGN_REGEX_SPACE);
|
19
|
-
let compare : Compare | undefined;
|
20
|
-
if (compareRegex) {
|
21
|
-
dice = dice.replace(SIGN_REGEX_SPACE, "");
|
22
|
-
const calc = compareRegex[1];
|
23
|
-
const sign = calc.match(/[+-\/\*\^]/)?.[0];
|
24
|
-
const compareSign = compareRegex[0].match(SIGN_REGEX)?.[0];
|
25
|
-
if (sign) {
|
26
|
-
const toCalc = calc.replace(SIGN_REGEX, "").replace(/\s/g, "");
|
27
|
-
const total = evaluate(toCalc);
|
28
|
-
dice = dice.replace(SIGN_REGEX_SPACE, `${compareSign}${total}`);
|
29
|
-
compare = {
|
30
|
-
sign: compareSign as "<" | ">" | ">=" | "<=" | "=" | "!=" | "==",
|
31
|
-
value: total,
|
32
|
-
};
|
33
|
-
} else compare = {
|
34
|
-
sign: compareSign as "<" | ">" | ">=" | "<=" | "=" | "!=" | "==",
|
35
|
-
value: parseInt(calc, 10),
|
36
|
-
};
|
37
|
-
}
|
38
|
-
const modifier = dice.matchAll(/(\+|\-|%|\/|\^|\*|\*{2})(\d+)/gi);
|
39
|
-
let modificator : Modifier | undefined;
|
40
|
-
for (const mod of modifier) {
|
41
|
-
//calculate the modifier if multiple
|
42
|
-
if (modificator) {
|
43
|
-
const sign = modificator.sign;
|
44
|
-
let value = modificator.value;
|
45
|
-
if (sign)
|
46
|
-
value = calculator(sign, value, parseInt(mod[2], 10));
|
47
|
-
modificator = {
|
48
|
-
sign: mod[1] as Sign,
|
49
|
-
value,
|
50
|
-
};
|
51
|
-
} else {
|
52
|
-
modificator = {
|
53
|
-
sign: mod[1] as Sign,
|
54
|
-
value: parseInt(mod[2], 10),
|
55
|
-
};
|
56
|
-
}
|
57
|
-
}
|
58
|
-
|
59
|
-
if (dice.match(/\d+?#(.*)/)) {
|
60
|
-
const diceArray = dice.split("#");
|
61
|
-
const numberOfDice = parseInt(diceArray[0], 10);
|
62
|
-
const diceToRoll = diceArray[1].replace(COMMENT_REGEX, "");
|
63
|
-
const commentsMatch = diceArray[1].match(COMMENT_REGEX);
|
64
|
-
const comments = commentsMatch ? commentsMatch[2] : undefined;
|
65
|
-
const roller = new DiceRoller();
|
66
|
-
//remove comments if any
|
67
|
-
for (let i = 0; i < numberOfDice; i++) {
|
68
|
-
roller.roll(diceToRoll);
|
69
|
-
}
|
70
|
-
return {
|
71
|
-
dice: diceToRoll,
|
72
|
-
result: roller.output,
|
73
|
-
comment: comments,
|
74
|
-
compare: compare ? compare : undefined,
|
75
|
-
modifier: modificator,
|
76
|
-
};
|
77
|
-
}
|
78
|
-
const roller = new DiceRoller();
|
79
|
-
const diceWithoutComment = dice.replace(COMMENT_REGEX, "");
|
80
|
-
roller.roll(diceWithoutComment);
|
81
|
-
const commentMatch = dice.match(COMMENT_REGEX);
|
82
|
-
const comment = commentMatch ? commentMatch[2] : undefined;
|
83
|
-
return {
|
84
|
-
dice,
|
85
|
-
result: roller.output,
|
86
|
-
comment,
|
87
|
-
compare: compare ? compare : undefined,
|
88
|
-
modifier: modificator,
|
89
|
-
};
|
90
|
-
}
|
91
|
-
/**
|
92
|
-
* Evaluate a formula and replace "^" by "**" if any
|
93
|
-
* @param {Sign} sign
|
94
|
-
* @param {number} value
|
95
|
-
* @param {number} total
|
96
|
-
* @returns
|
97
|
-
*/
|
98
|
-
export function calculator(sign: Sign, value: number, total: number): number {
|
99
|
-
if (sign === "^") sign = "**";
|
100
|
-
return evaluate(`${total} ${sign} ${value}`);
|
101
|
-
}
|
102
|
-
|
103
|
-
|
104
|
-
|
package/core/interface.ts
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
export interface Resultat {
|
2
|
-
dice: string;
|
3
|
-
result: string;
|
4
|
-
comment?: string;
|
5
|
-
compare?: Compare | undefined;
|
6
|
-
modifier?: Modifier;
|
7
|
-
}
|
8
|
-
|
9
|
-
|
10
|
-
export interface Compare {
|
11
|
-
sign: "<" | ">" | ">=" | "<=" | "=" | "!=" | "==";
|
12
|
-
value: number;
|
13
|
-
}
|
14
|
-
|
15
|
-
export type Sign = "+" | "-" | "*" | "/" | "%" | "^" | "**";
|
16
|
-
|
17
|
-
export interface Modifier {
|
18
|
-
sign?: Sign;
|
19
|
-
value: number;
|
20
|
-
}
|
21
|
-
|
22
|
-
export type Statistic = {
|
23
|
-
[name: string] : {
|
24
|
-
max?: number;
|
25
|
-
min?: number;
|
26
|
-
combinaison?: string;
|
27
|
-
}
|
28
|
-
}
|
29
|
-
|
30
|
-
/**
|
31
|
-
* @example
|
32
|
-
* diceType: 1d20+$>=20
|
33
|
-
* The dice throw will be 1d20 + statistique that must be >= 20
|
34
|
-
* @example
|
35
|
-
* diceType: 1d20<=$
|
36
|
-
* The dice throw will be 1d20 that must be <= statistique
|
37
|
-
*/
|
38
|
-
export interface StatisticalTemplate {
|
39
|
-
/** Allow to force the user to choose a name for them characters */
|
40
|
-
charName?: boolean
|
41
|
-
statistics?: Statistic
|
42
|
-
/**
|
43
|
-
* A total can be set, it allows to calculate the total value of a future register member
|
44
|
-
* If the sum of the value > total, the bot will send a message to the user to inform him that the total is exceeded and an error will be thrown
|
45
|
-
* @note statistique that have a formula will be ignored from the total
|
46
|
-
*/
|
47
|
-
total?: number
|
48
|
-
/** A dice type in the notation supported by the bot */
|
49
|
-
diceType?: string;
|
50
|
-
/**
|
51
|
-
* How the success/echec will be done
|
52
|
-
*/
|
53
|
-
critical?: Critical;
|
54
|
-
/** Special dice for damage */
|
55
|
-
damage?: {
|
56
|
-
[name: string]: string;
|
57
|
-
}
|
58
|
-
}
|
59
|
-
|
60
|
-
export interface Critical {
|
61
|
-
success?: number,
|
62
|
-
failure?: number,
|
63
|
-
}
|