Exemple #1
0
        public IQuestion <GameAnswer> GetQuestion(User user)
        {
            var gameIndex = _runningGames.FindIndex(g => g.UserId == user.UserId);
            var game      = _runningGames[gameIndex];

            // TODO: Throw error if no game has been found
            // TODO: Check if current question is unanswered

            var quizQuestionSpecification =
                new QuizQuestionSpecification(categories: game.Categories, excludeQuestions: game.QuestionIds);
            var quizQuestions = _questionRepository.Query(quizQuestionSpecification);

            var          random = new Random();
            QuizQuestion quizQuestion;

            if (quizQuestions.Count != 0)
            {
                var index = random.Next(quizQuestions.Count);
                quizQuestion = quizQuestions[index];
            }
            else
            {
                throw new NoMoreQuestionsException(
                          "There are no more questions available in the selected categories for this quiz!");
            }

            var quizAnswerSpecification = new QuizAnswerSpecification(questionId: quizQuestion.QuestionId);
            var quizAnswers             = _answerRepository.Query(quizAnswerSpecification);

            quizQuestion.Answers = quizAnswers;

            var correctAnswer = quizAnswers.First(a => a.Correct);

            var roundSpecification = new RoundSpecification(questionId: quizQuestion.QuestionId);
            var rounds             = _roundRepository.Query(roundSpecification);
            var correctlyAnswered  = rounds.Count(r => r.AnswerId == correctAnswer.AnswerId);

            var question = new GameQuestion(quizQuestion, rounds.Count, correctlyAnswered);

            _runningGames[gameIndex].AskQuestion(question);

            return(question);
        }
 public void AskQuestion(GameQuestion gameQuestion)
 {
     gameQuestion.TimeAsked = DateTime.Now;
     CurrentQuestion        = gameQuestion;
 }