Files
habitica/website/server/libs/spells.js
Phillip Thelen 98f9d2a8f4 Update to new method for fcm (#15238)
* begin moving to new fcm library

* Add error handling

* Add opening notification to correct screen

* Fix tests and make async

* lint fix

* Rename pushNotificationstest..js to pushNotifications.test.js

* fix(potions): remove Fungi Potion time banner

* 5.24.3

* update(content): add 2024-06 content prebuild (#15231)

* update sprites

* add 2024-06 content

* add 2024-06 enchanted armoire items

* update sprites

* update sprites

* fix errors found in testing

* Fix liveliness probes being rate limited (#15236)

* Do not rate limit any liveliness probes

* update example config

* Translated using Weblate (German)

Currently translated at 96.2% (181 of 188 strings)

Translated using Weblate (Japanese)

Currently translated at 99.4% (769 of 773 strings)

Translated using Weblate (German)

Currently translated at 93.6% (176 of 188 strings)

Translated using Weblate (Japanese)

Currently translated at 96.2% (2972 of 3089 strings)

Translated using Weblate (Japanese)

Currently translated at 100.0% (232 of 232 strings)

Translated using Weblate (Japanese)

Currently translated at 96.8% (841 of 868 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (94 of 94 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (113 of 113 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (131 of 131 strings)

Translated using Weblate (Japanese)

Currently translated at 100.0% (286 of 286 strings)

Translated using Weblate (German)

Currently translated at 86.7% (163 of 188 strings)

Translated using Weblate (German)

Currently translated at 85.1% (160 of 188 strings)

Translated using Weblate (German)

Currently translated at 84.0% (158 of 188 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (94 of 94 strings)

Translated using Weblate (German)

Currently translated at 83.5% (157 of 188 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (113 of 113 strings)

Translated using Weblate (German)

Currently translated at 82.9% (156 of 188 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (113 of 113 strings)

Translated using Weblate (German)

Currently translated at 81.9% (154 of 188 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (113 of 113 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (8 of 8 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (8 of 8 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (188 of 188 strings)

Translated using Weblate (German)

Currently translated at 79.2% (149 of 188 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (189 of 189 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (8 of 8 strings)

Translated using Weblate (German)

Currently translated at 90.6% (2799 of 3089 strings)

Translated using Weblate (German)

Currently translated at 77.6% (146 of 188 strings)

Translated using Weblate (German)

Currently translated at 90.5% (2797 of 3089 strings)

Translated using Weblate (German)

Currently translated at 90.4% (2794 of 3089 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (131 of 131 strings)

Translated using Weblate (German)

Currently translated at 90.1% (2786 of 3089 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (188 of 188 strings)

Translated using Weblate (German)

Currently translated at 77.1% (145 of 188 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 98.7% (763 of 773 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (868 of 868 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (131 of 131 strings)

Translated using Weblate (Spanish)

Currently translated at 100.0% (286 of 286 strings)

Translated using Weblate (German)

Currently translated at 90.0% (2782 of 3089 strings)

Translated using Weblate (Spanish)

Currently translated at 100.0% (773 of 773 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (378 of 378 strings)

Translated using Weblate (Spanish)

Currently translated at 100.0% (868 of 868 strings)

Translated using Weblate (Spanish)

Currently translated at 100.0% (167 of 167 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (259 of 259 strings)

Translated using Weblate (Japanese)

Currently translated at 100.0% (259 of 259 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (131 of 131 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (286 of 286 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (239 of 239 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (188 of 188 strings)

Translated using Weblate (French)

Currently translated at 100.0% (188 of 188 strings)

Translated using Weblate (German)

Currently translated at 75.0% (141 of 188 strings)

Translated using Weblate (Spanish)

Currently translated at 99.0% (766 of 773 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (189 of 189 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (868 of 868 strings)

Translated using Weblate (Japanese)

Currently translated at 98.8% (764 of 773 strings)

Translated using Weblate (Japanese)

Currently translated at 99.6% (258 of 259 strings)

Translated using Weblate (Japanese)

Currently translated at 100.0% (378 of 378 strings)

Translated using Weblate (Japanese)

Currently translated at 100.0% (188 of 188 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (140 of 140 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (131 of 131 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (286 of 286 strings)

Translated using Weblate (Ukrainian)

Currently translated at 62.5% (1931 of 3089 strings)

Translated using Weblate (German)

Currently translated at 89.8% (2777 of 3089 strings)

Translated using Weblate (French)

Currently translated at 100.0% (188 of 188 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 98.5% (762 of 773 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (868 of 868 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (286 of 286 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (286 of 286 strings)

Translated using Weblate (French)

Currently translated at 82.9% (156 of 188 strings)

Translated using Weblate (German)

Currently translated at 93.0% (241 of 259 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (286 of 286 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (8 of 8 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (427 of 427 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 98.5% (762 of 773 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (868 of 868 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (167 of 167 strings)

Translated using Weblate (Japanese)

Currently translated at 99.2% (257 of 259 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 98.5% (762 of 773 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (868 of 868 strings)

Translated using Weblate (German)

Currently translated at 92.2% (239 of 259 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (286 of 286 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (239 of 239 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (188 of 188 strings)

Translated using Weblate (German)

Currently translated at 91.8% (238 of 259 strings)

Translated using Weblate (Ukrainian)

Currently translated at 100.0% (131 of 131 strings)

Translated using Weblate (Chinese (Simplified))

Currently translated at 98.5% (762 of 773 strings)

Translated using Weblate (German)

Currently translated at 90.3% (234 of 259 strings)

Co-authored-by: Finrod <963505255@qq.com>
Co-authored-by: Jaime Martí <jaumemarti77@icloud.com>
Co-authored-by: Kem Kembo <medamamef@gmail.com>
Co-authored-by: Sophie LE MASLE <sophiesuff@gmail.com>
Co-authored-by: TOMA Mitsuru <toma0001@gmail.com>
Co-authored-by: Tetiana <merekka13@gmail.com>
Co-authored-by: Toro Mor <thomas.bizer@gmx.de>
Co-authored-by: Weblate <noreply@weblate.org>
Translate-URL: https://translate.habitica.com/projects/habitica/achievements/es/
Translate-URL: https://translate.habitica.com/projects/habitica/achievements/zh_Hans/
Translate-URL: https://translate.habitica.com/projects/habitica/backgrounds/es/
Translate-URL: https://translate.habitica.com/projects/habitica/backgrounds/ja/
Translate-URL: https://translate.habitica.com/projects/habitica/backgrounds/zh_Hans/
Translate-URL: https://translate.habitica.com/projects/habitica/character/uk/
Translate-URL: https://translate.habitica.com/projects/habitica/character/zh_Hans/
Translate-URL: https://translate.habitica.com/projects/habitica/content/ja/
Translate-URL: https://translate.habitica.com/projects/habitica/content/uk/
Translate-URL: https://translate.habitica.com/projects/habitica/faq/de/
Translate-URL: https://translate.habitica.com/projects/habitica/faq/fr/
Translate-URL: https://translate.habitica.com/projects/habitica/faq/ja/
Translate-URL: https://translate.habitica.com/projects/habitica/faq/zh_Hans/
Translate-URL: https://translate.habitica.com/projects/habitica/gear/de/
Translate-URL: https://translate.habitica.com/projects/habitica/gear/ja/
Translate-URL: https://translate.habitica.com/projects/habitica/gear/uk/
Translate-URL: https://translate.habitica.com/projects/habitica/generic/zh_Hans/
Translate-URL: https://translate.habitica.com/projects/habitica/groups/zh_Hans/
Translate-URL: https://translate.habitica.com/projects/habitica/inventory/uk/
Translate-URL: https://translate.habitica.com/projects/habitica/limited/es/
Translate-URL: https://translate.habitica.com/projects/habitica/limited/ja/
Translate-URL: https://translate.habitica.com/projects/habitica/limited/uk/
Translate-URL: https://translate.habitica.com/projects/habitica/limited/zh_Hans/
Translate-URL: https://translate.habitica.com/projects/habitica/npc/uk/
Translate-URL: https://translate.habitica.com/projects/habitica/overview/uk/
Translate-URL: https://translate.habitica.com/projects/habitica/pets/uk/
Translate-URL: https://translate.habitica.com/projects/habitica/quests/uk/
Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/es/
Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/ja/
Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/zh_Hans/
Translate-URL: https://translate.habitica.com/projects/habitica/settings/de/
Translate-URL: https://translate.habitica.com/projects/habitica/settings/ja/
Translate-URL: https://translate.habitica.com/projects/habitica/settings/uk/
Translate-URL: https://translate.habitica.com/projects/habitica/subscriber/ja/
Translate-URL: https://translate.habitica.com/projects/habitica/tasks/zh_Hans/
Translation: Habitica/Achievements
Translation: Habitica/Backgrounds
Translation: Habitica/Character
Translation: Habitica/Content
Translation: Habitica/Faq
Translation: Habitica/Gear
Translation: Habitica/Generic
Translation: Habitica/Groups
Translation: Habitica/Inventory
Translation: Habitica/Limited
Translation: Habitica/Npc
Translation: Habitica/Overview
Translation: Habitica/Pets
Translation: Habitica/Quests
Translation: Habitica/Questscontent
Translation: Habitica/Settings
Translation: Habitica/Subscriber
Translation: Habitica/Tasks

* 5.25.0

* Fix dockerfile (#15241)

* Fix issue with l4p not resetting properly (#15240)

* actually clear out seeking field on user. Even when creating a party

* Add tests to ensure party.seeking is cleared

* fix(lint): don't assign unused const

---------

Co-authored-by: Sabe Jones <sabe@habitica.com>

---------

Co-authored-by: Sabe Jones <sabe@habitica.com>
Co-authored-by: Natalie <78037386+CuriousMagpie@users.noreply.github.com>
Co-authored-by: Weblate <noreply@weblate.org>
Co-authored-by: Finrod <963505255@qq.com>
Co-authored-by: Jaime Martí <jaumemarti77@icloud.com>
Co-authored-by: Kem Kembo <medamamef@gmail.com>
Co-authored-by: Sophie LE MASLE <sophiesuff@gmail.com>
Co-authored-by: TOMA Mitsuru <toma0001@gmail.com>
Co-authored-by: Tetiana <merekka13@gmail.com>
Co-authored-by: Toro Mor <thomas.bizer@gmx.de>
Co-authored-by: Rafał Jagielski <jagielski.rafal.uwm@gmail.com>
2024-06-11 13:19:03 -05:00

332 lines
10 KiB
JavaScript

import { model as User } from '../models/user';
import { chatModel as Chat } from '../models/message';
import * as Tasks from '../models/task';
import {
NotFound,
BadRequest,
NotAuthorized,
} from './errors';
import common from '../../common';
import {
model as Group,
} from '../models/group';
import { apiError } from './apiError';
const partyMembersFields = 'profile.name stats achievements items.special pinnedItems notifications flags';
// Excluding notifications and flags from the list of public fields to return.
const partyMembersPublicFields = 'profile.name stats achievements items.special';
// @TODO: After refactoring individual spells, move quantity to the calculations
async function castTaskSpell (res, req, targetId, user, spell, quantity = 1) {
if (!targetId) throw new BadRequest(res.t('targetIdUUID'));
const task = await Tasks.Task.findOne({
userId: user._id,
_id: targetId,
}).exec();
if (!task) throw new NotFound(res.t('messageTaskNotFound'));
if (task.challenge.id) throw new BadRequest(res.t('challengeTasksNoCast'));
if (task.group.id) throw new BadRequest(res.t('groupTasksNoCast'));
for (let i = 0; i < quantity; i += 1) {
spell.cast(user, task, req);
}
const results = await Promise.all([
user.save(),
task.save(),
]);
return results;
}
async function castMultiTaskSpell (req, user, spell, quantity = 1) {
const tasks = await Tasks.Task.find({
userId: user._id,
...Tasks.taskIsGroupOrChallengeQuery,
}).exec();
for (let i = 0; i < quantity; i += 1) {
spell.cast(user, tasks, req);
}
const toSave = tasks
.filter(t => t.isModified())
.map(t => t.save());
toSave.unshift(user.save());
const saved = await Promise.all(toSave);
const response = {
tasks: saved,
user,
};
return response;
}
async function castSelfSpell (req, user, spell, quantity = 1) {
for (let i = 0; i < quantity; i += 1) {
spell.cast(user, null, req);
}
common.setDebuffPotionItems(user);
await user.save();
}
async function getPartyMembers (user, party) {
let partyMembers;
if (!party) {
// Act as solo party
partyMembers = [user];
} else {
partyMembers = await User
.find({
'party._id': party._id,
_id: { $ne: user._id }, // add separately
})
.select(partyMembersFields)
.exec();
partyMembers.unshift(user);
}
return partyMembers;
}
async function castPartySpell (req, party, user, spell, quantity = 1) {
let partyMembers;
if (spell.bulk) {
const data = { };
if (party) {
data.query = { 'party._id': party._id };
} else {
data.query = { _id: user._id };
}
spell.cast(user, data);
await User.updateMany(data.query, data.update);
await user.save();
partyMembers = await getPartyMembers(user, party);
} else {
partyMembers = await getPartyMembers(user, party);
for (let i = 0; i < quantity; i += 1) {
spell.cast(user, partyMembers, req);
}
await Promise.all(partyMembers.map(m => m.save()));
}
return partyMembers;
}
async function castUserSpell (res, req, party, targetId, user, spell, quantity = 1) {
let partyMembers;
if (!party && (!targetId || user._id === targetId)) {
partyMembers = user;
} else {
if (!targetId) throw new BadRequest(res.t('targetIdUUID'));
if (!party) throw new NotFound(res.t('partyNotFound'));
partyMembers = await User
.findOne({ _id: targetId, 'party._id': party._id })
.select(partyMembersFields)
.exec();
}
if (!partyMembers) throw new NotFound(res.t('userWithIDNotFound', { userId: targetId }));
for (let i = 0; i < quantity; i += 1) {
spell.cast(user, partyMembers, req);
}
common.setDebuffPotionItems(partyMembers);
if (partyMembers !== user) {
await Promise.all([
user.save(),
partyMembers.save(),
]);
} else {
await partyMembers.save(); // partyMembers is user
}
return partyMembers;
}
async function castSpell (req, res, { isV3 = false }) {
const { user } = res.locals;
const { spellId } = req.params;
const { targetId } = req.query;
const quantity = req.body.quantity || 1;
// optional because not required by all targetTypes, presence is checked later if necessary
req.checkQuery('targetId', res.t('targetIdUUID')).optional().isUUID();
const reqValidationErrors = req.validationErrors();
if (reqValidationErrors) throw reqValidationErrors;
const klass = common.content.spells.special[spellId] ? 'special' : user.stats.class;
const spell = common.content.spells[klass][spellId];
if (!spell) throw new NotFound(apiError('spellNotFound', { spellId }));
if (spell.mana > user.stats.mp) throw new NotAuthorized(res.t('notEnoughMana'));
if (spell.value > user.stats.gp && !spell.previousPurchase) throw new NotAuthorized(res.t('messageNotEnoughGold'));
if (spell.lvl > user.stats.lvl) throw new NotAuthorized(res.t('spellLevelTooHigh', { level: spell.lvl }));
const targetType = spell.target;
if (targetType === 'task') {
const results = await castTaskSpell(res, req, targetId, user, spell, quantity);
let userToJson = results[0];
if (isV3) userToJson = await userToJson.toJSONWithInbox();
res.respond(200, {
user: userToJson,
task: results[1],
});
} else if (targetType === 'self') {
const spellName = spell.key;
// Check if stealth skill has been previously casted or not.
// See #12361 for more details.
if (spellName === 'stealth') {
const incompleteDailiesDue = await Tasks.Task.countDocuments({
userId: user._id,
type: 'daily',
completed: false,
isDue: true,
}).exec();
if (user.stats.buffs.stealth >= incompleteDailiesDue) {
throw new BadRequest(res.t('spellAlreadyCast'));
}
}
await castSelfSpell(req, user, spell, quantity);
let userToJson = user;
if (isV3) userToJson = await userToJson.toJSONWithInbox();
res.respond(200, {
user: userToJson,
});
} else if (targetType === 'tasks') { // new target type in v3: when all the user's tasks are necessary
const response = await castMultiTaskSpell(req, user, spell, quantity);
if (isV3) response.user = await response.user.toJSONWithInbox();
res.respond(200, response);
} else if (targetType === 'party' || targetType === 'user') {
const party = await Group.getGroup({ groupId: 'party', user });
// arrays of users when targetType is 'party' otherwise single users
let partyMembers;
if (targetType === 'party') {
partyMembers = await castPartySpell(req, party, user, spell, quantity);
} else {
partyMembers = await castUserSpell(
res,
req,
party,
targetId,
user,
spell,
quantity,
);
}
let partyMembersRes = Array.isArray(partyMembers) ? partyMembers : [partyMembers];
// Only return some fields.
// We can't just return the selected fields because they're private
partyMembersRes = partyMembersRes
.map(partyMember => common.pickDeep(
partyMember.toJSON(),
common.$w(partyMembersPublicFields),
));
let userToJson = user;
if (isV3) userToJson = await userToJson.toJSONWithInbox();
res.respond(200, {
partyMembers: partyMembersRes,
user: userToJson,
});
if (party && !spell.silent) {
const lastMessage = await Chat.findOne({ groupId: party._id })
.sort('-timestamp')
.exec();
if (targetType === 'user') { // Single target spell, check for repeat
if (lastMessage && lastMessage.info.spell === spellId
&& lastMessage.info.user === user.profile.name
&& lastMessage.info.target === partyMembers.profile.name) {
const newChatMessage = await party.sendChat({
message: `\`${common.i18n.t('chatCastSpellUserTimes', {
username: user.profile.name,
spell: spell.text(),
target: partyMembers.profile.name,
times: lastMessage.info.times + 1,
}, 'en')}\``,
info: {
type: 'spell_cast_user_multi',
user: user.profile.name,
class: klass,
spell: spellId,
target: partyMembers.profile.name,
times: lastMessage.info.times + 1,
},
});
await newChatMessage.save();
await lastMessage.deleteOne();
} else { // Single target spell, not repeated
const newChatMessage = await party.sendChat({
message: `\`${common.i18n.t('chatCastSpellUser', { username: user.profile.name, spell: spell.text(), target: partyMembers.profile.name }, 'en')}\``,
info: {
type: 'spell_cast_user',
user: user.profile.name,
class: klass,
spell: spellId,
target: partyMembers.profile.name,
times: 1,
},
});
await newChatMessage.save();
}
} else if (lastMessage && lastMessage.info.spell === spellId // Party spell, check for repeat
&& lastMessage.info.user === user.profile.name) {
const newChatMessage = await party.sendChat({
message: `\`${common.i18n.t('chatCastSpellPartyTimes', {
username: user.profile.name,
spell: spell.text(),
times: lastMessage.info.times + 1,
}, 'en')}\``,
info: {
type: 'spell_cast_party_multi',
user: user.profile.name,
class: klass,
spell: spellId,
times: lastMessage.info.times + 1,
},
});
await newChatMessage.save();
await lastMessage.deleteOne();
} else {
const newChatMessage = await party.sendChat({ // Non-repetitive partywide spell
message: `\`${common.i18n.t('chatCastSpellParty', { username: user.profile.name, spell: spell.text() }, 'en')}\``,
info: {
type: 'spell_cast_party',
user: user.profile.name,
class: klass,
spell: spellId,
times: 1,
},
});
await newChatMessage.save();
}
}
}
}
export {
castTaskSpell,
castMultiTaskSpell,
castSelfSpell,
castPartySpell,
castUserSpell,
castSpell,
};