uberEmu RoomManager Stabilized C#

Results 1 to 9 of 9
  1. #1
    Account Upgraded | Title Enabled! wichard is offline
    MemberRank
    Jul 2009 Join Date
    The NetherlandsLocation
    649Posts

    uberEmu RoomManager Stabilized C#

    Hello ragezone, today i'll release my code of the roommanager.

    What's new?
    - Rooms will be unloaded faster, for better preforming.
    - Better ProcessEngine, Recoded and cleaned up.
    - Some lists edited.
    - Enabled My ItemsRollerCycle (Not Released Yet) (So dont enable)

    Replace the whole RoomManager.cs with this code:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Threading;
    
    using Uber.HabboHotel.Items;
    using Uber.HabboHotel.GameClients;
    using Uber.Storage;
    
    namespace Uber.HabboHotel.Rooms
    {
        class RoomManager
        {
            public int MAX_PETS_PER_ROOM = 25;
            private bool Breakz0nesRollerCycle;
            private Dictionary<Room, int> Penalties; 
            private List<Room> Rooms;
            private Dictionary<string, RoomModel> Models;
            private List<TeleUserData> TeleActions;
            private Thread CycleThread;
    
            private List<uint> RoomsToUnload;
    
            public int LoadedRoomsCount
            {
                get
                {
                    return this.Rooms.Count;
                }
            }
    
            public RoomManager()
            {
                this.Rooms = new List<Room>();
                this.Models = new Dictionary<string, RoomModel>();
                this.TeleActions = new List<TeleUserData>();
                this.Penalties = new Dictionary<Room, int>();
    
                this.Breakz0nesRollerCycle = false;
    
                this.RoomsToUnload = new List<uint>();
    
                this.CycleThread = new Thread(Cycle);
                this.CycleThread.Priority = ThreadPriority.AboveNormal;
                this.CycleThread.Start();
            }
    
            public void AddTeleAction(TeleUserData Act)
            {
                lock (this.TeleActions)
                {
                    this.TeleActions.Add(Act);
                }
            }
    
            public void Cycle()
            {
                while (true)
                {
                    DateTime ExecutionStart = DateTime.Now;
    
                    try
                    {
                        if (this.Rooms.Count > 0)
                        {
                            lock (this.Rooms)
                            {
                                foreach (Room Room in this.Rooms)
                                {
                                    if (Room.UserList.Count > 0)
                                    {
                                        Room.ProcessRoom();
                                    }
                                    else
                                    {
                                        lock (this.Penalties)
                                        {
                                            if (this.Penalties.ContainsKey(Room))
                                            {
                                                if (this.Penalties[Room] >= 10)
                                                    RequestRoomUnload(Room.RoomId, true);
                                                else
                                                    this.Penalties[Room] = this.Penalties[Room] + 1;
                                            }
                                            else this.Penalties.Add(Room, 1);
                                        }
                                    }
                                }
                            }
                        }
    
                        if (this.RoomsToUnload.Count > 0)
                        {
                            lock (this.RoomsToUnload)
                            {
                                foreach (uint RoomId in this.RoomsToUnload)
                                {
                                    UnloadRoom(RoomId);
                                }
    
                                this.RoomsToUnload.Clear();
                            }
                        }
    
                        if (this.TeleActions.Count > 0)
                        {
                            lock (this.TeleActions)
                            {
                                foreach (TeleUserData TeleUserData in this.TeleActions)
                                {
                                    TeleUserData.Execute();
                                }
    
                                this.TeleActions.Clear();
                            }
                        }
                    }
    
                    catch (InvalidOperationException)
                    {
                        // debug sucker
                    }
    
                    finally
                    {
                        DateTime ExecutionComplete = DateTime.Now;
                        TimeSpan Diff = ExecutionComplete - ExecutionStart;
    
                        double sleepTime = 500 - Diff.TotalMilliseconds;
    
                        if (sleepTime < 0)
                        {
                            sleepTime = 0;
                        }
    
                        if (sleepTime > 500)
                        {
                            sleepTime = 500;
                        }
    
                        Thread.Sleep((int)Math.Floor(sleepTime));
                    }
                }
            }
    
            public List<Room> GetEventRoomsForCategory(int Category)
            {
                List<Room> EventRooms = new List<Room>();
    
                lock (this.Rooms)
                {
                    foreach (Room Room in this.Rooms)
                    {
                        if (Room.Event == null)
                            continue;
    
                        if (Category > 0 && Room.Event.Category != Category)
                            continue;
    
                        EventRooms.Add(Room);
    
                    }
                }
    
                return EventRooms;
            }
    
            public void LoadModels()
            {
                DataTable Data = null;
                Models.Clear();
    
                using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                {
                    Data = dbClient.ReadDataTable("SELECT id,door_x,door_y,door_z,door_dir,heightmap,public_items,club_only FROM room_models");
                }
    
                if (Data == null)
                {
                    return;
                }
    
                foreach (DataRow Row in Data.Rows)
                {
                    Models.Add((string)Row["id"], new RoomModel((string)Row["id"], (int)Row["door_x"],
                        (int)Row["door_y"], (Double)Row["door_z"], (int)Row["door_dir"], (string)Row["heightmap"],
                        (string)Row["public_items"], UberEnvironment.EnumToBool(Row["club_only"].ToString())));
                }
            }
    
            public RoomModel GetModel(string Model)
            {
                if (Models.ContainsKey(Model))
                {
                    return Models[Model];
                }
    
                return null;
            }
    
            public RoomData GenerateNullableRoomData(uint RoomId)
            {
                if (GenerateRoomData(RoomId) != null)
                {
                    return GenerateRoomData(RoomId);
                }
    
                RoomData Data = new RoomData();
                Data.FillNull(RoomId);
                return Data;
            }
    
            public RoomData GenerateRoomData(uint RoomId)
            {
                RoomData Data = new RoomData();
    
                if (IsRoomLoaded(RoomId))
                {
                    Data.Fill(GetRoom(RoomId));
                }
                else
                {
                    DataRow Row = null;
    
                    using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                    {
                        Row = dbClient.ReadDataRow("SELECT * FROM rooms WHERE id = '" + RoomId + "' LIMIT 1");
                    }
    
                    if (Row == null)
                    {
                        return null;
                    }
    
                    Data.Fill(Row);
                }
    
                return Data;
            }
    
            public bool IsRoomLoaded(uint RoomId)
            {
                lock (this.Rooms)
                {
                    foreach (Room Room in this.Rooms)
                    {
                        if (Room.RoomId == RoomId)
                            return true;
                    }
                }
    
                return false;
            }
    
            public void LoadRoom(uint Id)
            {
    
                if (IsRoomLoaded(Id))
                {
                    return;
                }
    
                RoomData Data = GenerateRoomData(Id);
    
                if (Data == null)
                {
                    return;
                }
    
                Room Room = new Room(Data.Id, Data.Name, Data.Description, Data.Type, Data.Owner, Data.Category, Data.State,
                    Data.UsersMax, Data.ModelName, Data.CCTs, Data.Score, Data.Tags, Data.AllowPets, Data.AllowPetsEating,
                    Data.AllowWalkthrough, Data.Hidewall, Data.Icon, Data.Password, Data.Wallpaper, Data.Floor, Data.Landscape);
    
                lock (this.Rooms)
                    Rooms.Add(Room);
    
                Room.InitBots();
                Room.InitPets();
    
                if (Breakz0nesRollerCycle)
                {
                    Room.RollerCycle = new Thread(Room.CycleRollers);
                    Room.RollerCycle.Priority = ThreadPriority.Lowest;
                    Room.RollerCycle.Start();
                }
    
                UberEnvironment.GetLogging().WriteLine("RoomManager Succesfully Loaded Room : " + Room.Name + " @" + DateTime.Now + "");
            }
    
            public void RequestRoomUnload(uint Id,bool Penalty)
            {
                if (!IsRoomLoaded(Id))
                {
                    return;
                }
    
                GetRoom(Id).KeepAlive = false;
    
                lock (RoomsToUnload)
                    RoomsToUnload.Add(Id);
    
                if (Penalty)
                    lock (this.Penalties)
                        this.Penalties.Remove(GetRoom(Id));
            }
    
            public void UnloadRoom(uint Id)
            {
                Room Room = GetRoom(Id);
    
                if (Room == null)
                {
                    return;
                }
    
                Room.Destroy();
    
                lock (this.Rooms)
                    this.Rooms.Remove(Room);
    
                UberEnvironment.GetLogging().WriteLine("RoomManager Succesfully UnLoaded Room : " + Room.Name + " @" + DateTime.Now + "");
            }
    
    
            public Room GetRoom(uint RoomId)
            {
                lock (this.Rooms)
                {
                    foreach (Room Room in this.Rooms)
                    {
                        if (Room.RoomId == RoomId)
                        {
                            return Room;
                        }
                    }
                }
    
                return null;
            }
    
            public RoomData CreateRoom(GameClient Session, string Name, string Model)
            {
                Name = UberEnvironment.FilterInjectionChars(Name);
    
                if (!Models.ContainsKey(Model))
                {
                    Session.SendNotif("Sorry, this room model has not been added yet. Try again later.");
                    return null;
                }
    
                if (Models[Model].ClubOnly && !Session.GetHabbo().HasFuse("fuse_use_special_room_layouts"))
                {
                    Session.SendNotif("You must be an Club member to use that room layout.");
                    return null;
                }
    
                if (Name.Length < 3)
                {
                    Session.SendNotif("Room name is too short for room creation!");
                    return null;
                }
    
                using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                {
                    dbClient.AddParamWithValue("caption", Name);
                    dbClient.AddParamWithValue("model", Model);
                    dbClient.AddParamWithValue("username", Session.GetHabbo().Username);
                    dbClient.ExecuteQuery("INSERT INTO rooms (roomtype,caption,owner,model_name) VALUES ('private',@caption,@username,@model)");
                }
    
                uint RoomId = 0;
    
                using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                {
                    dbClient.AddParamWithValue("caption", Name);
                    dbClient.AddParamWithValue("username", Session.GetHabbo().Username);
                    RoomId = (uint)dbClient.ReadDataRow("SELECT id FROM rooms WHERE owner = @username AND caption = @caption ORDER BY id DESC")[0];
                }
    
                return GenerateRoomData(RoomId);
            }
        }
    
        class TeleUserData
        {
            private RoomUser User;
            private uint RoomId;
            private uint TeleId;
    
            public TeleUserData(RoomUser User, uint RoomId, uint TeleId)
            {
                this.User = User;
                this.RoomId = RoomId;
                this.TeleId = TeleId;
            }
    
            public void Execute()
            {
                if (User == null || User.IsBot)
                {
                    return;
                }
    
                User.GetClient().GetHabbo().IsTeleporting = true;
                User.GetClient().GetHabbo().TeleporterId = TeleId;
                User.GetClient().GetMessageHandler().PrepareRoomForUser(RoomId, "");
            }
        }
    }
    Don't forget to push THE BUTTON (Thanks) !


  2. #2
    sexiess is a sin. Subway is offline
    MemberRank
    Jun 2010 Join Date
    2,491Posts

    Re: uberEmu RoomManager Stabilized C#

    Nice man, you should develop Matty emulator to make it better(just saying).

  3. #3
    Banned PEjump2 is offline
    BannedRank
    Jan 2010 Join Date
    The NetherlandsLocation
    2,838Posts

    Re: uberEmu RoomManager Stabilized C#

    Any credits to Matty13? Cuz some parts are copy & pasted from his last emu? :P

  4. #4
    Account Upgraded | Title Enabled! wichard is offline
    MemberRank
    Jul 2009 Join Date
    The NetherlandsLocation
    649Posts

    Re: uberEmu RoomManager Stabilized C#

    pejump i started from stamdard roommanager, no copys.

  5. #5
    HTML,CSS and a bit C# Richardjuhh is offline
    MemberRank
    Dec 2010 Join Date
    NetherlandsLocation
    351Posts

    Re: uberEmu RoomManager Stabilized C#

    Nice Wichard!
    i didnt use blah emu anymore because i fckd up the whole emu with orignal userlist and didnt work...

  6. #6
    Member XenoGFX is offline
    MemberRank
    May 2010 Join Date
    97Posts

    Re: uberEmu RoomManager Stabilized C#

    just sayin, unloading rooms fast isnt a good idea cause when 1000 people are on you can imagine some lag caused by unloading rooms fast. make it idle longer so that if the user requests the same room it is just provided not needed to reload all the items from the database. Good work.

    ps..memory is faster then reloading it from the db.
    Last edited by XenoGFX; 01-04-11 at 05:45 PM.

  7. #7
    Account Upgraded | Title Enabled! wichard is offline
    MemberRank
    Jul 2009 Join Date
    The NetherlandsLocation
    649Posts

    Re: uberEmu RoomManager Stabilized C#

    you can set the unload time

  8. #8
    Account Upgraded | Title Enabled! Miggs is offline
    MemberRank
    Oct 2010 Join Date
    711Posts

    Re: uberEmu RoomManager Stabilized C#

    Nice work :)

  9. #9
    Proficient Member klaudio007 is offline
    MemberRank
    Dec 2007 Join Date
    ChileLocation
    190Posts

    Re: uberEmu RoomManager Stabilized C#

    it's give me error in line:
    UberEnvironment.GetGame().GetRoomManager().RequestRoomUnload(Id);

    in room.cs, fix it please



Advertisement