示例#1
0
        /// <summary>
        /// Deep copy BoatHit object
        /// </summary>
        /// <returns>Deep copy</returns>
        public BoatHit DeepCopy()
        {
            BoatHit newBoatHit = (BoatHit)this.MemberwiseClone();

            if (newBoatHit.Boat != null && newBoatHit.Boat.Player != null && newBoatHit.Boat.Player.Account != null)
            {
                newBoatHit.Boat.Player.Account.Password = null;
            }

            return(newBoatHit);
        }
        /// <summary>
        /// Shoot on a square in one a Player's board
        /// </summary>
        /// <param name="targetPlayerId">Id for target player (int)</param>
        /// <param name="shooterPlayerId">Id for shooter player (int)</param>
        /// <param name="position">Position to hit (Position)</param>
        /// <returns>Hit result (bool)</returns>
        public static bool Shoot(int targetPlayerId, int shooterPlayerId, string gameBoardKey, int x, int y)
        {
            GameBoard game = _context.GameBoards
                             .Include(g => g.Players)
                             .ThenInclude(p => p.AlreadyHitPositions)
                             .ThenInclude(ph => ph.Position)
                             .Where(g => g.Key == gameBoardKey)
                             .FirstOrDefault();

            if (game == null)
            {
                throw new Exception("Game doesn't exist!");
            }

            if (!game.Players.Any(p => p.Id == shooterPlayerId))
            {
                throw new Exception("You are not allowed to play on this board!");
            }

            if (shooterPlayerId != game.TurnPlayerId)
            {
                throw new Exception("Not your turn!");
            }

            Player playerVictim;
            Player playerAttacker;

            // Check which player is victim and which is attacker
            if (targetPlayerId == game.Players.ElementAt(0).Id)
            {
                playerVictim   = game.Players.ElementAt(0);
                playerAttacker = game.Players.ElementAt(1);
            }
            else if (targetPlayerId == game.Players.ElementAt(1).Id)
            {
                playerVictim   = game.Players.ElementAt(1);
                playerAttacker = game.Players.ElementAt(0);
            }
            else
            {
                throw new KeyNotFoundException("You can only shoot on player, which playes on your gameboard!");
            }

            Position position = GetPositionOnXAndY(x, y);

            // Check so victim is not already hit on this position
            if (playerVictim.AlreadyHitPositions.Find(a => a.PositionId == position.Id) != null)
            {
                throw new Exception("Position is already hit!");
            }

            // Add a hit to victim
            PlayerHit playerHit = new PlayerHit
            {
                PositionId = position.Id,
                PlayerId   = playerVictim.Id
            };

            _context.PlayerHits.Add(playerHit);

            // Update GameBoard's last update to now
            game.LastUpdate = DateTime.Now;

            Boat boat;

            bool result = IsAnyBoatHere(targetPlayerId, position, out boat);

            if (result)
            {
                BoatHit boatHit = new BoatHit
                {
                    PositionId = position.Id,
                    BoatId     = boat.Id
                };

                _context.BoatHits.Add(boatHit);
            }
            else
            {
                game.TurnPlayer = playerVictim;
            }

            _context.Update(game);
            _context.SaveChanges();

            return(result);
        }