public BoardRecord(IBattleshipDbContext context)
 {
     _context = context;
     // Currently these defaults are specified on the CreateBoardCommand object. As we are bypassing that to insert this record, we need to define defaults again here.
     // TODO: is there a better place to define them, so it will be only once? Or do they need to be constants/config somewhere?
     _board = new Board
     {
         DimensionX = 10,
         DimensionY = 10
     };
 }
예제 #2
0
        public CommandValidator(IBattleshipDbContext context)
        {
            _context = context;

            RuleFor(x => x.AttackerPlayerId)
            .GreaterThan(0)
            .NotEmpty();

            RuleFor(x => x.BoardId)
            .GreaterThan(0)
            .NotEmpty();

            RuleFor(x => x.AttackX)
            .GreaterThan(0)
            .WithMessage("Attack X must be greater than zero.")
            .Custom((attackX, context) =>
            {
                var command = context.InstanceToValidate as Command;
                var board   = _context.Boards.Find(command.BoardId);
                if (board == null)
                {
                    return;                     // If no board found, cannot continue with validation.
                }
                if (attackX > board.DimensionX)
                {
                    context.AddFailure($"Attack X ({attackX}) cannot be larger than board dimension ({board.DimensionX})");
                }
            })
            .NotEmpty();

            RuleFor(x => x.AttackY)
            .GreaterThan(0)
            .WithMessage("Attack Y must be greater than zero.")
            .Custom((attackY, context) =>
            {
                var command = context.InstanceToValidate as Command;
                var board   = _context.Boards.Find(command.BoardId);
                if (board == null)
                {
                    return;                     // If no board found, cannot continue with validation.
                }
                if (attackY > board.DimensionY)
                {
                    context.AddFailure($"Attack Y ({attackY}) cannot be larger than board dimension ({board.DimensionY})");
                }
            })
            .NotEmpty();
        }
예제 #3
0
 public ShipPartRecord(IBattleshipDbContext context)
 {
     _context  = context;
     _shipPart = new ShipPart();
 }
예제 #4
0
 public ShotRecord(IBattleshipDbContext context)
 {
     _context = context;
     _shot    = new Shot();
 }
예제 #5
0
 public Handler(IBattleshipDbContext context, IMapper mapper)
 {
     _context = context;
     _mapper  = mapper;
 }
 public GameRecord(IBattleshipDbContext context)
 {
     _context = context;
     _game    = new Game();
 }
예제 #7
0
 public Handler(IBattleshipDbContext context) => _context = context;
예제 #8
0
 public PlayerRecord(IBattleshipDbContext context)
 {
     _context = context;
     _player  = new Player();
 }
예제 #9
0
 public ShipRecord(IBattleshipDbContext context)
 {
     _context = context;
     _ship    = new Ship();
 }
        public CommandValidator(IBattleshipDbContext context)
        {
            _context = context;

            RuleFor(x => x.BoardId)
            .GreaterThan(0)
            .NotEmpty();

            RuleFor(x => x.BowX)
            .GreaterThan(0)
            .WithMessage("Bow X must be greater than zero.")
            .Custom((bowX, context) =>
            {
                var command = context.InstanceToValidate as Command;
                var board   = _context.Boards.Find(command.BoardId);
                if (board == null)
                {
                    return;                     // If no board found, cannot continue with validation.
                }
                if (bowX > board.DimensionX)
                {
                    context.AddFailure($"Bow X ({bowX}) cannot be larger than board dimension ({board.DimensionX})");
                }
                if (command.Orientation == ShipOrientation.Horizontal)
                {
                    if (bowX + command.Length > board.DimensionX)
                    {
                        context.AddFailure("Ship too large to fit on board.");
                    }
                }
            })
            .NotEmpty();

            RuleFor(x => x.BowY)
            .GreaterThan(0)
            .WithMessage("Bow Y must be greater than zero.")
            .Custom((bowY, context) =>
            {
                var command = context.InstanceToValidate as Command;
                var board   = _context.Boards.Find(command.BoardId);
                if (board == null)
                {
                    return;                     // If no board found, cannot continue with validation.
                }
                if (bowY > board.DimensionX)
                {
                    context.AddFailure($"Bow Y ({bowY}) cannot be larger than board dimension ({board.DimensionY})");
                }
                if (command.Orientation == ShipOrientation.Vertical)
                {
                    if (bowY + command.Length > board.DimensionY)
                    {
                        context.AddFailure("Ship too large to fit on board.");
                    }
                }
            })
            .NotEmpty();

            RuleFor(x => x.Length)
            .GreaterThanOrEqualTo(2)
            .WithMessage("Minimum length of Ship is 2.")
            .LessThanOrEqualTo(4)
            .WithMessage("Maximum length of Ship is 4.")
            .NotEmpty();

            RuleFor(x => x.Orientation).IsInEnum().NotNull();
        }