mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Merge branch 'api-v3' into api-v3-adapt-v2
This commit is contained in:
@@ -1,15 +1,22 @@
|
||||
import refPush from '../libs/refPush';
|
||||
import validator from 'validator';
|
||||
import i18n from '../i18n';
|
||||
import {
|
||||
BadRequest,
|
||||
} from '../libs/errors';
|
||||
import _ from 'lodash';
|
||||
|
||||
module.exports = function(user, req, cb) {
|
||||
var wh;
|
||||
module.exports = function addWebhook (user, req = {}) {
|
||||
let wh;
|
||||
wh = user.preferences.webhooks;
|
||||
refPush(wh, {
|
||||
|
||||
if (!validator.isURL(_.get(req, 'body.url'))) throw new BadRequest(i18n.t('invalidUrl', req.language));
|
||||
if (!validator.isBoolean(_.get(req, 'body.enabled'))) throw new BadRequest(i18n.t('invalidEnabled', req.language));
|
||||
|
||||
user.markModified('preferences.webhooks');
|
||||
|
||||
return refPush(wh, {
|
||||
url: req.body.url,
|
||||
enabled: req.body.enabled || true,
|
||||
id: req.body.id
|
||||
enabled: req.body.enabled,
|
||||
});
|
||||
if (typeof user.markModified === "function") {
|
||||
user.markModified('preferences.webhooks');
|
||||
}
|
||||
return typeof cb === "function" ? cb(null, user.preferences.webhooks) : void 0;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
module.exports = function(user, req, cb) {
|
||||
delete user.preferences.webhooks[req.params.id];
|
||||
if (typeof user.markModified === "function") {
|
||||
user.markModified('preferences.webhooks');
|
||||
}
|
||||
return typeof cb === "function" ? cb(null, user.preferences.webhooks) : void 0;
|
||||
import _ from 'lodash';
|
||||
|
||||
module.exports = function deleteWebhook (user, req) {
|
||||
delete user.preferences.webhooks[_.get(req, 'params.id')];
|
||||
user.markModified('preferences.webhooks');
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
25
common/script/ops/resetGear.js
Normal file
25
common/script/ops/resetGear.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import _ from 'lodash';
|
||||
import content from '../content/index';
|
||||
|
||||
module.exports = function resetGear (user) {
|
||||
let gear = user.items.gear;
|
||||
|
||||
_.each(['equipped', 'costume'], function resetUserGear (type) {
|
||||
gear[type] = {};
|
||||
gear[type].armor = 'armor_base_0';
|
||||
gear[type].weapon = 'weapon_warrior_0';
|
||||
gear[type].head = 'head_base_0';
|
||||
gear[type].shield = 'shield_base_0';
|
||||
});
|
||||
|
||||
// Gear.owned is a Mongo object so the _.each function iterates over hidden properties.
|
||||
// The content.gear.flat[k] check should prevent this causing an error
|
||||
_.each(gear.owned, function resetOwnedGear (v, k) {
|
||||
if (gear.owned[k] && content.gear.flat[k] && content.gear.flat[k].value) {
|
||||
gear.owned[k] = false;
|
||||
}
|
||||
});
|
||||
|
||||
gear.owned.weapon_warrior_0 = true; // eslint-disable-line camelcase
|
||||
user.preferences.costume = false;
|
||||
};
|
||||
@@ -1,72 +1,106 @@
|
||||
import content from '../content/index';
|
||||
import i18n from '../i18n';
|
||||
import _ from 'lodash';
|
||||
import {
|
||||
NotAuthorized,
|
||||
} from '../libs/errors';
|
||||
import splitWhitespace from '../libs/splitWhitespace';
|
||||
import randomVal from '../fns/randomVal';
|
||||
|
||||
module.exports = function(user, req, cb, analytics) {
|
||||
var analyticsData, base, cl, gearOwned, item, losableItems, lostItem, lostStat;
|
||||
if (!(user.stats.hp <= 0)) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 400,
|
||||
message: "Cannot revive if not dead"
|
||||
}) : void 0;
|
||||
module.exports = function revive (user, req = {}, analytics) {
|
||||
if (user.stats.hp > 0) {
|
||||
throw new NotAuthorized(i18n.t('cannotRevive', req.language));
|
||||
}
|
||||
|
||||
_.merge(user.stats, {
|
||||
hp: 50,
|
||||
exp: 0,
|
||||
gp: 0
|
||||
gp: 0,
|
||||
});
|
||||
|
||||
if (user.stats.lvl > 1) {
|
||||
user.stats.lvl--;
|
||||
}
|
||||
lostStat = user.fns.randomVal(_.reduce(['str', 'con', 'per', 'int'], (function(m, k) {
|
||||
|
||||
let lostStat = randomVal(user, _.reduce(['str', 'con', 'per', 'int'], function findRandomStat (m, k) {
|
||||
if (user.stats[k]) {
|
||||
m[k] = k;
|
||||
}
|
||||
return m;
|
||||
}), {}));
|
||||
}, {}));
|
||||
|
||||
if (lostStat) {
|
||||
user.stats[lostStat]--;
|
||||
}
|
||||
cl = user.stats["class"];
|
||||
gearOwned = (typeof (base = user.items.gear.owned).toObject === "function" ? base.toObject() : void 0) || user.items.gear.owned;
|
||||
losableItems = {};
|
||||
_.each(gearOwned, function(v, k) {
|
||||
var itm;
|
||||
if (v) {
|
||||
itm = content.gear.flat['' + k];
|
||||
|
||||
let base = user.items.gear.owned;
|
||||
let gearOwned;
|
||||
|
||||
if (typeof base.toObject === 'function') {
|
||||
gearOwned = base.toObject();
|
||||
} else {
|
||||
gearOwned = user.items.gear.owned;
|
||||
}
|
||||
|
||||
let losableItems = {};
|
||||
let userClass = user.stats.class;
|
||||
|
||||
_.each(gearOwned, function findLosableItems (value, key) {
|
||||
let itm;
|
||||
if (value) {
|
||||
itm = content.gear.flat[key];
|
||||
|
||||
if (itm) {
|
||||
if ((itm.value > 0 || k === 'weapon_warrior_0') && (itm.klass === cl || (itm.klass === 'special' && (!itm.specialClass || itm.specialClass === cl)) || itm.klass === 'armoire')) {
|
||||
return losableItems['' + k] = '' + k;
|
||||
let itemHasValueOrWarrior0 = itm.value > 0 || key === 'weapon_warrior_0';
|
||||
|
||||
let itemClassEqualsUserClass = itm.klass === userClass;
|
||||
|
||||
let itemClassSpecial = itm.klass === 'special';
|
||||
let itemNotSpecialOrUserClassIsSpecial = !itm.specialClass || itm.specialClass === userClass;
|
||||
let itemIsSpecial = itemNotSpecialOrUserClassIsSpecial && itemClassSpecial;
|
||||
|
||||
let itemIsArmoire = itm.klass === 'armoire';
|
||||
|
||||
if (itemHasValueOrWarrior0 && (itemClassEqualsUserClass || itemIsSpecial || itemIsArmoire)) {
|
||||
losableItems[key] = key;
|
||||
return losableItems[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
lostItem = user.fns.randomVal(losableItems);
|
||||
if (item = content.gear.flat[lostItem]) {
|
||||
|
||||
let lostItem = randomVal(user, losableItems);
|
||||
|
||||
let message = '';
|
||||
let item = content.gear.flat[lostItem];
|
||||
|
||||
if (item) {
|
||||
user.items.gear.owned[lostItem] = false;
|
||||
|
||||
if (user.items.gear.equipped[item.type] === lostItem) {
|
||||
user.items.gear.equipped[item.type] = item.type + "_base_0";
|
||||
user.items.gear.equipped[item.type] = `${item.type}_base_0`;
|
||||
}
|
||||
|
||||
if (user.items.gear.costume[item.type] === lostItem) {
|
||||
user.items.gear.costume[item.type] = item.type + "_base_0";
|
||||
user.items.gear.costume[item.type] = `${item.type}_base_0`;
|
||||
}
|
||||
|
||||
message = i18n.t('messageLostItem', { itemText: item.text(req.language)}, req.language);
|
||||
}
|
||||
if (typeof user.markModified === "function") {
|
||||
user.markModified('items.gear');
|
||||
|
||||
if (analytics) {
|
||||
analytics.track('Death', {
|
||||
uuid: user._id,
|
||||
lostItem,
|
||||
gaLabel: lostItem,
|
||||
category: 'behavior',
|
||||
});
|
||||
}
|
||||
analyticsData = {
|
||||
uuid: user._id,
|
||||
lostItem: lostItem,
|
||||
gaLabel: lostItem,
|
||||
category: 'behavior'
|
||||
|
||||
let response = {
|
||||
data: _.pick(user, splitWhitespace('user.items')),
|
||||
message,
|
||||
};
|
||||
if (analytics != null) {
|
||||
analytics.track('Death', analyticsData);
|
||||
}
|
||||
return typeof cb === "function" ? cb((item ? {
|
||||
code: 200,
|
||||
message: i18n.t('messageLostItem', {
|
||||
itemText: item.text(req.language)
|
||||
}, req.language)
|
||||
} : null), user) : void 0;
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
@@ -1,23 +1,42 @@
|
||||
import content from '../content/index';
|
||||
import i18n from '../../../common/script/i18n';
|
||||
import _ from 'lodash';
|
||||
import splitWhitespace from '../libs/splitWhitespace';
|
||||
import {
|
||||
NotFound,
|
||||
NotAuthorized,
|
||||
BadRequest,
|
||||
} from '../libs/errors';
|
||||
|
||||
module.exports = function(user, req, cb) {
|
||||
var key, ref, type;
|
||||
ref = req.params, key = ref.key, type = ref.type;
|
||||
if (type !== 'eggs' && type !== 'hatchingPotions' && type !== 'food') {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 404,
|
||||
message: ":type not found. Must bes in [eggs, hatchingPotions, food]"
|
||||
}) : void 0;
|
||||
const ACCEPTEDTYPES = ['eggs', 'hatchingPotions', 'food'];
|
||||
|
||||
module.exports = function sell (user, req = {}) {
|
||||
let key = _.get(req.params, 'key');
|
||||
let type = _.get(req.params, 'type');
|
||||
|
||||
if (!type) {
|
||||
throw new BadRequest(i18n.t('typeRequired', req.language));
|
||||
}
|
||||
|
||||
if (!key) {
|
||||
throw new BadRequest(i18n.t('keyRequired', req.language));
|
||||
}
|
||||
|
||||
if (ACCEPTEDTYPES.indexOf(type) === -1) {
|
||||
throw new NotAuthorized(i18n.t('typeNotSellable', {acceptedTypes: ACCEPTEDTYPES.join(', ')}, req.language));
|
||||
}
|
||||
|
||||
if (!user.items[type][key]) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 404,
|
||||
message: ":key not found for user.items." + type
|
||||
}) : void 0;
|
||||
throw new NotFound(i18n.t('userItemsKeyNotFound', {type}, req.language));
|
||||
}
|
||||
|
||||
user.items[type][key]--;
|
||||
user.stats.gp += content[type][key].value;
|
||||
return typeof cb === "function" ? cb(null, _.pick(user, splitWhitespace('stats items'))) : void 0;
|
||||
|
||||
let response = {
|
||||
data: _.pick(user, splitWhitespace('stats items')),
|
||||
message: i18n.t('sold', {type, key}),
|
||||
};
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
@@ -1,63 +1,85 @@
|
||||
import i18n from '../i18n';
|
||||
import _ from 'lodash';
|
||||
import splitWhitespace from '../libs/splitWhitespace';
|
||||
import dotSet from '../libs/dotSet';
|
||||
import {
|
||||
NotAuthorized,
|
||||
BadRequest,
|
||||
} from '../libs/errors';
|
||||
|
||||
module.exports = function(user, req, cb, analytics) {
|
||||
var alreadyOwns, analyticsData, cost, fullSet, k, path, split, v;
|
||||
path = req.query.path;
|
||||
fullSet = ~path.indexOf(",");
|
||||
cost = ~path.indexOf('background.') ? fullSet ? 3.75 : 1.75 : fullSet ? 1.25 : 0.5;
|
||||
alreadyOwns = !fullSet && user.fns.dotGet("purchased." + path) === true;
|
||||
if ((user.balance < cost || !user.balance) && !alreadyOwns) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 401,
|
||||
message: i18n.t('notEnoughGems', req.language)
|
||||
}) : void 0;
|
||||
module.exports = function unlock (user, req = {}, analytics) {
|
||||
let path = _.get(req.query, 'path');
|
||||
|
||||
if (!path) {
|
||||
throw new BadRequest(i18n.t('pathRequired', req.language));
|
||||
}
|
||||
if (fullSet) {
|
||||
_.each(path.split(","), function(p) {
|
||||
if (~path.indexOf('gear.')) {
|
||||
user.fns.dotSet("" + p, true);
|
||||
true;
|
||||
} else {
|
||||
|
||||
let isFullSet = path.indexOf(',') !== -1;
|
||||
let cost;
|
||||
let isBackground = path.indexOf('background.') !== -1;
|
||||
|
||||
if (isBackground && isFullSet) {
|
||||
cost = 3.75;
|
||||
} else if (isBackground) {
|
||||
cost = 1.75;
|
||||
} else if (isFullSet) {
|
||||
cost = 1.25;
|
||||
} else {
|
||||
cost = 0.5;
|
||||
}
|
||||
|
||||
let alreadyOwns = !isFullSet && user.fns.dotGet(`purchased.${path}`) === true;
|
||||
|
||||
if ((!user.balance || user.balance < cost) && !alreadyOwns) {
|
||||
throw new NotAuthorized(i18n.t('notEnoughGems', req.language));
|
||||
}
|
||||
|
||||
if (isFullSet) {
|
||||
_.each(path.split(','), function markItemsAsPurchased (pathPart) {
|
||||
if (path.indexOf('gear.') !== -1) {
|
||||
dotSet(user, pathPart, true);
|
||||
return true;
|
||||
}
|
||||
user.fns.dotSet("purchased." + p, true);
|
||||
|
||||
dotSet(user, `purchased.${pathPart}`, true);
|
||||
return true;
|
||||
});
|
||||
} else {
|
||||
if (alreadyOwns) {
|
||||
split = path.split('.');
|
||||
v = split.pop();
|
||||
k = split.join('.');
|
||||
if (k === 'background' && v === user.preferences.background) {
|
||||
v = '';
|
||||
let split = path.split('.');
|
||||
let value = split.pop();
|
||||
let key = split.join('.');
|
||||
if (key === 'background' && value === user.preferences.background) {
|
||||
value = '';
|
||||
}
|
||||
user.fns.dotSet("preferences." + k, v);
|
||||
return typeof cb === "function" ? cb(null, req) : void 0;
|
||||
dotSet(user, `preferences.${key}`, value);
|
||||
|
||||
throw new NotAuthorized(i18n.t('alreadyUnlocked', req.language));
|
||||
}
|
||||
user.fns.dotSet("purchased." + path, true);
|
||||
dotSet(user, `purchased.${path}`, true);
|
||||
}
|
||||
|
||||
if (path.indexOf('gear.') === -1) {
|
||||
user.markModified('purchased');
|
||||
}
|
||||
|
||||
user.balance -= cost;
|
||||
if (~path.indexOf('gear.')) {
|
||||
if (typeof user.markModified === "function") {
|
||||
user.markModified('gear.owned');
|
||||
}
|
||||
} else {
|
||||
if (typeof user.markModified === "function") {
|
||||
user.markModified('purchased');
|
||||
}
|
||||
|
||||
if (analytics) {
|
||||
analytics.track('acquire item', {
|
||||
uuid: user._id,
|
||||
itemKey: path,
|
||||
itemType: 'customization',
|
||||
acquireMethod: 'Gems',
|
||||
gemCost: cost / 0.25,
|
||||
category: 'behavior',
|
||||
});
|
||||
}
|
||||
analyticsData = {
|
||||
uuid: user._id,
|
||||
itemKey: path,
|
||||
itemType: 'customization',
|
||||
acquireMethod: 'Gems',
|
||||
gemCost: cost / .25,
|
||||
category: 'behavior'
|
||||
|
||||
let response = {
|
||||
data: _.pick(user, splitWhitespace('purchased preferences items')),
|
||||
message: i18n.t('unlocked'),
|
||||
};
|
||||
if (analytics != null) {
|
||||
analytics.track('acquire item', analyticsData);
|
||||
}
|
||||
return typeof cb === "function" ? cb(null, _.pick(user, splitWhitespace('purchased preferences items'))) : void 0;
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
import _ from 'lodash';
|
||||
import validator from 'validator';
|
||||
import i18n from '../i18n';
|
||||
import {
|
||||
BadRequest,
|
||||
} from '../libs/errors';
|
||||
|
||||
module.exports = function(user, req, cb) {
|
||||
_.merge(user.preferences.webhooks[req.params.id], req.body);
|
||||
if (typeof user.markModified === "function") {
|
||||
user.markModified('preferences.webhooks');
|
||||
}
|
||||
return typeof cb === "function" ? cb(null, user.preferences.webhooks) : void 0;
|
||||
module.exports = function updateWebhook (user, req) {
|
||||
if (!validator.isURL(req.body.url)) throw new BadRequest(i18n.t('invalidUrl', req.language));
|
||||
if (!validator.isBoolean(req.body.enabled)) throw new BadRequest(i18n.t('invalidEnabled', req.language));
|
||||
|
||||
user.markModified('preferences.webhooks');
|
||||
user.preferences.webhooks[req.params.id].url = req.body.url;
|
||||
user.preferences.webhooks[req.params.id].enabled = req.body.enabled;
|
||||
|
||||
return user.preferences.webhooks[req.params.id];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user