using System; using System.Collections.Generic; using Chop.Database; using System.Data; using Chop.User; using Chop.Game.Messages.IDs; namespace Chop.Game.Messenger { public class HabboConsole { public Habbo mHabbo; public Dictionary mBuddies; public HabboConsole(Habbo pHabbo) { mBuddies = new Dictionary(); mHabbo = pHabbo; } public void LoadBuddies(bool pNotifyOnlineBuddies) { mBuddies.Clear(); DataTable table; using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("id", mHabbo.mID); table = dbClient.ReadDataTable("SELECT * FROM console_friends WHERE accepted = '1' AND (receiverid = @id OR senderid = @id)"); } //ResponseBuilder message = new ResponseBuilder(OutgoingMessages.friend foreach (DataRow row in table.Rows) { int sBuddyID = Convert.ToInt32(row["receiverid"]) == mHabbo.mID ? Convert.ToInt32(row["senderid"]) : Convert.ToInt32(row["receiverid"]); if (Core.ClientManager.GetClientByHabbo(sBuddyID) != null) // Buddy online { GameClient sBuddyClient = Core.ClientManager.GetClientByHabbo(sBuddyID); AddFriend(sBuddyClient.mHabbo, true, pNotifyOnlineBuddies); } else // Load the user { Habbo sBuddy = Habbo.LoadByID(sBuddyID); AddFriend(sBuddy, true, pNotifyOnlineBuddies); } } } public ResponseBuilder GetBuddyRequests() { // TODO: I really hate this code! DataTable table; using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("id", mHabbo.mID); table = dbClient.ReadDataTable("SELECT * FROM console_friends WHERE accepted = '0' AND receiverid = @id"); } ResponseBuilder message = new ResponseBuilder(OutgoingMessages.BuddyRequestsEvent); message.AppendInt32(table.Rows.Count); // totalReqCount (NOT USED, CAN BE WHATEVER YOU WANT) message.AppendInt32(table.Rows.Count); foreach (DataRow row in table.Rows) { GameClient client = Core.ClientManager.GetClientByHabbo(Convert.ToInt32(row["senderid"])); Habbo habbo; if (client == null) // need to go to db habbo = Habbo.LoadByID(Convert.ToInt32(row["senderid"])); else habbo = client.mHabbo; message.AppendInt32(mHabbo.mID); // requestId/requesterUserId message.AppendString(habbo.mName); // requesterName message.AppendString(mHabbo.mFigure); // figureString } return message; } public ResponseBuilder GetBuddyListResponse() { ResponseBuilder Response = new ResponseBuilder(); Response.AppendInt32(mBuddies.Count); foreach (Habbo habbo in mBuddies.Values) { Response.AppendString(habbo.mName); Response.AppendString(habbo.mConsoleMotto); Response.AppendString("0"); Response.AppendString(habbo.mLastVisit); } return Response; } public void SendBuddyRequest(string pReceiverName) { // check if user exists, TODO: Friend request block? (and error handling) if (Core.HabboManager.HabboExists(pReceiverName)) { int sReceiverID = Core.HabboManager.GetID(pReceiverName); if (sReceiverID == 0) return; if (mBuddies.ContainsKey(sReceiverID)) // Aleredy friends return; // insert into database using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("senderid", mHabbo.mID); dbClient.AddParamWithValue("receiverid", sReceiverID); dbClient.ExecuteQuery("INSERT INTO console_friends(senderid,receiverid) VALUES(@senderid,@receiverid)"); // Accepted is 0 default } // inform receiver if online if (Core.ClientManager.GetClientByHabbo(pReceiverName) != null) // online { ResponseBuilder sResponse = new ResponseBuilder(OutgoingMessages.NewBuddyRequestEvent); // "BD" sResponse.AppendInt32(mHabbo.mID); sResponse.AppendString(mHabbo.mName); sResponse.AppendString(mHabbo.mID.ToString()); Core.ClientManager.GetClientByHabbo(pReceiverName).SendMessage(sResponse); } } } public void AcceptBuddyRequest(int pSenderID) { // check if user exists, TODO: error handling? // EDIT: Not really need this check.. if (Core.HabboManager.HabboExists(pSenderID)) { // insert into database using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("senderid", pSenderID); dbClient.AddParamWithValue("receiverid", mHabbo.mID); // delete the accepting users request to the sender if exists. dbClient.ExecuteQuery("DELETE FROM console_friends WHERE senderid = @receiverid AND receiverid = @senderid"); dbClient.ExecuteQuery("UPDATE console_friends SET accepted = '1' WHERE senderid = @senderid AND receiverid = @receiverid"); } // inform sender if online if (Core.ClientManager.GetClientByHabbo(pSenderID) != null) // online { Core.ClientManager.GetClientByHabbo(pSenderID).mConsole.AddFriend(mHabbo, true, true); AddFriend(Core.ClientManager.GetClientByHabbo(pSenderID).mHabbo, true, true); } else AddFriend(Habbo.LoadByID(pSenderID), true, true); } } public void DeclineBuddyRequest(int pSenderID) { using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("senderid", pSenderID); dbClient.AddParamWithValue("receiverid", mHabbo.mID); // delete the request if exists dbClient.ExecuteQuery("DELETE FROM console_friends WHERE senderid = @senderid AND receiverid = @receiverid AND accepted = '0'"); } } public void DeclineAllBuddyRequests() { // delete the requests using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("receiverid", mHabbo.mID); dbClient.ExecuteQuery("DELETE FROM console_friends WHERE receiverid = @receiverid AND accepted = '0'"); } } public void DeleteBuddy(int pBuddyID) { using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("senderid", pBuddyID); dbClient.AddParamWithValue("receiverid", mHabbo.mID); // delete if exists dbClient.ExecuteQuery("DELETE FROM console_friends WHERE ((senderid = @senderid AND receiverid = @receiverid) OR (senderid = @receiverid AND receiverid = @senderid)) AND accepted = '1'"); // messages //dbClient.ExecuteQuery("DELETE FROM console_mail WHERE (toid = @receiverid AND fromid = @senderid) OR (toid = @senderid AND fromid = @receiverid)"); } RemoveFriend(pBuddyID); if (Core.ClientManager.GetClientByHabbo(pBuddyID) != null) Core.ClientManager.GetClientByHabbo(pBuddyID).mConsole.RemoveFriend(mHabbo.mID); } public void DeleteAllBuddies() { using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("userid", mHabbo.mID); dbClient.ExecuteQuery("DELETE FROM console_friends WHERE (senderid = @userid OR receiverid = @userid) AND accepted = '1'"); // messages //dbClient.ExecuteQuery("DELETE FROM console_mail WHERE (toid = @receiverid AND fromid = @senderid) OR (toid = @senderid AND fromid = @receiverid)"); } foreach (int buddy in mBuddies.Keys) { RemoveFriend(buddy); if (Core.ClientManager.GetClientByHabbo(buddy) != null) Core.ClientManager.GetClientByHabbo(buddy).mConsole.RemoveFriend(mHabbo.mID); } } public void AddFriend(Habbo pBuddy, bool pIsNew, bool pNotice) // for sending packet and adding to collection only (no sql) { if (pIsNew && !mBuddies.ContainsKey(pBuddy.mID)) // add to mBuddies mBuddies.Add(pBuddy.mID, pBuddy); if (pNotice) { ResponseBuilder sResponse = new ResponseBuilder(137); // "BI" //sResponse.AppendInt32(pBuddy.mID); //sResponse.AppendString(pBuddy.mName); //sResponse.AppendInt32(pBuddy.mGender == 'M'); //sResponse.AppendString(pBuddy.mConsoleMotto); //sResponse.AppendWiredBoolean(pBuddy.mClient != null); // aka online //sResponse.AppendString(pBuddy.GetPosition()); // in guestroom = Floor1b, (else your message) //sResponse.AppendString(pBuddy.mLastVisit); //sResponse.AppendString(pBuddy.mFigure); mHabbo.mClient.SendMessage(sResponse); } } public void RemoveFriend(int pBuddyID) // for sending packet and removing from collection only (no sql) { if (mBuddies.ContainsKey(pBuddyID)) mBuddies.Remove(pBuddyID); // TODO: Send messenger upate //ResponseBuilder sResponse = new ResponseBuilder(138); // "BJ" //sResponse.AppendWired(1); // count? //sResponse.AppendWired(pBuddyID); //mHabbo.mClient.SendMessage(sResponse); } public void SendInstantMessage(int pReceiver, string pMessage) { // TODO: Errors and stuff // Send packet to buddy if online if (Core.ClientManager.GetClientByHabbo(pReceiver) != null) // online { ResponseBuilder sResponse = new ResponseBuilder(OutgoingMessages.NewConsoleMessageEvent); // "BF" sResponse.AppendInt32(mHabbo.mID); sResponse.AppendString(pMessage); Core.ClientManager.GetClientByHabbo(pReceiver).SendMessage(sResponse); } } public void DeleteMessage(int pMessageID) { using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("id", pMessageID); dbClient.AddParamWithValue("to", mHabbo.mID); dbClient.ExecuteQuery("DELETE FROM console_mail WHERE id = @id AND toid = @to"); } } public string GetUpdates() { LoadBuddies(false); // TODO: update only users with changes! ResponseBuilder Builder = new ResponseBuilder(); Builder.AppendInt32(0); // cat length Builder.AppendInt32(mBuddies.Count); //Builder.AppendInt32(0); //foreach (Habbo buddy in mBuddies.Values) //{ // Builder.AppendInt32 // Builder.AppendInt32(buddy.mID); // Builder.AppendString(buddy.mConsoleMotto); // Builder.AppendBoolean(buddy.mClient != null); // aka online // Builder.AppendString(buddy.GetPosition()); // in guestroom = Floor1b, (else your message) // if (buddy.mClient == null) // Builder.AppendString(buddy.mLastVisit); //} return Builder.ToString(); } internal void SendRoomInvite(string pMessage, List pReceiverList) { ResponseBuilder message = new ResponseBuilder(OutgoingMessages.RoomInviteEvent); message.AppendInt32(mHabbo.mID); message.AppendString(pMessage); foreach (int receiver in pReceiverList) { if (Core.ClientManager.GetClientByHabbo(receiver) != null) // online { Core.ClientManager.GetClientByHabbo(receiver).SendMessage(message); } } } } }