Esempio n. 1
0
        public PlayerVsStatisticBotMakeStepCommandResult ExecuteCommand(PlayerVsStatisticBotMakeStepCommand command)
        {
            PlayerVsStatisticBotMakeStepCommandResult 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 PlayerVsStatisticBotMakeStepCommandResult
                    {
                        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 PlayerVsStatisticBotMakeStepCommandResult
                {
                    CellSize             = GameFieldConstants.LineLength,
                    GameId               = command.GameId,
                    GameField            = nextGameField,
                    GameProcessStatistic = gameProcessStatistic
                };
            }

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

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

            return(this.View(answer));
        }