Ejemplo n.º 1
0
        private bool PerformMove(int userId, int gameId, int figureId, int toRow, int toCol)
        {
            GameRepository gameRepository = data.GetGameRepository();
            Game           game           = gameRepository.Get(gameId);

            if (game.UserIdInTurn != userId)
            {
                return(false);
            }

            FigureRepository figureRepository = data.GetFigureRepository();
            Figure           figure           = figureRepository.Get(figureId);

            if (!ValidateMove(figure, toRow, toCol))
            {
                return(false);
            }

            Figure hitFigure = figureRepository.GetFigureByGameAndPosition(gameId, toRow, toCol);

            if (hitFigure != null && hitFigure.IsWhite != figure.IsWhite)
            {
                figureRepository.Delete(hitFigure);
            }
            else if (hitFigure != null && hitFigure.IsWhite == figure.IsWhite)
            {
                return(false);
            }

            figure.PositionRow = toRow;
            figure.PositionCol = toCol;
            figureRepository.Update(figureId, figure);

            if (game.WhitePlayerId == game.UserIdInTurn)
            {
                game.UserIdInTurn = game.BlackPlayerId;
            }
            else
            {
                game.UserIdInTurn = game.WhitePlayerId;
            }

            gameRepository.Update(game.Id, game);

            UserRepository userRepository = data.GetUserRepository();
            User           userInTurn     = userRepository.Get(game.UserIdInTurn.GetValueOrDefault(0));

            var gameStartedMessageText = string.Format("{0} it is your move {1}", userInTurn.Nickname, game.Name);

            MessagesRepository messageRepository = data.GetMessagesRepository();

            messageRepository.CreateGameMessage(game.Id, game.UserIdInTurn.GetValueOrDefault(0), gameStartedMessageText, UserMessageTypeGameMove);

            return(true);
        }