tiny-essentials 1.9.0 → 1.9.2
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/TinyBasicsEs.js +155 -98
- package/dist/TinyBasicsEs.min.js +1 -1
- package/dist/TinyEssentials.js +307 -126
- package/dist/TinyEssentials.min.js +1 -1
- package/dist/TinyLevelUp.js +125 -15
- package/dist/TinyLevelUp.min.js +1 -1
- package/dist/TinyPromiseQueue.js +27 -13
- package/dist/TinyPromiseQueue.min.js +1 -1
- package/dist/legacy/firebase/mySQL.cjs +1 -1
- package/dist/legacy/libs/userLevel.cjs +125 -15
- package/dist/legacy/libs/userLevel.d.mts +86 -59
- package/dist/legacy/libs/userLevel.mjs +123 -15
- package/dist/v1/basics/objFilter.cjs +155 -98
- package/dist/v1/basics/objFilter.d.mts +16 -26
- package/dist/v1/basics/objFilter.mjs +146 -77
- package/dist/v1/libs/TinyPromiseQueue.cjs +27 -13
- package/dist/v1/libs/TinyPromiseQueue.d.mts +38 -0
- package/dist/v1/libs/TinyPromiseQueue.mjs +26 -13
- package/docs/basics/objFilter.md +7 -0
- package/docs/libs/TinyLevelUp.md +208 -68
- package/package.json +2 -1
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Represents a user object used.
|
|
5
|
+
*
|
|
6
|
+
* @typedef {Object} UserEditor
|
|
7
|
+
* @property {number} exp - Current experience points of the user.
|
|
8
|
+
* @property {number} level - Current level of the user.
|
|
9
|
+
* @property {number} totalExp - Total accumulated experience.
|
|
10
|
+
*/
|
|
11
|
+
|
|
3
12
|
/**
|
|
4
13
|
* Class to manage user level-up logic based on experience points.
|
|
5
14
|
*/
|
|
@@ -10,26 +19,95 @@ class TinyLevelUp {
|
|
|
10
19
|
* @param {number} expLevel - Base experience needed to level up (per level).
|
|
11
20
|
*/
|
|
12
21
|
constructor(giveExp, expLevel) {
|
|
22
|
+
if (typeof giveExp !== 'number' || Number.isNaN(giveExp))
|
|
23
|
+
throw new Error('giveExp must be a valid number');
|
|
24
|
+
if (typeof expLevel !== 'number' || Number.isNaN(expLevel))
|
|
25
|
+
throw new Error('expLevel must be a valid number');
|
|
13
26
|
this.giveExp = giveExp;
|
|
14
27
|
this.expLevel = expLevel;
|
|
15
28
|
}
|
|
16
29
|
|
|
17
30
|
/**
|
|
18
|
-
*
|
|
31
|
+
* Creates a new user object starting at level 0 with 0 experience.
|
|
32
|
+
* @returns {UserEditor} A fresh user object.
|
|
33
|
+
*/
|
|
34
|
+
createUser() {
|
|
35
|
+
return {
|
|
36
|
+
exp: 0,
|
|
37
|
+
level: 1,
|
|
38
|
+
totalExp: 0,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Validates if the given user object has valid numeric properties.
|
|
44
|
+
* Throws an error if any property is invalid.
|
|
45
|
+
*
|
|
46
|
+
* @param {UserEditor} user - The user object to validate.
|
|
47
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
48
|
+
*/
|
|
49
|
+
validateUser(user) {
|
|
50
|
+
if (typeof user.exp !== 'number' || Number.isNaN(user.exp))
|
|
51
|
+
throw new Error('exp must be a valid number');
|
|
52
|
+
if (typeof user.level !== 'number' || Number.isNaN(user.level))
|
|
53
|
+
throw new Error('level must be a valid number');
|
|
54
|
+
if (user.level < 1) throw new Error('level must be at least 1');
|
|
55
|
+
if (typeof user.totalExp !== 'number' || Number.isNaN(user.totalExp))
|
|
56
|
+
throw new Error('totalExp must be a valid number');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Checks if the given user object is valid by verifying its numeric properties.
|
|
61
|
+
*
|
|
62
|
+
* @param {UserEditor} user - The user object to check.
|
|
63
|
+
* @returns {boolean} `true` if all properties (exp, level, totalExp) are valid numbers; otherwise `false`.
|
|
64
|
+
*/
|
|
65
|
+
isValidUser(user) {
|
|
66
|
+
if (typeof user.exp !== 'number' || Number.isNaN(user.exp)) return false;
|
|
67
|
+
if (typeof user.level !== 'number' || Number.isNaN(user.level)) return false;
|
|
68
|
+
if (user.level < 1) return false;
|
|
69
|
+
if (typeof user.totalExp !== 'number' || Number.isNaN(user.totalExp)) return false;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Returns the base experience value used for random experience generation.
|
|
75
|
+
* Throws an error if the internal giveExp value is not a valid number.
|
|
76
|
+
*
|
|
77
|
+
* @returns {number} The base experience value.
|
|
78
|
+
* @throws {Error} If giveExp is not a valid number.
|
|
19
79
|
*/
|
|
80
|
+
getGiveExpBase() {
|
|
81
|
+
if (typeof this.giveExp !== 'number' || Number.isNaN(this.giveExp))
|
|
82
|
+
throw new Error('giveExp must be a valid number');
|
|
83
|
+
return this.giveExp;
|
|
84
|
+
}
|
|
20
85
|
|
|
21
86
|
/**
|
|
22
|
-
*
|
|
87
|
+
* Returns the base experience required to level up.
|
|
88
|
+
* Throws an error if the internal expLevel value is not a valid number.
|
|
89
|
+
*
|
|
90
|
+
* @returns {number} The base experience needed per level.
|
|
91
|
+
* @throws {Error} If expLevel is not a valid number.
|
|
23
92
|
*/
|
|
93
|
+
getExpLevelBase() {
|
|
94
|
+
if (typeof this.expLevel !== 'number' || Number.isNaN(this.expLevel))
|
|
95
|
+
throw new Error('expLevel must be a valid number');
|
|
96
|
+
return this.expLevel;
|
|
97
|
+
}
|
|
24
98
|
|
|
25
99
|
/**
|
|
26
100
|
* Validates and adjusts the user's level based on their current experience.
|
|
27
101
|
* @param {UserEditor} user - The user object containing experience and level properties.
|
|
28
|
-
* @returns {
|
|
102
|
+
* @returns {UserEditor} The updated user object.
|
|
103
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
29
104
|
*/
|
|
30
105
|
expValidator(user) {
|
|
106
|
+
const expLevel = this.getExpLevelBase();
|
|
107
|
+
this.validateUser(user);
|
|
108
|
+
|
|
31
109
|
let extraValue = 0;
|
|
32
|
-
const nextLevelExp =
|
|
110
|
+
const nextLevelExp = expLevel * user.level;
|
|
33
111
|
|
|
34
112
|
// Level Up
|
|
35
113
|
if (user.exp >= nextLevelExp) {
|
|
@@ -44,7 +122,7 @@ class TinyLevelUp {
|
|
|
44
122
|
if (user.exp < 1 && user.level > 1) {
|
|
45
123
|
user.level--;
|
|
46
124
|
extraValue = Math.abs(user.exp);
|
|
47
|
-
user.exp =
|
|
125
|
+
user.exp = expLevel * user.level;
|
|
48
126
|
|
|
49
127
|
if (extraValue > 0) return this.remove(user, extraValue, 'extra');
|
|
50
128
|
}
|
|
@@ -54,12 +132,14 @@ class TinyLevelUp {
|
|
|
54
132
|
|
|
55
133
|
/**
|
|
56
134
|
* Calculates the total experience based on the user's level.
|
|
57
|
-
* @param {
|
|
135
|
+
* @param {UserEditor} user - The user object containing experience and level properties.
|
|
58
136
|
* @returns {number} The total experience of the user.
|
|
137
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
59
138
|
*/
|
|
60
139
|
getTotalExp(user) {
|
|
140
|
+
this.validateUser(user);
|
|
61
141
|
let totalExp = 0;
|
|
62
|
-
for (let p = 1; p <= user.level; p++) totalExp += this.
|
|
142
|
+
for (let p = 1; p <= user.level; p++) totalExp += this.getExpLevelBase() * p;
|
|
63
143
|
totalExp += user.exp;
|
|
64
144
|
return totalExp;
|
|
65
145
|
}
|
|
@@ -70,34 +150,52 @@ class TinyLevelUp {
|
|
|
70
150
|
* @returns {number} The generated experience points.
|
|
71
151
|
*/
|
|
72
152
|
expGenerator(multi = 1) {
|
|
73
|
-
|
|
153
|
+
if (typeof multi !== 'number' || Number.isNaN(multi))
|
|
154
|
+
throw new Error('multi must be a valid number');
|
|
155
|
+
return Math.floor(Math.random() * this.getGiveExpBase()) * multi;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Calculates how much experience is missing to next level.
|
|
160
|
+
* @param {UserEditor} user
|
|
161
|
+
* @returns {number}
|
|
162
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
163
|
+
*/
|
|
164
|
+
getMissingExp(user) {
|
|
165
|
+
return this.getProgress(user) - user.exp;
|
|
74
166
|
}
|
|
75
167
|
|
|
76
168
|
/**
|
|
77
169
|
* Gets the experience points required to reach the next level.
|
|
78
|
-
* @param {
|
|
170
|
+
* @param {UserEditor} user - The user object containing the level.
|
|
79
171
|
* @returns {number} The experience required for the next level.
|
|
172
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
80
173
|
*/
|
|
81
174
|
progress(user) {
|
|
82
|
-
return this.
|
|
175
|
+
return this.getProgress(user);
|
|
83
176
|
}
|
|
84
177
|
|
|
85
178
|
/**
|
|
86
179
|
* Gets the experience points required to reach the next level.
|
|
87
|
-
* @param {
|
|
180
|
+
* @param {UserEditor} user - The user object containing the level.
|
|
88
181
|
* @returns {number} The experience required for the next level.
|
|
182
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
89
183
|
*/
|
|
90
184
|
getProgress(user) {
|
|
91
|
-
|
|
185
|
+
this.validateUser(user);
|
|
186
|
+
return this.getExpLevelBase() * user.level;
|
|
92
187
|
}
|
|
93
188
|
|
|
94
189
|
/**
|
|
95
190
|
* Sets the experience value for the user, adjusting their level if necessary.
|
|
96
191
|
* @param {UserEditor} user - The user object.
|
|
97
192
|
* @param {number} value - The new experience value to set.
|
|
98
|
-
* @returns {
|
|
193
|
+
* @returns {UserEditor} The updated user object.
|
|
99
194
|
*/
|
|
100
195
|
set(user, value) {
|
|
196
|
+
if (typeof value !== 'number' || Number.isNaN(value))
|
|
197
|
+
throw new Error('value must be a valid number');
|
|
198
|
+
|
|
101
199
|
user.exp = value;
|
|
102
200
|
this.expValidator(user);
|
|
103
201
|
user.totalExp = this.getTotalExp(user);
|
|
@@ -110,9 +208,15 @@ class TinyLevelUp {
|
|
|
110
208
|
* @param {number} [extraExp] - Additional experience to be added.
|
|
111
209
|
* @param {'add' | 'extra'} [type] - Type of addition ('add' or 'extra').
|
|
112
210
|
* @param {number} [multi] - Multiplier for experience generation.
|
|
113
|
-
* @returns {
|
|
211
|
+
* @returns {UserEditor} The updated user object.
|
|
114
212
|
*/
|
|
115
213
|
give(user, extraExp = 0, type = 'add', multi = 1) {
|
|
214
|
+
if (typeof multi !== 'number' || Number.isNaN(multi))
|
|
215
|
+
throw new Error('multi must be a valid number');
|
|
216
|
+
if (typeof extraExp !== 'number' || Number.isNaN(extraExp))
|
|
217
|
+
throw new Error('extraExp must be a valid number');
|
|
218
|
+
if (typeof type !== 'string') throw new Error('type must be a valid string');
|
|
219
|
+
|
|
116
220
|
if (type === 'add') user.exp += this.expGenerator(multi) + extraExp;
|
|
117
221
|
else if (type === 'extra') user.exp += extraExp;
|
|
118
222
|
|
|
@@ -127,9 +231,15 @@ class TinyLevelUp {
|
|
|
127
231
|
* @param {number} [extraExp] - Additional experience to remove.
|
|
128
232
|
* @param {'add' | 'extra'} [type] - Type of removal ('add' or 'extra').
|
|
129
233
|
* @param {number} [multi] - Multiplier for experience generation.
|
|
130
|
-
* @returns {
|
|
234
|
+
* @returns {UserEditor} The updated user object.
|
|
131
235
|
*/
|
|
132
236
|
remove(user, extraExp = 0, type = 'add', multi = 1) {
|
|
237
|
+
if (typeof multi !== 'number' || Number.isNaN(multi))
|
|
238
|
+
throw new Error('multi must be a valid number');
|
|
239
|
+
if (typeof extraExp !== 'number' || Number.isNaN(extraExp))
|
|
240
|
+
throw new Error('extraExp must be a valid number');
|
|
241
|
+
if (typeof type !== 'string') throw new Error('type must be a valid string');
|
|
242
|
+
|
|
133
243
|
if (type === 'add') user.exp -= this.expGenerator(multi) + extraExp;
|
|
134
244
|
else if (type === 'extra') user.exp -= extraExp;
|
|
135
245
|
|
|
@@ -1,4 +1,29 @@
|
|
|
1
1
|
export default TinyLevelUp;
|
|
2
|
+
/**
|
|
3
|
+
* Represents a user object used.
|
|
4
|
+
*/
|
|
5
|
+
export type UserEditor = {
|
|
6
|
+
/**
|
|
7
|
+
* - Current experience points of the user.
|
|
8
|
+
*/
|
|
9
|
+
exp: number;
|
|
10
|
+
/**
|
|
11
|
+
* - Current level of the user.
|
|
12
|
+
*/
|
|
13
|
+
level: number;
|
|
14
|
+
/**
|
|
15
|
+
* - Total accumulated experience.
|
|
16
|
+
*/
|
|
17
|
+
totalExp: number;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Represents a user object used.
|
|
21
|
+
*
|
|
22
|
+
* @typedef {Object} UserEditor
|
|
23
|
+
* @property {number} exp - Current experience points of the user.
|
|
24
|
+
* @property {number} level - Current level of the user.
|
|
25
|
+
* @property {number} totalExp - Total accumulated experience.
|
|
26
|
+
*/
|
|
2
27
|
/**
|
|
3
28
|
* Class to manage user level-up logic based on experience points.
|
|
4
29
|
*/
|
|
@@ -12,104 +37,106 @@ declare class TinyLevelUp {
|
|
|
12
37
|
giveExp: number;
|
|
13
38
|
expLevel: number;
|
|
14
39
|
/**
|
|
15
|
-
*
|
|
40
|
+
* Creates a new user object starting at level 0 with 0 experience.
|
|
41
|
+
* @returns {UserEditor} A fresh user object.
|
|
42
|
+
*/
|
|
43
|
+
createUser(): UserEditor;
|
|
44
|
+
/**
|
|
45
|
+
* Validates if the given user object has valid numeric properties.
|
|
46
|
+
* Throws an error if any property is invalid.
|
|
47
|
+
*
|
|
48
|
+
* @param {UserEditor} user - The user object to validate.
|
|
49
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
16
50
|
*/
|
|
51
|
+
validateUser(user: UserEditor): void;
|
|
17
52
|
/**
|
|
18
|
-
*
|
|
53
|
+
* Checks if the given user object is valid by verifying its numeric properties.
|
|
54
|
+
*
|
|
55
|
+
* @param {UserEditor} user - The user object to check.
|
|
56
|
+
* @returns {boolean} `true` if all properties (exp, level, totalExp) are valid numbers; otherwise `false`.
|
|
19
57
|
*/
|
|
58
|
+
isValidUser(user: UserEditor): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Returns the base experience value used for random experience generation.
|
|
61
|
+
* Throws an error if the internal giveExp value is not a valid number.
|
|
62
|
+
*
|
|
63
|
+
* @returns {number} The base experience value.
|
|
64
|
+
* @throws {Error} If giveExp is not a valid number.
|
|
65
|
+
*/
|
|
66
|
+
getGiveExpBase(): number;
|
|
67
|
+
/**
|
|
68
|
+
* Returns the base experience required to level up.
|
|
69
|
+
* Throws an error if the internal expLevel value is not a valid number.
|
|
70
|
+
*
|
|
71
|
+
* @returns {number} The base experience needed per level.
|
|
72
|
+
* @throws {Error} If expLevel is not a valid number.
|
|
73
|
+
*/
|
|
74
|
+
getExpLevelBase(): number;
|
|
20
75
|
/**
|
|
21
76
|
* Validates and adjusts the user's level based on their current experience.
|
|
22
77
|
* @param {UserEditor} user - The user object containing experience and level properties.
|
|
23
|
-
* @returns {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
level: number;
|
|
28
|
-
totalExp: any;
|
|
29
|
-
}): {
|
|
30
|
-
exp: number;
|
|
31
|
-
level: number;
|
|
32
|
-
totalExp: number;
|
|
33
|
-
};
|
|
78
|
+
* @returns {UserEditor} The updated user object.
|
|
79
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
80
|
+
*/
|
|
81
|
+
expValidator(user: UserEditor): UserEditor;
|
|
34
82
|
/**
|
|
35
83
|
* Calculates the total experience based on the user's level.
|
|
36
|
-
* @param {
|
|
84
|
+
* @param {UserEditor} user - The user object containing experience and level properties.
|
|
37
85
|
* @returns {number} The total experience of the user.
|
|
86
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
38
87
|
*/
|
|
39
|
-
getTotalExp(user:
|
|
40
|
-
exp: number;
|
|
41
|
-
level: number;
|
|
42
|
-
}): number;
|
|
88
|
+
getTotalExp(user: UserEditor): number;
|
|
43
89
|
/**
|
|
44
90
|
* Generates random experience points based on the configured multiplier.
|
|
45
91
|
* @param {number} [multi] - A multiplier for the generated experience.
|
|
46
92
|
* @returns {number} The generated experience points.
|
|
47
93
|
*/
|
|
48
94
|
expGenerator(multi?: number): number;
|
|
95
|
+
/**
|
|
96
|
+
* Calculates how much experience is missing to next level.
|
|
97
|
+
* @param {UserEditor} user
|
|
98
|
+
* @returns {number}
|
|
99
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
100
|
+
*/
|
|
101
|
+
getMissingExp(user: UserEditor): number;
|
|
49
102
|
/**
|
|
50
103
|
* Gets the experience points required to reach the next level.
|
|
51
|
-
* @param {
|
|
104
|
+
* @param {UserEditor} user - The user object containing the level.
|
|
52
105
|
* @returns {number} The experience required for the next level.
|
|
106
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
53
107
|
*/
|
|
54
|
-
progress(user:
|
|
55
|
-
level: number;
|
|
56
|
-
}): number;
|
|
108
|
+
progress(user: UserEditor): number;
|
|
57
109
|
/**
|
|
58
110
|
* Gets the experience points required to reach the next level.
|
|
59
|
-
* @param {
|
|
111
|
+
* @param {UserEditor} user - The user object containing the level.
|
|
60
112
|
* @returns {number} The experience required for the next level.
|
|
113
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
61
114
|
*/
|
|
62
|
-
getProgress(user:
|
|
63
|
-
level: number;
|
|
64
|
-
}): number;
|
|
115
|
+
getProgress(user: UserEditor): number;
|
|
65
116
|
/**
|
|
66
117
|
* Sets the experience value for the user, adjusting their level if necessary.
|
|
67
118
|
* @param {UserEditor} user - The user object.
|
|
68
119
|
* @param {number} value - The new experience value to set.
|
|
69
|
-
* @returns {
|
|
70
|
-
*/
|
|
71
|
-
set(user:
|
|
72
|
-
exp: number;
|
|
73
|
-
level: number;
|
|
74
|
-
totalExp: any;
|
|
75
|
-
}, value: number): {
|
|
76
|
-
exp: number;
|
|
77
|
-
level: number;
|
|
78
|
-
totalExp: number;
|
|
79
|
-
};
|
|
120
|
+
* @returns {UserEditor} The updated user object.
|
|
121
|
+
*/
|
|
122
|
+
set(user: UserEditor, value: number): UserEditor;
|
|
80
123
|
/**
|
|
81
124
|
* Adds experience to the user, adjusting their level if necessary.
|
|
82
125
|
* @param {UserEditor} user - The user object.
|
|
83
126
|
* @param {number} [extraExp] - Additional experience to be added.
|
|
84
127
|
* @param {'add' | 'extra'} [type] - Type of addition ('add' or 'extra').
|
|
85
128
|
* @param {number} [multi] - Multiplier for experience generation.
|
|
86
|
-
* @returns {
|
|
87
|
-
*/
|
|
88
|
-
give(user:
|
|
89
|
-
exp: number;
|
|
90
|
-
level: number;
|
|
91
|
-
totalExp: any;
|
|
92
|
-
}, extraExp?: number, type?: "add" | "extra", multi?: number): {
|
|
93
|
-
exp: number;
|
|
94
|
-
level: number;
|
|
95
|
-
totalExp: number;
|
|
96
|
-
};
|
|
129
|
+
* @returns {UserEditor} The updated user object.
|
|
130
|
+
*/
|
|
131
|
+
give(user: UserEditor, extraExp?: number, type?: "add" | "extra", multi?: number): UserEditor;
|
|
97
132
|
/**
|
|
98
133
|
* Removes experience from the user, adjusting their level if necessary.
|
|
99
134
|
* @param {UserEditor} user - The user object.
|
|
100
135
|
* @param {number} [extraExp] - Additional experience to remove.
|
|
101
136
|
* @param {'add' | 'extra'} [type] - Type of removal ('add' or 'extra').
|
|
102
137
|
* @param {number} [multi] - Multiplier for experience generation.
|
|
103
|
-
* @returns {
|
|
104
|
-
*/
|
|
105
|
-
remove(user:
|
|
106
|
-
exp: number;
|
|
107
|
-
level: number;
|
|
108
|
-
totalExp: any;
|
|
109
|
-
}, extraExp?: number, type?: "add" | "extra", multi?: number): {
|
|
110
|
-
exp: number;
|
|
111
|
-
level: number;
|
|
112
|
-
totalExp: number;
|
|
113
|
-
};
|
|
138
|
+
* @returns {UserEditor} The updated user object.
|
|
139
|
+
*/
|
|
140
|
+
remove(user: UserEditor, extraExp?: number, type?: "add" | "extra", multi?: number): UserEditor;
|
|
114
141
|
}
|
|
115
142
|
//# sourceMappingURL=userLevel.d.mts.map
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a user object used.
|
|
3
|
+
*
|
|
4
|
+
* @typedef {Object} UserEditor
|
|
5
|
+
* @property {number} exp - Current experience points of the user.
|
|
6
|
+
* @property {number} level - Current level of the user.
|
|
7
|
+
* @property {number} totalExp - Total accumulated experience.
|
|
8
|
+
*/
|
|
1
9
|
/**
|
|
2
10
|
* Class to manage user level-up logic based on experience points.
|
|
3
11
|
*/
|
|
@@ -8,23 +16,93 @@ class TinyLevelUp {
|
|
|
8
16
|
* @param {number} expLevel - Base experience needed to level up (per level).
|
|
9
17
|
*/
|
|
10
18
|
constructor(giveExp, expLevel) {
|
|
19
|
+
if (typeof giveExp !== 'number' || Number.isNaN(giveExp))
|
|
20
|
+
throw new Error('giveExp must be a valid number');
|
|
21
|
+
if (typeof expLevel !== 'number' || Number.isNaN(expLevel))
|
|
22
|
+
throw new Error('expLevel must be a valid number');
|
|
11
23
|
this.giveExp = giveExp;
|
|
12
24
|
this.expLevel = expLevel;
|
|
13
25
|
}
|
|
14
26
|
/**
|
|
15
|
-
*
|
|
27
|
+
* Creates a new user object starting at level 0 with 0 experience.
|
|
28
|
+
* @returns {UserEditor} A fresh user object.
|
|
16
29
|
*/
|
|
30
|
+
createUser() {
|
|
31
|
+
return {
|
|
32
|
+
exp: 0,
|
|
33
|
+
level: 1,
|
|
34
|
+
totalExp: 0,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
17
37
|
/**
|
|
18
|
-
*
|
|
38
|
+
* Validates if the given user object has valid numeric properties.
|
|
39
|
+
* Throws an error if any property is invalid.
|
|
40
|
+
*
|
|
41
|
+
* @param {UserEditor} user - The user object to validate.
|
|
42
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
19
43
|
*/
|
|
44
|
+
validateUser(user) {
|
|
45
|
+
if (typeof user.exp !== 'number' || Number.isNaN(user.exp))
|
|
46
|
+
throw new Error('exp must be a valid number');
|
|
47
|
+
if (typeof user.level !== 'number' || Number.isNaN(user.level))
|
|
48
|
+
throw new Error('level must be a valid number');
|
|
49
|
+
if (user.level < 1)
|
|
50
|
+
throw new Error('level must be at least 1');
|
|
51
|
+
if (typeof user.totalExp !== 'number' || Number.isNaN(user.totalExp))
|
|
52
|
+
throw new Error('totalExp must be a valid number');
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Checks if the given user object is valid by verifying its numeric properties.
|
|
56
|
+
*
|
|
57
|
+
* @param {UserEditor} user - The user object to check.
|
|
58
|
+
* @returns {boolean} `true` if all properties (exp, level, totalExp) are valid numbers; otherwise `false`.
|
|
59
|
+
*/
|
|
60
|
+
isValidUser(user) {
|
|
61
|
+
if (typeof user.exp !== 'number' || Number.isNaN(user.exp))
|
|
62
|
+
return false;
|
|
63
|
+
if (typeof user.level !== 'number' || Number.isNaN(user.level))
|
|
64
|
+
return false;
|
|
65
|
+
if (user.level < 1)
|
|
66
|
+
return false;
|
|
67
|
+
if (typeof user.totalExp !== 'number' || Number.isNaN(user.totalExp))
|
|
68
|
+
return false;
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns the base experience value used for random experience generation.
|
|
73
|
+
* Throws an error if the internal giveExp value is not a valid number.
|
|
74
|
+
*
|
|
75
|
+
* @returns {number} The base experience value.
|
|
76
|
+
* @throws {Error} If giveExp is not a valid number.
|
|
77
|
+
*/
|
|
78
|
+
getGiveExpBase() {
|
|
79
|
+
if (typeof this.giveExp !== 'number' || Number.isNaN(this.giveExp))
|
|
80
|
+
throw new Error('giveExp must be a valid number');
|
|
81
|
+
return this.giveExp;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns the base experience required to level up.
|
|
85
|
+
* Throws an error if the internal expLevel value is not a valid number.
|
|
86
|
+
*
|
|
87
|
+
* @returns {number} The base experience needed per level.
|
|
88
|
+
* @throws {Error} If expLevel is not a valid number.
|
|
89
|
+
*/
|
|
90
|
+
getExpLevelBase() {
|
|
91
|
+
if (typeof this.expLevel !== 'number' || Number.isNaN(this.expLevel))
|
|
92
|
+
throw new Error('expLevel must be a valid number');
|
|
93
|
+
return this.expLevel;
|
|
94
|
+
}
|
|
20
95
|
/**
|
|
21
96
|
* Validates and adjusts the user's level based on their current experience.
|
|
22
97
|
* @param {UserEditor} user - The user object containing experience and level properties.
|
|
23
|
-
* @returns {
|
|
98
|
+
* @returns {UserEditor} The updated user object.
|
|
99
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
24
100
|
*/
|
|
25
101
|
expValidator(user) {
|
|
102
|
+
const expLevel = this.getExpLevelBase();
|
|
103
|
+
this.validateUser(user);
|
|
26
104
|
let extraValue = 0;
|
|
27
|
-
const nextLevelExp =
|
|
105
|
+
const nextLevelExp = expLevel * user.level;
|
|
28
106
|
// Level Up
|
|
29
107
|
if (user.exp >= nextLevelExp) {
|
|
30
108
|
user.level++;
|
|
@@ -37,7 +115,7 @@ class TinyLevelUp {
|
|
|
37
115
|
if (user.exp < 1 && user.level > 1) {
|
|
38
116
|
user.level--;
|
|
39
117
|
extraValue = Math.abs(user.exp);
|
|
40
|
-
user.exp =
|
|
118
|
+
user.exp = expLevel * user.level;
|
|
41
119
|
if (extraValue > 0)
|
|
42
120
|
return this.remove(user, extraValue, 'extra');
|
|
43
121
|
}
|
|
@@ -45,13 +123,15 @@ class TinyLevelUp {
|
|
|
45
123
|
}
|
|
46
124
|
/**
|
|
47
125
|
* Calculates the total experience based on the user's level.
|
|
48
|
-
* @param {
|
|
126
|
+
* @param {UserEditor} user - The user object containing experience and level properties.
|
|
49
127
|
* @returns {number} The total experience of the user.
|
|
128
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
50
129
|
*/
|
|
51
130
|
getTotalExp(user) {
|
|
131
|
+
this.validateUser(user);
|
|
52
132
|
let totalExp = 0;
|
|
53
133
|
for (let p = 1; p <= user.level; p++)
|
|
54
|
-
totalExp += this.
|
|
134
|
+
totalExp += this.getExpLevelBase() * p;
|
|
55
135
|
totalExp += user.exp;
|
|
56
136
|
return totalExp;
|
|
57
137
|
}
|
|
@@ -61,31 +141,47 @@ class TinyLevelUp {
|
|
|
61
141
|
* @returns {number} The generated experience points.
|
|
62
142
|
*/
|
|
63
143
|
expGenerator(multi = 1) {
|
|
64
|
-
|
|
144
|
+
if (typeof multi !== 'number' || Number.isNaN(multi))
|
|
145
|
+
throw new Error('multi must be a valid number');
|
|
146
|
+
return Math.floor(Math.random() * this.getGiveExpBase()) * multi;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Calculates how much experience is missing to next level.
|
|
150
|
+
* @param {UserEditor} user
|
|
151
|
+
* @returns {number}
|
|
152
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
153
|
+
*/
|
|
154
|
+
getMissingExp(user) {
|
|
155
|
+
return this.getProgress(user) - user.exp;
|
|
65
156
|
}
|
|
66
157
|
/**
|
|
67
158
|
* Gets the experience points required to reach the next level.
|
|
68
|
-
* @param {
|
|
159
|
+
* @param {UserEditor} user - The user object containing the level.
|
|
69
160
|
* @returns {number} The experience required for the next level.
|
|
161
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
70
162
|
*/
|
|
71
163
|
progress(user) {
|
|
72
|
-
return this.
|
|
164
|
+
return this.getProgress(user);
|
|
73
165
|
}
|
|
74
166
|
/**
|
|
75
167
|
* Gets the experience points required to reach the next level.
|
|
76
|
-
* @param {
|
|
168
|
+
* @param {UserEditor} user - The user object containing the level.
|
|
77
169
|
* @returns {number} The experience required for the next level.
|
|
170
|
+
* @throws {Error} If any property (exp, level, totalExp) is not a valid number.
|
|
78
171
|
*/
|
|
79
172
|
getProgress(user) {
|
|
80
|
-
|
|
173
|
+
this.validateUser(user);
|
|
174
|
+
return this.getExpLevelBase() * user.level;
|
|
81
175
|
}
|
|
82
176
|
/**
|
|
83
177
|
* Sets the experience value for the user, adjusting their level if necessary.
|
|
84
178
|
* @param {UserEditor} user - The user object.
|
|
85
179
|
* @param {number} value - The new experience value to set.
|
|
86
|
-
* @returns {
|
|
180
|
+
* @returns {UserEditor} The updated user object.
|
|
87
181
|
*/
|
|
88
182
|
set(user, value) {
|
|
183
|
+
if (typeof value !== 'number' || Number.isNaN(value))
|
|
184
|
+
throw new Error('value must be a valid number');
|
|
89
185
|
user.exp = value;
|
|
90
186
|
this.expValidator(user);
|
|
91
187
|
user.totalExp = this.getTotalExp(user);
|
|
@@ -97,9 +193,15 @@ class TinyLevelUp {
|
|
|
97
193
|
* @param {number} [extraExp] - Additional experience to be added.
|
|
98
194
|
* @param {'add' | 'extra'} [type] - Type of addition ('add' or 'extra').
|
|
99
195
|
* @param {number} [multi] - Multiplier for experience generation.
|
|
100
|
-
* @returns {
|
|
196
|
+
* @returns {UserEditor} The updated user object.
|
|
101
197
|
*/
|
|
102
198
|
give(user, extraExp = 0, type = 'add', multi = 1) {
|
|
199
|
+
if (typeof multi !== 'number' || Number.isNaN(multi))
|
|
200
|
+
throw new Error('multi must be a valid number');
|
|
201
|
+
if (typeof extraExp !== 'number' || Number.isNaN(extraExp))
|
|
202
|
+
throw new Error('extraExp must be a valid number');
|
|
203
|
+
if (typeof type !== 'string')
|
|
204
|
+
throw new Error('type must be a valid string');
|
|
103
205
|
if (type === 'add')
|
|
104
206
|
user.exp += this.expGenerator(multi) + extraExp;
|
|
105
207
|
else if (type === 'extra')
|
|
@@ -114,9 +216,15 @@ class TinyLevelUp {
|
|
|
114
216
|
* @param {number} [extraExp] - Additional experience to remove.
|
|
115
217
|
* @param {'add' | 'extra'} [type] - Type of removal ('add' or 'extra').
|
|
116
218
|
* @param {number} [multi] - Multiplier for experience generation.
|
|
117
|
-
* @returns {
|
|
219
|
+
* @returns {UserEditor} The updated user object.
|
|
118
220
|
*/
|
|
119
221
|
remove(user, extraExp = 0, type = 'add', multi = 1) {
|
|
222
|
+
if (typeof multi !== 'number' || Number.isNaN(multi))
|
|
223
|
+
throw new Error('multi must be a valid number');
|
|
224
|
+
if (typeof extraExp !== 'number' || Number.isNaN(extraExp))
|
|
225
|
+
throw new Error('extraExp must be a valid number');
|
|
226
|
+
if (typeof type !== 'string')
|
|
227
|
+
throw new Error('type must be a valid string');
|
|
120
228
|
if (type === 'add')
|
|
121
229
|
user.exp -= this.expGenerator(multi) + extraExp;
|
|
122
230
|
else if (type === 'extra')
|