mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
fix(stats): enforce sensible maxima in db
This commit is contained in:
@@ -148,7 +148,7 @@ import svgGold from '@/assets/svg/gold.svg';
|
||||
import level from '@/assets/svg/level.svg';
|
||||
import streakIcon from '@/assets/svg/streak.svg';
|
||||
import { mapState } from '@/libs/store';
|
||||
import { MAX_LEVEL_HARD_CAP } from '../../../../../common/script/constants';
|
||||
import { MAX_LEVEL_HARD_CAP, MAX_FIELD_HARD_CAP } from '../../../../../common/script/constants';
|
||||
|
||||
export default {
|
||||
components: { SaveCancelButtons },
|
||||
@@ -231,10 +231,6 @@ export default {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.restoreValues.lvl > MAX_LEVEL_HARD_CAP) {
|
||||
this.restoreValues.lvl = MAX_LEVEL_HARD_CAP;
|
||||
}
|
||||
|
||||
const userChangedLevel = this.restoreValues.lvl !== this.user.stats.lvl;
|
||||
const userDidNotChangeExp = this.restoreValues.exp === this.user.stats.exp;
|
||||
if (userChangedLevel && userDidNotChangeExp) {
|
||||
@@ -265,6 +261,9 @@ export default {
|
||||
) {
|
||||
this.restoreValues[stat] = this.user.stats[stat];
|
||||
valid = false;
|
||||
} else if (this.restoreValues[stat] > MAX_FIELD_HARD_CAP) {
|
||||
this.restoreValues[stat] = MAX_FIELD_HARD_CAP;
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,6 +273,9 @@ export default {
|
||||
|| inputLevel < 1) {
|
||||
this.restoreValues.lvl = this.user.stats.lvl;
|
||||
valid = false;
|
||||
} else if (inputLevel > MAX_LEVEL_HARD_CAP) {
|
||||
this.restoreValues.lvl = MAX_LEVEL_HARD_CAP;
|
||||
valid = false;
|
||||
}
|
||||
|
||||
const inputStreak = Number(this.restoreValues.streak);
|
||||
|
||||
@@ -2,6 +2,7 @@ export const MAX_HEALTH = 50;
|
||||
export const MAX_LEVEL = 100;
|
||||
export const MAX_STAT_POINTS = MAX_LEVEL;
|
||||
export const MAX_LEVEL_HARD_CAP = 9999;
|
||||
export const MAX_FIELD_HARD_CAP = 99999999;
|
||||
export const ATTRIBUTES = ['str', 'int', 'con', 'per'];
|
||||
export const MAX_INCENTIVES = 500;
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
MAX_INCENTIVES,
|
||||
MAX_LEVEL,
|
||||
MAX_LEVEL_HARD_CAP,
|
||||
MAX_FIELD_HARD_CAP,
|
||||
MAX_STAT_POINTS,
|
||||
MAX_SUMMARY_SIZE_FOR_CHALLENGES,
|
||||
MAX_SUMMARY_SIZE_FOR_GUILDS,
|
||||
@@ -124,6 +125,7 @@ api.constants = {
|
||||
MAX_MESSAGE_LENGTH,
|
||||
MAX_GIFT_MESSAGE_LENGTH,
|
||||
MAX_LEVEL_HARD_CAP,
|
||||
MAX_FIELD_HARD_CAP,
|
||||
};
|
||||
// TODO Move these under api.constants
|
||||
api.maxLevel = MAX_LEVEL;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user