Ejemplo n.º 1
0
        public async Task <ComputerAnswerDTO> Play(ValueOnlyGuessDTO ValueOnlyUserGuessDTO)
        {
            if (!ModelState.IsValid)
            {
                return(null);
            }
            Game game = await _gameHandler.GetActiveGame(HttpContext);

            _context.Attach(game);
            if (game == null)
            {
                return(null);
            }

            GuessOutcome userGuessOutcome = await _gameHandler.GetUserGuessOutcome(HttpContext, ValueOnlyUserGuessDTO.Value);

            Guess userGuess = new Guess()
            {
                Value        = ValueOnlyUserGuessDTO.Value,
                GuessOutcome = userGuessOutcome,
                GuessMaker   = GuessMaker.User,
                Game         = game
            };

            _context.Attach(userGuess);

            string AIGuess = await _gameHandler.GetAIGuess(HttpContext);

            GuessOutcome aiGuessOutcomeDTO = await _gameHandler.GetAIGuessOutcome(HttpContext, AIGuess);

            Guess aiGuess = new Guess()
            {
                Value        = AIGuess,
                GuessOutcome = aiGuessOutcomeDTO,
                GuessMaker   = GuessMaker.AI,
                Game         = game
            };

            _context.Attach(aiGuess);

            GuessDTO          aiGuessDTO     = new GuessDTO(aiGuess);
            GuessDTO          userGuessDTO   = new GuessDTO(userGuess);
            ComputerAnswerDTO computerAnswer = new ComputerAnswerDTO
            {
                AIGuess   = aiGuessDTO,
                UserGuess = userGuessDTO
            };

            if (userGuessOutcome.BullsNumber == ValueOnlyUserGuessDTO.Value.Length)//All numbers match - User wins
            {
                game.WonByUser             = true;
                computerAnswer.UserVictory = true;
                computerAnswer.AIGuess     = null;
            }
            else if (aiGuessOutcomeDTO.BullsNumber == AIGuess.Length)//All numbers match - AI wins
            {
                game.WonByAI             = true;
                computerAnswer.AIVictory = true;
            }
            await _context.SaveChangesAsync();

            return(computerAnswer);
        }