Esempio n. 1
0
        /// <summary>
        /// Unloads the room.
        /// </summary>
        /// <param name="room">The room.</param>
        /// <param name="reason">The reason.</param>
        internal void UnloadRoom(Room room, string reason)
        {
            if (room == null || room.RoomData == null)
            {
                return;
            }

            if (room.RoomData.Description.Contains("SPACE"))
            {
                spaceManager.initiated = false;
            }

            if (room.RoomData.Description.Contains("TREES"))
            {
                woodManager.initiated = false;
            }

            if (room.RoomData.Description.Contains("FARM"))
            {
                farmingManager.initiated = false;
            }

            if (room.RoomData.Description.Contains("CASINO"))
            {
                SlotsManager.initiated = false;
            }

            if (Plus.GetGame().MafiaWars.Started)
            {
                if (room.RoomData.Description.Contains("MAFIAWARS"))
                {
                    return;
                }
            }

            if (room.RoomId == ColourManager.GameLobby && ColourManager.LobbyLoop != null)
            {
                ColourManager.LobbyLoop.On = false;
                ColourManager.LobbyLoop    = null;
            }

            if (room.RoomData.Description.Contains("MAFIAWARLOBBY") && Plus.GetGame().MafiaWars.LobbyLoop != null)
            {
                Plus.GetGame().MafiaWars.LobbyLoop.On = false;
                Plus.GetGame().MafiaWars.LobbyLoop = null;
            }


            if (Plus.GetGame().GetNavigator().PrivateCategories.Contains(room.RoomData.Category))
            {
                ((FlatCat)Plus.GetGame().GetNavigator().PrivateCategories[room.RoomData.Category]).UsersNow -=
                    room.UserCount;
            }
            room.RoomData.UsersNow = 0;
            var state = "open";

            if (room.RoomData.State == 1)
            {
                state = "locked";
            }
            else if (room.RoomData.State > 1)
            {
                state = "password";
            }
            using (var queryReactor = Plus.GetDatabaseManager().GetQueryReactor())
            {
                queryReactor.SetQuery(
                    "UPDATE rooms_data SET caption = @caption, description = @description, password = @password, category = " +
                    room.RoomData.Category + ", state = '" + state + "', tags = @tags, users_now = '0', users_max = " +
                    room.RoomData.UsersMax + ", allow_pets = '" + Plus.BoolToEnum(room.RoomData.AllowPets) +
                    "', allow_pets_eat = '" +
                    Plus.BoolToEnum(room.RoomData.AllowPetsEating) + "', allow_walkthrough = '" +
                    Plus.BoolToEnum(room.RoomData.AllowWalkThrough) +
                    "', hidewall = '" + Plus.BoolToEnum(room.RoomData.HideWall) + "', floorthick = " +
                    room.RoomData.FloorThickness +
                    ", wallthick = " + room.RoomData.WallThickness + ", mute_settings='" + room.RoomData.WhoCanMute +
                    "', kick_settings='" + room.RoomData.WhoCanKick + "',ban_settings='" + room.RoomData.WhoCanBan +
                    "', walls_height = '" + room.RoomData.WallHeight +
                    "', chat_type = @chat_t,chat_balloon = @chat_b,chat_speed = @chat_s,chat_max_distance = @chat_m,chat_flood_protection = @chat_f, trade_state = '" +
                    room.RoomData.TradeState + "' WHERE id = " +
                    room.RoomId);
                queryReactor.AddParameter("caption", room.RoomData.Name);
                queryReactor.AddParameter("description", room.RoomData.Description);
                queryReactor.AddParameter("password", room.RoomData.PassWord);
                queryReactor.AddParameter("tags", string.Join(",", room.RoomData.Tags));
                queryReactor.AddParameter("chat_t", room.RoomData.ChatType);
                queryReactor.AddParameter("chat_b", room.RoomData.ChatBalloon);
                queryReactor.AddParameter("chat_s", room.RoomData.ChatSpeed);
                queryReactor.AddParameter("chat_m", room.RoomData.ChatMaxDistance);
                queryReactor.AddParameter("chat_f", room.RoomData.ChatFloodProtection);
                queryReactor.RunQuery();
            }

            Room junkRoom;

            if (!LoadedRooms.TryRemove(room.RoomId, out junkRoom))
            {
                return;
            }

            Out.WriteLine(string.Format("Room '{0}' [{1}] was unloaded! Reason: " + reason, room.RoomData.Name, room.RoomId),
                          "", ConsoleColor.DarkGray);
            foreach (var current in room.GetRoomUserManager().UserList.Values.Where(current => current != null))
            {
                if (current.IsPet)
                {
                    using (var queryReactor = Plus.GetDatabaseManager().GetQueryReactor())
                    {
                        queryReactor.SetQuery("UPDATE bots SET x=@x, y=@y, z=@z WHERE id=@id LIMIT 1;");
                        queryReactor.AddParameter("x", current.X);
                        queryReactor.AddParameter("y", current.Y);
                        queryReactor.AddParameter("z", current.Z);
                        queryReactor.AddParameter("id", current.PetData.PetId);
                        queryReactor.RunQuery();

                        current.BotAI.Dispose();
                    }
                }
                else if (current.IsBot)
                {
                    using (var queryReactor = Plus.GetDatabaseManager().GetQueryReactor())
                    {
                        queryReactor.SetQuery(
                            "UPDATE bots SET x=@x, y=@y, z=@z, name=@name, motto=@motto, look=@look, rotation=@rotation, dance=@dance WHERE id=@id LIMIT 1;");
                        queryReactor.AddParameter("name", current.BotData.Name);
                        queryReactor.AddParameter("motto", current.BotData.Motto);
                        queryReactor.AddParameter("look", current.BotData.Look);
                        queryReactor.AddParameter("rotation", current.BotData.Rot);
                        queryReactor.AddParameter("dance", current.BotData.DanceId);
                        queryReactor.AddParameter("x", current.X);
                        queryReactor.AddParameter("y", current.Y);
                        queryReactor.AddParameter("z", current.Z);
                        queryReactor.AddParameter("id", current.BotData.BotId);
                        queryReactor.RunQuery();
                    }

                    current.BotAI.Dispose();
                }
                else
                {
                    if (current.GetClient() == null)
                    {
                        continue;
                    }
                    room.GetRoomUserManager().RemoveUserFromRoom(current.GetClient(), true, false);
                    current.GetClient().CurrentRoomUserId = -1;
                }
            }

            lock (room.RoomData.RoomChat)
            {
                foreach (Chatlog Chat in room.RoomData.RoomChat)
                {
                    Chat.Save(room.RoomId);
                }
            }

            room.Destroy();
        }