Exemple #1
0
        /// <summary>
        /// Deletes the quiz.
        /// </summary>
        /// <param name="id">The quiz's id.</param>
        /// <returns>Refresh page.</returns>
        public async Task <IActionResult> OnPostDeleteAsync(int id)
        {
            QuizData      quizData     = new QuizData(_db);
            DataQuizModel newQuizModel = new DataQuizModel
            {
                Duration = DisplayQuiz.Duration,
                Id       = DisplayQuiz.Id,
                Quiz     = DisplayQuiz.Quiz,
            };

            await quizData.DeleteQuiz(newQuizModel);

            return(RedirectToPage("./quiz"));
        }
Exemple #2
0
        /// <summary>
        /// Adds a new quiz.
        /// </summary>
        /// <returns>Refresh the page.</returns>
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            QuizData      quizData     = new QuizData(_db);
            DataQuizModel newQuizModel = new DataQuizModel
            {
                Duration = DisplayQuiz.Duration,
                Id       = DisplayQuiz.Id,
                Quiz     = DisplayQuiz.Quiz,
            };

            await quizData.InsertQuiz(newQuizModel);

            return(RedirectToPage("./quiz"));
        }
Exemple #3
0
        public Task DeleteQuiz(DataQuizModel quizModel)
        {
            string sql = @"delete from quiz where id=@Id;";

            return(_db.SaveData(sql, quizModel));
        }
Exemple #4
0
        public Task InsertQuiz(DataQuizModel quizModel)
        {
            string sql = @"insert into quiz (quiz, duration) values(@Quiz, @Duration);";

            return(_db.SaveData(sql, quizModel));
        }
Exemple #5
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"));
            }
        }