예제 #1
0
        public virtual void Handle(GameClient session, ClientMessage message)
        {
            if (session?.GetHabbo()?.GetMessenger() != null)
            {
                uint userId = 0;
                bool blockFriendRequests = false;

                GameClient target = Skylight.GetGame().GetGameClientManager().GetGameClientByUsername(this.Username);
                if (target?.GetHabbo()?.GetUserSettings() == null)
                {
                    DataRow dataRow = null;
                    using (DatabaseClient dbClient = Skylight.GetDatabaseManager().GetClient())
                    {
                        dbClient.AddParamWithValue("username", this.Username);
                        dataRow = dbClient.ReadDataRow("SELECT id, block_newfriends FROM users WHERE username = @username LIMIT 1");
                    }

                    if (dataRow != null)
                    {
                        userId = (uint)dataRow["id"];
                        blockFriendRequests = TextUtilies.StringToBool((string)dataRow["block_newfriends"]);
                    }
                }
                else
                {
                    userId = target.GetHabbo().ID;
                    blockFriendRequests = target.GetHabbo().GetUserSettings().BlockNewFriends;
                }

                if (userId > 0 && userId != session.GetHabbo().ID)
                {
                    if (blockFriendRequests)
                    {
                        session.SendMessage(new MessengerSendFriendRequestErrorComposerHandler(MessengerSendFriendRequestErrorCode.FriendRequestsDisabled));
                    }
                    else
                    {
                        if (session.GetHabbo().GetMessenger().TrySendFriendRequestTo(userId) && !(target?.GetHabbo()?.GetMessenger()?.HasFriendRequestPendingFrom(session.GetHabbo().ID) ?? true))
                        {
                            bool insertSuccess = false;
                            using (DatabaseClient dbClient = Skylight.GetDatabaseManager().GetClient())
                            {
                                dbClient.AddParamWithValue("toid", userId);
                                dbClient.AddParamWithValue("userid", session.GetHabbo().ID);
                                insertSuccess = dbClient.ExecuteNonQuery("INSERT INTO messenger_requests (to_id, from_id) VALUES (@toid, @userid)") > 0;
                            }

                            if (insertSuccess)
                            {
                                target?.GetHabbo()?.GetMessenger()?.AddFriendRequest(new MessengerRequest(userId, session.GetHabbo().ID, session.GetHabbo().Username, session.GetHabbo().Look));
                            }
                        }
                    }
                }
                else
                {
                    session.SendMessage(new MessengerSendFriendRequestErrorComposerHandler(MessengerSendFriendRequestErrorCode.RequestNotFound));
                }
            }
        }
예제 #2
0
        public void TradeItems()
        {
            TradeUser userOne = this.Traders[0];
            TradeUser userTwo = this.Traders[1];

            try
            {
                List <uint> tradeOffer1 = userOne.OfferedItems.Keys.ToList();
                List <uint> tradeOffer2 = userTwo.OfferedItems.Keys.ToList();

                //foreach (uint itemId in tradeOffer1)
                //{
                //    if (userOne.RoomUser.GetClient().GetHabbo().GetInventoryManager().TryGetItem(itemId) == null)
                //    {
                //        this.SendToBoth(new TradeCancelComposerHandler(userOne.UserID, TradeCancelErrorCode.ItemsUnavaible));
                //        return;
                //    }
                //}

                //foreach (uint itemId in tradeOffer2)
                //{
                //    if (userTwo.RoomUser.GetClient().GetHabbo().GetInventoryManager().TryGetItem(itemId) == null)
                //    {
                //        this.SendToBoth(new TradeCancelComposerHandler(userTwo.UserID, TradeCancelErrorCode.ItemsUnavaible));
                //        return;
                //    }
                //}

                using (DatabaseClient dbClient = Skylight.GetDatabaseManager().GetClient())
                {
                    dbClient.AddParamWithValue("userId1", userOne.UserID);
                    dbClient.AddParamWithValue("userId2", userTwo.UserID);

                    dbClient.StartTransaction();

                    try
                    {
                        if (tradeOffer1.Count > 0)
                        {
                            if (dbClient.ExecuteNonQuery("UPDATE items SET room_id = '0', user_id = @userId2 WHERE id IN(" + string.Join(",", tradeOffer1) + ") LIMIT " + tradeOffer1.Count) != tradeOffer1.Count)
                            {
                                dbClient.Rollback();

                                this.SendToBoth(new TradeCancelComposerHandler(userOne.UserID, TradeCancelErrorCode.ItemsUnavaible));
                                return;
                            }
                        }

                        if (tradeOffer2.Count > 0)
                        {
                            if (dbClient.ExecuteNonQuery("UPDATE items SET room_id = '0', user_id = @userId1 WHERE id IN(" + string.Join(",", tradeOffer2) + ") LIMIT " + tradeOffer2.Count) != tradeOffer2.Count)
                            {
                                dbClient.Rollback();

                                this.SendToBoth(new TradeCancelComposerHandler(userTwo.UserID, TradeCancelErrorCode.ItemsUnavaible));
                                return;
                            }
                        }

                        dbClient.Commit();
                    }
                    catch
                    {
                        dbClient.Rollback();

                        throw;
                    }
                }

                try
                {
                    userOne?.RoomUser?.Session?.GetHabbo()?.GetInventoryManager()?.UpdateInventoryItems(true);
                }
                catch
                {
                }

                try
                {
                    userTwo?.RoomUser?.Session?.GetHabbo()?.GetInventoryManager()?.UpdateInventoryItems(true);
                }
                catch
                {
                }
            }
            catch (Exception ex)
            {
                Logging.LogException("Error in TradeItems task! " + ex.ToString());

                try
                {
                    userOne?.RoomUser?.Session?.SendNotif("Trade failed due to critical error!");
                }
                catch
                {
                }

                try
                {
                    userTwo?.RoomUser?.Session?.SendNotif("Trade failed due to critical error!");
                }
                catch
                {
                }
            }
        }