Пример #1
0
        private async Task <int> GetAchievedAmountForUserAsync(string userId, int milestoneTypeId)
        {
            int amount = 0;

            // get the number of questions answered correctly
            if (milestoneTypeId == (int)MilestoneTypeValues.QuestionsAnsweredCorrectly)
            {
                amount = await TriviaService.GetQuestionsAnsweredCorrectlyCountAsync(userId);
            }
            // get the number of quizzes completed successfully
            else if (milestoneTypeId == (int)MilestoneTypeValues.QuizzesCompletedSuccessfully)
            {
                var completedQuizzes = await TriviaService.GetCompletedQuizzesByUserAsync(userId);

                amount = completedQuizzes.Count;
            }
            // get the number of gifts sent
            else if (milestoneTypeId == (int)MilestoneTypeValues.GiftSent)
            {
                amount = await ProfileService.GetSentGiftCountForUserAsync(userId);
            }
            // get the number of gifts purchased
            else if (milestoneTypeId == (int)MilestoneTypeValues.GiftsPurchased)
            {
                amount = await ProfileService.GetPurchasedItemCountForUserAsync(userId, (int)StoreItemTypeValues.Gift);
            }
            // get the number of points obtained
            else if (milestoneTypeId == (int)MilestoneTypeValues.PointsObtained)
            {
                amount = await ProfileService.GetLifetimePointsForUserAsync(userId);
            }
            // get the number of profiles reviewed
            else if (milestoneTypeId == (int)MilestoneTypeValues.ProfileReviewsWritten)
            {
                amount = await ProfileService.GetReviewsWrittenCountByUserAsync(userId);
            }
            // get the number of images uploaded
            else if (milestoneTypeId == (int)MilestoneTypeValues.ProfileImagesUploaded)
            {
                amount = await ProfileService.GetImagesUploadedCountByUserAsync(userId);
            }
            // get the number of titles purchased
            else if (milestoneTypeId == (int)MilestoneTypeValues.TitlesPurchased)
            {
                amount = await ProfileService.GetPurchasedItemCountForUserAsync(userId, (int)StoreItemTypeValues.Title);
            }
            // get the number of tags awarded
            else if (milestoneTypeId == (int)MilestoneTypeValues.TagsAwarded)
            {
                amount = await ProfileService.GetTagAwardCountForUserAsync(userId);
            }
            else if (milestoneTypeId == (int)MilestoneTypeValues.Trekkie ||
                     milestoneTypeId == (int)MilestoneTypeValues.RebelAlliance ||
                     milestoneTypeId == (int)MilestoneTypeValues.HighWarlord)
            {
                int quizId = 0;
                if (milestoneTypeId == (int)MilestoneTypeValues.Trekkie)
                {
                    quizId = (int)QuizValues.StarTrek_TOS;
                }
                else if (milestoneTypeId == (int)MilestoneTypeValues.RebelAlliance)
                {
                    quizId = (int)QuizValues.StarWarsCharacters;
                }
                else if (milestoneTypeId == (int)MilestoneTypeValues.HighWarlord)
                {
                    quizId = (int)QuizValues.WorldOfWarcraft_HighWarlord;
                }

                bool hasUserCompletedQuiz = await TriviaService.IsQuizCompletedByUserAsync(userId, quizId);

                amount = hasUserCompletedQuiz ? 1 : 0;
            }
            else if (milestoneTypeId == (int)MilestoneTypeValues.GoldMedalist)
            {
                bool hasUserCompletedQuiz1 = await TriviaService.IsQuizCompletedByUserAsync(userId, (int)QuizValues.SummerOlympics);

                bool hasUserCompletedQuiz2 = await TriviaService.IsQuizCompletedByUserAsync(userId, (int)QuizValues.WinterOlympics);

                amount = (hasUserCompletedQuiz1 && hasUserCompletedQuiz2) ? 1 : 0;
            }
            else if (milestoneTypeId == (int)MilestoneTypeValues.HighFive)
            {
                int daysAgo = 1;
                int countOfQuizzesCompletedInLastDay = await TriviaService.CountOfQuizzesCompletedAsync(userId, daysAgo);

                amount = countOfQuizzesCompletedInLastDay >= 5 ? 1 : 0;
            }
            else if (milestoneTypeId == (int)MilestoneTypeValues.MultiTalented)
            {
                // if the categories "touched" by the user (completed a quiz in that category) are equal to the number of total categories, achievement!
                int quizCategoryCount = (await TriviaService.GetQuizCategoriesAsync()).Count;
                int quizCategoriesTouchedByUserCount = await TriviaService.GetQuizCategoriesTouchedByUserCountAsync(userId);

                amount = quizCategoriesTouchedByUserCount == quizCategoryCount ? 1 : 0;
            }
            else if (milestoneTypeId == (int)MilestoneTypeValues.FriendlyExchange)
            {
                TimeSpan duration = TimeSpan.FromMinutes(30);

                // has anyone even sent us something in the last 30 minutes?
                var giftsSentToCurrentUser = await ProfileService.GetGiftsSentToUserAsync(userId, duration);

                // someone sent us something in the last 30 minutes! yay, we're popular
                if (giftsSentToCurrentUser.Count > 0)
                {
                    // these are the user ids of the people who sent us gifts
                    var userIdsForGiftsSent = giftsSentToCurrentUser.Select(x => x.FromUserId);

                    // now check if we sent any gifts to anyone who sent us a gift in the last 30 minutes
                    var giftsSentToUsers = await ProfileService.GetGiftsSentToUsersFromUserAsync(userId, userIdsForGiftsSent, duration);

                    // we sent someone a gift that sent us a gift in the last 30 minutes
                    if (giftsSentToUsers.Count > 0)
                    {
                        amount = 1;
                    }
                }
            }
            else if (milestoneTypeId == (int)MilestoneTypeValues.ReferralSignUps)
            {
                int count = await UserService.GetReferralsRedeemedCountAsync(userId);

                amount = count >= 3 ? 1 : 0;
            }

            return(amount);
        }