@dicelette/core 1.0.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/.eslintrc.js +14 -0
- package/LICENSE +674 -0
- package/README.md +2 -0
- package/core/dice.ts +104 -0
- package/core/index.d.ts +71 -0
- package/core/utils.ts +65 -0
- package/core/verify_template.ts +263 -0
- package/jest.config.js +6 -0
- package/package.json +33 -0
- package/tests/verify_template.test.ts +244 -0
- package/tsconfig.json +32 -0
@@ -0,0 +1,244 @@
|
|
1
|
+
// FILEPATH: /c:/Users/simonettili/Documents/Github/discord-dicelette/src/utils/verify_template.test.ts
|
2
|
+
import { StatisticalTemplate } from "../core/interface";
|
3
|
+
import { generateStatsDice, replaceFormulaInDice } from "../core/utils";
|
4
|
+
import { diceRandomParse,evalCombinaison, generateRandomStat,testCombinaison, testDamageRoll, verifyTemplateValue } from "../core/verify_template";
|
5
|
+
|
6
|
+
describe("verify_template", () => {
|
7
|
+
describe("evalCombinaison", () => {
|
8
|
+
it("should evaluate the combination correctly", () => {
|
9
|
+
const combinaison = { stat1: "stat2 + 3" };
|
10
|
+
const stats = { stat2: 2 };
|
11
|
+
const result = evalCombinaison(combinaison, stats);
|
12
|
+
expect(result).toEqual({ stat1: 5 });
|
13
|
+
});
|
14
|
+
|
15
|
+
it("should throw an error for invalid formula", () => {
|
16
|
+
const combinaison = { stat1: "stat2 + " };
|
17
|
+
const stats = { stat2: 2 };
|
18
|
+
expect(() => evalCombinaison(combinaison, stats)).toThrow();
|
19
|
+
});
|
20
|
+
});
|
21
|
+
|
22
|
+
describe("verifyRandomGenerator", () => {
|
23
|
+
// Add more tests for different scenarios
|
24
|
+
it("should verify the random generator correctly", () => {
|
25
|
+
const total = 100;
|
26
|
+
const max = 50;
|
27
|
+
const min = 10;
|
28
|
+
const result = generateRandomStat(total, max, min);
|
29
|
+
expect(result).toBeGreaterThanOrEqual(min);
|
30
|
+
expect(result).toBeLessThanOrEqual(max);
|
31
|
+
expect(result).toBeLessThanOrEqual(total);
|
32
|
+
});
|
33
|
+
|
34
|
+
it ("should verify with no max", () => {
|
35
|
+
const total = 100;
|
36
|
+
const min = 1;
|
37
|
+
const result = generateRandomStat(total, undefined, min);
|
38
|
+
expect(result).toBeGreaterThanOrEqual(min);
|
39
|
+
expect(result).toBeLessThanOrEqual(total);
|
40
|
+
});
|
41
|
+
|
42
|
+
it ("should verify with no min", () => {
|
43
|
+
const total = 100;
|
44
|
+
const max = 99;
|
45
|
+
const result = generateRandomStat(total, max, undefined);
|
46
|
+
expect(result).toBeGreaterThanOrEqual(0);
|
47
|
+
expect(result).toBeLessThanOrEqual(max);
|
48
|
+
});
|
49
|
+
|
50
|
+
it ("should verify with no min and max", () => {
|
51
|
+
const total = 100;
|
52
|
+
const result = generateRandomStat(total, undefined, undefined);
|
53
|
+
expect(result).toBeGreaterThanOrEqual(0);
|
54
|
+
expect(result).toBeLessThanOrEqual(total);
|
55
|
+
});
|
56
|
+
|
57
|
+
it ("should verify with no total", () => {
|
58
|
+
const max = 99;
|
59
|
+
const min = 1;
|
60
|
+
const result = generateRandomStat(undefined, max, min);
|
61
|
+
expect(result).toBeGreaterThanOrEqual(min);
|
62
|
+
expect(result).toBeLessThanOrEqual(max);
|
63
|
+
});
|
64
|
+
|
65
|
+
it ("should verify with no total, min and max", () => {
|
66
|
+
const result = generateRandomStat(undefined, undefined, undefined);
|
67
|
+
expect(result).toBeGreaterThanOrEqual(0);
|
68
|
+
expect(result).toBeLessThanOrEqual(100);
|
69
|
+
});
|
70
|
+
});
|
71
|
+
|
72
|
+
describe("verifyTemplateValue", () => {
|
73
|
+
// Add more tests for different scenarios
|
74
|
+
it("should verify the template correctly", () => {
|
75
|
+
const template = {
|
76
|
+
statistics: { stat1: { max: 10, min: 1 } },
|
77
|
+
diceType: "1d20+{{ceil(($-10)/2)}}>20",
|
78
|
+
damage: {
|
79
|
+
"piercing": "1d6+2",
|
80
|
+
}
|
81
|
+
};
|
82
|
+
const result = verifyTemplateValue(template);
|
83
|
+
expect(result).toEqual(template);
|
84
|
+
});
|
85
|
+
|
86
|
+
it("testing no statistic, only damage", () => {
|
87
|
+
const template = {
|
88
|
+
diceType: "d6",
|
89
|
+
damage: {
|
90
|
+
"piercing": "1d6+2>20",
|
91
|
+
}
|
92
|
+
};
|
93
|
+
const result = verifyTemplateValue(template);
|
94
|
+
expect(result).toEqual(template);
|
95
|
+
});
|
96
|
+
|
97
|
+
it("should throw an error for invalid dice type", () => {
|
98
|
+
const template = {
|
99
|
+
statistics: { stat1: { max: 10, min: 1, combinaison: "stat2 + 3" } },
|
100
|
+
diceType: "invalid",
|
101
|
+
};
|
102
|
+
expect(() => verifyTemplateValue(template)).toThrow();
|
103
|
+
});
|
104
|
+
});
|
105
|
+
|
106
|
+
describe("combinaison", () => {
|
107
|
+
// Add more tests for different scenarios
|
108
|
+
it("should throw an error because they are no stat2", () => {
|
109
|
+
const template: StatisticalTemplate = {
|
110
|
+
statistics: { stat1: { max: 10, min: 1, combinaison: "stat2 + 3" } },
|
111
|
+
diceType: "d6",
|
112
|
+
};
|
113
|
+
expect(() => testCombinaison(template)).toThrow();
|
114
|
+
});
|
115
|
+
it("validate formula for dice", () => {
|
116
|
+
const template: StatisticalTemplate = {
|
117
|
+
statistics: { stat1: { max: 10, min: 1, combinaison: "stat2 + 3" } },
|
118
|
+
diceType: "d6+{{$}}>20",
|
119
|
+
};
|
120
|
+
expect(() => testCombinaison(template)).toThrow();
|
121
|
+
});
|
122
|
+
it("validate formula for dice", () => {
|
123
|
+
const template: StatisticalTemplate = {
|
124
|
+
statistics: { stat1: { max: 10, min: 1, combinaison: "stat2 + 3" } },
|
125
|
+
diceType: "d6+5>{{$}}",
|
126
|
+
};
|
127
|
+
expect(() => testCombinaison(template)).toThrow();
|
128
|
+
});
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
it("create combinaison dice formula for skill dice with statistic", () => {
|
134
|
+
const testTemplate: StatisticalTemplate = {
|
135
|
+
statistics: { stat1: { max: 10, min: 1 } },
|
136
|
+
diceType: "1d20",
|
137
|
+
damage: {
|
138
|
+
"piercing": "1d6 + stat1>stat1",
|
139
|
+
}
|
140
|
+
};
|
141
|
+
const expectedFormula = diceRandomParse("1d20 + {{ceil((stat1-10)/2)}}>stat1", testTemplate);
|
142
|
+
expect(expectedFormula).toEqual(expectedFormula);
|
143
|
+
});
|
144
|
+
it("Test a roll with a combinaison on the dice", () => {
|
145
|
+
const template: StatisticalTemplate = {
|
146
|
+
statistics: { stat1: { max: 10, min: 1, combinaison: "stat2 + 3" } },
|
147
|
+
diceType: "1d20",
|
148
|
+
damage: {
|
149
|
+
"piercing": "1d20stat1*2>stat1",
|
150
|
+
}
|
151
|
+
};
|
152
|
+
expect(() => testDamageRoll(template)).not.toThrow();
|
153
|
+
});
|
154
|
+
it("Test a roll with a combinaison on the dice and accents", () => {
|
155
|
+
const template: StatisticalTemplate = {
|
156
|
+
statistics: { éducation: { max: 10, min: 1 } },
|
157
|
+
diceType: "1d20",
|
158
|
+
damage: {
|
159
|
+
"piercing": "1déducation>20",
|
160
|
+
}
|
161
|
+
};
|
162
|
+
expect(() => testDamageRoll(template)).not.toThrow();
|
163
|
+
});
|
164
|
+
});
|
165
|
+
describe("roll_string_creation", () => {
|
166
|
+
it("creating roll dice with formula", () => {
|
167
|
+
const dice = "1d20+$>20";
|
168
|
+
const userStat = 10;
|
169
|
+
const calculation = replaceFormulaInDice(dice.replaceAll("$", userStat.toString()));
|
170
|
+
const formula = `${calculation} coucou`;
|
171
|
+
const expectedFormula = "1d20+10>20 coucou";
|
172
|
+
expect(formula).toEqual(expectedFormula);
|
173
|
+
});
|
174
|
+
it("creating roll dice with success formula", () => {
|
175
|
+
const dice = "1d20+5>{{$*2}}";
|
176
|
+
const userStat = 10;
|
177
|
+
const calculation = replaceFormulaInDice(dice.replaceAll("$", userStat.toString()));
|
178
|
+
const formula = `${calculation} coucou`;
|
179
|
+
const expectedFormula = "1d20+5>20 coucou";
|
180
|
+
expect(formula).toEqual(expectedFormula);
|
181
|
+
});
|
182
|
+
it("creating roll dice with complicated formula", () => {
|
183
|
+
const dice = "1d20+{{ceil((10-$)/2)}}>20";
|
184
|
+
const userStat = 5;
|
185
|
+
const calculation = replaceFormulaInDice(dice.replaceAll("$", userStat.toString()));
|
186
|
+
const formula = `${calculation} coucou`;
|
187
|
+
const expectedFormula = "1d20+3>20 coucou";
|
188
|
+
expect(formula).toEqual(expectedFormula);
|
189
|
+
});
|
190
|
+
it("creating roll dice with negative formula", () => {
|
191
|
+
const dice = "1d20+{{ceil(($-10)/2)}}>20";
|
192
|
+
const userStat = 5;
|
193
|
+
const calculation = replaceFormulaInDice(dice.replaceAll("$", userStat.toString()));
|
194
|
+
const expectedFormula = "1d20-2>20";
|
195
|
+
expect(calculation).toEqual(expectedFormula);
|
196
|
+
});
|
197
|
+
});
|
198
|
+
describe("skill_dice_creation", () => {
|
199
|
+
it("creating roll dice with face formula", () => {
|
200
|
+
let dice = "1dstat1>20";
|
201
|
+
const userStat = {
|
202
|
+
stat1: 5,
|
203
|
+
stat2: 10
|
204
|
+
};
|
205
|
+
dice = generateStatsDice(dice, userStat);
|
206
|
+
const formula = `${dice} cc`;
|
207
|
+
const expectedFormula = "1d5>20 cc";
|
208
|
+
expect(formula).toEqual(expectedFormula);
|
209
|
+
});
|
210
|
+
it("creating complicated roll dice with face formula", () => {
|
211
|
+
let dice = "1d20+{{ceil((stat1-10)/2)}}>20";
|
212
|
+
const userStat = {
|
213
|
+
stat1: 10,
|
214
|
+
stat2: 10
|
215
|
+
};
|
216
|
+
dice = generateStatsDice(dice, userStat);
|
217
|
+
const formula = `${dice} cc`;
|
218
|
+
const expectedFormula = "1d20+0>20 cc";
|
219
|
+
expect(formula).toEqual(expectedFormula);
|
220
|
+
});
|
221
|
+
it("create a simple dice adding bonus superior to stats", () => {
|
222
|
+
let dice = "1d20+stat1>stat1";
|
223
|
+
const userStat = {
|
224
|
+
stat1: 5,
|
225
|
+
stat2: 10
|
226
|
+
};
|
227
|
+
dice = generateStatsDice(dice, userStat);
|
228
|
+
const formula = `${dice} cc`;
|
229
|
+
const expectedFormula = "1d20+5>5 cc";
|
230
|
+
expect(formula).toEqual(expectedFormula);
|
231
|
+
});
|
232
|
+
it("creating complicated roll dice with comparator as formula", () => {
|
233
|
+
let dice = "1d20+stat1>{{ceil(stat1/2)}}";
|
234
|
+
const userStat = {
|
235
|
+
stat1: 5,
|
236
|
+
stat2: 10
|
237
|
+
};
|
238
|
+
dice = generateStatsDice(dice, userStat);
|
239
|
+
const formula = `${dice} cc`;
|
240
|
+
const expectedFormula = "1d20+5>3 cc";
|
241
|
+
expect(formula).toEqual(expectedFormula);
|
242
|
+
});
|
243
|
+
});
|
244
|
+
});
|
package/tsconfig.json
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"target": "ESNext",
|
4
|
+
"module": "commonjs",
|
5
|
+
"rootDir": "./",
|
6
|
+
"outDir": "./dist/",
|
7
|
+
"strict": true,
|
8
|
+
"moduleResolution": "node",
|
9
|
+
"importHelpers": true,
|
10
|
+
"experimentalDecorators": true,
|
11
|
+
"esModuleInterop": true,
|
12
|
+
"skipLibCheck": true,
|
13
|
+
"allowSyntheticDefaultImports": true,
|
14
|
+
"allowJs": true,
|
15
|
+
"checkJs": false,
|
16
|
+
"resolveJsonModule": true,
|
17
|
+
"forceConsistentCasingInFileNames": true,
|
18
|
+
"removeComments": true,
|
19
|
+
"typeRoots": [
|
20
|
+
"node_modules/@types"
|
21
|
+
],
|
22
|
+
"sourceMap": false,
|
23
|
+
"baseUrl": "./"
|
24
|
+
},
|
25
|
+
"include": [
|
26
|
+
"./**/*.ts"
|
27
|
+
],
|
28
|
+
"exclude": [
|
29
|
+
"node_modules",
|
30
|
+
"dist"
|
31
|
+
],
|
32
|
+
}
|