-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
269 lines (218 loc) · 6.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
var _ = require('lodash');
var RPGLevel = function RPGLevel() {
this._exp = 0;
// This is necessary-Exp delta list for Level-Up.
// For example, [0, 2, 4, 8] means what
// it needs total Exps [Lv1=0, Lv2=2, Lv3=6, Lv4=14].
// For that reason, [0] is always to contain 0.
this._necessaryExps = [];
this._minLevel = 1;
this._cachedStates = null;
};
RPGLevel.prototype.defineExpTable = function defineExpTable(formula, options) {
this._necessaryExps = this._generateNecessaryExps(formula, options);
};
RPGLevel.prototype.defineExpTableByArray = function defineExpTableByArray(necessaryExps) {
this._necessaryExps = necessaryExps;
};
/**
* @param {Function} formula
* @param {Object|undefined} options
* @return {Array}
*/
RPGLevel.prototype._generateNecessaryExps = function _generateNecessaryExps(formula, options) {
var self = this;
options = _.assign({
startLevel: 1,
maxLevel: 99
}, options || {});
var exps = [];
var previousExp = 0;
var previousTotalExp = 0;
var memo = {};
return _.range(this._minLevel, options.maxLevel + 1).map(function(level) {
var exp;
if (level <= options.startLevel) {
exp = 0;
} else {
exp = formula(level, {
minLevel: self._minLevel,
startLevel: options.startLevel,
maxLevel: options.maxLevel,
exps: exps.slice(),
previousExp: previousExp,
previousTotalExp: previousTotalExp,
memo: memo
});
previousExp = exp;
previousTotalExp += exp;
}
exps.push(exp);
return exp;
});
};
RPGLevel.prototype.getMinLevel = function getMinLevel() {
return this._minLevel;
};
RPGLevel.prototype.getMaxLevel = function getMaxLevel() {
return this._necessaryExps.length;
};
RPGLevel.prototype._getIndexByLevel = function _getIndexByLevel(level) {
return level - 1;
};
RPGLevel.prototype._getLevelByIndex = function _getLevelByIndex(index) {
return Number(index) + 1;
};
RPGLevel.prototype._getStartIndex = function _getStartIndex() {
for (var i = 0; i < this._necessaryExps.length; i++) {
if (this._necessaryExps[i] > 0) {
return Number(i) - 1;
}
}
};
RPGLevel.prototype.getStartLevel = function getStartLevel() {
return this._getLevelByIndex(this._getStartIndex());
};
RPGLevel.prototype.getExp = function getExp() {
return this._exp;
};
RPGLevel.prototype.getNecessaryExpByLevel = function getNecessaryExpByLevel(level) {
return this._necessaryExps[this._getIndexByLevel(level)];
};
RPGLevel.prototype.getTotalNecessaryExp = function getTotalNecessaryExp(fromLevel, toLevel) {
var self = this;
if (fromLevel > toLevel) {
throw new Error('toLevel should be greater than fromLevel');
}
if (fromLevel < this._minLevel || toLevel > this.getMaxLevel()) {
throw new Error('fromLevel or toLevel is out of range');
}
return _.reduce(_.range(fromLevel, toLevel + 1), function(total, level) {
return total + self.getNecessaryExpByLevel(level);
}, 0);
};
RPGLevel.prototype.getMaxExp = function getMaxExp() {
return this.getTotalNecessaryExp(this.getMinLevel(), this.getMaxLevel());
};
RPGLevel.prototype._cleanCaches = function _cleanCaches() {
this._cachedStates = null;
};
RPGLevel.prototype.setExp = function setExp(exp) {
this._exp = parseInt(exp, 10);
this._cleanCaches();
};
RPGLevel.prototype.resetExp = function resetExp() {
this.setExp(0);
};
RPGLevel.prototype.setLevel = function setLevel(level) {
var exp = this.getTotalNecessaryExp(this.getStartLevel(), level);
this.setExp(exp);
};
RPGLevel.prototype._hasCachedStates = function _hasCachedStates() {
return !!this._cachedStates;
};
RPGLevel.prototype.getStates = function getStates() {
var self = this;
if (this._hasCachedStates()) {
return _.cloneDeep(this._cachedStates);
}
var myLevel = 0;
var totalNecessaryExp = 0;
var necessaryExpForNext = 0;
var gainedExpForNext = 0;
this._necessaryExps.every(function(necessaryExp, idx) {
var level = self._getLevelByIndex(idx);
totalNecessaryExp += necessaryExp;
if (self.getExp() >= totalNecessaryExp) {
myLevel = level;
return true;
} else {
necessaryExpForNext = necessaryExp;
gainedExpForNext = self.getExp() - (totalNecessaryExp - necessaryExp);
return false;
}
});
var states = {
level: myLevel,
necessaryExpForNext: necessaryExpForNext,
gainedExpForNext: gainedExpForNext,
lackExpForNext: necessaryExpForNext - gainedExpForNext
};
this._cachedStates = _.cloneDeep(states);
return states;
};
RPGLevel.prototype.getLevel = function getLevel() {
return this.getStates().level;
};
RPGLevel.prototype.isMaxLevel = function isMaxLevel() {
return this.getLevel() === this.getMaxLevel();
};
RPGLevel.prototype._updateExp = function _updateExp(exp) {
var beforeLevel = this.getLevel();
var nextExp = this.getExp() + exp;
if (nextExp > this.getMaxExp()) {
nextExp = this.getMaxExp();
} else if (nextExp < 0) {
nextExp = 0;
}
this.setExp(nextExp);
var afterLevel = this.getLevel();
return {
beforeLevel: beforeLevel,
afterLevel: afterLevel
};
};
RPGLevel.prototype._createExpUpdateResults = function _createExpUpdateResults(
beforeExp, afterExp, beforeLevel, afterLevel
) {
return {
beforeExp: beforeExp,
afterExp: afterExp,
expDelta: afterExp - beforeExp,
beforeLevel: beforeLevel,
afterLevel: afterLevel,
levelDelta: afterLevel - beforeLevel,
isLevelUp: afterLevel > beforeLevel,
isLevelDown: afterLevel < beforeLevel
};
};
RPGLevel.prototype.gainExp = function gainExp(exp) {
var beforeExp = this.getExp();
var updated = this._updateExp(exp);
return this._createExpUpdateResults(
beforeExp, this.getExp(), updated.beforeLevel, updated.afterLevel);
};
RPGLevel.prototype.drainExp = function drainExp(exp) {
var beforeExp = this.getExp();
var updated = this._updateExp(-exp);
return this._createExpUpdateResults(
beforeExp, this.getExp(), updated.beforeLevel, updated.afterLevel);
};
RPGLevel.prototype.gainLevel = function gainLevel(levelUpCount) {
var fromLevel = this.getLevel();
var toLevel = fromLevel + levelUpCount;
if (toLevel > this.getMaxLevel()) {
toLevel = this.getMaxLevel();
}
// It is always adjusting like the following:
//
// Lv=1, Current/Next 3/5
// (+1 Level Up)
// Lv=2, Current/Next 0/10 (Ignores remained 3 exp)
var deltaExp = this.getTotalNecessaryExp(this.getMinLevel(), toLevel) - this.getExp();
return this.gainExp(deltaExp);
};
RPGLevel.prototype.drainLevel = function drainLevel(levelDownCount) {
var toLevel = this.getLevel() - levelDownCount;
if (toLevel < this.getMinLevel()) {
toLevel = this.getMinLevel();
}
// It is always adjusting like the following:
//
// Lv=2, Current/Next 3/10
// (-1 Level Down)
// Lv=1, Current/Next 4/5 (Always (max-1) exp)
var deltaExp = this.getExp() - this.getTotalNecessaryExp(this.getMinLevel(), toLevel + 1) + 1;
return this.drainExp(deltaExp);
};
module.exports = RPGLevel;