Advertisement
Guest User

Room.cs

a guest
Dec 16th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 38.04 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Linq;
  4. using System.Collections;
  5. using System.Threading.Tasks;
  6. using System.Collections.Generic;
  7.  
  8. using Plus.Core;
  9. using Plus.HabboHotel.GameClients;
  10. using Plus.HabboHotel.Groups;
  11. using Plus.HabboHotel.Items;
  12. using Plus.HabboHotel.Rooms.AI;
  13. using Plus.HabboHotel.Rooms.Games;
  14. using Plus.Communication.Interfaces;
  15. using Plus.Communication.Packets.Outgoing;
  16.  
  17.  
  18. using Plus.HabboHotel.Rooms.Instance;
  19.  
  20. using Plus.HabboHotel.Items.Data.Toner;
  21. using Plus.HabboHotel.Rooms.Games.Freeze;
  22. using Plus.HabboHotel.Items.Data.Moodlight;
  23.  
  24. using Plus.Communication.Packets.Outgoing.Rooms.Avatar;
  25. using Plus.Communication.Packets.Outgoing.Rooms.Engine;
  26. using Plus.Communication.Packets.Outgoing.Rooms.Session;
  27.  
  28.  
  29. using Plus.HabboHotel.Rooms.Games.Football;
  30. using Plus.HabboHotel.Rooms.Games.Banzai;
  31. using Plus.HabboHotel.Rooms.Games.Teams;
  32. using Plus.HabboHotel.Rooms.Trading;
  33. using Plus.HabboHotel.Rooms.AI.Speech;
  34. using Plus.Database.Interfaces;
  35. using Plus.Communication.Packets.Outgoing.Rooms.Polls;
  36. using Plus.Communication.Packets.Outgoing.Notifications;
  37.  
  38. namespace Plus.HabboHotel.Rooms
  39. {
  40.     public class Room : RoomData
  41.     {
  42.         public bool isCrashed;
  43.         public bool mDisposed;
  44.         public bool RoomMuted;
  45.         public bool DiscoMode;
  46.         public DateTime lastTimerReset;
  47.         public DateTime lastRegeneration;
  48.  
  49.  
  50.  
  51.         public Task ProcessTask;
  52.         public ArrayList ActiveTrades;
  53.  
  54.         public TonerData TonerData;
  55.         public MoodlightData MoodlightData;
  56.  
  57.         public Dictionary<int, double> Bans;
  58.         public Dictionary<int, double> MutedUsers;
  59.  
  60.  
  61.         private Dictionary<int, List<RoomUser>> Tents;
  62.         private String question;
  63.         private HashSet<int> yesVotes;
  64.         private HashSet<int> noVotes;
  65.         public List<int> UsersWithRights;
  66.         private GameManager _gameManager;
  67.         private Freeze _freeze;
  68.         private Soccer _soccer;
  69.         private BattleBanzai _banzai;
  70.  
  71.         private Gamemap _gamemap;
  72.         private GameItemHandler _gameItemHandler;
  73.  
  74.         private RoomData _roomData;
  75.         public TeamManager teambanzai;
  76.         public TeamManager teamfreeze;
  77.  
  78.         private RoomUserManager _roomUserManager;
  79.         private RoomItemHandling _roomItemHandling;
  80.  
  81.         private List<string> _wordFilterList;
  82.  
  83.         private FilterComponent _filterComponent = null;
  84.         private WiredComponent _wiredComponent = null;
  85.  
  86.         public int IsLagging { get; set; }
  87.         public int IdleTime { get; set; }
  88.  
  89.         //private ProcessComponent _process = null;
  90.  
  91.         public Room(RoomData Data)
  92.         {
  93.             this.IsLagging = 0;
  94.             this.IdleTime = 0;
  95.  
  96.             this._roomData = Data;
  97.             RoomMuted = false;
  98.             mDisposed = false;
  99.  
  100.             this.Id = Data.Id;
  101.             this.Name = Data.Name;
  102.             this.Description = Data.Description;
  103.             this.OwnerName = Data.OwnerName;
  104.             this.OwnerId = Data.OwnerId;
  105.  
  106.             this.Category = Data.Category;
  107.             this.Type = Data.Type;
  108.             this.Access = Data.Access;
  109.             this.UsersNow = 0;
  110.             this.UsersMax = Data.UsersMax;
  111.             this.ModelName = Data.ModelName;
  112.             this.Score = Data.Score;
  113.             this.Tags = new List<string>();
  114.             foreach (string tag in Data.Tags)
  115.             {
  116.                 Tags.Add(tag);
  117.             }
  118.  
  119.             this.AllowPets = Data.AllowPets;
  120.             this.AllowPetsEating = Data.AllowPetsEating;
  121.             this.RoomBlockingEnabled = Data.RoomBlockingEnabled;
  122.             this.Hidewall = Data.Hidewall;
  123.             this.Group = Data.Group;
  124.  
  125.             this.Password = Data.Password;
  126.             this.Wallpaper = Data.Wallpaper;
  127.             this.Floor = Data.Floor;
  128.             this.Landscape = Data.Landscape;
  129.  
  130.             this.WallThickness = Data.WallThickness;
  131.             this.FloorThickness = Data.FloorThickness;
  132.  
  133.             this.chatMode = Data.chatMode;
  134.             this.chatSize = Data.chatSize;
  135.             this.chatSpeed = Data.chatSpeed;
  136.             this.chatDistance = Data.chatDistance;
  137.             this.extraFlood = Data.extraFlood;
  138.  
  139.             this.TradeSettings = Data.TradeSettings;
  140.  
  141.             this.WhoCanBan = Data.WhoCanBan;
  142.             this.WhoCanKick = Data.WhoCanKick;
  143.             this.WhoCanBan = Data.WhoCanBan;
  144.  
  145.             this.PushEnabled = Data.PushEnabled;
  146.             this.PullEnabled = Data.PullEnabled;
  147.             this.SPullEnabled = Data.SPullEnabled;
  148.             this.SPushEnabled = Data.SPushEnabled;
  149.             this.EnablesEnabled = Data.EnablesEnabled;
  150.             this.RespectNotificationsEnabled = Data.RespectNotificationsEnabled;
  151.             this.PetMorphsAllowed = Data.PetMorphsAllowed;
  152.  
  153.             this.ActiveTrades = new ArrayList();
  154.             this.Bans = new Dictionary<int, double>();
  155.             this.MutedUsers = new Dictionary<int, double>();
  156.             this.Tents = new Dictionary<int, List<RoomUser>>();
  157.  
  158.             _gamemap = new Gamemap(this);
  159.             if (_roomItemHandling == null)
  160.                 _roomItemHandling = new RoomItemHandling(this);
  161.             _roomUserManager = new RoomUserManager(this);
  162.  
  163.             this._filterComponent = new FilterComponent(this);
  164.             this._wiredComponent = new WiredComponent(this);
  165.  
  166.             GetRoomItemHandler().LoadFurniture();
  167.             GetGameMap().GenerateMaps();
  168.  
  169.             this.LoadPromotions();
  170.             this.LoadRights();
  171.             this.LoadBans();
  172.             this.LoadFilter();
  173.             this.InitBots();
  174.             this.InitPets();
  175.  
  176.             Data.UsersNow = 1;
  177.         }
  178.  
  179.         public List<string> WordFilterList
  180.         {
  181.             get { return this._wordFilterList; }
  182.             set { this._wordFilterList = value; }
  183.         }
  184.  
  185.         #region Room Bans
  186.  
  187.         public bool UserIsBanned(int pId)
  188.         {
  189.             return Bans.ContainsKey(pId);
  190.         }
  191.  
  192.         public void RemoveBan(int pId)
  193.         {
  194.             Bans.Remove(pId);
  195.         }
  196.  
  197.         public void AddBan(int pId, long Time)
  198.         {
  199.             if (!Bans.ContainsKey(Convert.ToInt32(pId)))
  200.                 Bans.Add(pId, PlusEnvironment.GetUnixTimestamp() + Time);
  201.  
  202.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  203.             {
  204.                 dbClient.RunQuery("REPLACE INTO `room_bans` VALUES (" + pId + ", " + Id + ", " + (PlusEnvironment.GetUnixTimestamp() + Time) + ")");
  205.             }
  206.         }
  207.  
  208.         public List<int> BannedUsers()
  209.         {
  210.             var Bans = new List<int>();
  211.  
  212.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  213.             {
  214.                 dbClient.SetQuery("SELECT user_id FROM room_bans WHERE expire > UNIX_TIMESTAMP() AND room_id=" + Id);
  215.                 DataTable Table = dbClient.getTable();
  216.  
  217.                 foreach (DataRow Row in Table.Rows)
  218.                 {
  219.                     Bans.Add(Convert.ToInt32(Row[0]));
  220.                 }
  221.             }
  222.  
  223.             return Bans;
  224.         }
  225.  
  226.         public bool HasBanExpired(int pId)
  227.         {
  228.             if (!UserIsBanned(pId))
  229.                 return true;
  230.  
  231.             if (Bans[pId] < PlusEnvironment.GetUnixTimestamp())
  232.                 return true;
  233.  
  234.             return false;
  235.         }
  236.  
  237.         public void Unban(int UserId)
  238.         {
  239.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  240.             {
  241.                 dbClient.RunQuery("DELETE FROM `room_bans` WHERE `user_id` = '" + UserId + "' AND `room_id` = '" + Id + "' LIMIT 1");
  242.             }
  243.  
  244.             if (Bans.ContainsKey(UserId))
  245.                 Bans.Remove(UserId);
  246.         }
  247.  
  248.         #endregion
  249.  
  250.         #region Trading
  251.  
  252.         public bool HasActiveTrade(RoomUser User)
  253.         {
  254.             if (User.IsBot)
  255.                 return false;
  256.  
  257.             return HasActiveTrade(User.GetClient().GetHabbo().Id);
  258.         }
  259.  
  260.         public bool HasActiveTrade(int UserId)
  261.         {
  262.             if (ActiveTrades.Count == 0)
  263.                 return false;
  264.  
  265.             foreach (Trade Trade in ActiveTrades.ToArray())
  266.             {
  267.                 if (Trade.ContainsUser(UserId))
  268.                     return true;
  269.             }
  270.             return false;
  271.         }
  272.  
  273.         public Trade GetUserTrade(int UserId)
  274.         {
  275.             foreach (Trade Trade in ActiveTrades.ToArray())
  276.             {
  277.                 if (Trade.ContainsUser(UserId))
  278.                 {
  279.                     return Trade;
  280.                 }
  281.             }
  282.  
  283.             return null;
  284.         }
  285.  
  286.         public void TryStartTrade(RoomUser UserOne, RoomUser UserTwo)
  287.         {
  288.             if (UserOne == null || UserTwo == null || UserOne.IsBot || UserTwo.IsBot || UserOne.IsTrading ||
  289.                 UserTwo.IsTrading || HasActiveTrade(UserOne) || HasActiveTrade(UserTwo))
  290.                 return;
  291.  
  292.             ActiveTrades.Add(new Trade(UserOne.GetClient().GetHabbo().Id, UserTwo.GetClient().GetHabbo().Id, RoomId));
  293.         }
  294.  
  295.         public void TryStopTrade(int UserId)
  296.         {
  297.             Trade Trade = GetUserTrade(UserId);
  298.  
  299.             if (Trade == null)
  300.                 return;
  301.  
  302.             Trade.CloseTrade(UserId);
  303.             ActiveTrades.Remove(Trade);
  304.         }
  305.  
  306.         #endregion
  307.  
  308.  
  309.         public int UserCount
  310.         {
  311.             get { return _roomUserManager.GetRoomUsers().Count; }
  312.         }
  313.  
  314.         public int RoomId
  315.         {
  316.             get { return Id; }
  317.         }
  318.  
  319.         public bool CanTradeInRoom
  320.         {
  321.             get { return true; }
  322.         }
  323.  
  324.         public RoomData RoomData
  325.         {
  326.             get { return _roomData; }
  327.         }
  328.  
  329.         public Gamemap GetGameMap()
  330.         {
  331.             return _gamemap;
  332.         }
  333.  
  334.         public RoomItemHandling GetRoomItemHandler()
  335.         {
  336.             if (_roomItemHandling == null)
  337.             {
  338.                 _roomItemHandling = new RoomItemHandling(this);
  339.             }
  340.             return _roomItemHandling;
  341.         }
  342.  
  343.         public RoomUserManager GetRoomUserManager()
  344.         {
  345.             return _roomUserManager;
  346.         }
  347.  
  348.         public HashSet<int> getNoVotes()
  349.         {
  350.             return noVotes;
  351.         }
  352.  
  353.         public HashSet<int> getYesVotes()
  354.         {
  355.             return yesVotes;
  356.         }
  357.  
  358.         public String getQuestion()
  359.         {
  360.             return question;
  361.         }
  362.  
  363.         public Soccer GetSoccer()
  364.         {
  365.             if (_soccer == null)
  366.                 _soccer = new Soccer(this);
  367.  
  368.             return _soccer;
  369.         }
  370.  
  371.         public TeamManager GetTeamManagerForBanzai()
  372.         {
  373.             if (teambanzai == null)
  374.                 teambanzai = TeamManager.createTeamforGame("banzai");
  375.             return teambanzai;
  376.         }
  377.  
  378.         public TeamManager GetTeamManagerForFreeze()
  379.         {
  380.             if (teamfreeze == null)
  381.                 teamfreeze = TeamManager.createTeamforGame("freeze");
  382.             return teamfreeze;
  383.         }
  384.  
  385.         public BattleBanzai GetBanzai()
  386.         {
  387.             if (_banzai == null)
  388.                 _banzai = new BattleBanzai(this);
  389.             return _banzai;
  390.         }
  391.  
  392.         public Freeze GetFreeze()
  393.         {
  394.             if (_freeze == null)
  395.                 _freeze = new Freeze(this);
  396.             return _freeze;
  397.         }
  398.  
  399.         public GameManager GetGameManager()
  400.         {
  401.             if (_gameManager == null)
  402.                 _gameManager = new GameManager(this);
  403.             return _gameManager;
  404.         }
  405.  
  406.         public GameItemHandler GetGameItemHandler()
  407.         {
  408.             if (_gameItemHandler == null)
  409.                 _gameItemHandler = new GameItemHandler(this);
  410.             return _gameItemHandler;
  411.         }
  412.  
  413.         public bool GotSoccer()
  414.         {
  415.             return (_soccer != null);
  416.         }
  417.  
  418.         public bool GotBanzai()
  419.         {
  420.             return (_banzai != null);
  421.         }
  422.  
  423.         public bool GotFreeze()
  424.         {
  425.             return (_freeze != null);
  426.         }
  427.  
  428.         public void ClearTags()
  429.         {
  430.             Tags.Clear();
  431.         }
  432.  
  433.         public void AddTagRange(List<string> tags)
  434.         {
  435.             Tags.AddRange(tags);
  436.         }
  437.  
  438.         public void InitBots()
  439.         {
  440.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  441.             {
  442.                 dbClient.SetQuery("SELECT `id`,`room_id`,`name`,`motto`,`look`,`x`,`y`,`z`,`rotation`,`gender`,`user_id`,`ai_type`,`walk_mode`,`automatic_chat`,`speaking_interval`,`mix_sentences`,`chat_bubble` FROM `bots` WHERE `room_id` = '" + RoomId + "' AND `ai_type` != 'pet'");
  443.                 DataTable Data = dbClient.getTable();
  444.                 if (Data == null)
  445.                     return;
  446.  
  447.                 foreach (DataRow Bot in Data.Rows)
  448.                 {
  449.                     dbClient.SetQuery("SELECT `text` FROM `bots_speech` WHERE `bot_id` = '" + Convert.ToInt32(Bot["id"]) + "'");
  450.                     DataTable BotSpeech = dbClient.getTable();
  451.  
  452.                     List<RandomSpeech> Speeches = new List<RandomSpeech>();
  453.  
  454.                     foreach (DataRow Speech in BotSpeech.Rows)
  455.                     {
  456.                         Speeches.Add(new RandomSpeech(Convert.ToString(Speech["text"]), Convert.ToInt32(Bot["id"])));
  457.                     }
  458.  
  459.                     _roomUserManager.DeployBot(new RoomBot(Convert.ToInt32(Bot["id"]), Convert.ToInt32(Bot["room_id"]), Convert.ToString(Bot["ai_type"]), Convert.ToString(Bot["walk_mode"]), Convert.ToString(Bot["name"]), Convert.ToString(Bot["motto"]), Convert.ToString(Bot["look"]), int.Parse(Bot["x"].ToString()), int.Parse(Bot["y"].ToString()), int.Parse(Bot["z"].ToString()), int.Parse(Bot["rotation"].ToString()), 0, 0, 0, 0, ref Speeches, "M", 0, Convert.ToInt32(Bot["user_id"].ToString()), Convert.ToBoolean(Bot["automatic_chat"]), Convert.ToInt32(Bot["speaking_interval"]), PlusEnvironment.EnumToBool(Bot["mix_sentences"].ToString()), Convert.ToInt32(Bot["chat_bubble"])), null);
  460.                 }
  461.             }
  462.         }
  463.  
  464.         public void InitPets()
  465.         {
  466.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  467.             {
  468.                 dbClient.SetQuery("SELECT `id`,`user_id`,`room_id`,`name`,`x`,`y`,`z` FROM `bots` WHERE `room_id` = '" + RoomId + "' AND `ai_type` = 'pet'");
  469.                 DataTable Data = dbClient.getTable();
  470.  
  471.                 if (Data == null)
  472.                     return;
  473.  
  474.                 foreach (DataRow Row in Data.Rows)
  475.                 {
  476.                     dbClient.SetQuery("SELECT `type`,`race`,`color`,`experience`,`energy`,`nutrition`,`respect`,`createstamp`,`have_saddle`,`anyone_ride`,`hairdye`,`pethair`,`gnome_clothing` FROM `bots_petdata` WHERE `id` = '" + Row[0] + "' LIMIT 1");
  477.                     DataRow mRow = dbClient.getRow();
  478.                     if (mRow == null)
  479.                         continue;
  480.  
  481.                     Pet Pet = new Pet(Convert.ToInt32(Row["id"]), Convert.ToInt32(Row["user_id"]), Convert.ToInt32(Row["room_id"]), Convert.ToString(Row["name"]), Convert.ToInt32(mRow["type"]), Convert.ToString(mRow["race"]),
  482.                         Convert.ToString(mRow["color"]), Convert.ToInt32(mRow["experience"]), Convert.ToInt32(mRow["energy"]), Convert.ToInt32(mRow["nutrition"]), Convert.ToInt32(mRow["respect"]), Convert.ToDouble(mRow["createstamp"]), Convert.ToInt32(Row["x"]), Convert.ToInt32(Row["y"]),
  483.                         Convert.ToDouble(Row["z"]), Convert.ToInt32(mRow["have_saddle"]), Convert.ToInt32(mRow["anyone_ride"]), Convert.ToInt32(mRow["hairdye"]), Convert.ToInt32(mRow["pethair"]), Convert.ToString(mRow["gnome_clothing"]));
  484.  
  485.                     var RndSpeechList = new List<RandomSpeech>();
  486.  
  487.                     _roomUserManager.DeployBot(new RoomBot(Pet.PetId, RoomId, "pet", "freeroam", Pet.Name, "", Pet.Look, Pet.X, Pet.Y, Convert.ToInt32(Pet.Z), 0, 0, 0, 0, 0, ref RndSpeechList, "", 0, Pet.OwnerId, false, 0, false, 0), Pet);
  488.                 }
  489.             }
  490.         }
  491.  
  492.         public FilterComponent GetFilter()
  493.         {
  494.             return this._filterComponent;
  495.         }
  496.  
  497.         public WiredComponent GetWired()
  498.         {
  499.             return this._wiredComponent;
  500.         }
  501.  
  502.         public void LoadPromotions()
  503.         {
  504.             DataRow GetPromotion = null;
  505.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  506.             {
  507.                 dbClient.SetQuery("SELECT * FROM `room_promotions` WHERE `room_id` = " + this.Id + " LIMIT 1;");
  508.                 GetPromotion = dbClient.getRow();
  509.  
  510.                 if (GetPromotion != null)
  511.                 {
  512.                     if (Convert.ToDouble(GetPromotion["timestamp_expire"]) > PlusEnvironment.GetUnixTimestamp())
  513.                         RoomData._promotion = new RoomPromotion(Convert.ToString(GetPromotion["title"]), Convert.ToString(GetPromotion["description"]), Convert.ToDouble(GetPromotion["timestamp_start"]), Convert.ToDouble(GetPromotion["timestamp_expire"]), Convert.ToInt32(GetPromotion["category_id"]));
  514.                 }
  515.             }
  516.         }
  517.  
  518.         public void LoadRights()
  519.         {
  520.             UsersWithRights = new List<int>();
  521.             if (Group != null)
  522.                 return;
  523.  
  524.             DataTable Data = null;
  525.  
  526.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  527.             {
  528.                 dbClient.SetQuery("SELECT room_rights.user_id FROM room_rights WHERE room_id = @roomid");
  529.                 dbClient.AddParameter("roomid", Id);
  530.                 Data = dbClient.getTable();
  531.             }
  532.  
  533.             if (Data != null)
  534.             {
  535.                 foreach (DataRow Row in Data.Rows)
  536.                 {
  537.                     UsersWithRights.Add(Convert.ToInt32(Row["user_id"]));
  538.                 }
  539.             }
  540.         }
  541.  
  542.         private void LoadFilter()
  543.         {
  544.             this._wordFilterList = new List<string>();
  545.  
  546.             DataTable Data = null;
  547.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  548.             {
  549.                 dbClient.SetQuery("SELECT * FROM `room_filter` WHERE `room_id` = @roomid;");
  550.                 dbClient.AddParameter("roomid", Id);
  551.                 Data = dbClient.getTable();
  552.             }
  553.  
  554.             if (Data == null)
  555.                 return;
  556.  
  557.             foreach (DataRow Row in Data.Rows)
  558.             {
  559.                 this._wordFilterList.Add(Convert.ToString(Row["word"]));
  560.             }
  561.         }
  562.  
  563.         public void LoadBans()
  564.         {
  565.             this.Bans = new Dictionary<int, double>();
  566.  
  567.             DataTable Bans;
  568.  
  569.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  570.             {
  571.                 dbClient.SetQuery("SELECT user_id, expire FROM room_bans WHERE room_id = " + Id);
  572.                 Bans = dbClient.getTable();
  573.             }
  574.  
  575.             if (Bans == null)
  576.                 return;
  577.  
  578.             foreach (DataRow ban in Bans.Rows)
  579.             {
  580.                 this.Bans.Add(Convert.ToInt32(ban[0]), Convert.ToDouble(ban[1]));
  581.             }
  582.         }
  583.  
  584.         public bool CheckRights(GameClient Session)
  585.         {
  586.             return CheckRights(Session, false);
  587.         }
  588.  
  589.         public bool CheckRights(GameClient Session, bool RequireOwnership, bool CheckForGroups = false)
  590.         {
  591.             try
  592.             {
  593.                 if (Session == null || Session.GetHabbo() == null)
  594.                     return false;
  595.  
  596.                 if (Session.GetHabbo().Username == OwnerName && Type == "private")
  597.                     return true;
  598.  
  599.                 if (Session.GetHabbo().GetPermissions().HasRight("room_any_owner"))
  600.                     return true;
  601.  
  602.                 if (!RequireOwnership && Type == "private")
  603.                 {
  604.                     if (Session.GetHabbo().GetPermissions().HasRight("room_any_rights"))
  605.                         return true;
  606.  
  607.                     if (UsersWithRights.Contains(Session.GetHabbo().Id))
  608.                         return true;
  609.                 }
  610.  
  611.                 if (CheckForGroups && Type == "private")
  612.                 {
  613.                     if (Group == null)
  614.                         return false;
  615.  
  616.                     if (Group.IsAdmin(Session.GetHabbo().Id))
  617.                         return true;
  618.  
  619.                     if (Group.AdminOnlyDeco == 0)
  620.                     {
  621.                         if (Group.IsAdmin(Session.GetHabbo().Id))
  622.                             return true;
  623.                     }
  624.                 }
  625.             }
  626.             catch (Exception e) { Logging.HandleException(e, "Room.CheckRights"); }
  627.             return false;
  628.         }
  629.  
  630.         public void OnUserShoot(RoomUser User, Item Ball)
  631.         {
  632.             Func<Item, bool> predicate = null;
  633.             string Key = null;
  634.             foreach (Item item in this.GetRoomItemHandler().GetFurniObjects(Ball.GetX, Ball.GetY).ToList())
  635.             {
  636.                 if (item.GetBaseItem().ItemName.StartsWith("fball_goal_"))
  637.                 {
  638.                     Key = item.GetBaseItem().ItemName.Split(new char[] { '_' })[2];
  639.                     User.UnIdle();
  640.                     User.DanceId = 0;
  641.  
  642.  
  643.                     PlusEnvironment.GetGame().GetAchievementManager().ProgressAchievement(User.GetClient(), "ACH_FootballGoalScored", 1);
  644.  
  645.                     SendMessage(new ActionComposer(User.VirtualId, 1));
  646.                 }
  647.             }
  648.  
  649.             if (Key != null)
  650.             {
  651.                 if (predicate == null)
  652.                 {
  653.                     predicate = p => p.GetBaseItem().ItemName == ("fball_score_" + Key);
  654.                 }
  655.  
  656.                 foreach (Item item2 in this.GetRoomItemHandler().GetFloor.Where<Item>(predicate).ToList())
  657.                 {
  658.                     if (item2.GetBaseItem().ItemName == ("fball_score_" + Key))
  659.                     {
  660.                         if (!String.IsNullOrEmpty(item2.ExtraData))
  661.                             item2.ExtraData = (Convert.ToInt32(item2.ExtraData) + 1).ToString();
  662.                         else
  663.                             item2.ExtraData = "1";
  664.                         item2.UpdateState();
  665.                     }
  666.                 }
  667.             }
  668.         }
  669.         public void startQuestion(String question)
  670.         {
  671.             this.question = question;
  672.             this.yesVotes = new HashSet<int>();
  673.             this.noVotes = new HashSet<int>();
  674.  
  675.             this.SendMessage(new QuickPollMessageComposer(question));
  676.         }
  677.  
  678.         public void endQuestion()
  679.         {
  680.             this.question = null;
  681.             this.SendMessage(new QuickPollResultsMessageComposer(this.yesVotes.Count(), this.noVotes.Count()));
  682.             if(this.yesVotes != null)
  683.             {
  684.                 this.yesVotes.Clear();
  685.                 this.yesVotes.Clear();
  686.             }
  687.             if(this.noVotes != null)
  688.             {
  689.                 this.noVotes.Clear();
  690.             }
  691.         }
  692.         public void ProcessRoom()
  693.         {
  694.             if (isCrashed || mDisposed)
  695.                 return;
  696.  
  697.             try
  698.             {
  699.                 if (this.GetRoomUserManager().GetRoomUsers().Count == 0)
  700.                     this.IdleTime++;
  701.                 else if (this.IdleTime > 0)
  702.                     this.IdleTime = 0;
  703.  
  704.                 if (this.RoomData.HasActivePromotion && this.RoomData.Promotion.HasExpired)
  705.                 {
  706.                     this.RoomData.EndPromotion();
  707.                 }
  708.  
  709.                 if (this.IdleTime >= 60 && !this.RoomData.HasActivePromotion)
  710.                 {
  711.                     PlusEnvironment.GetGame().GetRoomManager().UnloadRoom(this);
  712.                     return;
  713.                 }
  714.  
  715.                 try { GetRoomItemHandler().OnCycle(); }
  716.                 catch (Exception e)
  717.                 {
  718.                     Logging.LogException("Room ID [" + RoomId + "] is currently having issues cycling the room items." + e.ToString());
  719.                 }
  720.  
  721.                 try { GetRoomUserManager().OnCycle(); }
  722.                 catch (Exception e)
  723.                 {
  724.                     Logging.LogException("Room ID [" + RoomId + "] is currently having issues cycling the room users." + e.ToString());
  725.                 }
  726.  
  727.                 #region Status Updates
  728.                 try
  729.                 {
  730.                     GetRoomUserManager().SerializeStatusUpdates();
  731.                 }
  732.                 catch (Exception e)
  733.                 {
  734.                     Logging.LogException("Room ID [" + RoomId + "] is currently having issues cycling the room user statuses." + e.ToString());
  735.                 }
  736.                 #endregion
  737.  
  738.                 #region Game Item Cycle
  739.                 try
  740.                 {
  741.                     if (_gameItemHandler != null)
  742.                         _gameItemHandler.OnCycle();
  743.                 }
  744.                 catch (Exception e)
  745.                 {
  746.                     Logging.LogException("Room ID [" + RoomId + "] is currently having issues cycling the game items." + e.ToString());
  747.                 }
  748.                 #endregion
  749.  
  750.                 try { GetWired().OnCycle(); }
  751.                 catch (Exception e)
  752.                 {
  753.                     Logging.LogException("Room ID [" + RoomId + "] is currently having issues cycling wired." + e.ToString());
  754.                 }
  755.  
  756.             }
  757.             catch (Exception e)
  758.             {
  759.                 Logging.WriteLine("Room ID [" + RoomId + "] has crashed.");
  760.                 Logging.LogException("Room ID [" + RoomId + "] has crashed." + e.ToString());
  761.                 OnRoomCrash(e);
  762.             }
  763.         }
  764.  
  765.         private void OnRoomCrash(Exception e)
  766.         {
  767.             Logging.LogThreadException(e.ToString(), "Room cycle task for room " + RoomId);
  768.  
  769.             try
  770.             {
  771.                 foreach (RoomUser user in _roomUserManager.GetRoomUsers().ToList())
  772.                 {
  773.                     if (user == null || user.GetClient() == null)
  774.                         continue;
  775.  
  776.                     user.GetClient().SendNotification("Sorry, it appears that room has crashed!");//Unhandled exception in room: " + e);
  777.  
  778.                     try
  779.                     {
  780.                         GetRoomUserManager().RemoveUserFromRoom(user.GetClient(), true, false);
  781.                     }
  782.                     catch (Exception e2) { Logging.LogException(e2.ToString()); }
  783.                 }
  784.             }
  785.             catch (Exception e3) { Logging.LogException(e3.ToString()); }
  786.  
  787.             isCrashed = true;
  788.             PlusEnvironment.GetGame().GetRoomManager().UnloadRoom(this, true);
  789.         }
  790.  
  791.  
  792.         public bool CheckMute(GameClient Session)
  793.         {
  794.             if (MutedUsers.ContainsKey(Session.GetHabbo().Id))
  795.             {
  796.                 if (MutedUsers[Session.GetHabbo().Id] < PlusEnvironment.GetUnixTimestamp())
  797.                 {
  798.                     MutedUsers.Remove(Session.GetHabbo().Id);
  799.                 }
  800.                 else
  801.                 {
  802.                     return true;
  803.                 }
  804.             }
  805.  
  806.             if (Session.GetHabbo().TimeMuted > 0 || (RoomMuted && Session.GetHabbo().Username != OwnerName))
  807.                 return true;
  808.  
  809.             return false;
  810.         }
  811.  
  812.         public void AddChatlog(int Id, string Message)
  813.         {
  814.             using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  815.             {
  816.                 dbClient.SetQuery("INSERT INTO `chatlogs` (user_id, room_id, message, timestamp) VALUES (@user, @room, @message, @time)");
  817.                 dbClient.AddParameter("user", Id);
  818.                 dbClient.AddParameter("room", RoomId);
  819.                 dbClient.AddParameter("message", Message);
  820.                 dbClient.AddParameter("time", PlusEnvironment.GetUnixTimestamp());
  821.                 dbClient.RunQuery();
  822.             }
  823.         }
  824.  
  825.         public void SendObjects(GameClient Session)
  826.         {
  827.             Room Room = Session.GetHabbo().CurrentRoom;
  828.  
  829.             Session.SendMessage(new HeightMapComposer(Room.GetGameMap().Model.Heightmap));
  830.             Session.SendMessage(new FloorHeightMapComposer(Room.GetGameMap().Model.GetRelativeHeightmap(), Room.GetGameMap().StaticModel.WallHeight));
  831.  
  832.             foreach (RoomUser RoomUser in _roomUserManager.GetUserList().ToList())
  833.             {
  834.                 if (RoomUser == null)
  835.                     continue;
  836.  
  837.                 Session.SendMessage(new UsersComposer(RoomUser));
  838.  
  839.                 if (RoomUser.IsBot && RoomUser.BotData.DanceId > 0)
  840.                     Session.SendMessage(new DanceComposer(RoomUser, RoomUser.BotData.DanceId));
  841.                 else if (!RoomUser.IsBot && !RoomUser.IsPet && RoomUser.IsDancing)
  842.                     Session.SendMessage(new DanceComposer(RoomUser, RoomUser.DanceId));
  843.  
  844.                 if (RoomUser.IsAsleep)
  845.                     Session.SendMessage(new SleepComposer(RoomUser, true));
  846.  
  847.                 if (RoomUser.CarryItemID > 0 && RoomUser.CarryTimer > 0)
  848.                     Session.SendMessage(new CarryObjectComposer(RoomUser.VirtualId, RoomUser.CarryItemID));
  849.  
  850.                 if (!RoomUser.IsBot && !RoomUser.IsPet && RoomUser.CurrentEffect > 0)
  851.                     Room.SendMessage(new AvatarEffectComposer(RoomUser.VirtualId, RoomUser.CurrentEffect));
  852.             }
  853.  
  854.             Session.SendMessage(new UserUpdateComposer(_roomUserManager.GetUserList().ToList()));
  855.             Session.SendMessage(new ObjectsComposer(Room.GetRoomItemHandler().GetFloor.ToArray(), this));
  856.             Session.SendMessage(new ItemsComposer(Room.GetRoomItemHandler().GetWall.ToArray(), this));
  857.         }
  858.  
  859.         #region Tents
  860.         public void AddTent(int TentId)
  861.         {
  862.             if (Tents.ContainsKey(TentId))
  863.                 Tents.Remove(TentId);
  864.  
  865.             Tents.Add(TentId, new List<RoomUser>());
  866.         }
  867.  
  868.         public void RemoveTent(int TentId, Item Item)
  869.         {
  870.             if (!Tents.ContainsKey(TentId))
  871.                 return;
  872.  
  873.             List<RoomUser> Users = Tents[TentId];
  874.             foreach (RoomUser User in Users.ToList())
  875.             {
  876.                 if (User == null || User.GetClient() == null || User.GetClient().GetHabbo() == null)
  877.                     continue;
  878.  
  879.                 User.GetClient().GetHabbo().TentId = 0;
  880.             }
  881.  
  882.             if (Tents.ContainsKey(TentId))
  883.                 Tents.Remove(TentId);
  884.         }
  885.  
  886.         public void AddUserToTent(int TentId, RoomUser User, Item Item)
  887.         {
  888.             if (User != null && User.GetClient() != null && User.GetClient().GetHabbo() != null)
  889.             {
  890.                 if (!Tents.ContainsKey(TentId))
  891.                     Tents.Add(TentId, new List<RoomUser>());
  892.  
  893.                 if (!Tents[TentId].Contains(User))
  894.                     Tents[TentId].Add(User);
  895.                 User.GetClient().GetHabbo().TentId = TentId;
  896.             }
  897.         }
  898.  
  899.         public void RemoveUserFromTent(int TentId, RoomUser User, Item Item)
  900.         {
  901.             if (User != null && User.GetClient() != null && User.GetClient().GetHabbo() != null)
  902.             {
  903.                 if (!Tents.ContainsKey(TentId))
  904.                     Tents.Add(TentId, new List<RoomUser>());
  905.  
  906.                 if (Tents[TentId].Contains(User))
  907.                     Tents[TentId].Remove(User);
  908.  
  909.                 User.GetClient().GetHabbo().TentId = 0;
  910.             }
  911.         }
  912.  
  913.         public void SendToTent(int Id, int TentId, IServerPacket Packet)
  914.         {
  915.             if (!Tents.ContainsKey(TentId))
  916.                 return;
  917.  
  918.             foreach (RoomUser User in Tents[TentId].ToList())
  919.             {
  920.                 if (User == null || User.GetClient() == null || User.GetClient().GetHabbo() == null || User.GetClient().GetHabbo().MutedUsers.Contains(Id) || User.GetClient().GetHabbo().TentId != TentId)
  921.                     continue;
  922.  
  923.                 User.GetClient().SendMessage(Packet);
  924.             }
  925.         }
  926.         #endregion
  927.  
  928.         #region Communication (Packets)
  929.         public void SendMessage(IServerPacket Message, bool UsersWithRightsOnly = false)
  930.         {
  931.             if (Message == null)
  932.                 return;
  933.  
  934.             try
  935.             {
  936.  
  937.                 List<RoomUser> Users = this._roomUserManager.GetUserList().ToList();
  938.  
  939.                 if (this == null || this._roomUserManager == null || Users == null)
  940.                     return;
  941.  
  942.                 foreach (RoomUser User in Users)
  943.                 {
  944.                     if (User == null || User.IsBot)
  945.                         continue;
  946.  
  947.                     if (User.GetClient() == null || User.GetClient().GetConnection() == null)
  948.                         continue;
  949.  
  950.                     if (UsersWithRightsOnly && !this.CheckRights(User.GetClient()))
  951.                         continue;
  952.  
  953.                     User.GetClient().SendMessage(Message);
  954.                 }
  955.             }
  956.             catch (Exception e)
  957.             {
  958.                 Logging.HandleException(e, "Room.SendMessage");
  959.             }
  960.         }
  961.  
  962.         public void BroadcastPacket(byte[] Packet)
  963.         {
  964.             foreach (RoomUser User in this._roomUserManager.GetUserList().ToList())
  965.             {
  966.                 if (User == null || User.IsBot)
  967.                     continue;
  968.  
  969.                 if (User.GetClient() == null || User.GetClient().GetConnection() == null)
  970.                     continue;
  971.  
  972.                 User.GetClient().GetConnection().SendData(Packet);
  973.             }
  974.         }
  975.  
  976.         public void SendMessage(List<ServerPacket> Messages)
  977.         {
  978.             if (Messages.Count == 0)
  979.                 return;
  980.  
  981.             try
  982.             {
  983.                 byte[] TotalBytes = new byte[0];
  984.                 int Current = 0;
  985.  
  986.                 foreach (ServerPacket Packet in Messages.ToList())
  987.                 {
  988.                     byte[] ToAdd = Packet.GetBytes();
  989.                     int NewLen = TotalBytes.Length + ToAdd.Length;
  990.  
  991.                     Array.Resize(ref TotalBytes, NewLen);
  992.  
  993.                     for (int i = 0; i < ToAdd.Length; i++)
  994.                     {
  995.                         TotalBytes[Current] = ToAdd[i];
  996.                         Current++;
  997.                     }
  998.                 }
  999.  
  1000.                 this.BroadcastPacket(TotalBytes);
  1001.             }
  1002.             catch (Exception e)
  1003.             {
  1004.                 Logging.HandleException(e, "Room.SendMessage List<ServerPacket>");
  1005.             }
  1006.         }
  1007.         #endregion
  1008.  
  1009.         private void SaveAI()
  1010.         {
  1011.             foreach (RoomUser User in GetRoomUserManager().GetRoomUsers().ToList())
  1012.             {
  1013.                 if (User == null || !User.IsBot)
  1014.                     continue;
  1015.  
  1016.                 if (User.IsBot)
  1017.                 {
  1018.                     using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  1019.                     {
  1020.                         dbClient.SetQuery("UPDATE bots SET x=@x, y=@y, z=@z, name=@name, look=@look, rotation=@rotation WHERE id=@id LIMIT 1;");
  1021.                         dbClient.AddParameter("name", User.BotData.Name);
  1022.                         dbClient.AddParameter("look", User.BotData.Look);
  1023.                         dbClient.AddParameter("rotation", User.BotData.Rot);
  1024.                         dbClient.AddParameter("x", User.X);
  1025.                         dbClient.AddParameter("y", User.Y);
  1026.                         dbClient.AddParameter("z", User.Z);
  1027.                         dbClient.AddParameter("id", User.BotData.BotId);
  1028.                         dbClient.RunQuery();
  1029.                     }
  1030.                 }
  1031.             }
  1032.         }
  1033.  
  1034.         public void Dispose()
  1035.         {
  1036.             SendMessage(new CloseConnectionComposer());
  1037.  
  1038.             if (!mDisposed)
  1039.             {
  1040.                 isCrashed = false;
  1041.                 mDisposed = true;
  1042.  
  1043.                 try
  1044.                 {
  1045.                     if (ProcessTask != null && ProcessTask.IsCompleted)
  1046.                         ProcessTask.Dispose();
  1047.                 }
  1048.                 catch { }
  1049.  
  1050.                 GetRoomItemHandler().SaveFurniture();
  1051.  
  1052.                 using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  1053.                 {
  1054.                     dbClient.RunQuery("UPDATE `rooms` SET `users_now` = '0' WHERE `id` = '" + Id + "' LIMIT 1");
  1055.                 }
  1056.  
  1057.                 if (this._roomUserManager.PetCount > 0)
  1058.                     this._roomUserManager.UpdatePets();
  1059.  
  1060.                 this.SaveAI();
  1061.  
  1062.                 UsersNow = 0;
  1063.                 RoomData.UsersNow = 0;
  1064.  
  1065.                 UsersWithRights.Clear();
  1066.                 Bans.Clear();
  1067.                 MutedUsers.Clear();
  1068.                 Tents.Clear();
  1069.  
  1070.                 this.TonerData = null;
  1071.                 this.MoodlightData = null;
  1072.                 if(this.yesVotes != null)
  1073.                 {
  1074.                     this.yesVotes.Clear();
  1075.                 }
  1076.                 if(this.noVotes != null)
  1077.                 {
  1078.                     this.noVotes.Clear();
  1079.                 }
  1080.                 this._filterComponent.Cleanup();
  1081.                 this._wiredComponent.Cleanup();
  1082.  
  1083.                 if (this._gameItemHandler != null)
  1084.                     this._gameItemHandler.Dispose();
  1085.  
  1086.                 if (this._gameManager != null)
  1087.                     this._gameManager.Dispose();
  1088.  
  1089.                 if (this._freeze != null)
  1090.                     this._freeze.Dispose();
  1091.  
  1092.                 if (this._banzai != null)
  1093.                     this._banzai.Dispose();
  1094.  
  1095.                 if (this._soccer != null)
  1096.                     this._soccer.Dispose();
  1097.  
  1098.                 if (this._gamemap != null)
  1099.                     this._gamemap.Dispose();
  1100.  
  1101.                 if (this._roomUserManager != null)
  1102.                     this._roomUserManager.Dispose();
  1103.  
  1104.                 if (this._roomItemHandling != null)
  1105.                     this._roomItemHandling.Dispose();
  1106.  
  1107.  
  1108.  
  1109.                 if (ActiveTrades.Count > 0)
  1110.                     ActiveTrades.Clear();
  1111.             }
  1112.         }
  1113.     }
  1114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement