@dicelette/core 1.20.0 → 1.20.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/README.md +140 -146
- package/dist/index.d.mts +108 -93
- package/dist/index.d.ts +108 -93
- package/dist/index.js +254 -235
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +251 -233
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -43,6 +43,7 @@ __export(index_exports, {
|
|
|
43
43
|
generateRandomStat: () => generateRandomStat,
|
|
44
44
|
generateStatsDice: () => generateStatsDice,
|
|
45
45
|
getEngine: () => getEngine,
|
|
46
|
+
getEngineId: () => getEngineId2,
|
|
46
47
|
isNumber: () => isNumber,
|
|
47
48
|
randomInt: () => randomInt,
|
|
48
49
|
replaceExpByRandom: () => replaceExpByRandom,
|
|
@@ -57,98 +58,20 @@ __export(index_exports, {
|
|
|
57
58
|
module.exports = __toCommonJS(index_exports);
|
|
58
59
|
|
|
59
60
|
// src/dice.ts
|
|
60
|
-
var import_rpg_dice_roller2 = require("@dice-roller/rpg-dice-roller");
|
|
61
|
-
var import_mathjs2 = require("mathjs");
|
|
62
|
-
|
|
63
|
-
// src/utils.ts
|
|
64
|
-
var import_mathjs = require("mathjs");
|
|
65
|
-
var import_uniformize = require("uniformize");
|
|
66
61
|
var import_rpg_dice_roller = require("@dice-roller/rpg-dice-roller");
|
|
67
|
-
var
|
|
68
|
-
function
|
|
69
|
-
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
70
|
-
}
|
|
71
|
-
function standardizeDice(dice) {
|
|
72
|
-
return dice.replace(
|
|
73
|
-
/(\[[^\]]+\])|([^[]+)/g,
|
|
74
|
-
(match, insideBrackets, outsideText) => insideBrackets ? insideBrackets : outsideText.standardize()
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
function generateStatsDice(originalDice, stats, dollarValue) {
|
|
78
|
-
let dice = originalDice.standardize();
|
|
79
|
-
if (stats && Object.keys(stats).length > 0) {
|
|
80
|
-
const allStats = Object.keys(stats);
|
|
81
|
-
for (const stat of allStats) {
|
|
82
|
-
const regex = new RegExp(`(?!\\[)${escapeRegex(stat.standardize())}(?!\\])`, "gi");
|
|
83
|
-
if (dice.match(regex)) {
|
|
84
|
-
const statValue = stats[stat];
|
|
85
|
-
dice = dice.replace(regex, statValue.toString());
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
if (dollarValue) dice = dice.replaceAll("$", dollarValue);
|
|
90
|
-
return replaceFormulaInDice(dice);
|
|
91
|
-
}
|
|
92
|
-
function replaceFormulaInDice(dice) {
|
|
93
|
-
const formula = /(?<formula>\{{2}(.+?)}{2})/gim;
|
|
94
|
-
let match;
|
|
95
|
-
let modifiedDice = dice;
|
|
96
|
-
while ((match = formula.exec(dice)) !== null) {
|
|
97
|
-
if (match.groups?.formula) {
|
|
98
|
-
const formulae = match.groups.formula.replaceAll("{{", "").replaceAll("}}", "");
|
|
99
|
-
try {
|
|
100
|
-
const result = (0, import_mathjs.evaluate)(formulae);
|
|
101
|
-
modifiedDice = modifiedDice.replace(match.groups.formula, result.toString());
|
|
102
|
-
} catch (error) {
|
|
103
|
-
throw new FormulaError(match.groups.formula, "replaceFormulasInDice", error);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return cleanedDice(modifiedDice);
|
|
108
|
-
}
|
|
109
|
-
function cleanedDice(dice) {
|
|
110
|
-
return dice.replaceAll("+-", "-").replaceAll("--", "+").replaceAll("++", "+").replaceAll("=>", ">=").replaceAll("=<", "<=").trimEnd();
|
|
111
|
-
}
|
|
112
|
-
function isNumber(value) {
|
|
113
|
-
return value !== void 0 && (typeof value === "number" || !Number.isNaN(Number(value)) && typeof value === "string" && value.trim().length > 0);
|
|
114
|
-
}
|
|
115
|
-
function replaceExpByRandom(dice, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
|
|
116
|
-
const diceRegex = /\{exp( ?\|\| ?(?<default>\d+))?}/gi;
|
|
117
|
-
return dice.replace(diceRegex, (_match, _p1, _p2, _offset, _string, groups) => {
|
|
118
|
-
const defaultValue = groups?.default;
|
|
119
|
-
return defaultValue ?? randomInt(1, 999, engine).toString();
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
function getEngine(engine) {
|
|
123
|
-
switch (engine) {
|
|
124
|
-
case "nativeMath":
|
|
125
|
-
return import_rpg_dice_roller.NumberGenerator.engines.nativeMath;
|
|
126
|
-
case "browserCrypto":
|
|
127
|
-
return import_rpg_dice_roller.NumberGenerator.engines.browserCrypto;
|
|
128
|
-
case "nodeCrypto":
|
|
129
|
-
return import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto;
|
|
130
|
-
default:
|
|
131
|
-
return import_rpg_dice_roller.NumberGenerator.engines.nativeMath;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
function randomInt(min, max, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
|
|
135
|
-
const rng = new import_random_js.Random(engine || void 0);
|
|
136
|
-
return rng.integer(min, max);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// src/dice.ts
|
|
140
|
-
function getCompare(dice, compareRegex, engine = import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto) {
|
|
62
|
+
var import_mathjs = require("mathjs");
|
|
63
|
+
function getCompare(dice, compareRegex, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
|
|
141
64
|
if (dice.match(/((\{.*,(.*)+\}|([><=!]+\d+f))[><=!]+\d+\}?)|\{(.*)([><=!]+).*\}/))
|
|
142
65
|
return { dice, compare: void 0 };
|
|
143
66
|
dice = dice.replace(SIGN_REGEX_SPACE, "");
|
|
144
67
|
let compare;
|
|
145
68
|
const calc = compareRegex[1];
|
|
146
|
-
const sign = calc.match(/[
|
|
69
|
+
const sign = calc.match(/[+-/*^]/)?.[0];
|
|
147
70
|
const compareSign = compareRegex[0].match(SIGN_REGEX)?.[0];
|
|
148
71
|
if (sign) {
|
|
149
72
|
const toCalc = calc.replace(SIGN_REGEX, "").replace(/\s/g, "").replace(/;(.*)/, "");
|
|
150
73
|
const rCompare = rollCompare(toCalc, engine);
|
|
151
|
-
const total = (0,
|
|
74
|
+
const total = (0, import_mathjs.evaluate)(rCompare.value.toString());
|
|
152
75
|
dice = dice.replace(SIGN_REGEX_SPACE, `${compareSign}${total}`);
|
|
153
76
|
compare = {
|
|
154
77
|
sign: compareSign,
|
|
@@ -167,18 +90,18 @@ function getCompare(dice, compareRegex, engine = import_rpg_dice_roller2.NumberG
|
|
|
167
90
|
}
|
|
168
91
|
return { dice, compare };
|
|
169
92
|
}
|
|
170
|
-
function rollCompare(value, engine =
|
|
93
|
+
function rollCompare(value, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
|
|
171
94
|
if (isNumber(value)) return { value: Number.parseInt(value, 10) };
|
|
172
95
|
const rollComp = roll(value, engine);
|
|
173
96
|
if (!rollComp?.total)
|
|
174
|
-
return { value: (0,
|
|
97
|
+
return { value: (0, import_mathjs.evaluate)(value), diceResult: value };
|
|
175
98
|
return {
|
|
176
99
|
dice: value,
|
|
177
100
|
value: rollComp.total,
|
|
178
101
|
diceResult: rollComp?.result
|
|
179
102
|
};
|
|
180
103
|
}
|
|
181
|
-
function createCriticalCustom(dice, customCritical, template, engine =
|
|
104
|
+
function createCriticalCustom(dice, customCritical, template, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
|
|
182
105
|
const compareRegex = dice.match(SIGN_REGEX_SPACE);
|
|
183
106
|
let customDice = dice;
|
|
184
107
|
const compareValue = diceTypeRandomParse(customCritical.value, template, engine);
|
|
@@ -210,7 +133,7 @@ function getModifier(dice) {
|
|
|
210
133
|
}
|
|
211
134
|
return modificator;
|
|
212
135
|
}
|
|
213
|
-
function roll(dice, engine =
|
|
136
|
+
function roll(dice, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
|
|
214
137
|
dice = standardizeDice(dice).replace(/^\+/, "").replaceAll("=>", ">=").replaceAll("=<", "<=").trimStart();
|
|
215
138
|
if (!dice.includes("d")) return void 0;
|
|
216
139
|
dice = dice.replaceAll(DETECT_CRITICAL, "").trimEnd();
|
|
@@ -226,11 +149,11 @@ function roll(dice, engine = import_rpg_dice_roller2.NumberGenerator.engines.nod
|
|
|
226
149
|
if (dice.match(/\d+?#(.*)/)) {
|
|
227
150
|
const diceArray = dice.split("#");
|
|
228
151
|
const numberOfDice = Number.parseInt(diceArray[0], 10);
|
|
229
|
-
|
|
152
|
+
const diceToRoll = diceArray[1].replace(COMMENT_REGEX, "");
|
|
230
153
|
const commentsMatch = diceArray[1].match(COMMENT_REGEX);
|
|
231
154
|
const comments = commentsMatch ? commentsMatch[2] : void 0;
|
|
232
|
-
const roller2 = new
|
|
233
|
-
|
|
155
|
+
const roller2 = new import_rpg_dice_roller.DiceRoller();
|
|
156
|
+
import_rpg_dice_roller.NumberGenerator.generator.engine = engine;
|
|
234
157
|
for (let i = 0; i < numberOfDice; i++) {
|
|
235
158
|
try {
|
|
236
159
|
roller2.roll(diceToRoll);
|
|
@@ -247,8 +170,8 @@ function roll(dice, engine = import_rpg_dice_roller2.NumberGenerator.engines.nod
|
|
|
247
170
|
total: roller2.total
|
|
248
171
|
};
|
|
249
172
|
}
|
|
250
|
-
const roller = new
|
|
251
|
-
|
|
173
|
+
const roller = new import_rpg_dice_roller.DiceRoller();
|
|
174
|
+
import_rpg_dice_roller.NumberGenerator.generator.engine = engine;
|
|
252
175
|
const diceWithoutComment = dice.replace(COMMENT_REGEX, "").trimEnd();
|
|
253
176
|
try {
|
|
254
177
|
roller.roll(diceWithoutComment);
|
|
@@ -268,7 +191,7 @@ function roll(dice, engine = import_rpg_dice_roller2.NumberGenerator.engines.nod
|
|
|
268
191
|
}
|
|
269
192
|
function calculator(sign, value, total) {
|
|
270
193
|
if (sign === "^") sign = "**";
|
|
271
|
-
return (0,
|
|
194
|
+
return (0, import_mathjs.evaluate)(`${total} ${sign} ${value}`);
|
|
272
195
|
}
|
|
273
196
|
function inverseSign(sign) {
|
|
274
197
|
switch (sign) {
|
|
@@ -288,7 +211,7 @@ function inverseSign(sign) {
|
|
|
288
211
|
return "==";
|
|
289
212
|
}
|
|
290
213
|
}
|
|
291
|
-
function replaceInFormula(element, diceResult, compareResult, res, engine =
|
|
214
|
+
function replaceInFormula(element, diceResult, compareResult, res, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
|
|
292
215
|
const { formule, diceAll } = replaceText(
|
|
293
216
|
element,
|
|
294
217
|
diceResult.total ?? 0,
|
|
@@ -298,7 +221,7 @@ function replaceInFormula(element, diceResult, compareResult, res, engine = impo
|
|
|
298
221
|
const invertedSign = res ? compareResult.compare.sign : inverseSign(compareResult.compare.sign);
|
|
299
222
|
let evaluateRoll;
|
|
300
223
|
try {
|
|
301
|
-
evaluateRoll = (0,
|
|
224
|
+
evaluateRoll = (0, import_mathjs.evaluate)(compareResult.dice);
|
|
302
225
|
return `${validSign} ${diceAll}: ${formule} = ${evaluateRoll}${invertedSign}${compareResult.compare?.value}`;
|
|
303
226
|
} catch (error) {
|
|
304
227
|
const evaluateRoll2 = roll(compareResult.dice, engine);
|
|
@@ -307,13 +230,13 @@ function replaceInFormula(element, diceResult, compareResult, res, engine = impo
|
|
|
307
230
|
return `${validSign} ${diceAll}: ${formule} = ${evaluateRoll2}${invertedSign}${compareResult.compare?.value}`;
|
|
308
231
|
}
|
|
309
232
|
}
|
|
310
|
-
function compareSignFormule(toRoll, compareRegex, element, diceResult, engine =
|
|
233
|
+
function compareSignFormule(toRoll, compareRegex, element, diceResult, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
|
|
311
234
|
let results = "";
|
|
312
235
|
const compareResult = getCompare(toRoll, compareRegex, engine);
|
|
313
236
|
const toCompare = `${compareResult.dice}${compareResult.compare?.sign}${compareResult.compare?.value}`;
|
|
314
237
|
let res;
|
|
315
238
|
try {
|
|
316
|
-
res = (0,
|
|
239
|
+
res = (0, import_mathjs.evaluate)(toCompare);
|
|
317
240
|
} catch (error) {
|
|
318
241
|
res = roll(toCompare, engine);
|
|
319
242
|
}
|
|
@@ -322,7 +245,7 @@ function compareSignFormule(toRoll, compareRegex, element, diceResult, engine =
|
|
|
322
245
|
} else if (res instanceof Object) {
|
|
323
246
|
const diceResult2 = res;
|
|
324
247
|
if (diceResult2.compare) {
|
|
325
|
-
const toEvaluate = (0,
|
|
248
|
+
const toEvaluate = (0, import_mathjs.evaluate)(
|
|
326
249
|
`${diceResult2.total}${diceResult2.compare.sign}${diceResult2.compare.value}`
|
|
327
250
|
);
|
|
328
251
|
const sign = toEvaluate ? "\u2713" : "\u2715";
|
|
@@ -344,7 +267,7 @@ function formatComment(dice) {
|
|
|
344
267
|
const commentsMatch = commentsRegex.exec(dice);
|
|
345
268
|
return commentsMatch?.groups?.comments ? `__${commentsMatch.groups.comments}__ \u2014 ` : "";
|
|
346
269
|
}
|
|
347
|
-
function sharedRolls(dice, engine =
|
|
270
|
+
function sharedRolls(dice, engine = import_rpg_dice_roller.NumberGenerator.engines.nodeCrypto) {
|
|
348
271
|
if (dice.match(/\d+?#(.*?)/))
|
|
349
272
|
throw new DiceTypeError(
|
|
350
273
|
dice,
|
|
@@ -380,7 +303,13 @@ function sharedRolls(dice, engine = import_rpg_dice_roller2.NumberGenerator.engi
|
|
|
380
303
|
let toRoll = element.replace(SYMBOL_DICE, `${diceResult.total}`);
|
|
381
304
|
const compareRegex = toRoll.match(SIGN_REGEX_SPACE);
|
|
382
305
|
if (compareRegex) {
|
|
383
|
-
const compareResult = compareSignFormule(
|
|
306
|
+
const compareResult = compareSignFormule(
|
|
307
|
+
toRoll,
|
|
308
|
+
compareRegex,
|
|
309
|
+
element,
|
|
310
|
+
diceResult,
|
|
311
|
+
engine
|
|
312
|
+
);
|
|
384
313
|
toRoll = compareResult.dice;
|
|
385
314
|
results.push(compareResult.results);
|
|
386
315
|
} else {
|
|
@@ -390,7 +319,7 @@ function sharedRolls(dice, engine = import_rpg_dice_roller2.NumberGenerator.engi
|
|
|
390
319
|
diceResult.dice
|
|
391
320
|
);
|
|
392
321
|
try {
|
|
393
|
-
const evaluated = (0,
|
|
322
|
+
const evaluated = (0, import_mathjs.evaluate)(toRoll);
|
|
394
323
|
results.push(`\u25C8 ${comment}${diceAll}: ${formule} = ${evaluated}`);
|
|
395
324
|
total += Number.parseInt(evaluated, 10);
|
|
396
325
|
} catch (error) {
|
|
@@ -416,6 +345,222 @@ function sharedRolls(dice, engine = import_rpg_dice_roller2.NumberGenerator.engi
|
|
|
416
345
|
};
|
|
417
346
|
}
|
|
418
347
|
|
|
348
|
+
// src/errors.ts
|
|
349
|
+
var DiceTypeError = class extends Error {
|
|
350
|
+
dice;
|
|
351
|
+
cause;
|
|
352
|
+
method;
|
|
353
|
+
constructor(dice, cause, method) {
|
|
354
|
+
super(dice);
|
|
355
|
+
this.name = "Invalid_Dice_Type";
|
|
356
|
+
this.dice = dice;
|
|
357
|
+
this.cause = cause;
|
|
358
|
+
this.method = method;
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
var FormulaError = class extends Error {
|
|
362
|
+
formula;
|
|
363
|
+
cause;
|
|
364
|
+
method;
|
|
365
|
+
constructor(formula, cause, method) {
|
|
366
|
+
super(formula);
|
|
367
|
+
this.name = "Invalid_Formula";
|
|
368
|
+
this.formula = formula;
|
|
369
|
+
this.cause = cause;
|
|
370
|
+
this.method = method;
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
var MaxGreater = class extends Error {
|
|
374
|
+
name;
|
|
375
|
+
value;
|
|
376
|
+
max;
|
|
377
|
+
constructor(value, max) {
|
|
378
|
+
super(value.toString());
|
|
379
|
+
this.name = "Max_Greater";
|
|
380
|
+
this.value = value;
|
|
381
|
+
this.max = max;
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
var EmptyObjectError = class extends Error {
|
|
385
|
+
name;
|
|
386
|
+
constructor() {
|
|
387
|
+
super();
|
|
388
|
+
this.name = "Empty_Object";
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
var TooManyDice = class extends Error {
|
|
392
|
+
name;
|
|
393
|
+
constructor() {
|
|
394
|
+
super();
|
|
395
|
+
this.name = "Too_Many_Dice";
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
var TooManyStats = class extends Error {
|
|
399
|
+
name;
|
|
400
|
+
constructor() {
|
|
401
|
+
super();
|
|
402
|
+
this.name = "Too_Many_Stats";
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
var NoStatisticsError = class extends Error {
|
|
406
|
+
name;
|
|
407
|
+
constructor() {
|
|
408
|
+
super();
|
|
409
|
+
this.name = "No_Statistics";
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
// src/interfaces/constant.ts
|
|
414
|
+
var COMMENT_REGEX = /\s+(#|\/{2}|\[|\/\*)(?<comment>.*)/;
|
|
415
|
+
var SIGN_REGEX = /[><=!]+/;
|
|
416
|
+
var SIGN_REGEX_SPACE = /[><=!]+(\S+)/;
|
|
417
|
+
var SYMBOL_DICE = "&";
|
|
418
|
+
var DETECT_CRITICAL = /\{\*?c[fs]:[<>=!]+(.+?)}/gim;
|
|
419
|
+
|
|
420
|
+
// src/interfaces/zod.ts
|
|
421
|
+
var import_zod = require("zod");
|
|
422
|
+
var statisticValueSchema = import_zod.z.object({
|
|
423
|
+
max: import_zod.z.number().min(0).transform((val) => val === 0 ? void 0 : val).optional(),
|
|
424
|
+
min: import_zod.z.number().min(0).transform((val) => val === 0 ? void 0 : val).optional(),
|
|
425
|
+
combinaison: import_zod.z.string().transform((str) => str.trim() || void 0).optional(),
|
|
426
|
+
exclude: import_zod.z.boolean().optional()
|
|
427
|
+
}).superRefine((data, ctx) => {
|
|
428
|
+
if (data.max !== void 0 && data.min !== void 0 && data.max <= data.min) {
|
|
429
|
+
ctx.addIssue({
|
|
430
|
+
code: "custom",
|
|
431
|
+
message: `Max_Greater; ${data.min}; ${data.max}`,
|
|
432
|
+
path: ["max"]
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
var statisticSchema = import_zod.z.record(import_zod.z.string(), statisticValueSchema).optional().refine((stats) => !stats || Object.keys(stats).length <= 25, {
|
|
437
|
+
message: "TooManyStats"
|
|
438
|
+
});
|
|
439
|
+
var criticalSchema = import_zod.z.object({
|
|
440
|
+
success: import_zod.z.string().or(import_zod.z.number().min(0)).optional(),
|
|
441
|
+
failure: import_zod.z.string().or(import_zod.z.number().min(0)).optional()
|
|
442
|
+
}).transform((values) => {
|
|
443
|
+
if (values.success === "") values.success = void 0;
|
|
444
|
+
if (values.failure === "") values.failure = void 0;
|
|
445
|
+
if (values.failure === 0) values.failure = void 0;
|
|
446
|
+
if (values.success === 0) values.success = void 0;
|
|
447
|
+
values.success = Number.parseInt(values.success, 10);
|
|
448
|
+
values.failure = Number.parseInt(values.failure, 10);
|
|
449
|
+
return values;
|
|
450
|
+
});
|
|
451
|
+
var criticalValueSchema = import_zod.z.object({
|
|
452
|
+
sign: import_zod.z.enum(["<", ">", "<=", ">=", "!=", "=="]),
|
|
453
|
+
value: import_zod.z.string(),
|
|
454
|
+
onNaturalDice: import_zod.z.boolean().optional(),
|
|
455
|
+
affectSkill: import_zod.z.boolean().optional()
|
|
456
|
+
});
|
|
457
|
+
var damageSchema = import_zod.z.record(import_zod.z.string(), import_zod.z.string()).optional().refine((stats) => !stats || Object.keys(stats).length <= 25, {
|
|
458
|
+
message: "TooManyDice"
|
|
459
|
+
});
|
|
460
|
+
var customCriticalSchema = import_zod.z.record(import_zod.z.string(), criticalValueSchema).optional().refine((stats) => !stats || Object.keys(stats).length <= 22, {
|
|
461
|
+
message: "TooManyDice"
|
|
462
|
+
});
|
|
463
|
+
var templateSchema = import_zod.z.object({
|
|
464
|
+
charName: import_zod.z.boolean().optional(),
|
|
465
|
+
statistics: statisticSchema,
|
|
466
|
+
total: import_zod.z.number().min(0).transform((val) => val === 0 ? void 0 : val).optional(),
|
|
467
|
+
forceDistrib: import_zod.z.boolean().optional(),
|
|
468
|
+
diceType: import_zod.z.string().optional(),
|
|
469
|
+
critical: criticalSchema.optional(),
|
|
470
|
+
customCritical: customCriticalSchema,
|
|
471
|
+
damage: damageSchema
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
// src/utils.ts
|
|
475
|
+
var import_mathjs2 = require("mathjs");
|
|
476
|
+
var import_uniformize = require("uniformize");
|
|
477
|
+
var import_rpg_dice_roller2 = require("@dice-roller/rpg-dice-roller");
|
|
478
|
+
var import_random_js = require("random-js");
|
|
479
|
+
function escapeRegex(string) {
|
|
480
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
481
|
+
}
|
|
482
|
+
function standardizeDice(dice) {
|
|
483
|
+
return dice.replace(
|
|
484
|
+
/(\[[^\]]+\])|([^[]+)/g,
|
|
485
|
+
(_match, insideBrackets, outsideText) => insideBrackets ? insideBrackets : outsideText.standardize()
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
function generateStatsDice(originalDice, stats, dollarValue) {
|
|
489
|
+
let dice = originalDice.standardize();
|
|
490
|
+
if (stats && Object.keys(stats).length > 0) {
|
|
491
|
+
const allStats = Object.keys(stats);
|
|
492
|
+
for (const stat of allStats) {
|
|
493
|
+
const regex = new RegExp(`(?!\\[)${escapeRegex(stat.standardize())}(?!\\])`, "gi");
|
|
494
|
+
if (dice.match(regex)) {
|
|
495
|
+
const statValue = stats[stat];
|
|
496
|
+
dice = dice.replace(regex, statValue.toString());
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
if (dollarValue) dice = dice.replaceAll("$", dollarValue);
|
|
501
|
+
return replaceFormulaInDice(dice);
|
|
502
|
+
}
|
|
503
|
+
function replaceFormulaInDice(dice) {
|
|
504
|
+
const formula = /(?<formula>\{{2}(.+?)}{2})/gim;
|
|
505
|
+
let match;
|
|
506
|
+
let modifiedDice = dice;
|
|
507
|
+
while ((match = formula.exec(dice)) !== null) {
|
|
508
|
+
if (match.groups?.formula) {
|
|
509
|
+
const formulae = match.groups.formula.replaceAll("{{", "").replaceAll("}}", "");
|
|
510
|
+
try {
|
|
511
|
+
const result = (0, import_mathjs2.evaluate)(formulae);
|
|
512
|
+
modifiedDice = modifiedDice.replace(match.groups.formula, result.toString());
|
|
513
|
+
} catch (error) {
|
|
514
|
+
throw new FormulaError(match.groups.formula, "replaceFormulasInDice", error);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return cleanedDice(modifiedDice);
|
|
519
|
+
}
|
|
520
|
+
function cleanedDice(dice) {
|
|
521
|
+
return dice.replaceAll("+-", "-").replaceAll("--", "+").replaceAll("++", "+").replaceAll("=>", ">=").replaceAll("=<", "<=").trimEnd();
|
|
522
|
+
}
|
|
523
|
+
function isNumber(value) {
|
|
524
|
+
return value !== void 0 && (typeof value === "number" || !Number.isNaN(Number(value)) && typeof value === "string" && value.trim().length > 0);
|
|
525
|
+
}
|
|
526
|
+
function replaceExpByRandom(dice, engine = import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto) {
|
|
527
|
+
const diceRegex = /\{exp( ?\|\| ?(?<default>\d+))?}/gi;
|
|
528
|
+
return dice.replace(diceRegex, (_match, _p1, _p2, _offset, _string, groups) => {
|
|
529
|
+
const defaultValue = groups?.default;
|
|
530
|
+
return defaultValue ?? randomInt(1, 999, engine).toString();
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
function randomInt(min, max, engine = import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto, rng) {
|
|
534
|
+
if (!rng) rng = new import_random_js.Random(engine || void 0);
|
|
535
|
+
return rng.integer(min, max);
|
|
536
|
+
}
|
|
537
|
+
function getEngineId2(engine) {
|
|
538
|
+
if (engine === import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto) return "nodeCrypto";
|
|
539
|
+
if (engine === import_rpg_dice_roller2.NumberGenerator.engines.nativeMath) return "nativeMath";
|
|
540
|
+
if (engine === import_rpg_dice_roller2.NumberGenerator.engines.browserCrypto) return "browserCrypto";
|
|
541
|
+
try {
|
|
542
|
+
const e = engine;
|
|
543
|
+
if (e && typeof e === "object") {
|
|
544
|
+
if (typeof e.name === "string" && e.name) return e.name;
|
|
545
|
+
if (e.constructor?.name) return e.constructor.name;
|
|
546
|
+
}
|
|
547
|
+
} catch {
|
|
548
|
+
}
|
|
549
|
+
return "unknown";
|
|
550
|
+
}
|
|
551
|
+
function getEngine(engine) {
|
|
552
|
+
switch (engine) {
|
|
553
|
+
case "nativeMath":
|
|
554
|
+
return import_rpg_dice_roller2.NumberGenerator.engines.nativeMath;
|
|
555
|
+
case "browserCrypto":
|
|
556
|
+
return import_rpg_dice_roller2.NumberGenerator.engines.browserCrypto;
|
|
557
|
+
case "nodeCrypto":
|
|
558
|
+
return import_rpg_dice_roller2.NumberGenerator.engines.nodeCrypto;
|
|
559
|
+
default:
|
|
560
|
+
return import_rpg_dice_roller2.NumberGenerator.engines.nativeMath;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
419
564
|
// src/verify_template.ts
|
|
420
565
|
var import_mathjs3 = require("mathjs");
|
|
421
566
|
var import_random_js2 = require("random-js");
|
|
@@ -449,8 +594,8 @@ function diceRandomParse(value, template, engine = import_rpg_dice_roller3.Numbe
|
|
|
449
594
|
for (const name of statNames) {
|
|
450
595
|
const regex = new RegExp(escapeRegex(name.standardize()), "gi");
|
|
451
596
|
if (value.match(regex)) {
|
|
452
|
-
let max
|
|
453
|
-
let min
|
|
597
|
+
let max;
|
|
598
|
+
let min;
|
|
454
599
|
const foundStat = template.statistics?.[name];
|
|
455
600
|
if (foundStat) {
|
|
456
601
|
max = foundStat.max;
|
|
@@ -542,8 +687,7 @@ function verifyTemplateValue(template, verify = true, engine = import_rpg_dice_r
|
|
|
542
687
|
engine
|
|
543
688
|
);
|
|
544
689
|
const rolled = roll(cleanedDice2, engine);
|
|
545
|
-
if (!rolled)
|
|
546
|
-
throw new DiceTypeError(cleanedDice2, "no_roll_result", "no roll result");
|
|
690
|
+
if (!rolled) throw new DiceTypeError(cleanedDice2, "no_roll_result", "no roll result");
|
|
547
691
|
}
|
|
548
692
|
if (statistiqueTemplate.customCritical) {
|
|
549
693
|
if (!statistiqueTemplate.diceType) {
|
|
@@ -618,141 +762,15 @@ function testStatCombinaison(template, engine = import_rpg_dice_roller3.NumberGe
|
|
|
618
762
|
}
|
|
619
763
|
function generateRandomStat(total = 100, max, min, engine = import_rpg_dice_roller3.NumberGenerator.engines.nodeCrypto) {
|
|
620
764
|
let randomStatValue = total + 1;
|
|
765
|
+
const random = new import_random_js2.Random(engine || import_rpg_dice_roller3.NumberGenerator.engines.nodeCrypto);
|
|
621
766
|
while (randomStatValue >= total || randomStatValue === 0) {
|
|
622
|
-
|
|
623
|
-
if (max
|
|
624
|
-
else if (
|
|
625
|
-
else
|
|
626
|
-
else randomStatValue = random2.integer(1, total);
|
|
767
|
+
if (max && min) randomStatValue = randomInt(min, max, engine, random);
|
|
768
|
+
else if (max) randomStatValue = randomInt(1, max, engine, random);
|
|
769
|
+
else if (min) randomStatValue = randomInt(min, total, engine, random);
|
|
770
|
+
else randomStatValue = randomInt(1, total, engine, random);
|
|
627
771
|
}
|
|
628
772
|
return randomStatValue;
|
|
629
773
|
}
|
|
630
|
-
|
|
631
|
-
// src/errors.ts
|
|
632
|
-
var DiceTypeError = class extends Error {
|
|
633
|
-
dice;
|
|
634
|
-
cause;
|
|
635
|
-
method;
|
|
636
|
-
constructor(dice, cause, method) {
|
|
637
|
-
super(dice);
|
|
638
|
-
this.name = "Invalid_Dice_Type";
|
|
639
|
-
this.dice = dice;
|
|
640
|
-
this.cause = cause;
|
|
641
|
-
this.method = method;
|
|
642
|
-
}
|
|
643
|
-
};
|
|
644
|
-
var FormulaError = class extends Error {
|
|
645
|
-
formula;
|
|
646
|
-
cause;
|
|
647
|
-
method;
|
|
648
|
-
constructor(formula, cause, method) {
|
|
649
|
-
super(formula);
|
|
650
|
-
this.name = "Invalid_Formula";
|
|
651
|
-
this.formula = formula;
|
|
652
|
-
this.cause = cause;
|
|
653
|
-
this.method = method;
|
|
654
|
-
}
|
|
655
|
-
};
|
|
656
|
-
var MaxGreater = class extends Error {
|
|
657
|
-
name;
|
|
658
|
-
value;
|
|
659
|
-
max;
|
|
660
|
-
constructor(value, max) {
|
|
661
|
-
super(value.toString());
|
|
662
|
-
this.name = "Max_Greater";
|
|
663
|
-
this.value = value;
|
|
664
|
-
this.max = max;
|
|
665
|
-
}
|
|
666
|
-
};
|
|
667
|
-
var EmptyObjectError = class extends Error {
|
|
668
|
-
name;
|
|
669
|
-
constructor() {
|
|
670
|
-
super();
|
|
671
|
-
this.name = "Empty_Object";
|
|
672
|
-
}
|
|
673
|
-
};
|
|
674
|
-
var TooManyDice = class extends Error {
|
|
675
|
-
name;
|
|
676
|
-
constructor() {
|
|
677
|
-
super();
|
|
678
|
-
this.name = "Too_Many_Dice";
|
|
679
|
-
}
|
|
680
|
-
};
|
|
681
|
-
var TooManyStats = class extends Error {
|
|
682
|
-
name;
|
|
683
|
-
constructor() {
|
|
684
|
-
super();
|
|
685
|
-
this.name = "Too_Many_Stats";
|
|
686
|
-
}
|
|
687
|
-
};
|
|
688
|
-
var NoStatisticsError = class extends Error {
|
|
689
|
-
name;
|
|
690
|
-
constructor() {
|
|
691
|
-
super();
|
|
692
|
-
this.name = "No_Statistics";
|
|
693
|
-
}
|
|
694
|
-
};
|
|
695
|
-
|
|
696
|
-
// src/interfaces/constant.ts
|
|
697
|
-
var COMMENT_REGEX = /\s+(#|\/{2}|\[|\/\*)(?<comment>.*)/;
|
|
698
|
-
var SIGN_REGEX = /[><=!]+/;
|
|
699
|
-
var SIGN_REGEX_SPACE = /[><=!]+(\S+)/;
|
|
700
|
-
var SYMBOL_DICE = "&";
|
|
701
|
-
var DETECT_CRITICAL = /\{\*?c[fs]:[<>=!]+(.+?)}/gim;
|
|
702
|
-
|
|
703
|
-
// src/interfaces/zod.ts
|
|
704
|
-
var import_zod = require("zod");
|
|
705
|
-
var statisticValueSchema = import_zod.z.object({
|
|
706
|
-
max: import_zod.z.number().min(0).transform((val) => val === 0 ? void 0 : val).optional(),
|
|
707
|
-
min: import_zod.z.number().min(0).transform((val) => val === 0 ? void 0 : val).optional(),
|
|
708
|
-
combinaison: import_zod.z.string().transform((str) => str.trim() || void 0).optional(),
|
|
709
|
-
exclude: import_zod.z.boolean().optional()
|
|
710
|
-
}).superRefine((data, ctx) => {
|
|
711
|
-
if (data.max !== void 0 && data.min !== void 0 && data.max <= data.min) {
|
|
712
|
-
ctx.addIssue({
|
|
713
|
-
code: "custom",
|
|
714
|
-
message: `Max_Greater; ${data.min}; ${data.max}`,
|
|
715
|
-
path: ["max"]
|
|
716
|
-
});
|
|
717
|
-
}
|
|
718
|
-
});
|
|
719
|
-
var statisticSchema = import_zod.z.record(import_zod.z.string(), statisticValueSchema).optional().refine((stats) => !stats || Object.keys(stats).length <= 25, {
|
|
720
|
-
message: "TooManyStats"
|
|
721
|
-
});
|
|
722
|
-
var criticalSchema = import_zod.z.object({
|
|
723
|
-
success: import_zod.z.string().or(import_zod.z.number().min(0)).optional(),
|
|
724
|
-
failure: import_zod.z.string().or(import_zod.z.number().min(0)).optional()
|
|
725
|
-
}).transform((values) => {
|
|
726
|
-
if (values.success === "") values.success = void 0;
|
|
727
|
-
if (values.failure === "") values.failure = void 0;
|
|
728
|
-
if (values.failure === 0) values.failure = void 0;
|
|
729
|
-
if (values.success === 0) values.success = void 0;
|
|
730
|
-
values.success = Number.parseInt(values.success, 10);
|
|
731
|
-
values.failure = Number.parseInt(values.failure, 10);
|
|
732
|
-
return values;
|
|
733
|
-
});
|
|
734
|
-
var criticalValueSchema = import_zod.z.object({
|
|
735
|
-
sign: import_zod.z.enum(["<", ">", "<=", ">=", "!=", "=="]),
|
|
736
|
-
value: import_zod.z.string(),
|
|
737
|
-
onNaturalDice: import_zod.z.boolean().optional(),
|
|
738
|
-
affectSkill: import_zod.z.boolean().optional()
|
|
739
|
-
});
|
|
740
|
-
var damageSchema = import_zod.z.record(import_zod.z.string(), import_zod.z.string()).optional().refine((stats) => !stats || Object.keys(stats).length <= 25, {
|
|
741
|
-
message: "TooManyDice"
|
|
742
|
-
});
|
|
743
|
-
var customCriticalSchema = import_zod.z.record(import_zod.z.string(), criticalValueSchema).optional().refine((stats) => !stats || Object.keys(stats).length <= 22, {
|
|
744
|
-
message: "TooManyDice"
|
|
745
|
-
});
|
|
746
|
-
var templateSchema = import_zod.z.object({
|
|
747
|
-
charName: import_zod.z.boolean().optional(),
|
|
748
|
-
statistics: statisticSchema,
|
|
749
|
-
total: import_zod.z.number().min(0).transform((val) => val === 0 ? void 0 : val).optional(),
|
|
750
|
-
forceDistrib: import_zod.z.boolean().optional(),
|
|
751
|
-
diceType: import_zod.z.string().optional(),
|
|
752
|
-
critical: criticalSchema.optional(),
|
|
753
|
-
customCritical: customCriticalSchema,
|
|
754
|
-
damage: damageSchema
|
|
755
|
-
});
|
|
756
774
|
// Annotate the CommonJS export names for ESM import in node:
|
|
757
775
|
0 && (module.exports = {
|
|
758
776
|
COMMENT_REGEX,
|
|
@@ -778,6 +796,7 @@ var templateSchema = import_zod.z.object({
|
|
|
778
796
|
generateRandomStat,
|
|
779
797
|
generateStatsDice,
|
|
780
798
|
getEngine,
|
|
799
|
+
getEngineId,
|
|
781
800
|
isNumber,
|
|
782
801
|
randomInt,
|
|
783
802
|
replaceExpByRandom,
|