mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-14 21:27:23 +01:00
Show accurate experience notifications (#10676)
* Show accurate experience notifications Add unit tests for exp notifications * use array to compute exp and lvl values for notification changes * Add tests for user loosing xp cases
This commit is contained in:
committed by
Matteo Pagliazzi
parent
eee5f2f1df
commit
fe39ef72ff
@@ -88,6 +88,7 @@ import axios from 'axios';
|
||||
import moment from 'moment';
|
||||
import throttle from 'lodash/throttle';
|
||||
|
||||
import { toNextLevel } from '../../common/script/statHelpers';
|
||||
import { shouldDo } from '../../common/script/cron';
|
||||
import { mapState } from 'client/libs/store';
|
||||
import notifications from 'client/mixins/notifications';
|
||||
@@ -186,10 +187,8 @@ export default {
|
||||
...mapState({
|
||||
user: 'user.data',
|
||||
userHp: 'user.data.stats.hp',
|
||||
userExp: 'user.data.stats.exp',
|
||||
userGp: 'user.data.stats.gp',
|
||||
userMp: 'user.data.stats.mp',
|
||||
userLvl: 'user.data.stats.lvl',
|
||||
userNotifications: 'user.data.notifications',
|
||||
userAchievements: 'user.data.achievements', // @TODO: does this watch deeply?
|
||||
armoireEmpty: 'user.data.flags.armoireEmpty',
|
||||
@@ -204,6 +203,9 @@ export default {
|
||||
invitedToQuest () {
|
||||
return this.user.party.quest.RSVPNeeded && !this.user.party.quest.completed;
|
||||
},
|
||||
userExpAndLvl () {
|
||||
return [this.user.stats.exp, this.user.stats.lvl];
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
userHp (after, before) {
|
||||
@@ -222,11 +224,6 @@ export default {
|
||||
|
||||
if (after < 0) this.playSound('Minus_Habit');
|
||||
},
|
||||
userExp (after, before) {
|
||||
if (after === before) return;
|
||||
if (this.user.stats.lvl === 0) return;
|
||||
this.exp(after - before);
|
||||
},
|
||||
userGp (after, before) {
|
||||
if (after === before) return;
|
||||
if (this.user.stats.lvl === 0) return;
|
||||
@@ -252,10 +249,6 @@ export default {
|
||||
const mana = after - before;
|
||||
this.mp(mana);
|
||||
},
|
||||
userLvl (after, before) {
|
||||
if (after <= before || this.$store.state.isRunningYesterdailies) return;
|
||||
this.showLevelUpNotifications(after);
|
||||
},
|
||||
userClassSelect (after) {
|
||||
if (this.user.needsCron) return;
|
||||
if (!after) return;
|
||||
@@ -279,6 +272,9 @@ export default {
|
||||
if (after !== true) return;
|
||||
this.$root.$emit('bv::show::modal', 'quest-invitation');
|
||||
},
|
||||
userExpAndLvl (after, before) {
|
||||
this.displayUserExpAndLvlNotifications(after[0], before[0], after[1], before[1]);
|
||||
},
|
||||
},
|
||||
mounted () {
|
||||
Promise.all([
|
||||
@@ -310,6 +306,35 @@ export default {
|
||||
document.removeEventListener('keydown', this.checkNextCron);
|
||||
},
|
||||
methods: {
|
||||
displayUserExpAndLvlNotifications (afterExp, beforeExp, afterLvl, beforeLvl) {
|
||||
if (afterExp === beforeExp && afterLvl === beforeLvl) return;
|
||||
|
||||
// XP evaluation
|
||||
if (afterExp !== beforeExp) {
|
||||
if (this.user.stats.lvl === 0) return;
|
||||
|
||||
const lvlUps = afterLvl - beforeLvl;
|
||||
let exp = afterExp - beforeExp;
|
||||
|
||||
if (lvlUps > 0) {
|
||||
let level = Math.trunc(beforeLvl);
|
||||
exp += toNextLevel(level);
|
||||
|
||||
// loop if more than 1 lvl up
|
||||
for (let i = 1; i < lvlUps; i += 1) {
|
||||
level += 1;
|
||||
exp += toNextLevel(level);
|
||||
}
|
||||
}
|
||||
this.exp(exp);
|
||||
}
|
||||
|
||||
// Lvl evaluation
|
||||
if (afterLvl !== beforeLvl) {
|
||||
if (afterLvl <= beforeLvl || this.$store.state.isRunningYesterdailies) return;
|
||||
this.showLevelUpNotifications(afterLvl);
|
||||
}
|
||||
},
|
||||
checkUserAchievements () {
|
||||
if (this.user.needsCron) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user