@reldens/skills 0.56.0 → 0.59.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/lib/level.js CHANGED
@@ -1,31 +1,31 @@
1
- /**
2
- *
3
- * Reldens - Skills - Level
4
- *
5
- */
6
-
7
- const { Logger, sc } = require('@reldens/utils');
8
-
9
- class Level
10
- {
11
-
12
- constructor(props)
13
- {
14
- // @NOTE: Level.key will be a unique value in the Levels class and the value must be an integer.
15
- if(!sc.hasOwn(props, 'key') || isNaN(props.key)){
16
- Logger.critical('Invalid Level key.');
17
- return false;
18
- }
19
- if(!sc.hasOwn(props, 'modifiers') || 0 >= props.modifiers.length){
20
- Logger.warning('Level modifiers were not specified.', 'Level ID:', props.id, ' - Key:', props.key);
21
- }
22
- this.key = parseInt(props.key);
23
- this.modifiers = props.modifiers;
24
- // label could be used for messages like "you have reached level X!":
25
- this.label = sc.get(props, 'label', props.key);
26
- this.requiredExperience = sc.get(props, 'requiredExperience', sc.get(props, 'required_experience', 0));
27
- }
28
-
29
- }
30
-
31
- module.exports = Level;
1
+ /**
2
+ *
3
+ * Reldens - Skills - Level
4
+ *
5
+ */
6
+
7
+ const { Logger, sc } = require('@reldens/utils');
8
+
9
+ class Level
10
+ {
11
+
12
+ constructor(props)
13
+ {
14
+ // @NOTE: Level.key will be a unique value in the Levels class and the value must be an integer.
15
+ if(!sc.hasOwn(props, 'key') || isNaN(props.key)){
16
+ Logger.critical('Invalid Level key.');
17
+ return false;
18
+ }
19
+ if(!sc.hasOwn(props, 'modifiers') || 0 >= props.modifiers.length){
20
+ Logger.warning('Level modifiers were not specified.', 'Level ID:', props.id, ' - Key:', props.key);
21
+ }
22
+ this.key = parseInt(props.key);
23
+ this.modifiers = props.modifiers;
24
+ // label could be used for messages like "you have reached level X!":
25
+ this.label = sc.get(props, 'label', props.key);
26
+ this.requiredExperience = sc.get(props, 'requiredExperience', sc.get(props, 'required_experience', 0));
27
+ }
28
+
29
+ }
30
+
31
+ module.exports = Level;
package/lib/levels-set.js CHANGED
@@ -1,276 +1,276 @@
1
- /**
2
- *
3
- * Reldens - Skills - LevelsSet
4
- *
5
- */
6
-
7
- const SkillsEvents = require('./skills-events');
8
- const Level = require('./level');
9
- const { EventsManagerSingleton, Logger, sc } = require('@reldens/utils');
10
-
11
- class LevelsSet
12
- {
13
-
14
- constructor(props = {})
15
- {
16
- this.events = sc.get(props, 'events', EventsManagerSingleton);
17
- this.owner = sc.get(props, 'owner', false);
18
- }
19
-
20
- setOwner(props)
21
- {
22
- let owner = sc.get(props, 'owner');
23
- if(!owner){
24
- Logger.critical('Undefined owner for levels.');
25
- return false;
26
- }
27
- // @NOTE: getPosition must return an object with x and y props, like {x: 123, y: 456}.
28
- if('function' !== typeof owner.getPosition){
29
- Logger.critical('Undefined owner position method.');
30
- return false;
31
- }
32
- this.owner = owner;
33
- this.ownerIdProperty = sc.get(props, 'ownerIdProperty', 'id');
34
- }
35
-
36
- async init(props)
37
- {
38
- if(!this.owner){
39
- Logger.critical('Undefined owner for levels.');
40
- return false;
41
- }
42
- if('function' !== typeof this.owner.getPosition){
43
- Logger.critical('Undefined owner position method.');
44
- return false;
45
- }
46
- let levels = sc.get(props, 'levels', {});
47
- if(!levels || !Object.keys(levels).length){
48
- Logger.critical('Levels were not specified.');
49
- return false;
50
- }
51
- // fireEvent has to be executed after the owner was set since it will call the owner data to relate the events:
52
- await this.fireEvent(SkillsEvents.INIT_LEVEL_SET_START, this);
53
- // @NOTE: if autoFillRanges is enabled, you could have the first level with key = 1 and the next with key = 5,
54
- // this way the setLevels will fill up levels 2, 3 and 4 with the same level Object so the same level modifiers
55
- // will be applied automatically (same calculation for multiple levels).
56
- this.autoFillRanges = sc.get(props, 'autoFillRanges', false);
57
- this.autoFillExperienceMultiplier = sc.get(props, 'autoFillExperienceMultiplier', 1.5);
58
- this.currentLevel = sc.get(props, 'currentLevel', 0);
59
- this.currentExp = sc.get(props, 'currentExp', 0);
60
- this.setRequiredExperienceLimit = sc.get(props, 'setRequiredExperienceLimit', false);
61
- this.ownerIdProperty = sc.get(props, 'ownerIdProperty', 'id');
62
- // set levels is almost the last thing to happen here; first we need to validate all the other parameters:
63
- this.levels = await this.setLevels(levels);
64
- // @NOTE: if you set up levelsByExperience and autoFillRanges at the same time, the levels autogenerated will be
65
- // skipped because the experience on those will be lowed one. For example, level 1 required experience = 10p,
66
- // level 5 required experience = 20p; levels 2, 3 and 4 are auto-generated with required exp. = 20p, so when the
67
- // player reaches 20p of experience, it will jump to level 5. In summary, you shouldn't mix these options unless
68
- // you expect the described behavior.
69
- this.increaseLevelsWithExperience = sc.get(props, 'increaseLevelsWithExperience', true);
70
- // unless specified, levels by experience must just be sorted by their key attribute (without autofill):
71
- this.levelsByExperience = sc.get(props, 'levelsByExperience', this.sortLevelsBy(this.levels));
72
- await this.fireEvent(SkillsEvents.INIT_LEVEL_SET_END, this);
73
- }
74
-
75
- async setLevels(levels)
76
- {
77
- !this.autoFillRanges ? (this.levels = levels) : await this.createLevels(levels);
78
- await this.fireEvent(SkillsEvents.SET_LEVELS, this, levels);
79
- return this.levels;
80
- }
81
-
82
- async createLevels(levels)
83
- {
84
- this.levels = {};
85
- let levelsKeys = Object.keys(levels);
86
- for(let i of levelsKeys){
87
- let level = levels[i];
88
- this.levels[i] = level;
89
- let levelsKeysNextIndex = levelsKeys.indexOf(i) + 1;
90
- let nextLevelKey = levelsKeys.length > levelsKeysNextIndex ? levelsKeys[levelsKeysNextIndex] : false;
91
- let nextLevel = false;
92
- if(sc.hasOwn(levels, (nextLevelKey))){
93
- nextLevel = levels[nextLevelKey];
94
- }
95
- if(sc.hasOwn(nextLevel, 'key') && (nextLevel.key - level.key) > 0){
96
- await this.createAndAutofillLevelsInstances(level, nextLevel);
97
- }
98
- }
99
- }
100
-
101
- async createAndAutofillLevelsInstances(level, nextLevel)
102
- {
103
- for(let n = level.key + 1; n < nextLevel.key; n++){
104
- let requiredExp = Math.round(this.levels[n-1].requiredExperience * this.autoFillExperienceMultiplier);
105
- let newLevelProps = {
106
- key: n,
107
- modifiers: level.modifiers,
108
- requiredExperience: requiredExp,
109
- };
110
- this.levels[n] = new Level(newLevelProps);
111
- await this.fireEvent(SkillsEvents.GENERATED_LEVELS, this);
112
- }
113
- }
114
-
115
- sortLevelsBy(levels, sortField = 'key')
116
- {
117
- return Object.keys(levels).sort((a,b) => {
118
- return (levels[a][sortField] > levels[b][sortField]) ? 1 : -1;
119
- });
120
- }
121
-
122
- async levelUp()
123
- {
124
- let lastLevelKey = Object.keys(this.levels).pop();
125
- if(this.currentLevel >= this.levels[lastLevelKey].key){
126
- // reached top level:
127
- return false;
128
- }
129
- // first we need to increase the level:
130
- this.currentLevel++;
131
- // then apply the new level modifiers:
132
- await this.applyLevelModifiers();
133
- await this.fireEvent(SkillsEvents.LEVEL_UP, this);
134
- }
135
-
136
- async levelDown()
137
- {
138
- if(1 >= this.currentLevel){
139
- // reached first level:
140
- return false;
141
- }
142
- // here we first need to revert the modifiers of the current level:
143
- await this.applyLevelModifiers(true);
144
- // then reduce the level:
145
- this.currentLevel--;
146
- await this.fireEvent(SkillsEvents.LEVEL_DOWN, this);
147
- }
148
-
149
- async applyLevelModifiers(revert)
150
- {
151
- let currentLevelInstance = this.getLevelInstance(this.currentLevel);
152
- if(!currentLevelInstance){
153
- // this will usually happen if you don't set a proper end level without required experience.
154
- Logger.error(['Current level instance not found.', 'Current Level Key:', this.currentLevel]);
155
- return false;
156
- }
157
- await this.fireEvent(SkillsEvents.LEVEL_APPLY_MODIFIERS, this, currentLevelInstance);
158
- if(!currentLevelInstance.modifiers || 0 === currentLevelInstance.modifiers.length){
159
- return false;
160
- }
161
- for(let modifier of currentLevelInstance.modifiers){
162
- revert ? modifier.revert(this.owner) : modifier.apply(this.owner);
163
- }
164
- }
165
-
166
- getLevelInstance(level)
167
- {
168
- // @NOTE: if you only specify 1 level, the same calculation will be used for all the levels.
169
- return sc.get(this.levels, level, false);
170
- }
171
-
172
- async addExperience(number)
173
- {
174
- let newTotalExp = this.currentExp + number;
175
- let currentLevelIndex = this.levelsByExperience.indexOf(this.currentLevel.toString());
176
- let nextLevelIndex = currentLevelIndex + 1;
177
- let nextLevelKey = this.levelsByExperience[nextLevelIndex];
178
- let nextLevel = this.levels[nextLevelKey];
179
- let nextLevelExp = 0;
180
- let isLevelUp = false;
181
- if(
182
- this.increaseLevelsWithExperience
183
- && sc.hasOwn(this.levels, this.currentLevel)
184
- && nextLevel && newTotalExp >= nextLevel.requiredExperience
185
- ){
186
- for(let levelByExp of this.levelsByExperience.slice(nextLevelIndex)){
187
- if(newTotalExp >= this.levels[levelByExp].requiredExperience){
188
- isLevelUp = true;
189
- await this.levelUp();
190
- }
191
- if(newTotalExp < this.levels[levelByExp].requiredExperience){
192
- nextLevelExp = this.levels[levelByExp].requiredExperience;
193
- break;
194
- }
195
- }
196
- }
197
- let expNextLevelIndex = !isLevelUp ? nextLevelIndex : (nextLevelIndex + 1)
198
- if((!nextLevel || expNextLevelIndex === this.levelsByExperience.length) && this.setRequiredExperienceLimit){
199
- newTotalExp = this.getLevelInstance(this.currentLevel).requiredExperience;
200
- }
201
- this.currentExp = newTotalExp;
202
- await this.fireEvent(SkillsEvents.LEVEL_EXPERIENCE_ADDED,
203
- this,
204
- number,
205
- newTotalExp,
206
- currentLevelIndex,
207
- nextLevelIndex,
208
- nextLevelKey,
209
- nextLevel,
210
- nextLevelExp,
211
- isLevelUp
212
- );
213
- }
214
-
215
- getNextLevelExperience()
216
- {
217
- let currentLevelIndex = this.levelsByExperience.indexOf(this.currentLevel.toString());
218
- let currentLevel = this.getLevelInstance(this.currentLevel);
219
- let nextLevelIndex = currentLevelIndex + 1;
220
- let nextLevelKey = this.levelsByExperience[nextLevelIndex];
221
- let nextLevel = this.getLevelInstance(nextLevelKey);
222
- if(!nextLevel){
223
- return currentLevel.requiredExperience;
224
- }
225
- if(nextLevel && this.currentExp < nextLevel.requiredExperience){
226
- return nextLevel.requiredExperience;
227
- }
228
- if(!sc.hasOwn(this.levels, this.currentLevel) || !nextLevel){
229
- return nextLevel.requiredExperience;
230
- }
231
- for(let levelByExp of this.levelsByExperience.slice(nextLevelIndex)){
232
- if(this.currentExp < this.levels[levelByExp].requiredExperience){
233
- return this.levels[levelByExp].requiredExperience;
234
- }
235
- }
236
- return nextLevel.requiredExperience;
237
- }
238
-
239
- getOwnerId()
240
- {
241
- return this.owner[this.ownerIdProperty];
242
- }
243
-
244
- getOwnerEventKey()
245
- {
246
- return sc.get(this.owner, 'eventsPrefix', 'skills.ownerId.'+this.getOwnerId());
247
- }
248
-
249
- getOwnerUniqueEventKey(suffix)
250
- {
251
- let uniqueKey = sc.isFunction(this.owner.eventUniqueKey)
252
- ? this.owner.eventUniqueKey()
253
- : 'skills.ownerId.'+this.getOwnerId()+'.uKey.'+sc.getTime();
254
- return uniqueKey+(suffix ? '.'+suffix : '');
255
- }
256
-
257
- async fireEvent(eventName, ...args)
258
- {
259
- return await this.events.emit(this.eventFullName(eventName), ...args);
260
- }
261
-
262
- listenEvent(eventName, callback, removeKey, masterKey)
263
- {
264
- return this.events.onWithKey(this.eventFullName(eventName), callback, removeKey, masterKey);
265
- }
266
-
267
- eventFullName(eventName)
268
- {
269
- // @NOTE: appending the level set owner ID to the event name, so we can pick up this event only for this
270
- // owner automatically:
271
- return this.getOwnerEventKey()+'.'+eventName;
272
- }
273
-
274
- }
275
-
276
- module.exports = LevelsSet;
1
+ /**
2
+ *
3
+ * Reldens - Skills - LevelsSet
4
+ *
5
+ */
6
+
7
+ const SkillsEvents = require('./skills-events');
8
+ const Level = require('./level');
9
+ const { EventsManagerSingleton, Logger, sc } = require('@reldens/utils');
10
+
11
+ class LevelsSet
12
+ {
13
+
14
+ constructor(props = {})
15
+ {
16
+ this.events = sc.get(props, 'events', EventsManagerSingleton);
17
+ this.owner = sc.get(props, 'owner', false);
18
+ }
19
+
20
+ setOwner(props)
21
+ {
22
+ let owner = sc.get(props, 'owner');
23
+ if(!owner){
24
+ Logger.critical('Undefined owner for levels.');
25
+ return false;
26
+ }
27
+ // @NOTE: getPosition must return an object with x and y props, like {x: 123, y: 456}.
28
+ if('function' !== typeof owner.getPosition){
29
+ Logger.critical('Undefined owner position method.');
30
+ return false;
31
+ }
32
+ this.owner = owner;
33
+ this.ownerIdProperty = sc.get(props, 'ownerIdProperty', 'id');
34
+ }
35
+
36
+ async init(props)
37
+ {
38
+ if(!this.owner){
39
+ Logger.critical('Undefined owner for levels.');
40
+ return false;
41
+ }
42
+ if('function' !== typeof this.owner.getPosition){
43
+ Logger.critical('Undefined owner position method.');
44
+ return false;
45
+ }
46
+ let levels = sc.get(props, 'levels', {});
47
+ if(!levels || !Object.keys(levels).length){
48
+ Logger.critical('Levels were not specified.');
49
+ return false;
50
+ }
51
+ // fireEvent has to be executed after the owner was set since it will call the owner data to relate the events:
52
+ await this.fireEvent(SkillsEvents.INIT_LEVEL_SET_START, this);
53
+ // @NOTE: if autoFillRanges is enabled, you could have the first level with key = 1 and the next with key = 5,
54
+ // this way the setLevels will fill up levels 2, 3 and 4 with the same level Object so the same level modifiers
55
+ // will be applied automatically (same calculation for multiple levels).
56
+ this.autoFillRanges = sc.get(props, 'autoFillRanges', false);
57
+ this.autoFillExperienceMultiplier = sc.get(props, 'autoFillExperienceMultiplier', 1.5);
58
+ this.currentLevel = sc.get(props, 'currentLevel', 0);
59
+ this.currentExp = sc.get(props, 'currentExp', 0);
60
+ this.setRequiredExperienceLimit = sc.get(props, 'setRequiredExperienceLimit', false);
61
+ this.ownerIdProperty = sc.get(props, 'ownerIdProperty', 'id');
62
+ // set levels is almost the last thing to happen here; first we need to validate all the other parameters:
63
+ this.levels = await this.setLevels(levels);
64
+ // @NOTE: if you set up levelsByExperience and autoFillRanges at the same time, the levels autogenerated will be
65
+ // skipped because the experience on those will be lowed one. For example, level 1 required experience = 10p,
66
+ // level 5 required experience = 20p; levels 2, 3 and 4 are auto-generated with required exp. = 20p, so when the
67
+ // player reaches 20p of experience, it will jump to level 5. In summary, you shouldn't mix these options unless
68
+ // you expect the described behavior.
69
+ this.increaseLevelsWithExperience = sc.get(props, 'increaseLevelsWithExperience', true);
70
+ // unless specified, levels by experience must just be sorted by their key attribute (without autofill):
71
+ this.levelsByExperience = sc.get(props, 'levelsByExperience', this.sortLevelsBy(this.levels));
72
+ await this.fireEvent(SkillsEvents.INIT_LEVEL_SET_END, this);
73
+ }
74
+
75
+ async setLevels(levels)
76
+ {
77
+ !this.autoFillRanges ? (this.levels = levels) : await this.createLevels(levels);
78
+ await this.fireEvent(SkillsEvents.SET_LEVELS, this, levels);
79
+ return this.levels;
80
+ }
81
+
82
+ async createLevels(levels)
83
+ {
84
+ this.levels = {};
85
+ let levelsKeys = Object.keys(levels);
86
+ for(let i of levelsKeys){
87
+ let level = levels[i];
88
+ this.levels[i] = level;
89
+ let levelsKeysNextIndex = levelsKeys.indexOf(i) + 1;
90
+ let nextLevelKey = levelsKeys.length > levelsKeysNextIndex ? levelsKeys[levelsKeysNextIndex] : false;
91
+ let nextLevel = false;
92
+ if(sc.hasOwn(levels, (nextLevelKey))){
93
+ nextLevel = levels[nextLevelKey];
94
+ }
95
+ if(sc.hasOwn(nextLevel, 'key') && (nextLevel.key - level.key) > 0){
96
+ await this.createAndAutofillLevelsInstances(level, nextLevel);
97
+ }
98
+ }
99
+ }
100
+
101
+ async createAndAutofillLevelsInstances(level, nextLevel)
102
+ {
103
+ for(let n = level.key + 1; n < nextLevel.key; n++){
104
+ let requiredExp = Math.round(this.levels[n-1].requiredExperience * this.autoFillExperienceMultiplier);
105
+ let newLevelProps = {
106
+ key: n,
107
+ modifiers: level.modifiers,
108
+ requiredExperience: requiredExp,
109
+ };
110
+ this.levels[n] = new Level(newLevelProps);
111
+ await this.fireEvent(SkillsEvents.GENERATED_LEVELS, this);
112
+ }
113
+ }
114
+
115
+ sortLevelsBy(levels, sortField = 'key')
116
+ {
117
+ return Object.keys(levels).sort((a,b) => {
118
+ return (levels[a][sortField] > levels[b][sortField]) ? 1 : -1;
119
+ });
120
+ }
121
+
122
+ async levelUp()
123
+ {
124
+ let lastLevelKey = Object.keys(this.levels).pop();
125
+ if(this.currentLevel >= this.levels[lastLevelKey].key){
126
+ // reached top level:
127
+ return false;
128
+ }
129
+ // first we need to increase the level:
130
+ this.currentLevel++;
131
+ // then apply the new level modifiers:
132
+ await this.applyLevelModifiers();
133
+ await this.fireEvent(SkillsEvents.LEVEL_UP, this);
134
+ }
135
+
136
+ async levelDown()
137
+ {
138
+ if(1 >= this.currentLevel){
139
+ // reached first level:
140
+ return false;
141
+ }
142
+ // here we first need to revert the modifiers of the current level:
143
+ await this.applyLevelModifiers(true);
144
+ // then reduce the level:
145
+ this.currentLevel--;
146
+ await this.fireEvent(SkillsEvents.LEVEL_DOWN, this);
147
+ }
148
+
149
+ async applyLevelModifiers(revert)
150
+ {
151
+ let currentLevelInstance = this.getLevelInstance(this.currentLevel);
152
+ if(!currentLevelInstance){
153
+ // this will usually happen if you don't set a proper end level without required experience.
154
+ Logger.error(['Current level instance not found.', 'Current Level Key:', this.currentLevel]);
155
+ return false;
156
+ }
157
+ await this.fireEvent(SkillsEvents.LEVEL_APPLY_MODIFIERS, this, currentLevelInstance);
158
+ if(!currentLevelInstance.modifiers || 0 === currentLevelInstance.modifiers.length){
159
+ return false;
160
+ }
161
+ for(let modifier of currentLevelInstance.modifiers){
162
+ revert ? modifier.revert(this.owner) : modifier.apply(this.owner);
163
+ }
164
+ }
165
+
166
+ getLevelInstance(level)
167
+ {
168
+ // @NOTE: if you only specify 1 level, the same calculation will be used for all the levels.
169
+ return sc.get(this.levels, level, false);
170
+ }
171
+
172
+ async addExperience(number)
173
+ {
174
+ let newTotalExp = this.currentExp + number;
175
+ let currentLevelIndex = this.levelsByExperience.indexOf(this.currentLevel.toString());
176
+ let nextLevelIndex = currentLevelIndex + 1;
177
+ let nextLevelKey = this.levelsByExperience[nextLevelIndex];
178
+ let nextLevel = this.levels[nextLevelKey];
179
+ let nextLevelExp = 0;
180
+ let isLevelUp = false;
181
+ if(
182
+ this.increaseLevelsWithExperience
183
+ && sc.hasOwn(this.levels, this.currentLevel)
184
+ && nextLevel && newTotalExp >= nextLevel.requiredExperience
185
+ ){
186
+ for(let levelByExp of this.levelsByExperience.slice(nextLevelIndex)){
187
+ if(newTotalExp >= this.levels[levelByExp].requiredExperience){
188
+ isLevelUp = true;
189
+ await this.levelUp();
190
+ }
191
+ if(newTotalExp < this.levels[levelByExp].requiredExperience){
192
+ nextLevelExp = this.levels[levelByExp].requiredExperience;
193
+ break;
194
+ }
195
+ }
196
+ }
197
+ let expNextLevelIndex = !isLevelUp ? nextLevelIndex : (nextLevelIndex + 1)
198
+ if((!nextLevel || expNextLevelIndex === this.levelsByExperience.length) && this.setRequiredExperienceLimit){
199
+ newTotalExp = this.getLevelInstance(this.currentLevel).requiredExperience;
200
+ }
201
+ this.currentExp = newTotalExp;
202
+ await this.fireEvent(SkillsEvents.LEVEL_EXPERIENCE_ADDED,
203
+ this,
204
+ number,
205
+ newTotalExp,
206
+ currentLevelIndex,
207
+ nextLevelIndex,
208
+ nextLevelKey,
209
+ nextLevel,
210
+ nextLevelExp,
211
+ isLevelUp
212
+ );
213
+ }
214
+
215
+ getNextLevelExperience()
216
+ {
217
+ let currentLevelIndex = this.levelsByExperience.indexOf(this.currentLevel.toString());
218
+ let currentLevel = this.getLevelInstance(this.currentLevel);
219
+ let nextLevelIndex = currentLevelIndex + 1;
220
+ let nextLevelKey = this.levelsByExperience[nextLevelIndex];
221
+ let nextLevel = this.getLevelInstance(nextLevelKey);
222
+ if(!nextLevel){
223
+ return currentLevel.requiredExperience;
224
+ }
225
+ if(nextLevel && this.currentExp < nextLevel.requiredExperience){
226
+ return nextLevel.requiredExperience;
227
+ }
228
+ if(!sc.hasOwn(this.levels, this.currentLevel) || !nextLevel){
229
+ return nextLevel.requiredExperience;
230
+ }
231
+ for(let levelByExp of this.levelsByExperience.slice(nextLevelIndex)){
232
+ if(this.currentExp < this.levels[levelByExp].requiredExperience){
233
+ return this.levels[levelByExp].requiredExperience;
234
+ }
235
+ }
236
+ return nextLevel.requiredExperience;
237
+ }
238
+
239
+ getOwnerId()
240
+ {
241
+ return this.owner[this.ownerIdProperty];
242
+ }
243
+
244
+ getOwnerEventKey()
245
+ {
246
+ return sc.get(this.owner, 'eventsPrefix', 'skills.ownerId.'+this.getOwnerId());
247
+ }
248
+
249
+ getOwnerUniqueEventKey(suffix)
250
+ {
251
+ let uniqueKey = sc.isFunction(this.owner.eventUniqueKey)
252
+ ? this.owner.eventUniqueKey()
253
+ : 'skills.ownerId.'+this.getOwnerId()+'.uKey.'+sc.getTime();
254
+ return uniqueKey+(suffix ? '.'+suffix : '');
255
+ }
256
+
257
+ async fireEvent(eventName, ...args)
258
+ {
259
+ return await this.events.emit(this.eventFullName(eventName), ...args);
260
+ }
261
+
262
+ listenEvent(eventName, callback, removeKey, masterKey)
263
+ {
264
+ return this.events.onWithKey(this.eventFullName(eventName), callback, removeKey, masterKey);
265
+ }
266
+
267
+ eventFullName(eventName)
268
+ {
269
+ // @NOTE: appending the level set owner ID to the event name, so we can pick up this event only for this
270
+ // owner automatically:
271
+ return this.getOwnerEventKey()+'.'+eventName;
272
+ }
273
+
274
+ }
275
+
276
+ module.exports = LevelsSet;