using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using DuckEmulator.HabboHotel.Inside.Storage.MySql.Database.Interfaces; using DuckEmulator.HabboHotel.Outside.Habbos; using DuckEmulator.HabboHotel.Outside.Habbos.HabStructure; using DuckEmulator.HabboHotel.Outside.Clients; namespace DuckEmulator.HabboHotel.Outside.Rooms { class RoomManager { internal Dictionary RoomModels; internal Dictionary AliveRooms; internal RoomManager() { AliveRooms = new Dictionary(); LoadRoomModels(); } internal void LoadRoomModels() { RoomModels = new Dictionary(); DataTable Data = null; using (IQueryAdapter Adapter = DuckEnvironment.GetAdapter()) { Adapter.setQuery("SELECT * FROM room_models"); Data = Adapter.getTable(); } if (Data == null) return; foreach (DataRow Row in Data.Rows) { RoomModels.Add((string)Row["id"], new RoomModel((string)Row["id"], (string)Row["map"], (int)Row["door_x"], (int)Row["door_y"], (int)Row["door_z"], (int)Row["door_rot"])); } foreach (Room Room in AliveRooms.Values.ToList()) { Room.GenerateMatrix(); } } internal Room LoadRoom(uint Id, bool Alive) { if (IsLoaded(Id)) return GetRoom(Id); DataRow Row = null; using (IQueryAdapter Adapter = DuckEnvironment.GetAdapter()) { Adapter.setQuery("SELECT * FROM private_rooms WHERE id = '" + Id + "' LIMIT 1"); Row = Adapter.getRow(); } if (Row == null) return null; Room Room = new Room((uint)Row["id"], Row["name"].ToString(), Row["owner"].ToString(), Row["room_model"].ToString(), (Row["allow_hidewall"].ToString() == "1"), int.Parse(Row["walls_type"].ToString()), int.Parse(Row["floor_type"].ToString()), int.Parse(Row["room_score"].ToString()), Row["description"].ToString(), Row["landscape"].ToString(), Row["wallpaper"].ToString(), Row["floor"].ToString(), (int)Row["state"], Row["password"].ToString(), (int)Row["max_users"], (int)Row["category"], Row["tags"].ToString(), (int)Row["icon_foreground"], (int)Row["icon_background"], Row["icon_items"].ToString(), (Row["allow_pets"].ToString() == "1"), (Row["allow_pets_eat"].ToString() == "1"), (Row["allow_walkthough"].ToString() == "1")); if (Alive) { Room.ThrowAlive(); AliveRooms.Add(Room.Id, Room); DuckEnvironment.WriteLine("Room throwed alive: " + Id, ConsoleColor.Blue); } return Room; } internal void UnloadRoom(uint Id) { if (!IsLoaded(Id)) return; GetRoom(Id).Dispose(); DuckEnvironment.WriteLine("Room disposed: " + Id, ConsoleColor.Blue); AliveRooms.Remove(Id); } internal List SeachRooms(string Query) { DataTable OwnerData = null; DataTable RoomData = null; var List = new Dictionary(); using (IQueryAdapter Adapter = DuckEnvironment.GetAdapter()) { Adapter.setQuery("SELECT id FROM private_rooms WHERE owner LIKE '%" + Query + "%'"); OwnerData = Adapter.getTable(); Adapter.setQuery("SELECT id FROM private_rooms WHERE name LIKE '%" + Query + "%'"); RoomData = Adapter.getTable(); } if (OwnerData != null) { foreach (DataRow Row in OwnerData.Rows) { if (!List.ContainsKey((uint)Row["id"])) { List.Add((uint)Row["id"],LoadRoom((uint)Row["id"],false)); } } } if (RoomData != null) { foreach (DataRow Row in RoomData.Rows) { if (!List.ContainsKey((uint)Row["id"])) { List.Add((uint)Row["id"], LoadRoom((uint)Row["id"], false)); } } } return List.Values.ToList(); } public uint GetRandomRoomIdWithUser(Client Client) { if (Client.GetHabbo() == null) { return 0; } var SpecialRooms = new List(); uint RoomId = new uint(); for (int i = 0; i < GetRooms().Count; i++) { Room Room = GetRooms()[i]; if (Room == null) { continue; } if (Room == null || !Room.IsAlive) { continue; } if (Room.Id == Client.GetHabbo().CurrentRoomId) { continue; } if (Room.UserCount > 0 && Room.Password == "") { SpecialRooms.Add(Room.Id); } } if (SpecialRooms.Count > 0) { Random Random = new Random(); int i = Random.Next(0, SpecialRooms.Count); RoomId = SpecialRooms[i]; } SpecialRooms = null; return RoomId; } internal List GetOwnRooms(string Owner) { var List = new List(); DataTable Data = null; using (IQueryAdapter Adapter = DuckEnvironment.GetAdapter()) { Adapter.setQuery("SELECT id FROM private_rooms WHERE owner = '" + Owner + "'"); Data = Adapter.getTable(); } if (Data != null) { foreach (DataRow Row in Data.Rows) { Room Room = LoadRoom(uint.Parse(Row[0].ToString()),false); if (Room != null) List.Add(Room); } } return List.ToList(); } internal List GetFavoriteRooms(HabboData Habbo) { var List = new List(); foreach (uint RoomId in Habbo.Favorites) { List.Add(LoadRoom(RoomId, false)); } return List.ToList(); } internal List GetLastVisted(HabboData Habbo) { var List = new Dictionary(); foreach (uint RoomId in Habbo.LastVisited) { if (!List.ContainsKey(RoomId)) { List.Add(RoomId, LoadRoom(RoomId, false)); } } return List.Values.ToList(); } internal List GetWhereFriendsAre(HabboData Habbo) { var List = new Dictionary(); foreach (Buddy Buddy in Habbo.GetMessenger().Buddies.Values.ToList()) { if (Buddy.InRoom) { if (!List.ContainsKey(Buddy.GetClient().GetHabbo().CurrentRoomId)) { List.Add(Buddy.GetClient().GetHabbo().CurrentRoomId, LoadRoom(Buddy.GetClient().GetHabbo().CurrentRoomId, false)); } } } return List.Values.ToList(); } internal List GetEvents(int Category) { var List = new Dictionary(); foreach (Room Room in AliveRooms.Values.ToList()) { if (!Room.HasEvent) continue; if (!List.ContainsKey(Room.Id) && (Room.Event.Category == Category || Category == -1)) { List.Add(Room.Id, Room); } } return List.Values.ToList(); } internal Room CreateRoom(string Name, string ModelName, string Username) { if (Name == "") return null; if (!ModelName.StartsWith("model_")) return null; using (IQueryAdapter Adapter = DuckEnvironment.GetAdapter()) { Adapter.runFastQuery("INSERT INTO private_rooms (name,owner,room_model) VALUES ('" + Name + "','" + Username + "','" + ModelName + "')"); Adapter.setQuery("SELECT id FROM private_rooms WHERE owner = '" + Username + "' ORDER BY id DESC"); try { return LoadRoom((uint)Adapter.getRow()[0], true); } catch { } } return null; } internal List GetRooms() { var List = new List(); foreach (Room Room in AliveRooms.Values.ToList()) { if (Room.UserCount > 0) { List.Add(Room); } } return List.ToList(); } internal List GetPopulairTags() { var List = new List(); foreach (Room Room in GetEvents(0).ToList()) { if (List.Count >= 50) continue; if (!List.Contains(Room.Event.Tags[0])) { List.Add(Room.Event.Tags[0]); } if (!List.Contains(Room.Event.Tags[1])) { List.Add(Room.Event.Tags[1]); } } return List.ToList(); } internal RoomModel RoomModel(string Model) { try { return RoomModels[Model]; } catch { return null; } } internal Room GetRoom(uint Id) { try { return AliveRooms[Id]; } catch { return null; } } internal Boolean IsLoaded(uint Id) { return (GetRoom(Id) != null); } internal void Dispose() { foreach (Room Room in AliveRooms.Values.ToList()) { Room.Dispose(); } AliveRooms = null; RoomModels = null; } } }