mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Ported rebirth. Add unit tests. Added rebirth route. Added integration tests
This commit is contained in:
@@ -1,21 +1,30 @@
|
||||
import content from '../content/index';
|
||||
import i18n from '../i18n';
|
||||
import _ from 'lodash';
|
||||
import { capByLevel } from '../statHelpers';
|
||||
import { MAX_LEVEL } from '../constants';
|
||||
import {
|
||||
NotAuthorized,
|
||||
} from '../libs/errors';
|
||||
import resetGear from './resetGear';
|
||||
import equip from './equip';
|
||||
|
||||
const USERSTATSLIST = ['per', 'int', 'con', 'str', 'points', 'gp', 'exp', 'mp'];
|
||||
|
||||
module.exports = function rebirth (user, tasks = [], req = {}, analytics) {
|
||||
let analyticsData;
|
||||
let flags;
|
||||
let lvl;
|
||||
let stats;
|
||||
|
||||
module.exports = function(user, req, cb, analytics) {
|
||||
var analyticsData, flags, gear, lvl, stats;
|
||||
if (user.balance < 2 && user.stats.lvl < MAX_LEVEL) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 401,
|
||||
message: i18n.t('notEnoughGems', req.language)
|
||||
}) : void 0;
|
||||
throw new NotAuthorized(i18n.t('notEnoughGems', req.language));
|
||||
}
|
||||
|
||||
analyticsData = {
|
||||
uuid: user._id,
|
||||
category: 'behavior'
|
||||
category: 'behavior',
|
||||
};
|
||||
|
||||
if (user.stats.lvl < MAX_LEVEL) {
|
||||
user.balance -= 2;
|
||||
analyticsData.acquireMethod = 'Gems';
|
||||
@@ -24,62 +33,52 @@ module.exports = function(user, req, cb, analytics) {
|
||||
analyticsData.gemCost = 0;
|
||||
analyticsData.acquireMethod = '> 100';
|
||||
}
|
||||
if (analytics != null) {
|
||||
|
||||
if (analytics) {
|
||||
analytics.track('Rebirth', analyticsData);
|
||||
}
|
||||
|
||||
lvl = capByLevel(user.stats.lvl);
|
||||
_.each(user.tasks, function(task) {
|
||||
|
||||
_.each(tasks, function resetTasks (task) {
|
||||
if (task.type !== 'reward') {
|
||||
task.value = 0;
|
||||
}
|
||||
if (task.type === 'daily') {
|
||||
return task.streak = 0;
|
||||
task.streak = 0;
|
||||
}
|
||||
});
|
||||
|
||||
stats = user.stats;
|
||||
stats.buffs = {};
|
||||
stats.hp = 50;
|
||||
stats.lvl = 1;
|
||||
stats["class"] = 'warrior';
|
||||
_.each(['per', 'int', 'con', 'str', 'points', 'gp', 'exp', 'mp'], function(value) {
|
||||
return stats[value] = 0;
|
||||
});
|
||||
// TODO during refactoring: move all gear code from rebirth() to its own function and then call it in reset() as well
|
||||
gear = user.items.gear;
|
||||
_.each(['equipped', 'costume'], function(type) {
|
||||
gear[type] = {};
|
||||
gear[type].armor = 'armor_base_0';
|
||||
gear[type].weapon = 'weapon_warrior_0';
|
||||
gear[type].head = 'head_base_0';
|
||||
return gear[type].shield = 'shield_base_0';
|
||||
stats.class = 'warrior';
|
||||
|
||||
_.each(USERSTATSLIST, function resetStats (value) {
|
||||
stats[value] = 0;
|
||||
});
|
||||
|
||||
resetGear(user);
|
||||
|
||||
if (user.items.currentPet) {
|
||||
user.ops.equip({
|
||||
equip(user, {
|
||||
params: {
|
||||
type: 'pet',
|
||||
key: user.items.currentPet
|
||||
}
|
||||
key: user.items.currentPet,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (user.items.currentMount) {
|
||||
user.ops.equip({
|
||||
equip(user, {
|
||||
params: {
|
||||
type: 'mount',
|
||||
key: user.items.currentMount
|
||||
}
|
||||
key: user.items.currentMount,
|
||||
},
|
||||
});
|
||||
}
|
||||
_.each(gear.owned, function(v, k) {
|
||||
if (gear.owned[k] && content.gear.flat[k].value) {
|
||||
gear.owned[k] = false;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
gear.owned.weapon_warrior_0 = true;
|
||||
if (typeof user.markModified === "function") {
|
||||
user.markModified('items.gear.owned');
|
||||
}
|
||||
user.preferences.costume = false;
|
||||
|
||||
flags = user.flags;
|
||||
if (!user.achievements.beastMaster) {
|
||||
flags.rebirthEnabled = false;
|
||||
@@ -88,13 +87,21 @@ module.exports = function(user, req, cb, analytics) {
|
||||
flags.dropsEnabled = false;
|
||||
flags.classSelected = false;
|
||||
flags.levelDrops = {};
|
||||
|
||||
if (!user.achievements.rebirths) {
|
||||
user.achievements.rebirths = 1;
|
||||
user.achievements.rebirthLevel = lvl;
|
||||
} else if (lvl > user.achievements.rebirthLevel || lvl === 100) {
|
||||
} else if (lvl > user.achievements.rebirthLevel || lvl === MAX_LEVEL) {
|
||||
user.achievements.rebirths++;
|
||||
user.achievements.rebirthLevel = lvl;
|
||||
}
|
||||
|
||||
user.stats.buffs = {};
|
||||
return typeof cb === "function" ? cb(null, user) : void 0;
|
||||
|
||||
let response = {
|
||||
data: user,
|
||||
message: i18n.t('rebirthComplete'),
|
||||
};
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user