コード例 #1
0
        /// <summary>
        /// Verifies and stores the map in the database
        /// </summary>
        /// <param name="key">Player secret key</param>
        /// <param name="cells">Selected ship cells on the map</param>
        public void UpdateMap(string key, List <Point> cells)
        {
            var player = _entities.Players.Include(t => t.Cells).FirstOrDefault(t => t.Key == key);

            if (player == null)
            {
                throw new KeyNotFoundException("Player with key " + key + " not found.");
            }

            if (player.MapValidated)
            {
                throw new EntityValidationException("Map changes are not allowed.");
            }

            var shipMap = new ShipMap();

            shipMap.ParseShips(cells);
            shipMap.Validate();

            player.MapValidated = true;

            var newCells = cells.Select(t => new ShipCell(t.X, t.Y)
            {
                PlayerId = player.Id
            });

            _entities.ShipCells.RemoveRange(player.Cells);
            _entities.ShipCells.AddRange(newCells);

            using (var transaction = _entities.Database.BeginTransaction())
            {
                try
                {
                    _entities.SaveChanges();
                    transaction.Commit();
                }
                catch (DbEntityValidationException exception)
                {
                    transaction.Rollback();
                    throw new EntityValidationException(exception);
                }
            }
        }