habitica/common/script/ops/openMysteryItem.js

33 lines
1,020 B
JavaScript
Raw Normal View History

2016-03-07 22:02:42 +00:00
import content from '../content/index';
2016-03-08 16:45:14 +00:00
module.exports = function(user, req, cb, analytics) {
2016-03-07 22:02:42 +00:00
var analyticsData, item, ref, ref1;
2016-03-08 16:45:14 +00:00
item = (ref = user.purchased.plan) != null ? (ref1 = ref.mysteryItems) != null ? ref1.shift() : void 0 : void 0;
2016-03-07 22:02:42 +00:00
if (!item) {
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb({
2016-03-07 22:02:42 +00:00
code: 400,
2016-03-08 16:45:14 +00:00
message: "Empty"
2016-03-07 22:02:42 +00:00
}) : void 0;
}
item = content.gear.flat[item];
user.items.gear.owned[item.key] = true;
2016-03-08 16:45:14 +00:00
if (typeof user.markModified === "function") {
2016-03-07 22:02:42 +00:00
user.markModified('purchased.plan.mysteryItems');
}
item.notificationType = 'Mystery';
analyticsData = {
uuid: user._id,
itemKey: item,
itemType: 'Subscriber Gear',
acquireMethod: 'Subscriber',
category: 'behavior'
};
2016-03-08 16:45:14 +00:00
if (analytics != null) {
2016-03-07 22:02:42 +00:00
analytics.track('open mystery item', analyticsData);
}
if (typeof window !== 'undefined') {
2016-03-08 16:45:14 +00:00
(user._tmp != null ? user._tmp : user._tmp = {}).drop = item;
2016-03-07 22:02:42 +00:00
}
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb(null, user.items.gear.owned) : void 0;
2016-03-07 22:02:42 +00:00
};