Ejemplo n.º 1
0
        public PlayerVsMonkeyBotMakeStepCommandResult ExecuteCommand(PlayerVsMonkeyBotMakeStepCommand command)
        {
            PlayerVsMonkeyBotMakeStepCommandResult result;

            using (var context = new TicTacToeContext())
            {
                var game  = context.Set <Game>().Include(game1 => game1.Field).FirstOrDefault(game1 => game1.GameId == command.GameId);
                var field = game.Field;

                var fieldCode = GameHelper.GetFieldByNumber(game.FieldNumber, field);
                var gameField = this.fieldStateConverter.StringToGameField(fieldCode);

                CellCondition[,] nextGameField;

                try
                {
                    nextGameField = this.stepMaker.MakeStep(gameField, command.X, command.Y);
                }
                catch (Exception)
                {
                    var previousProcessStatistic = this.gameProcessStatisticProvider.GetGameProcessStatistic(gameField);
                    return(new PlayerVsMonkeyBotMakeStepCommandResult
                    {
                        CellSize = GameFieldConstants.LineLength,
                        GameId = command.GameId,
                        GameField = gameField,
                        GameProcessStatistic = previousProcessStatistic,
                    });
                }

                var nextFieldCode = this.fieldStateConverter.GameFieldToString(nextGameField);
                var nextField     = GameHelper.GetFieldByCode(nextFieldCode, context);
                var nextNumber    = GameHelper.GetCodeNumber(nextFieldCode, nextField);

                var gameProcessStatistic = this.gameProcessStatisticProvider.GetGameProcessStatistic(nextGameField);

                game.Field       = nextField;
                game.FieldNumber = nextNumber;
                game.Proccess   += "|" + nextField.FieldId;
                context.Set <Game>().AddOrUpdate(game);
                context.SaveChanges();

                result = new PlayerVsMonkeyBotMakeStepCommandResult
                {
                    CellSize             = GameFieldConstants.LineLength,
                    GameId               = command.GameId,
                    GameField            = nextGameField,
                    GameProcessStatistic = gameProcessStatistic
                };
            }

            return(result);
        }
        public ActionResult MakeStep(PlayerVsMonkeyBotMakeStepCommand command)
        {
            var answer = this.playerVsMonkeyBotCommandHandler.ExecuteCommand(command);

            if (answer.GameProcessStatistic.GameStatus != GameStatus.InProgress)
            {
                if (answer.GameProcessStatistic.GameStatus == GameStatus.Draw)
                {
                    return(RedirectToAction("TakeDraw", new PlayerVsMonkeyBotTakeDrawCommand {
                        GameId = command.GameId
                    }));
                }
                else
                {
                    return(RedirectToAction("WinGame", new PlayerVsMonkeyBotWinGameCommand {
                        GameId = command.GameId
                    }));
                }
            }

            return(this.View(answer));
        }