示例#1
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
                {
                }
            }
        }