public PlayerVsPlayerMakeStepCommandResult ExecuteCommand(PlayerVsPlayerMakeStepCommand command)
        {
            PlayerVsPlayerMakeStepCommandResult 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 PlayerVsPlayerMakeStepCommandResult
                    {
                        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 PlayerVsPlayerMakeStepCommandResult
                {
                    CellSize             = GameFieldConstants.LineLength,
                    GameId               = command.GameId,
                    GameField            = nextGameField,
                    GameProcessStatistic = gameProcessStatistic
                };
            }

            return(result);
        }
示例#2
0
        public ActionResult MakeStep(PlayerVsPlayerMakeStepCommand command)
        {
            var answer = this.playerVsPlayerGameCommandHandler.ExecuteCommand(command);

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

            return(this.View(answer));
        }