public AnswerResponse GetAnswer(int answerId)
 {
     _log.LogInformation($"Get answer for answerId {answerId}");
     return(_gameContext.Answers
            .Where(x => x.Id == answerId)
            .Select(x => AnswerResponse.FromAnswer(x))
            .First());
 }
 public List <AnswerResponse> GetAnswers(string gameId)
 {
     _log.LogInformation($"Get answers for gameId {gameId}");
     return(_gameContext.Answers
            .Where(x => x.GameId == gameId)
            .Select(x => AnswerResponse.FromAnswer(x))
            .ToList());
 }
        public GameStatusResponse GetStatusResponse(string gameId, string playerId)
        {
            var status = GetStatus(gameId);

            if (status == null)
            {
                return(null);
            }

            var gameStatus = GameStatusResponse.FromStatus(status);

            gameStatus.IsCurrentChoice    = status.ChoosingPlayerId == playerId;
            gameStatus.RemainingQuestions = RemainingQuestions(gameId);

            // Return previous choice
            if (!gameStatus.IsCurrentChoice)
            {
                gameStatus.ChoiceA = _playData.GetPreviousChoiceA();
                gameStatus.ChoiceB = _playData.GetPreviousChoiceB();
            }
            else if (gameStatus.ChoiceA == null || gameStatus.ChoiceB == null)
            {
                // TODO: Something with EF not populating the object, can look at it later
                var choiceA = _gameContext.Answers.Find(status.ChoiceAId);
                var choiceB = _gameContext.Answers.Find(status.ChoiceBId);
                gameStatus.ChoiceA = AnswerResponse.FromAnswer(choiceA);
                gameStatus.ChoiceB = AnswerResponse.FromAnswer(choiceB);
            }

            if (gameStatus.ChoosingPlayer == null)
            {
                var player = _gameContext.Players.Find(status.ChoosingPlayerId);
                if (player != null)
                {
                    gameStatus.ChoosingPlayer            = PlayerResponse.FromPlayer(player);
                    gameStatus.ChoosingPlayer.IsChoosing = true;
                }
            }

            return(gameStatus);
        }
        public AnswerResponse AddAnswer(string request, string gameId)
        {
            _log.LogInformation($"Adding answer for gameId {gameId}: {request}");

            if (!_gameContext.Games.Any(x => x.Id == gameId))
            {
                return(null);
            }

            var answer = _gameContext.Answers.Add(
                new Answer
            {
                Text      = request,
                Submitted = DateTime.Now,
                GameId    = gameId
            });

            _gameContext.SaveChanges();

            return(AnswerResponse.FromAnswer(answer.Entity));
        }
        public GameStatusResponse MakeChoice(string gameId, int answerId)
        {
            // Update answers, eliminated and increase chosen count
            var status        = GetStatus(gameId);
            var choiceA       = GetAnswer(status.ChoiceAId);
            var choiceB       = GetAnswer(status.ChoiceBId);
            var currentPlayer = status.ChoosingPlayerId;

            if (status.ChoiceAId == answerId)
            {
                choiceA.ChosenCount++;
                choiceB.IsEliminated = true;
            }
            else if (status.ChoiceBId == answerId)
            {
                choiceB.ChosenCount++;
                choiceA.IsEliminated = true;
            }
            else
            {
                return(null);
            }

            // Set previous questions to current questions
            _playData.SetPreviousChoiceA(AnswerResponse.FromAnswer(status.ChoiceA));
            _playData.SetPreviousChoiceB(AnswerResponse.FromAnswer(status.ChoiceB));

            // Get new questions
            SetNewChoice(status);

            // New Player
            status.ChoosingPlayer = GetNextPlayer(gameId);

            _gameContext.GameState.Update(status);
            _gameContext.Players.Update(status.ChoosingPlayer);
            _gameContext.SaveChanges();

            return(GetStatusResponse(gameId, currentPlayer));
        }