示例#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);
        }
示例#2
0
        public List <Game> GetGames(User user = null)
        {
            var games = _gameRepository.List.ToList(); // Grab all the games to calculate the rank

            foreach (var game in games)
            {
                var roundSpecification = new RoundSpecification(game.GameId);
                var rounds             = _roundRepository.Query(roundSpecification);
                game.Rounds = rounds;

                var lastAnswerSpecification = new QuizAnswerSpecification(rounds.Last().AnswerId);
                var lastAnswer = _answerRepository.Query(lastAnswerSpecification).First();

                var gameWon = lastAnswer.Correct && rounds.Last().Duration <= 120;

                int duration = 0;
                int points   = 0;

                foreach (var round in rounds)
                {
                    duration += round.Duration;

                    if (!gameWon)
                    {
                        continue;
                    }

                    var answerSpecification = new QuizAnswerSpecification(round.AnswerId);
                    var answer = _answerRepository.Query(answerSpecification).First();
                    if (answer.Correct)
                    {
                        points += 30;
                    }
                }

                game.Duration = duration;
                game.Points   = points;

                game.WeightedPoints = game.Points / Math.Max(game.Duration, 1);

                var categoryGameSpecification = new CategoryGameSpecification(gameId: game.GameId);
                var categoryGames             = _categoryGameRepository.Query(categoryGameSpecification);
                foreach (var categoryGame in categoryGames)
                {
                    game.Categories.Add(categoryGame.CategoryId);
                }

                if (user != null && game.UserId != user.UserId)
                {
                    continue; // Saves time by not grabbing information for unneeded games
                }
            }

            var weightedPointsComparer = new GameWeightedPointsComparer();

            games.Sort(weightedPointsComparer);

            if (user != null)
            {
                games = games.Where(g => g.UserId == user.UserId || g.Points > 0 && !g.Hidden).ToList();

                var currRank = 1;
                foreach (var game in games)
                {
                    if (game.Points <= 0 || game.Hidden)
                    {
                        continue;
                    }
                    game.Rank = currRank;
                    currRank++;
                }

                games = games.Where(g => g.UserId == user.UserId).ToList();
                var dateComparer = new GameDateComparer();
                games.Sort(dateComparer);
            }
            else
            {
                games = games.Where(g => g.Points > 0 && !g.Hidden).ToList();

                for (int i = 0; i < games.Count; i++)
                {
                    games[i].Rank = i + 1;
                }
            }

            return(games);
        }