Exemplo n.º 1
0
        public async Task <int> CreateMatch()
        {
            var entity = new Match();
            await _battleShipDbContext.Matches.AddAsync(entity);

            await _battleShipDbContext.SaveChangesAsync();

            return(entity.MatchId);
        }
        public async Task <int> AddBoard(Board board)
        {
            try
            {
                _context.Boards.Add(board);
                return(await _context.SaveChangesAsync());
            }
            catch (Exception e) when(e.HResult == -2147024809)  // Game Already Exists in the Database
            {
                _logger.LogError("Board Already Exists", board);
                throw new DuplicateEntryException("Board");
            }

            catch (Exception e)
            {
                _logger.LogError("Insert Board Failed", board);
                throw new InsertFailedException("Board", e.Message);
            }
        }
        public async Task <Board> SetBoard(Match match, int SizeX, int SizeY)
        {
            var entity = new Board()
            {
                Match = match,
                X     = SizeX,
                Y     = SizeY
            };
            await _battleShipDbContext.Boards.AddAsync(entity);

            await _battleShipDbContext.SaveChangesAsync();

            return(entity);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> OnPostSubmit()
        {
            if (Game != null)
            {
                _context.Games?.Remove(Game);
            }

            Game newGame = new Game();

            newGame.Width           = int.Parse(Request.Form["gameWidth"]);
            newGame.Heigth          = int.Parse(Request.Form["gameHeigth"]);
            newGame.CarrierCount    = int.Parse(Request.Form["CarrierNumber"]);
            newGame.SubmarineCount  = int.Parse(Request.Form["SubmarineNumber"]);
            newGame.CruiserCount    = int.Parse(Request.Form["CruiserNumber"]);
            newGame.BattleshipCount = int.Parse(Request.Form["BattleshipNumber"]);
            newGame.PatrolCount     = int.Parse(Request.Form["PatrolNumber"]);
            newGame.CanGoToAnother  = Request.Form["can"] == "on";

            _context.Games?.Add(newGame);
            _context.SaveChanges();

            foreach (string shipType in ShipsTypes)
            {
                Ship newShip = new Ship();
                newShip.Type     = shipType;
                newShip.Length   = int.Parse(Request.Form[shipType + "Size"]);
                newShip.GameId   = newGame.Id;
                newShip.IsPlayer = true;
                shipsForSquare.Add(new ShipVM(newShip, int.Parse(Request.Form[shipType + "Number"])));
                _context.Ships?.Add(newShip);
            }
            foreach (string shipType in ShipsTypes)
            {
                Ship newShip = new Ship();
                newShip.Type     = shipType;
                newShip.Length   = int.Parse(Request.Form[shipType + "Size"]);
                newShip.GameId   = newGame.Id;
                newShip.IsPlayer = false;
                _context.Ships?.Add(newShip);
            }

            await _context.SaveChangesAsync();

            if (!CheckSquare(newGame.Width * newGame.Heigth, shipsForSquare, newGame.CanGoToAnother))
            {
                return(Redirect("./Settings?gameId=" + newGame.Id + "&error=on"));
            }

            return(Redirect("./OriginalSettingsGame?gameId=" + newGame.Id + "&settings=custom"));
        }
Exemplo n.º 5
0
        public async Task <int> CreateShip(CreateShipDto createShip)
        {
            var boardEntity = await _battleShipDbContext.Boards.FindAsync(createShip.BoardId);

            if (boardEntity == null)
            {
                throw new NotFoundException(nameof(Board), createShip.BoardId);
            }
            List <int> lstX = new List <int>();
            List <int> lstY = new List <int>();

            if (createShip.Orientation == BattleShipOrientation.Horizontal)
            {
                for (int t = 0; t < createShip.ShipLength; t++)
                {
                    lstY.Add(createShip.StartY + t);
                }
            }
            else
            {
                for (int t = 0; t < createShip.ShipLength; t++)
                {
                    lstX.Add(createShip.StartX + t);
                }
            }
            var overridenParts = _battleShipDbContext.BattleShips.Include(x => x.BattleShipCoordinates).
                                 Where(x => x.Board.BoardId == boardEntity.BoardId).SelectMany(x => x.BattleShipCoordinates, (p, q) => new
            {
                q.X,
                q.Y
            }).Where(part => lstX.Contains(part.X) && lstY.Contains(part.Y))?.ToList();

            if (overridenParts.Count > 0)
            {
                throw new BattleShipOverrideException();
            }
            var shipEntity = new BattleShip()
            {
                Board = boardEntity
            };
            await _battleShipDbContext.BattleShips.AddAsync(shipEntity);

            if (createShip.Orientation == BattleShipOrientation.Horizontal)
            {
                foreach (var item in lstX)
                {
                    await _battleShipDbContext.BattleShipCoordinates.AddAsync(new BattleShipCoordinate()
                    {
                        BattleShip = shipEntity,
                        X          = item,
                        Y          = createShip.StartY
                    });
                }
            }
            else
            {
                foreach (var item in lstY)
                {
                    await _battleShipDbContext.BattleShipCoordinates.AddAsync(new BattleShipCoordinate()
                    {
                        BattleShip = shipEntity,
                        X          = createShip.StartX,
                        Y          = item
                    });
                }
            }
            await _battleShipDbContext.SaveChangesAsync();

            return(shipEntity.BattleShipId);
        }