Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Quiz(int id, string seoName)
        {
            await SetNotificationsAsync();

            var currentUserId = User.Identity.GetUserId();

            // if there is no quiz by this id, return not found
            var quiz = await TriviaService.GetQuizAsync(id);

            if (quiz == null)
            {
                return(new HttpNotFoundResult());
            }

            // redirect to the SEO name if not provided
            string expectedSeoName = quiz.ToSEOName();

            if (seoName != expectedSeoName)
            {
                return(RedirectToAction("quiz", new { id = id, seoName = expectedSeoName }));
            }

            bool isAlreadyCompleted = false;
            List <QuestionViewModel>   questionListViewModel = new List <QuestionViewModel>();
            MinefieldQuestionViewModel minefieldQuestion     = new MinefieldQuestionViewModel();

            if (User.Identity.IsAuthenticated)
            {
                // check if the quiz has already been completed by the user
                isAlreadyCompleted = await TriviaService.IsQuizCompletedByUserAsync(currentUserId, quiz.Id);

                // if it has already been completed, get answered questions for quiz and user
                if (isAlreadyCompleted)
                {
                    if (quiz.QuizTypeId == (int)QuizTypeValues.Individual)
                    {
                        questionListViewModel = await GetQuestionsForIndividualQuizAsync(currentUserId, quiz.Id, quiz.Questions);
                    }
                    else
                    {
                        minefieldQuestion = await GetQuestionForMinefieldQuizAsync(currentUserId, quiz.Id, quiz.MinefieldQuestion);
                    }
                }
                // if it hasn't been completed, get unanswered questions for quiz
                else
                {
                    if (quiz.QuizTypeId == (int)QuizTypeValues.Individual)
                    {
                        questionListViewModel = Mapper.Map <ICollection <Question>, List <QuestionViewModel> >(quiz.Questions);
                    }
                    else
                    {
                        minefieldQuestion = Mapper.Map <MinefieldQuestion, MinefieldQuestionViewModel>(quiz.MinefieldQuestion);
                    }
                }
            }
            // user isn't authenticated, so anonymous users the questions
            else
            {
                if (quiz.QuizTypeId == (int)QuizTypeValues.Individual)
                {
                    questionListViewModel = Mapper.Map <ICollection <Question>, List <QuestionViewModel> >(quiz.Questions);
                }
                else
                {
                    minefieldQuestion = Mapper.Map <MinefieldQuestion, MinefieldQuestionViewModel>(quiz.MinefieldQuestion);
                }
            }

            var usersCompletedQuiz = await TriviaService.GetUsersCompletedQuizAsync(id, currentUserId);

            var tagsForQuiz = await GetTagsForQuizAsync(id);

            var questionViolationViewModel = await GetQuestionViolationViewModelAsync();

            var similarQuizzes = await TriviaService.GetSimilarQuizzesAsync(id, quiz.QuizCategoryId);

            var quizCategories = await TriviaService.GetQuizCategoriesAsync();

            var usersWithSimilarScores = await TriviaService.GetUsersWithSimilarScoresAsync(currentUserId, id, 4);

            QuizViewModel viewModel = new QuizViewModel()
            {
                Questions          = questionListViewModel,
                MinefieldQuestion  = minefieldQuestion,
                QuizName           = quiz.Name,
                QuizId             = id,
                QuizTypeId         = quiz.QuizTypeId,
                IsAlreadyCompleted = isAlreadyCompleted,
                QuizDescription    = quiz.Description,
                UsersCompletedQuiz = usersCompletedQuiz,
                Tags = tagsForQuiz,
                QuestionViolation      = questionViolationViewModel,
                ThumbnailImageUrl      = quiz.GetThumbnailImagePath(),
                ImageUrl               = quiz.GetImagePath(),
                SimilarQuizzes         = similarQuizzes,
                QuizCategories         = quizCategories,
                UsersWithSimilarScores = usersWithSimilarScores
            };

            await SetupAchievementProgressDetails(currentUserId, viewModel);

            return(View(viewModel));
        }