/// <summary>
        /// Get the quiz's questions.
        /// </summary>
        /// <param name="id">The quiz's id.</param>
        public async Task OnGetAsync(string id)
        {
            DisplayQuestions = new List <DisplayQuestionModel>();

            // Getting the current quiz
            QuizData quizData       = new QuizData(_db);
            var      dataQuizModels = await quizData.GetQuizById(id);

            DisplayQuizModel = new DisplayQuizModel
            {
                Duration = dataQuizModels[0].Duration,
                Id       = dataQuizModels[0].Id,
                Quiz     = dataQuizModels[0].Quiz,
            };

            // Getting links
            List <DisplayLinkQuizQuestionModel> displayLinkQuizQuestionModels = new List <DisplayLinkQuizQuestionModel>();
            LinkQuizQuestionData             linkQuizQuestionData             = new LinkQuizQuestionData(_db);
            List <DataLinkQuizQuestionModel> dataAccessLinkQuizQuestionModels = await linkQuizQuestionData.GetLinkedQuestions(id);

            foreach (DataLinkQuizQuestionModel dataAccessLinkQuizQuestionModel in dataAccessLinkQuizQuestionModels)
            {
                displayLinkQuizQuestionModels.Add(new DisplayLinkQuizQuestionModel
                {
                    Id         = dataAccessLinkQuizQuestionModel.Id,
                    QuestionId = dataAccessLinkQuizQuestionModel.QuestionId,
                    QuizId     = dataAccessLinkQuizQuestionModel.QuizId,
                });
            }

            DisplayLinks = displayLinkQuizQuestionModels;

            // Gettings questions for the dropdown list
            Questions = new SelectList(await GetQuestions(), "Id", "Question");
        }
Beispiel #2
0
        /// <summary>
        /// Display the question and its answers.
        /// </summary>
        /// <param name="id">The question's id</param>
        public async Task OnGetQuestionAsync(string id)
        {
            // ReBuilding SelectedQuiz...
            SelectedQuiz = new DisplayQuizModel {
                Id = id
            };

            // Quiz is selected, the question can be displayed
            // Getting Quiz's random question
            DisplayGameQuestionModel selectedQuestion = await GetRandomQuestionFromQuiz(SelectedQuiz.Id);

            // Getting Question's answers
            List <DisplayAnswerModel> questionsAnswers = await GetQuestionAnswers(selectedQuestion.Id);

            // Building the game question / answers object
            DisplayGameQuestionAnswer = new DisplayGameQuestionAnswerModel
            {
                Question = selectedQuestion,
                Answers  = questionsAnswers,
            };

            DisplayViewData();
        }
Beispiel #3
0
        /// <summary>
        /// Process the user answer to the question.
        /// </summary>
        /// <returns>Refresh the page.</returns>
        public async Task <IActionResult> OnPostAsync()
        {
            try
            {
                if (Session.EndSession == default(DateTime) || SelectedQuiz == null)
                {
                    // Session doesn't exists, the player wants to start the game, setting the game parameters and display them
                    // Getting the quiz as we only have the Id
                    QuizData      quizData      = new QuizData(_db);
                    DataQuizModel dataQuizModel = await quizData.GetQuizById(SelectedQuiz.Id)
                                                  .ContinueWith((x) => { return(x.Result[0]); });

                    SelectedQuiz = new DisplayQuizModel
                    {
                        Duration = dataQuizModel.Duration,
                        Id       = dataQuizModel.Id,
                        Quiz     = dataQuizModel.Quiz,
                    };

                    Session.EndSession = DateTime.Now.AddSeconds(SelectedQuiz.Duration);
                    Score.PlayerScore  = 0;
                    return(RedirectToPage("./game", "question", new { id = SelectedQuiz.Id }));
                }
                else
                {
                    // The session exists, the player just answered a question
                    if (DateTime.Now > Session.EndSession)
                    {
                        // Session expired, Game finished
                        Session.EndSession = default(DateTime);
                        string tempScore = Score.PlayerScore.ToString();
                        Score.PlayerScore = 0;

                        // Display a result page where the user can see its score
                        return(RedirectToPage("./resultpage", new { score = tempScore }));
                    }
                    else
                    {
                        // The player still has time, checking its answer...
                        bool answersAreCorrect = true;
                        foreach (DisplayAnswerModel answer in DisplayGameQuestionAnswer.Answers)
                        {
                            if (answer.Type != answer.PlayerAnswer)
                            {
                                answersAreCorrect = false;
                                break;
                            }
                        }

                        if (answersAreCorrect)
                        {
                            Score.PlayerScore++;
                        }

                        return(RedirectToPage("./game", "question", new { id = SelectedQuiz.Id }));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(RedirectToPage("./game"));
            }
        }