mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
Fixing Exponential Quest Reward Scrolls (#7800)
* adding quest owner specific rewards. closes #2715 * Updating model to prevent this from being a breaking change. * Removing duplicate translatable string and readding accidentally deleted portion * capitalizing according to pr. * fixing according to comments on pr * removing final mistakes * fixing whitespace * re-adding the onlyOwner field that got deleted when the index.js file was moved and fixed console errors. * moving cleaning of empty obejct for quest owner updates into quest owner updates method * Fixing so tests pass by updating variable name and removing unnecessary parameter definition. * adding a new test and refactoring client side code to use controller method.
This commit is contained in:
@@ -579,6 +579,39 @@ schema.statics.cleanGroupQuest = function cleanGroupQuest () {
|
||||
};
|
||||
};
|
||||
|
||||
function _getUserUpdateForQuestReward (itemToAward, allAwardedItems) {
|
||||
let updates = {
|
||||
$set: {},
|
||||
$inc: {},
|
||||
};
|
||||
let dropK = itemToAward.key;
|
||||
|
||||
switch (itemToAward.type) {
|
||||
case 'gear': {
|
||||
// TODO This means they can lose their new gear on death, is that what we want?
|
||||
updates.$set[`items.gear.owned.${dropK}`] = true;
|
||||
break;
|
||||
}
|
||||
case 'eggs':
|
||||
case 'food':
|
||||
case 'hatchingPotions':
|
||||
case 'quests': {
|
||||
updates.$inc[`items.${itemToAward.type}.${dropK}`] = _.where(allAwardedItems, {type: itemToAward.type, key: itemToAward.key}).length;
|
||||
break;
|
||||
}
|
||||
case 'pets': {
|
||||
updates.$set[`items.pets.${dropK}`] = 5;
|
||||
break;
|
||||
}
|
||||
case 'mounts': {
|
||||
updates.$set[`items.mounts.${dropK}`] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
updates = _.omit(updates, _.isEmpty);
|
||||
return updates;
|
||||
}
|
||||
|
||||
async function _updateUserWithRetries (userId, updates, numTry = 1) {
|
||||
return await User.update({_id: userId}, updates).exec()
|
||||
.then((raw) => {
|
||||
@@ -611,33 +644,18 @@ schema.methods.finishQuest = async function finishQuest (quest) {
|
||||
updates.$set['party.quest'] = _cleanQuestProgress({completed: questK}); // clear quest progress
|
||||
}
|
||||
|
||||
_.each(quest.drop.items, (item) => {
|
||||
let dropK = item.key;
|
||||
|
||||
switch (item.type) {
|
||||
case 'gear': {
|
||||
// TODO This means they can lose their new gear on death, is that what we want?
|
||||
updates.$set[`items.gear.owned.${dropK}`] = true;
|
||||
break;
|
||||
}
|
||||
case 'eggs':
|
||||
case 'food':
|
||||
case 'hatchingPotions':
|
||||
case 'quests': {
|
||||
updates.$inc[`items.${item.type}.${dropK}`] = _.where(quest.drop.items, {type: item.type, key: item.key}).length;
|
||||
break;
|
||||
}
|
||||
case 'pets': {
|
||||
updates.$set[`items.pets.${dropK}`] = 5;
|
||||
break;
|
||||
}
|
||||
case 'mounts': {
|
||||
updates.$set[`items.mounts.${dropK}`] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
_.each(_.reject(quest.drop.items, 'onlyOwner'), (item) => {
|
||||
_.merge(updates, _getUserUpdateForQuestReward(item, quest.drop.items));
|
||||
});
|
||||
|
||||
let questOwnerUpdates = {};
|
||||
let questLeader = this.quest.leader;
|
||||
|
||||
_.each(_.filter(quest.drop.items, 'onlyOwner'), (item) => {
|
||||
_.merge(questOwnerUpdates, _getUserUpdateForQuestReward(item, quest.drop.items));
|
||||
});
|
||||
_.merge(questOwnerUpdates, updates);
|
||||
|
||||
let participants = this._id === TAVERN_ID ? {} : this.getParticipatingQuestMembers();
|
||||
this.quest = {};
|
||||
this.markModified('quest');
|
||||
@@ -647,7 +665,11 @@ schema.methods.finishQuest = async function finishQuest (quest) {
|
||||
}
|
||||
|
||||
let promises = participants.map(userId => {
|
||||
return _updateUserWithRetries(userId, updates);
|
||||
if (userId === questLeader) {
|
||||
return _updateUserWithRetries(userId, questOwnerUpdates);
|
||||
} else {
|
||||
return _updateUserWithRetries(userId, updates);
|
||||
}
|
||||
});
|
||||
|
||||
return Bluebird.all(promises);
|
||||
|
||||
Reference in New Issue
Block a user