示例#1
0
        /// <summary>
        /// Removes saved room with all items and unseen rooms connected to it.
        /// </summary>
        /// <param name="room">
        /// Room to remove.
        /// </param>
        public async Task Remove(RoomModel room)
        {
            //to not nest transactions
            bool transaction;

            if (_gameDataAccess.Transaction == false)
            {
                _gameDataAccess.StartTransaction();
                transaction = true;
            }
            else
            {
                transaction = false;
            }

            var model = new Room
            {
                RoomID = room.Id
            };

            //get all equipement in room
            var weapons = await _gameDataAccess.GetByRoom(new Weapon { RoomID = model.RoomID });

            var armours = await _gameDataAccess.GetByRoom(new Armour { RoomID = model.RoomID });

            var potions = await _gameDataAccess.GetByRoom(new Potion { RoomID = model.RoomID });

            //and remove it
            foreach (var weapon in weapons)
            {
                await _gameDataAccess.Remove(weapon);
            }
            foreach (var armour in armours)
            {
                await _gameDataAccess.Remove(armour);
            }
            foreach (var potion in potions)
            {
                await _gameDataAccess.Remove(potion);
            }
            //get list of all adjacent rooms
            var list = await _gameDataAccess.GetCorridors(model);

            foreach (var corridor in list)
            {
                int id;
                if (corridor.RoomID == model.RoomID)
                {
                    id = corridor.RoomID1;
                }
                else
                {
                    id = corridor.RoomID;
                }

                var tmp = new Room
                {
                    RoomID = id
                };

                tmp = await _gameDataAccess.Get(tmp);

                //if the room was not seen
                if (tmp.Seen == 0)
                {
                    //remove it
                    await _gameDataAccess.Remove(tmp);
                }
            }
            //remove all connectiions to the room
            await _gameDataAccess.RemoveCorridors(model);

            await _gameDataAccess.Remove(model);

            if (transaction)
            {
                _gameDataAccess.Save();
            }
        }