@dicelette/core 1.0.10 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +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 +17 -7
- 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/core/utils.ts
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
import { evaluate } from "mathjs";
|
2
|
-
import removeAccents from "remove-accents";
|
3
|
-
|
4
|
-
/**
|
5
|
-
* Escape regex string
|
6
|
-
* @param string {string}
|
7
|
-
*/
|
8
|
-
export function escapeRegex(string: string) {
|
9
|
-
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
10
|
-
}
|
11
|
-
|
12
|
-
|
13
|
-
/**
|
14
|
-
* Replace the stat name by their value using stat and after evaluate any formula using `replaceFormulaInDice`
|
15
|
-
* @param originalDice {dice}
|
16
|
-
* @param stats {[name: string]: number}
|
17
|
-
*/
|
18
|
-
export function generateStatsDice(originalDice: string, stats?: {[name: string]: number}) {
|
19
|
-
let dice = originalDice;
|
20
|
-
if (stats && Object.keys(stats).length > 0) {
|
21
|
-
//damage field support adding statistic, like : 1d6 + strength
|
22
|
-
//check if the value contains a statistic & calculate if it's okay
|
23
|
-
//the dice will be converted before roll
|
24
|
-
const allStats = Object.keys(stats);
|
25
|
-
for (const stat of allStats) {
|
26
|
-
const regex = new RegExp(escapeRegex(removeAccents(stat)), "gi");
|
27
|
-
if (dice.match(regex)) {
|
28
|
-
const statValue = stats[stat];
|
29
|
-
dice = dice.replace(regex, statValue.toString());
|
30
|
-
}
|
31
|
-
}
|
32
|
-
}
|
33
|
-
return replaceFormulaInDice(dice);
|
34
|
-
|
35
|
-
}
|
36
|
-
|
37
|
-
/**
|
38
|
-
* Replace the {{}} in the dice string and evaluate the interior if any
|
39
|
-
* @param dice {string}
|
40
|
-
*/
|
41
|
-
export function replaceFormulaInDice(dice: string) {
|
42
|
-
const formula = /(?<formula>\{{2}(.+?)\}{2})/gmi;
|
43
|
-
const formulaMatch = formula.exec(dice);
|
44
|
-
if (formulaMatch?.groups?.formula) {
|
45
|
-
const formula = formulaMatch.groups.formula.replaceAll("{{", "").replaceAll("}}", "");
|
46
|
-
try {
|
47
|
-
const result = evaluate(formula);
|
48
|
-
return cleanedDice(dice.replace(formulaMatch.groups.formula, result.toString()));
|
49
|
-
} catch (error) {
|
50
|
-
throw new Error(`[error.invalidFormula, common.space]: ${formulaMatch.groups.formula}`);
|
51
|
-
}
|
52
|
-
}
|
53
|
-
return cleanedDice(dice);
|
54
|
-
}
|
55
|
-
|
56
|
-
/**
|
57
|
-
* Replace the ++ +- -- by their proper value:
|
58
|
-
* - `++` = `+`
|
59
|
-
* - `+-` = `-`
|
60
|
-
* - `--` = `+`
|
61
|
-
* @param dice {string}
|
62
|
-
*/
|
63
|
-
export function cleanedDice(dice: string) {
|
64
|
-
return dice.replaceAll("+-", "-").replaceAll("--", "+").replaceAll("++", "+");
|
65
|
-
}
|
package/core/verify_template.ts
DELETED
@@ -1,263 +0,0 @@
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2
|
-
import { evaluate } from "mathjs";
|
3
|
-
import {Random } from "random-js";
|
4
|
-
import removeAccents from "remove-accents";
|
5
|
-
|
6
|
-
import { Statistic, StatisticalTemplate } from "..";
|
7
|
-
import { roll } from "./dice";
|
8
|
-
import { escapeRegex, replaceFormulaInDice } from "./utils";
|
9
|
-
|
10
|
-
/**
|
11
|
-
* Verify if the provided dice work with random value
|
12
|
-
* @param testDice {string}
|
13
|
-
* @param stats {[name: string]: number}
|
14
|
-
*/
|
15
|
-
export function evalStatsDice(testDice: string, stats?: {[name: string]: number}) {
|
16
|
-
let dice = testDice;
|
17
|
-
if (stats && Object.keys(stats).length > 0) {
|
18
|
-
const allStats = Object.keys(stats);
|
19
|
-
for (const stat of allStats) {
|
20
|
-
const regex = new RegExp(escapeRegex(removeAccents(stat)), "gi");
|
21
|
-
if (testDice.match(regex)) {
|
22
|
-
const statValue = stats[stat];
|
23
|
-
dice = testDice.replace(regex, statValue.toString());
|
24
|
-
}
|
25
|
-
}
|
26
|
-
}
|
27
|
-
try {
|
28
|
-
if (!roll(replaceFormulaInDice(dice))) throw new Error(`[error.invalidDice.withoutDice, common.space] ${dice}`);
|
29
|
-
return testDice;
|
30
|
-
} catch (error) {
|
31
|
-
throw new Error(`[error.invalidDice.withoutDice, common.space]: ${testDice}\n${(error as Error).message}`);
|
32
|
-
}
|
33
|
-
}
|
34
|
-
|
35
|
-
/**
|
36
|
-
* Generate a random dice and remove the formula (+ evaluate it)
|
37
|
-
* Used for diceDamage only
|
38
|
-
* @param value {string}
|
39
|
-
* @param template {StatisticalTemplate}
|
40
|
-
* @returns
|
41
|
-
*/
|
42
|
-
export function diceRandomParse(value: string, template: StatisticalTemplate) {
|
43
|
-
if (!template.statistics) return value;
|
44
|
-
value = removeAccents(value);
|
45
|
-
const allStats = Object.keys(template.statistics).map(stat => removeAccents(stat).toLowerCase());
|
46
|
-
let newDice = value;
|
47
|
-
for (const stat of allStats) {
|
48
|
-
const regex = new RegExp(escapeRegex(stat), "gi");
|
49
|
-
if (value.match(regex)) {
|
50
|
-
let max: undefined | number = undefined;
|
51
|
-
let min: undefined | number = undefined;
|
52
|
-
const stats = template.statistics?.[stat];
|
53
|
-
if (stats) {
|
54
|
-
max = template.statistics[removeAccents(stat).toLowerCase()].max;
|
55
|
-
min = template.statistics[removeAccents(stat).toLowerCase()].min;
|
56
|
-
}
|
57
|
-
const total = template.total || 100;
|
58
|
-
const randomStatValue = generateRandomStat(total, max, min);
|
59
|
-
newDice = value.replace(regex, randomStatValue.toString());
|
60
|
-
}
|
61
|
-
}
|
62
|
-
return replaceFormulaInDice(newDice);
|
63
|
-
}
|
64
|
-
|
65
|
-
/**
|
66
|
-
* Same as damageDice but for DiceType
|
67
|
-
* @param dice {string}
|
68
|
-
* @param template {StatisticalTemplate}
|
69
|
-
*/
|
70
|
-
export function diceTypeRandomParse(dice: string, template: StatisticalTemplate) {
|
71
|
-
if (!template.statistics) return dice;
|
72
|
-
const firstStatNotCombinaison = Object.keys(template.statistics).find(stat => !template.statistics?.[stat].combinaison);
|
73
|
-
if (!firstStatNotCombinaison) return dice;
|
74
|
-
const stats = template.statistics[firstStatNotCombinaison];
|
75
|
-
const {min, max} = stats;
|
76
|
-
const total = template.total || 100;
|
77
|
-
const randomStatValue = generateRandomStat(total, max, min);
|
78
|
-
return replaceFormulaInDice(dice.replace("$", randomStatValue.toString()));
|
79
|
-
}
|
80
|
-
|
81
|
-
/**
|
82
|
-
* Random the combinaison and evaluate it to check if everything is valid
|
83
|
-
* @param combinaison {[name: string]: string}
|
84
|
-
* @param stats {[name: string]: string|number}
|
85
|
-
*/
|
86
|
-
export function evalCombinaison(combinaison: {[name: string]: string}, stats: {[name: string]: string | number}) {
|
87
|
-
const newStats: {[name: string]: number} = {};
|
88
|
-
for (const [stat, combin] of Object.entries(combinaison)) {
|
89
|
-
//replace the stats in formula
|
90
|
-
let formula = removeAccents(combin);
|
91
|
-
for (const [statName, value] of Object.entries(stats)) {
|
92
|
-
const regex = new RegExp(removeAccents(statName), "gi");
|
93
|
-
formula = formula.replace(regex, value.toString());
|
94
|
-
}
|
95
|
-
try {
|
96
|
-
const result = evaluate(formula);
|
97
|
-
newStats[stat] = result;
|
98
|
-
} catch (error) {
|
99
|
-
throw new Error(`[error.invalidFormula, common.space]: ${stat}`);
|
100
|
-
}
|
101
|
-
}
|
102
|
-
return newStats;
|
103
|
-
}
|
104
|
-
|
105
|
-
/**
|
106
|
-
* Evaluate one selected combinaison
|
107
|
-
* @param combinaison {string}
|
108
|
-
* @param stats {[name: string]: string|number}
|
109
|
-
*/
|
110
|
-
export function evalOneCombinaison(combinaison: string, stats: {[name: string]: string | number}) {
|
111
|
-
let formula = removeAccents(combinaison);
|
112
|
-
for (const [statName, value] of Object.entries(stats)) {
|
113
|
-
const regex = new RegExp(removeAccents(statName), "gi");
|
114
|
-
formula = formula.replace(regex, value.toString());
|
115
|
-
}
|
116
|
-
try {
|
117
|
-
return evaluate(formula);
|
118
|
-
} catch (error) {
|
119
|
-
throw new Error(`[error.invalidFormula, common.space]: ${combinaison}`);
|
120
|
-
}
|
121
|
-
}
|
122
|
-
|
123
|
-
/**
|
124
|
-
* Parse the provided JSON and verify each field to check if everything could work when rolling
|
125
|
-
* @param {any} template
|
126
|
-
* @returns {StatisticalTemplate}
|
127
|
-
*/
|
128
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
129
|
-
export function verifyTemplateValue(template: any): StatisticalTemplate {
|
130
|
-
const statistiqueTemplate: StatisticalTemplate = {
|
131
|
-
diceType: "",
|
132
|
-
statistics: {} as Statistic
|
133
|
-
};
|
134
|
-
if (!template.statistics) statistiqueTemplate.statistics = undefined;
|
135
|
-
else if (template.statistics && Object.keys(template.statistics).length > 0) {
|
136
|
-
for (const [key, value] of Object.entries(template.statistics)) {
|
137
|
-
const dataValue = value as { max?: number, min?: number, combinaison?: string };
|
138
|
-
if (dataValue.max && dataValue.min && dataValue.max <= dataValue.min)
|
139
|
-
throw new Error("[error.maxGreater]");
|
140
|
-
if (dataValue.max && dataValue.max <= 0 ) dataValue.max = undefined;
|
141
|
-
if (dataValue.min && dataValue.min <= 0 ) dataValue.min = undefined;
|
142
|
-
let formula = dataValue.combinaison ? removeAccents(dataValue.combinaison).toLowerCase() : undefined;
|
143
|
-
formula = formula && formula.trim().length > 0 ? formula : undefined;
|
144
|
-
if (!statistiqueTemplate.statistics) {
|
145
|
-
statistiqueTemplate.statistics = {} as Statistic;
|
146
|
-
}
|
147
|
-
statistiqueTemplate.statistics[key] = {
|
148
|
-
max: dataValue.max,
|
149
|
-
min: dataValue.min,
|
150
|
-
combinaison: formula || undefined,
|
151
|
-
};
|
152
|
-
}
|
153
|
-
}
|
154
|
-
if (template.diceType) {
|
155
|
-
try {
|
156
|
-
statistiqueTemplate.diceType = template.diceType;
|
157
|
-
diceTypeRandomParse(template.diceType, statistiqueTemplate);
|
158
|
-
} catch (e) {
|
159
|
-
throw new Error((e as Error).message);
|
160
|
-
}
|
161
|
-
}
|
162
|
-
|
163
|
-
|
164
|
-
if (template.critical && Object.keys(template.critical).length > 0){
|
165
|
-
statistiqueTemplate.critical = {
|
166
|
-
failure: template.critical.failure ?? undefined,
|
167
|
-
success: template.critical.success ?? undefined
|
168
|
-
};
|
169
|
-
|
170
|
-
}
|
171
|
-
if (template.total) {
|
172
|
-
if (template.total <= 0)
|
173
|
-
template.total = undefined;
|
174
|
-
statistiqueTemplate.total = template.total;
|
175
|
-
}
|
176
|
-
if (template.charName) statistiqueTemplate.charName = template.charName;
|
177
|
-
if (template.damage) statistiqueTemplate.damage = template.damage;
|
178
|
-
try {
|
179
|
-
testDamageRoll(statistiqueTemplate);
|
180
|
-
testCombinaison(statistiqueTemplate);
|
181
|
-
} catch (error) {
|
182
|
-
throw new Error((error as Error).message);
|
183
|
-
}
|
184
|
-
return statistiqueTemplate;
|
185
|
-
}
|
186
|
-
|
187
|
-
/**
|
188
|
-
* Test each damage roll from the template.damage
|
189
|
-
* @param {StatisticalTemplate} template
|
190
|
-
*/
|
191
|
-
export function testDamageRoll(template: StatisticalTemplate) {
|
192
|
-
if (!template.damage) return;
|
193
|
-
if (Object.keys(template.damage).length === 0) throw new Error("[error.emptyObject]");
|
194
|
-
if (Object.keys(template.damage).length > 25) throw new Error("[error.tooManyDice]");
|
195
|
-
for (const [name, dice] of Object.entries(template.damage)) {
|
196
|
-
if (!dice) continue;
|
197
|
-
const randomDiceParsed = diceRandomParse(dice, template);
|
198
|
-
try {
|
199
|
-
roll(randomDiceParsed);
|
200
|
-
} catch (error) {
|
201
|
-
throw new Error(`[error.invalidDice, common.space] ${name}`);
|
202
|
-
}
|
203
|
-
}
|
204
|
-
}
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
/**
|
209
|
-
* Test all combinaison with generated random value
|
210
|
-
* @param {StatisticalTemplate} template
|
211
|
-
*/
|
212
|
-
export function testCombinaison(template: StatisticalTemplate) {
|
213
|
-
if (!template.statistics) return;
|
214
|
-
const onlyCombinaisonStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => value.combinaison !== undefined));
|
215
|
-
const allOtherStats = Object.fromEntries(Object.entries(template.statistics).filter(([_, value]) => !value.combinaison));
|
216
|
-
if (Object.keys(onlyCombinaisonStats).length===0) return;
|
217
|
-
const allStats = Object.keys(template.statistics).filter(stat => !template.statistics![stat].combinaison);
|
218
|
-
if (allStats.length === 0)
|
219
|
-
throw new Error("[error.noStat]");
|
220
|
-
const error= [];
|
221
|
-
for (const [stat, value] of Object.entries(onlyCombinaisonStats)) {
|
222
|
-
let formula = value.combinaison as string;
|
223
|
-
for (const [other, data] of Object.entries(allOtherStats)) {
|
224
|
-
const {max, min} = data;
|
225
|
-
const total = template.total || 100;
|
226
|
-
const randomStatValue = generateRandomStat(total, max, min);
|
227
|
-
const regex = new RegExp(other, "gi");
|
228
|
-
formula = formula.replace(regex, randomStatValue.toString());
|
229
|
-
}
|
230
|
-
try {
|
231
|
-
evaluate(formula);
|
232
|
-
} catch (e) {
|
233
|
-
error.push(stat);
|
234
|
-
}
|
235
|
-
}
|
236
|
-
if (error.length > 0)
|
237
|
-
throw new Error(`[error.invalidFormula, common.space] ${error.join(", ")}`);
|
238
|
-
return;
|
239
|
-
}
|
240
|
-
|
241
|
-
/**
|
242
|
-
* Generate a random stat based on the template and the statistical min and max
|
243
|
-
* @param {number|undefined} total
|
244
|
-
* @param {number | undefined} max
|
245
|
-
* @param {number | undefined} min
|
246
|
-
* @returns
|
247
|
-
*/
|
248
|
-
export function generateRandomStat(total: number | undefined = 100, max?: number, min?: number) {
|
249
|
-
let randomStatValue = total + 1;
|
250
|
-
while (randomStatValue >= total) {
|
251
|
-
const random = new Random();
|
252
|
-
if (max && min)
|
253
|
-
randomStatValue = random.integer(min, max);
|
254
|
-
else if (max)
|
255
|
-
randomStatValue = random.integer(0, max);
|
256
|
-
else if (min)
|
257
|
-
randomStatValue = random.integer(min, total);
|
258
|
-
else
|
259
|
-
randomStatValue = random.integer(0, total);
|
260
|
-
}
|
261
|
-
return randomStatValue;
|
262
|
-
}
|
263
|
-
|
package/index.ts
DELETED
package/jest.config.js
DELETED
@@ -1,243 +0,0 @@
|
|
1
|
-
// FILEPATH: /c:/Users/simonettili/Documents/Github/discord-dicelette/src/utils/verify_template.test.ts
|
2
|
-
import * as core from "..";
|
3
|
-
|
4
|
-
|
5
|
-
describe("verify_template", () => {
|
6
|
-
describe("evalCombinaison", () => {
|
7
|
-
it("should evaluate the combination correctly", () => {
|
8
|
-
const combinaison = { stat1: "stat2 + 3" };
|
9
|
-
const stats = { stat2: 2 };
|
10
|
-
const result = core.evalCombinaison(combinaison, stats);
|
11
|
-
expect(result).toEqual({ stat1: 5 });
|
12
|
-
});
|
13
|
-
|
14
|
-
it("should throw an error for invalid formula", () => {
|
15
|
-
const combinaison = { stat1: "stat2 + " };
|
16
|
-
const stats = { stat2: 2 };
|
17
|
-
expect(() => core.evalCombinaison(combinaison, stats)).toThrow();
|
18
|
-
});
|
19
|
-
});
|
20
|
-
|
21
|
-
describe("verifyRandomGenerator", () => {
|
22
|
-
// Add more tests for different scenarios
|
23
|
-
it("should verify the random generator correctly", () => {
|
24
|
-
const total = 100;
|
25
|
-
const max = 50;
|
26
|
-
const min = 10;
|
27
|
-
const result = core.generateRandomStat(total, max, min);
|
28
|
-
expect(result).toBeGreaterThanOrEqual(min);
|
29
|
-
expect(result).toBeLessThanOrEqual(max);
|
30
|
-
expect(result).toBeLessThanOrEqual(total);
|
31
|
-
});
|
32
|
-
|
33
|
-
it ("should verify with no max", () => {
|
34
|
-
const total = 100;
|
35
|
-
const min = 1;
|
36
|
-
const result = core.generateRandomStat(total, undefined, min);
|
37
|
-
expect(result).toBeGreaterThanOrEqual(min);
|
38
|
-
expect(result).toBeLessThanOrEqual(total);
|
39
|
-
});
|
40
|
-
|
41
|
-
it ("should verify with no min", () => {
|
42
|
-
const total = 100;
|
43
|
-
const max = 99;
|
44
|
-
const result = core.generateRandomStat(total, max, undefined);
|
45
|
-
expect(result).toBeGreaterThanOrEqual(0);
|
46
|
-
expect(result).toBeLessThanOrEqual(max);
|
47
|
-
});
|
48
|
-
|
49
|
-
it ("should verify with no min and max", () => {
|
50
|
-
const total = 100;
|
51
|
-
const result = core.generateRandomStat(total, undefined, undefined);
|
52
|
-
expect(result).toBeGreaterThanOrEqual(0);
|
53
|
-
expect(result).toBeLessThanOrEqual(total);
|
54
|
-
});
|
55
|
-
|
56
|
-
it ("should verify with no total", () => {
|
57
|
-
const max = 99;
|
58
|
-
const min = 1;
|
59
|
-
const result = core.generateRandomStat(undefined, max, min);
|
60
|
-
expect(result).toBeGreaterThanOrEqual(min);
|
61
|
-
expect(result).toBeLessThanOrEqual(max);
|
62
|
-
});
|
63
|
-
|
64
|
-
it ("should verify with no total, min and max", () => {
|
65
|
-
const result = core.generateRandomStat(undefined, undefined, undefined);
|
66
|
-
expect(result).toBeGreaterThanOrEqual(0);
|
67
|
-
expect(result).toBeLessThanOrEqual(100);
|
68
|
-
});
|
69
|
-
});
|
70
|
-
|
71
|
-
describe("verifyTemplateValue", () => {
|
72
|
-
// Add more tests for different scenarios
|
73
|
-
it("should verify the template correctly", () => {
|
74
|
-
const template = {
|
75
|
-
statistics: { stat1: { max: 10, min: 1 } },
|
76
|
-
diceType: "1d20+{{ceil(($-10)/2)}}>20",
|
77
|
-
damage: {
|
78
|
-
"piercing": "1d6+2",
|
79
|
-
}
|
80
|
-
};
|
81
|
-
const result = core.verifyTemplateValue(template);
|
82
|
-
expect(result).toEqual(template);
|
83
|
-
});
|
84
|
-
|
85
|
-
it("testing no statistic, only damage", () => {
|
86
|
-
const template = {
|
87
|
-
diceType: "d6",
|
88
|
-
damage: {
|
89
|
-
"piercing": "1d6+2>20",
|
90
|
-
}
|
91
|
-
};
|
92
|
-
const result = core.verifyTemplateValue(template);
|
93
|
-
expect(result).toEqual(template);
|
94
|
-
});
|
95
|
-
|
96
|
-
it("should throw an error for invalid dice type", () => {
|
97
|
-
const template = {
|
98
|
-
statistics: { stat1: { max: 10, min: 1, combinaison: "stat2 + 3" } },
|
99
|
-
diceType: "invalid",
|
100
|
-
};
|
101
|
-
expect(() => core.verifyTemplateValue(template)).toThrow();
|
102
|
-
});
|
103
|
-
});
|
104
|
-
|
105
|
-
describe("combinaison", () => {
|
106
|
-
// Add more tests for different scenarios
|
107
|
-
it("should throw an error because they are no stat2", () => {
|
108
|
-
const template: core.StatisticalTemplate = {
|
109
|
-
statistics: { stat1: { max: 10, min: 1, combinaison: "stat2 + 3" } },
|
110
|
-
diceType: "d6",
|
111
|
-
};
|
112
|
-
expect(() => core.testCombinaison(template)).toThrow();
|
113
|
-
});
|
114
|
-
it("validate formula for dice", () => {
|
115
|
-
const template: core.StatisticalTemplate = {
|
116
|
-
statistics: { stat1: { max: 10, min: 1, combinaison: "stat2 + 3" } },
|
117
|
-
diceType: "d6+{{$}}>20",
|
118
|
-
};
|
119
|
-
expect(() => core.testCombinaison(template)).toThrow();
|
120
|
-
});
|
121
|
-
it("validate formula for dice", () => {
|
122
|
-
const template: core.StatisticalTemplate = {
|
123
|
-
statistics: { stat1: { max: 10, min: 1, combinaison: "stat2 + 3" } },
|
124
|
-
diceType: "d6+5>{{$}}",
|
125
|
-
};
|
126
|
-
expect(() => core.testCombinaison(template)).toThrow();
|
127
|
-
});
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
it("create combinaison dice formula for skill dice with statistic", () => {
|
133
|
-
const testTemplate: core.StatisticalTemplate = {
|
134
|
-
statistics: { stat1: { max: 10, min: 1 } },
|
135
|
-
diceType: "1d20",
|
136
|
-
damage: {
|
137
|
-
"piercing": "1d6 + stat1>stat1",
|
138
|
-
}
|
139
|
-
};
|
140
|
-
const expectedFormula = core.diceRandomParse("1d20 + {{ceil((stat1-10)/2)}}>stat1", testTemplate);
|
141
|
-
expect(expectedFormula).toEqual(expectedFormula);
|
142
|
-
});
|
143
|
-
it("Test a roll with a combinaison on the dice", () => {
|
144
|
-
const template: core.StatisticalTemplate = {
|
145
|
-
statistics: { stat1: { max: 10, min: 1, combinaison: "stat2 + 3" } },
|
146
|
-
diceType: "1d20",
|
147
|
-
damage: {
|
148
|
-
"piercing": "1d20stat1*2>stat1",
|
149
|
-
}
|
150
|
-
};
|
151
|
-
expect(() => core.testDamageRoll(template)).not.toThrow();
|
152
|
-
});
|
153
|
-
it("Test a roll with a combinaison on the dice and accents", () => {
|
154
|
-
const template: core.StatisticalTemplate = {
|
155
|
-
statistics: { éducation: { max: 10, min: 1 } },
|
156
|
-
diceType: "1d20",
|
157
|
-
damage: {
|
158
|
-
"piercing": "1déducation>20",
|
159
|
-
}
|
160
|
-
};
|
161
|
-
expect(() => core.testDamageRoll(template)).not.toThrow();
|
162
|
-
});
|
163
|
-
});
|
164
|
-
describe("roll_string_creation", () => {
|
165
|
-
it("creating roll dice with formula", () => {
|
166
|
-
const dice = "1d20+$>20";
|
167
|
-
const userStat = 10;
|
168
|
-
const calculation = core.replaceFormulaInDice(dice.replaceAll("$", userStat.toString()));
|
169
|
-
const formula = `${calculation} coucou`;
|
170
|
-
const expectedFormula = "1d20+10>20 coucou";
|
171
|
-
expect(formula).toEqual(expectedFormula);
|
172
|
-
});
|
173
|
-
it("creating roll dice with success formula", () => {
|
174
|
-
const dice = "1d20+5>{{$*2}}";
|
175
|
-
const userStat = 10;
|
176
|
-
const calculation = core.replaceFormulaInDice(dice.replaceAll("$", userStat.toString()));
|
177
|
-
const formula = `${calculation} coucou`;
|
178
|
-
const expectedFormula = "1d20+5>20 coucou";
|
179
|
-
expect(formula).toEqual(expectedFormula);
|
180
|
-
});
|
181
|
-
it("creating roll dice with complicated formula", () => {
|
182
|
-
const dice = "1d20+{{ceil((10-$)/2)}}>20";
|
183
|
-
const userStat = 5;
|
184
|
-
const calculation = core.replaceFormulaInDice(dice.replaceAll("$", userStat.toString()));
|
185
|
-
const formula = `${calculation} coucou`;
|
186
|
-
const expectedFormula = "1d20+3>20 coucou";
|
187
|
-
expect(formula).toEqual(expectedFormula);
|
188
|
-
});
|
189
|
-
it("creating roll dice with negative formula", () => {
|
190
|
-
const dice = "1d20+{{ceil(($-10)/2)}}>20";
|
191
|
-
const userStat = 5;
|
192
|
-
const calculation = core.replaceFormulaInDice(dice.replaceAll("$", userStat.toString()));
|
193
|
-
const expectedFormula = "1d20-2>20";
|
194
|
-
expect(calculation).toEqual(expectedFormula);
|
195
|
-
});
|
196
|
-
});
|
197
|
-
describe("skill_dice_creation", () => {
|
198
|
-
it("creating roll dice with face formula", () => {
|
199
|
-
let dice = "1dstat1>20";
|
200
|
-
const userStat = {
|
201
|
-
stat1: 5,
|
202
|
-
stat2: 10
|
203
|
-
};
|
204
|
-
dice = core.generateStatsDice(dice, userStat);
|
205
|
-
const formula = `${dice} cc`;
|
206
|
-
const expectedFormula = "1d5>20 cc";
|
207
|
-
expect(formula).toEqual(expectedFormula);
|
208
|
-
});
|
209
|
-
it("creating complicated roll dice with face formula", () => {
|
210
|
-
let dice = "1d20+{{ceil((stat1-10)/2)}}>20";
|
211
|
-
const userStat = {
|
212
|
-
stat1: 10,
|
213
|
-
stat2: 10
|
214
|
-
};
|
215
|
-
dice = core.generateStatsDice(dice, userStat);
|
216
|
-
const formula = `${dice} cc`;
|
217
|
-
const expectedFormula = "1d20+0>20 cc";
|
218
|
-
expect(formula).toEqual(expectedFormula);
|
219
|
-
});
|
220
|
-
it("create a simple dice adding bonus superior to stats", () => {
|
221
|
-
let dice = "1d20+stat1>stat1";
|
222
|
-
const userStat = {
|
223
|
-
stat1: 5,
|
224
|
-
stat2: 10
|
225
|
-
};
|
226
|
-
dice = core.generateStatsDice(dice, userStat);
|
227
|
-
const formula = `${dice} cc`;
|
228
|
-
const expectedFormula = "1d20+5>5 cc";
|
229
|
-
expect(formula).toEqual(expectedFormula);
|
230
|
-
});
|
231
|
-
it("creating complicated roll dice with comparator as formula", () => {
|
232
|
-
let dice = "1d20+stat1>{{ceil(stat1/2)}}";
|
233
|
-
const userStat = {
|
234
|
-
stat1: 5,
|
235
|
-
stat2: 10
|
236
|
-
};
|
237
|
-
dice = core.generateStatsDice(dice, userStat);
|
238
|
-
const formula = `${dice} cc`;
|
239
|
-
const expectedFormula = "1d20+5>3 cc";
|
240
|
-
expect(formula).toEqual(expectedFormula);
|
241
|
-
});
|
242
|
-
});
|
243
|
-
});
|
package/tree.txt
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
|
2
|
-
Source: E:\Documents\Github\dicelette\core
|
3
|
-
|
4
|
-
Mode Length Hierarchy
|
5
|
-
---- ------ ---------
|
6
|
-
d---- 121,97 KB core
|
7
|
-
-a--- 216,00 B ├── .eslintrc.js
|
8
|
-
-a--- 2,13 KB ├── .gitignore
|
9
|
-
-a--- 125,00 B ├── index.ts
|
10
|
-
-a--- 142,00 B ├── jest.config.js
|
11
|
-
-a--- 34,98 KB ├── LICENSE
|
12
|
-
-a--- 829,00 B ├── package.json
|
13
|
-
-a--- 76,68 KB ├── pnpm-lock.yaml
|
14
|
-
-a--- 6,14 KB ├── README.md
|
15
|
-
-a--- 0,00 B ├── tree.txt
|
16
|
-
-a--- 783,00 B ├── tsconfig.json
|
17
|
-
d---- 8,04 KB ├── tests
|
18
|
-
-a--- 8,04 KB │ └── verify_template.test.ts
|
19
|
-
d---- 14,39 KB ├── core
|
20
|
-
-a--- 3,08 KB │ ├── dice.ts
|
21
|
-
-a--- 2,01 KB │ ├── utils.ts
|
22
|
-
-a--- 9,30 KB │ └── verify_template.ts
|
23
|
-
d---- 1,48 KB └── @types
|
24
|
-
-a--- 1,48 KB └── index.d.ts
|
25
|
-
|
package/tsconfig.json
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"compilerOptions": {
|
3
|
-
"target": "ESNext",
|
4
|
-
"module": "ESNext",
|
5
|
-
"rootDir": "./",
|
6
|
-
"strict": true,
|
7
|
-
"moduleResolution": "node",
|
8
|
-
"importHelpers": true,
|
9
|
-
"experimentalDecorators": true,
|
10
|
-
"esModuleInterop": true,
|
11
|
-
"skipLibCheck": true,
|
12
|
-
"allowSyntheticDefaultImports": true,
|
13
|
-
"allowJs": true,
|
14
|
-
"checkJs": false,
|
15
|
-
"resolveJsonModule": true,
|
16
|
-
"forceConsistentCasingInFileNames": true,
|
17
|
-
"removeComments": true,
|
18
|
-
"typeRoots": [
|
19
|
-
"node_modules/@types",
|
20
|
-
"./core/@types"
|
21
|
-
],
|
22
|
-
"sourceMap": false,
|
23
|
-
"baseUrl": "./"
|
24
|
-
},
|
25
|
-
"include": [
|
26
|
-
"./**/*.ts"
|
27
|
-
],
|
28
|
-
"exclude": [
|
29
|
-
"node_modules",
|
30
|
-
"dist"
|
31
|
-
],
|
32
|
-
}
|