public async Task CreateFigureAsyncWithInvalidData_ShouldThrowException(FigureRequest request, Type expectedException)
        {
            //arrange
            var figureRepository = new FigureRepository(null);
            var figureService    = new FigureService(figureRepository);
            var controller       = new FigureController(figureService);

            //act & assert
            var exception = await Assert.ThrowsAsync(expectedException, async() => await controller.CreateFigureAsync(request));
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        protected FigureControllerTestsAbstract()
        {
            var figureRecord  = GetQueryableMockDbSet(_records, x => x.Id = _currentId);
            var dbContextMock = new Mock <IDatabaseContext>();

            dbContextMock.SetupGet(x => x.Figures).Returns(figureRecord);

            var figureRepository = new FigureRepository(dbContextMock.Object);
            var figureService    = new FigureService(figureRepository);

            _controller = new FigureController(figureService);
        }
Ejemplo n.º 4
0
        public List <FigureModel> GetFigure(string sessionKey, int gameId)
        {
            //hard code
            //  return this.data.figureRepository.GetGameFigures(gameId);
            FigureRepository figureRepository = data.GetFigureRepository();
            var figures = figureRepository.GetGameFigures(gameId);
            List <FigureModel> returnList = new List <FigureModel>();

            foreach (var figure in figures)
            {
                var checkListForDuplicate = returnList.Find(x => x.PosCol == figure.PositionCol && x.PosRow == figure.PositionRow);

                if (figure.GameId == gameId && checkListForDuplicate == null)
                {
                    returnList.Add(FigureModel.ConvertToModel(figure));
                }
            }
            return(returnList);
        }
Ejemplo n.º 5
0
        private bool ValidateMove(Figure figure, int toRow, int toCol)
        {
            PositionModel toPosition = new PositionModel()
            {
                Row = toRow, Col = toCol
            };
            PositionModel fromPosition = new PositionModel()
            {
                Row = figure.PositionRow, Col = figure.PositionCol
            };

            if (toRow > 8 || toCol > 8)
            {
                return(false);
            }

            if (toRow < 0 || toCol < 0)
            {
                return(false);
            }

            var currentFigure = FigureFactory.GetFigure(figure);

            List <PositionModel> possibleMoves = currentFigure.GetPossibleMoves();
            List <PositionModel> possibleHits  = currentFigure.GetPossibleHits();

            var checkedPosMoves = possibleMoves.Find(x => x.Col == toPosition.Col && x.Row == toPosition.Row);
            var checkedPosHits  = possibleHits.Find(x => x.Col == toPosition.Col && x.Row == toPosition.Row);

            if (checkedPosMoves == null && checkedPosHits == null)
            {
                return(false);
            }


            if (!currentFigure.CanJump())
            {
                FigureRepository figureRepository = data.GetFigureRepository();

                HashSet <PositionModel> movePath       = GetMovePath(fromPosition, toPosition);
                IQueryable <Figure>     allGameFigures = figureRepository.GetGameFigures(figure.GameId);

                HashSet <PositionModel> gameFiguresPositions = new HashSet <PositionModel>();
                foreach (Figure gameFig in allGameFigures)
                {
                    gameFiguresPositions.Add(new PositionModel()
                    {
                        Row = gameFig.PositionRow,
                        Col = gameFig.PositionCol
                    });
                }

                var positionIntersection = movePath.Intersect <PositionModel>(gameFiguresPositions);

                if (positionIntersection.Count() > 0)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 6
0
 public FigureRepositoryTests()
 {
     _repository = new FigureRepository();
 }