using System; using System.Collections.Generic; using System.Linq; using System.Text; using Chop.Database; using System.Data; using Chop.User; namespace Chop.Game.Console { 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)"); } foreach (DataRow row in table.Rows) { uint sBuddyID = Convert.ToUInt32(row["receiverid"]) == mHabbo.mID ? Convert.ToUInt32(row["senderid"]) : Convert.ToUInt32(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 string GetMail() { Response Builder = new Response(); DataTable table; using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("id", mHabbo.mID); table = dbClient.ReadDataTable("SELECT * FROM console_mail WHERE toid = @id"); } Builder.AppendWired(table.Rows.Count); foreach (DataRow row in table.Rows) { Builder.AppendWired(row["id"]); Builder.AppendWired(row["fromid"]); Builder.AppendString(row["time"]); Builder.AppendString(row["message"]); } return Builder.ToString(); } public Response GetBuddyRequests() { // TODO: I really hate this code! Response Builder = new Response(); 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"); } foreach (DataRow row in table.Rows) { Response sResponse = new Response(132); // "BD" sResponse.AppendWired(row["senderid"]); sResponse.AppendString(Core.HabboManager.GetName(Convert.ToUInt32(row["senderid"]))); sResponse.AppendChar(1); Builder.Append(sResponse); } return Builder; } public Response GetBuddyListResponse() { Response Response = new Response(); Response.AppendWired(mBuddies.Count); foreach (Habbo habbo in mBuddies.Values) { Response.AppendString(habbo.mName); Response.AppendString(habbo.mConsoleMotto); Response.AppendString('H'); 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)) { uint sReceiverID = Core.HabboManager.GetID(pReceiverName); 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 { Response sResponse = new Response(132); // "BD" sResponse.AppendWired(mHabbo.mID); sResponse.AppendString(mHabbo.mName); Core.ClientManager.GetClientByHabbo(pReceiverName).SendMessage(sResponse); } } } public void AcceptBuddyRequest(uint pSenderID) { // check if user exists, TODO: error handling? 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(uint 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(uint 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 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) { Response sResponse = new Response(137); // "BI" sResponse.AppendWired(pBuddy.mID); sResponse.AppendString(pBuddy.mName); sResponse.AppendWiredBoolean(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(uint pBuddyID) // for sending packet and removing from collection only (no sql) { if (mBuddies.ContainsKey(pBuddyID)) mBuddies.Remove(pBuddyID); Response sResponse = new Response(138); // "BJ" sResponse.AppendWired(1); // count? sResponse.AppendWired(pBuddyID); mHabbo.mClient.SendMessage(sResponse); } public void SendMessage(List pReceivers, string pMessage) { int sID; string sTime = DateTime.Now.ToString("dd-MM-yyyy HH':'mm"); foreach (uint sBuddyID in pReceivers) { // Insert into database and get count using (DatabaseClient dbClient = Core.DatabaseManager.GetClient()) { dbClient.AddParamWithValue("from", mHabbo.mID); dbClient.AddParamWithValue("to", sBuddyID); dbClient.AddParamWithValue("message", pMessage); dbClient.AddParamWithValue("now", sTime); sID = dbClient.ReadInt32("SELECT MAX(id) FROM console_mail WHERE toid = @to") + 1; dbClient.AddParamWithValue("id", sID); dbClient.ExecuteQuery("INSERT INTO console_mail(id,fromid,toid,message,time) VALUES(@id,@from,@to,@message,@now)"); } // Send packet to buddy if online if (Core.ClientManager.GetClientByHabbo(sBuddyID) != null) // online { Response sResponse = new Response(134); // "BF" sResponse.AppendWired(1); // Message count sResponse.AppendWired(sID); sResponse.AppendWired(mHabbo.mID); sResponse.AppendString(sTime); sResponse.AppendString(pMessage); Core.ClientManager.GetClientByHabbo(sBuddyID).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! Response Builder = new Response(); Builder.AppendWired(mBuddies.Count); foreach (Habbo buddy in mBuddies.Values) { Builder.AppendWired(buddy.mID); Builder.AppendString(buddy.mConsoleMotto); Builder.AppendWiredBoolean(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(); } } }