Example #1
0
        public async Task <ActionResult> Index()
        {
            var currentUserId = User.Identity.GetUserId();

            TriviaDashboardViewModel viewModel = new TriviaDashboardViewModel();

            viewModel.NewQuizzes = await TriviaService.GetNewQuizzesAsync();

            viewModel.DailyQuizzes = await TriviaService.GetDailyQuizzesAsync(5);

            viewModel.TrendingQuizzes = await GetTrendingQuizzesAsync();

            viewModel.PopularQuizzes = await GetPopularQuizzesAsync();

            // unauthenticated users don't have any quizzes to track
            if (User.Identity.IsAuthenticated)
            {
                viewModel.UnfinishedQuizzes = await TriviaService.GetUnfinishedQuizzesAsync(User.Identity.GetUserId());
            }

            // alters the viewmodel's list of quizzes to indicate if the quiz has already been completed by the currently logged in user
            await SetQuizzesCompletedByCurrentUser(currentUserId, viewModel);

            viewModel.RecentlyCompletedQuizzes = await TriviaService.GetUsersCompletedQuizzesAsync(currentUserId);

            viewModel.QuizCategories = await TriviaService.GetQuizCategoriesAsync();

            await SetNotificationsAsync();

            return(View(viewModel));
        }
Example #2
0
        public async Task <ActionResult> Timed()
        {
            QuestionViewModel viewModel = new QuestionViewModel();

            viewModel = await GetRandomQuestionViewModelAsync((int)QuestionTypeValues.Timed);

            if (viewModel != null)
            {
                viewModel.QuestionViolation = await GetQuestionViolationViewModelAsync();
            }
            else
            {
                viewModel      = new QuestionViewModel();
                viewModel.Tags = new List <TagViewModel>();
                viewModel.UsersAnsweredCorrectly = new List <UserAnsweredQuestionCorrectlyViewModel>();
            }

            viewModel.QuizCategories = await TriviaService.GetQuizCategoriesAsync();

            return(View(viewModel));
        }
Example #3
0
        public async Task <ActionResult> Category(int id, string seoName)
        {
            var quizCategory = await TriviaService.GetQuizCategoryAsync(id);

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

            string expectedSeoName = quizCategory.ToSEOName();

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

            await SetNotificationsAsync();

            var currentUserId = User.Identity.GetUserId();

            TriviaCategoryViewModel viewModel = new TriviaCategoryViewModel();

            var quizzesInCategory = await TriviaService.GetQuizzesInCategoryAsync(id);

            viewModel.Quizzes = Mapper.Map <IReadOnlyCollection <Quiz>, IReadOnlyCollection <QuizOverviewViewModel> >(quizzesInCategory);

            viewModel.QuizCategories = await TriviaService.GetQuizCategoriesAsync();

            viewModel.TrendingQuizzes = await GetTrendingQuizzesAsync();

            viewModel.PopularQuizzes = await GetPopularQuizzesAsync();

            viewModel.RecentlyCompletedQuizzes = await TriviaService.GetUsersCompletedQuizzesAsync(currentUserId);

            viewModel.ActiveQuizCategoryId   = quizCategory.Id;
            viewModel.ActiveQuizCategoryName = quizCategory.Name;
            viewModel.ActiveQuizCategoryIcon = quizCategory.FontAwesomeIconName;

            return(View(viewModel));
        }
Example #4
0
        public async Task <ActionResult> Top()
        {
            await SetNotificationsAsync();

            var currentUserId = User.Identity.GetUserId();

            TriviaTopPlayersViewModel viewModel = new TriviaTopPlayersViewModel();

            var topPlayers = await TriviaService.GetTopPlayersAsync();

            viewModel.Players = Mapper.Map <IReadOnlyCollection <Models.Profile>, IReadOnlyCollection <ProfileViewModel> >(topPlayers);

            viewModel.QuizCategories = await TriviaService.GetQuizCategoriesAsync();

            viewModel.TrendingQuizzes = await GetTrendingQuizzesAsync();

            viewModel.PopularQuizzes = await GetPopularQuizzesAsync();

            viewModel.RecentlyCompletedQuizzes = await TriviaService.GetUsersCompletedQuizzesAsync(currentUserId);

            return(View(viewModel));
        }
        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);
        }
Example #6
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));
        }