fix(stats): enforce sensible maxima in db

This commit is contained in:
Sabe Jones
2023-10-17 22:31:43 +00:00
parent 832acb1617
commit 4974712d6c
4 changed files with 26 additions and 5 deletions

View File

@@ -362,6 +362,8 @@ schema.pre('save', true, function preSaveUser (next, done) {
}
}
// Enforce min/max values without displaying schema errors to end user
if (this.isDirectSelected('preferences')) {
if (
_.isNaN(this.preferences.dayStart)
@@ -372,6 +374,20 @@ schema.pre('save', true, function preSaveUser (next, done) {
}
}
if (this.isSelected('stats')) {
const statMaximum = common.constants.MAX_FIELD_HARD_CAP;
const levelMaximum = common.constants.MAX_LEVEL_HARD_CAP;
_.each(['hp', 'mp', 'exp', 'gp'], stat => {
if (this.stats[stat] > statMaximum) {
this.stats[stat] = statMaximum;
}
});
if (this.stats.lvl > levelMaximum) {
this.stats.lvl = levelMaximum;
}
}
// our own version incrementer
if (this.isDirectSelected('_v')) {
if (_.isNaN(this._v) || !_.isNumber(this._v)) this._v = 0;