Files
habitica/website/common/script/ops/feed.js
Matteo Pagliazzi 8f5a0cfe79 Onboarding guide and initial achievements refactoring (#11536)
* add achievements to user

* add placeholder strings

* add to achievements to common script

* add onboarding achievements category

* add notifications

* more notifications

* award achievements

* wip notification panel

* add achievements icons and copy

* do not count onboarding tasks for the created task achievement

* add notes

* sprites, fixes and completion status and reward

* add onboarding panel

* add toggle

* fix toggle size

* fix tests

* fix typo

* add notification

* start adding modal

* fix remove button positionin, timeout, progress bar

* modal + fixes

* disable broken social links from level up modal

* change toggle icon color on hover

* add border bottom to onboarding guide panel

* add collapse animation

* expanded onboarding on first open

* onboarding: flip toggle colors

* onboarding: show progress bar all the time

* onboarding: fix panel closing on click

* onboarding modal: add close icon and fix padding

* wip: add migration for existing users

* fix titles in guide

* fix achievements copy

* do not award completed task achievement when direction is down

* start implementing new achievements

* start migrating client

* remove social links from achievements modals

* prevent skipping tutorial + fix achievement notification

* sync fixes

* start redesign achievement modal

* misc fixes to achievements, polish generic achievement modal and hatched pet modal

* add special badge for onboarding

* fix badge condition

* modals fixes

* hatched pet modal: add close icon

* fix badge typo

* fix justin button

* new scrolling behavior for dropdowns

* fix strings capitalization

* add common tests

* add api unit tests

* add date check

* achievements modal polishing

* typos

* add toggle for achievements categories

* typo

* fix test

* fix edit avatar modal cannot be closed

* finish migration and correct launch date

* fix migration

* migration fixes

* fix tests
2019-12-16 17:20:47 +01:00

124 lines
3.4 KiB
JavaScript

import forEach from 'lodash/forEach';
import findIndex from 'lodash/findIndex';
import get from 'lodash/get';
import keys from 'lodash/keys';
import upperFirst from 'lodash/upperFirst';
import i18n from '../i18n';
import content from '../content/index';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../libs/errors';
import errorMessage from '../libs/errorMessage';
import { checkOnboardingStatus } from '../libs/onboarding';
function evolve (user, pet, req) {
user.items.pets[pet.key] = -1;
user.items.mounts[pet.key] = true;
if (user.markModified) {
user.markModified('items.pets');
user.markModified('items.mounts');
}
if (pet.key === user.items.currentPet) {
user.items.currentPet = '';
}
return i18n.t('messageEvolve', {
egg: pet.text(req.language),
}, req.language);
}
export default function feed (user, req = {}) {
let pet = get(req, 'params.pet');
const foodK = get(req, 'params.food');
if (!pet || !foodK) throw new BadRequest(errorMessage('missingPetFoodFeed'));
pet = content.petInfo[pet];
if (!pet) {
throw new BadRequest(errorMessage('invalidPetName'));
}
const food = content.food[foodK];
if (!food) {
throw new NotFound(errorMessage('invalidFoodName', req.language));
}
const userPets = user.items.pets;
if (!userPets[pet.key]) {
throw new NotFound(i18n.t('messagePetNotFound', req.language));
}
if (!user.items.food[food.key]) {
throw new NotFound(i18n.t('messageFoodNotFound', req.language));
}
if (pet.type === 'special') {
throw new NotAuthorized(i18n.t('messageCannotFeedPet', req.language));
}
if (user.items.mounts[pet.key]) {
throw new NotAuthorized(i18n.t('messageAlreadyMount', req.language));
}
let message;
if (food.key === 'Saddle') {
message = evolve(user, pet, req);
} else {
const messageParams = {
egg: pet.text(req.language),
foodText: food.textThe(req.language),
};
if (food.target === pet.potion || pet.type === 'premium') {
userPets[pet.key] += 5;
message = i18n.t('messageLikesFood', messageParams, req.language);
} else {
userPets[pet.key] += 2;
message = i18n.t('messageDontEnjoyFood', messageParams, req.language);
}
if (user.markModified) user.markModified('items.pets');
if (userPets[pet.key] >= 50 && !user.items.mounts[pet.key]) {
message = evolve(user, pet, req);
}
if (!user.achievements.fedPet && user.addAchievement) {
user.addAchievement('fedPet');
checkOnboardingStatus(user);
}
}
user.items.food[food.key] -= 1;
if (user.markModified) user.markModified('items.food');
forEach(content.animalColorAchievements, achievement => {
if (!user.achievements[achievement.mountAchievement]) {
const mountIndex = findIndex(keys(content.dropEggs), animal => !user.items.mounts[`${animal}-${achievement.color}`]);
if (mountIndex === -1) {
user.achievements[achievement.mountAchievement] = true;
if (user.addNotification) {
const achievementString = `achievement${upperFirst(achievement.mountAchievement)}`;
user.addNotification(achievement.mountNotificationType, {
achievement: achievement.mountAchievement,
message: `${i18n.t('modalAchievement')} ${i18n.t(achievementString)}`,
modalText: i18n.t(`${achievementString}ModalText`),
});
}
}
}
});
return [
userPets[pet.key],
message,
];
}