Files
habitica/website/common/script/fns/updateStats.js
Sabe Jones 7df10d51b0 PRs 2017/04/07 - 2017/04/14 (#8691)
* Issue 8432: Pre-test for initial fix

* Add failing test, seeking help/advice

* Fixes achievements injection. Now trying with Guide

* Fixes tests

* Remoed logging

* Mock ENV function

* Removes test focus

* Remove Beat Master unlock for Rebirth

* Special message when complete all check-in prizes

* Updated comment

* fix(test): unpend party cap test

* remove wrong subscriptions from gift modal

* edit the new issue template to emphasise that the Report a Bug guild should be used first (#8659)

* fix(translation): resolve merge conflict

* fix(news): merge conflict

* Added email invite limit (#8664)

* Added email invite limit

* change error message for sending too many invitations to instruct them to email us

* fix test error message to use variable in locales string

* add comment to warn about keeping INVITES_LIMIT low

If INVITES_LIMIT is allowed to be greater than MAX_EMAIL_INVITES_BY_USER
then the inviter can send more than MAX_EMAIL_INVITES_BY_USER invitations
at once.

* Fix for automatic allocation not persisting #8641 (#8661)

* Fixed text of check-in prize when it is a set of backgrounds (#8599)

* Fixed text of check-in prize when it is a set of backgrounds

* Use existing i18n string for BGs name

* Added user.preferences.language as second parameter

* fix Shiny Seeds info about achievement (not given to caster) (#8679)

Ref: https://habitica.slack.com/archives/C02RK7DKF/p1492032261365388

* Release mergeback v3.86.0 (#8685)

* 3.85.0

* New User Tasks for Mobile (#8682)

* feat(mobile): different default tasks

* fix(linting): missing space

* fix(user): correct client type logic

* test(integration): tasks by platform

* fix(test): remove only

* test(user): deeper checks on tasks

* refactor(test): whitespace for readability

* feat(subs): Jackalope Pets (#8684)

* chore(sprites): compile

* chore(i18n): update locales

* 3.86.0
2017-04-21 15:38:24 -05:00

102 lines
2.8 KiB
JavaScript

import each from 'lodash/each';
import {
MAX_HEALTH,
MAX_STAT_POINTS,
} from '../constants';
import { toNextLevel } from '../statHelpers';
import autoAllocate from './autoAllocate';
module.exports = function updateStats (user, stats, req = {}, analytics) {
let allocatedStatPoints;
let totalStatPoints;
let experienceToNextLevel;
user.stats.hp = stats.hp > 0 ? stats.hp : 0;
user.stats.gp = stats.gp > 0 ? stats.gp : 0;
if (!user._tmp) user._tmp = {};
experienceToNextLevel = toNextLevel(user.stats.lvl);
if (stats.exp >= experienceToNextLevel) {
user.stats.exp = stats.exp;
while (stats.exp >= experienceToNextLevel) {
stats.exp -= experienceToNextLevel;
user.stats.lvl++;
experienceToNextLevel = toNextLevel(user.stats.lvl);
user.stats.hp = MAX_HEALTH;
allocatedStatPoints = user.stats.str + user.stats.int + user.stats.con + user.stats.per;
totalStatPoints = allocatedStatPoints + user.stats.points;
if (totalStatPoints >= MAX_STAT_POINTS) {
continue; // eslint-disable-line no-continue
}
if (user.preferences.automaticAllocation) {
autoAllocate(user);
} else {
user.stats.points = user.stats.lvl - allocatedStatPoints;
totalStatPoints = user.stats.points + allocatedStatPoints;
if (totalStatPoints > MAX_STAT_POINTS) {
user.stats.points = MAX_STAT_POINTS - allocatedStatPoints;
}
if (user.stats.points < 0) {
user.stats.points = 0;
}
}
}
}
user.stats.exp = stats.exp;
if (!user.flags.customizationsNotification && (user.stats.exp > 5 || user.stats.lvl > 1)) {
user.flags.customizationsNotification = true;
}
if (!user.flags.itemsEnabled && (user.stats.exp > 10 || user.stats.lvl > 1)) {
user.flags.itemsEnabled = true;
}
if (!user.flags.dropsEnabled && user.stats.lvl >= 3) {
user.flags.dropsEnabled = true;
user.addNotification('DROPS_ENABLED');
if (user.items.eggs.Wolf > 0) {
user.items.eggs.Wolf++;
} else {
user.items.eggs.Wolf = 1;
}
}
each({
vice1: 30,
atom1: 15,
moonstone1: 60,
goldenknight1: 40,
}, (lvl, k) => {
if (user.stats.lvl >= lvl && !user.flags.levelDrops[k]) {
user.flags.levelDrops[k] = true;
if (!user.items.quests[k])
user.items.quests[k] = 0;
user.items.quests[k]++;
user.markModified('flags.levelDrops');
if (analytics) {
analytics.track('acquire item', {
uuid: user._id,
itemKey: k,
acquireMethod: 'Level Drop',
category: 'behavior',
headers: req.headers,
});
}
user._tmp.drop = {
type: 'Quest',
key: k,
};
}
});
if (!user.flags.rebirthEnabled && user.stats.lvl >= 50) {
user.addNotification('REBIRTH_ENABLED');
user.flags.rebirthEnabled = true;
}
};