Exemple #1
0
        /// <summary>
        /// Updates the trade window
        /// </summary>
        public void TradeUpdate()
        {
            lock (Sync)
            {
                if (m_changesCount > 0)
                {
                    return;
                }
                if (m_changesCount < 0)
                {
                    m_changesCount = 0;
                    if (log.IsErrorEnabled)
                    {
                        log.Error("Changes count is less than 0, forgot to add m_changesCount++?\n\n" + Environment.StackTrace);
                    }
                }

                InventoryItem itemToCombine = (InventoryItem)PartnerTradeItems[0];
                // --------------------------------------------------------------
                // Luhz Crafting Update:
                // Players may now have any, and all, "primary" crafting skills.
                // AbstractCraftingSkill skill = CraftingMgr.getSkillbyEnum(m_owner.CraftingPrimarySkill);
                AbstractCraftingSkill skill = null;
                lock (m_owner.TradeWindow.Sync)
                {
                    foreach (InventoryItem i in (ArrayList)m_owner.TradeWindow.TradeItems.Clone())
                    {
                        if (i.Object_Type == (int)eObjectType.AlchemyTincture)
                        {
                            if (m_owner.GetCraftingSkillValue(eCraftingSkill.Alchemy) > 0)
                            {
                                skill = CraftingMgr.getSkillbyEnum(eCraftingSkill.Alchemy);
                                break;
                            }
                        }
                        else if (i.Object_Type == (int)eObjectType.SpellcraftGem)
                        {
                            if (m_owner.GetCraftingSkillValue(eCraftingSkill.SpellCrafting) > 0)
                            {
                                skill = CraftingMgr.getSkillbyEnum(eCraftingSkill.SpellCrafting);
                                break;
                            }
                        }
                    }
                }
                // --------------------------------------------------------------
                if (skill != null && skill is AdvancedCraftingSkill && itemToCombine != null)
                {
                    if (((AdvancedCraftingSkill)skill).IsAllowedToCombine(m_owner, itemToCombine))
                    {
                        if (skill is SpellCrafting)
                        {
                            ((SpellCrafting)skill).ShowSpellCraftingInfos(m_owner, itemToCombine);
                        }
                    }
                }

                m_owner.Out.SendTradeWindow();
            }
        }
        /// <summary>
        /// Called each time a player push the accept button to accept the trade
        /// </summary>
        public bool AcceptTrade()
        {
            m_tradeAccept = true;

            lock (Sync)
            {
                // --------------------------------------------------------------
                // Luhz Crafting Update:
                // Players may now have any, and all, "primary" crafting skills.
                // AbstractCraftingSkill skill = CraftingMgr.getSkillbyEnum(m_owner.CraftingPrimarySkill);
                AbstractCraftingSkill skill = null;
                lock (m_owner.TradeWindow.Sync)
                {
                    foreach (InventoryItem i in (ArrayList)m_owner.TradeWindow.TradeItems.Clone())
                    {
                        if (i.Object_Type == (int)eObjectType.AlchemyTincture)
                        {
                            if (m_owner.GetCraftingSkillValue(eCraftingSkill.Alchemy) > 0)
                            {
                                skill = CraftingMgr.getSkillbyEnum(eCraftingSkill.Alchemy);
                                break;
                            }
                        }
                        else if (i.Object_Type == (int)eObjectType.SpellcraftGem)
                        {
                            if (m_owner.GetCraftingSkillValue(eCraftingSkill.SpellCrafting) > 0)
                            {
                                skill = CraftingMgr.getSkillbyEnum(eCraftingSkill.SpellCrafting);
                                break;
                            }
                        }
                    }
                }

                // --------------------------------------------------------------
                if (skill != null && skill is AdvancedCraftingSkill)
                {
                    ((AdvancedCraftingSkill)skill).CombineItems(m_owner);
                }
            }

            CloseTrade();
            return(true);
        }
Exemple #3
0
        /// <summary>
        /// Called each time a player push the accept button to accept the trade
        /// </summary>
        public bool AcceptTrade()
        {
            lock (Sync)
            {
                m_tradeAccept = true;
                GamePlayer partner = m_partnerWindow.Owner;

                partner.Out.SendMessage(m_owner.Name + " has accepted the trade.", eChatType.CT_System, eChatLoc.CL_SystemWindow);

                // Check if the tradepartner has also agreed to the trade
                if (!m_partnerWindow.m_tradeAccept)
                {
                    return(false);
                }

                bool logTrade = ServerProperties.Properties.LOG_TRADES;
                if (m_owner.Client.Account.PrivLevel > 1 || partner.Client.Account.PrivLevel > 1)
                {
                    logTrade = true;
                }

                //Test if we and our partner have enough money
                bool enoughMoney        = m_owner.RemoveMoney(TradeMoney);
                bool partnerEnoughMoney = partner.RemoveMoney(m_partnerWindow.TradeMoney);

                //Check the preconditions
                if (!enoughMoney || !partnerEnoughMoney)
                {
                    if (!enoughMoney)
                    {
                        //Reset the money if we don't have enough
                        TradeMoney = 0;
                        if (partnerEnoughMoney)
                        {
                            partner.AddMoney(m_partnerWindow.TradeMoney);
                            InventoryLogging.LogInventoryAction(partner, m_owner, eInventoryActionType.Trade, m_partnerWindow.TradeMoney);
                        }

                        m_owner.Out.SendMessage("You don't have enough money.", eChatType.CT_Merchant, eChatLoc.CL_SystemWindow);
                        partner.Out.SendMessage(m_owner.Name + " doesn't have enough money.", eChatType.CT_Merchant, eChatLoc.CL_SystemWindow);
                    }
                    if (!partnerEnoughMoney)
                    {
                        //Reset the money if our partner doesn't have enough
                        m_partnerWindow.TradeMoney = 0;
                        if (enoughMoney)
                        {
                            m_owner.AddMoney(TradeMoney);
                            InventoryLogging.LogInventoryAction(m_owner, partner, eInventoryActionType.Trade, TradeMoney);
                        }

                        partner.Out.SendMessage("You don't have enough money.", eChatType.CT_Merchant, eChatLoc.CL_SystemWindow);
                        m_owner.Out.SendMessage(partner.Name + " doesn't have enough money.", eChatType.CT_Merchant, eChatLoc.CL_SystemWindow);
                    }

                    //Update our tradewindow and return
                    TradeUpdate();
                    return(false);
                }

                if (m_combine == true)
                {
                    GamePlayer crafter = (m_recipiant == true ? m_owner : partner);
                    // --------------------------------------------------------------
                    // Luhz Crafting Update:
                    // Players may now have any, and all, "primary" crafting skills.
                    // AbstractCraftingSkill skill = CraftingMgr.getSkillbyEnum(crafter.CraftingPrimarySkill);
                    AbstractCraftingSkill skill = null;
                    lock (crafter.TradeWindow.Sync)
                    {
                        foreach (InventoryItem i in (ArrayList)crafter.TradeWindow.TradeItems.Clone())
                        {
                            if (i.Object_Type == (int)eObjectType.AlchemyTincture)
                            {
                                if (m_owner.GetCraftingSkillValue(eCraftingSkill.Alchemy) > 0)
                                {
                                    skill = CraftingMgr.getSkillbyEnum(eCraftingSkill.Alchemy);
                                    break;
                                }
                            }
                            else if (i.Object_Type == (int)eObjectType.SpellcraftGem)
                            {
                                if (crafter.GetCraftingSkillValue(eCraftingSkill.SpellCrafting) > 0)
                                {
                                    skill = CraftingMgr.getSkillbyEnum(eCraftingSkill.SpellCrafting);
                                    break;
                                }
                            }
                        }
                    }
                    // --------------------------------------------------------------
                    if (skill != null && skill is AdvancedCraftingSkill)
                    {
                        ((AdvancedCraftingSkill)skill).CombineItems(crafter);
                    }
                }
                else if (m_repair == true)
                {
                    GamePlayer    crafter      = (m_recipiant == true ? m_owner : partner);
                    InventoryItem itemToRepair = (InventoryItem)(m_recipiant == true ? m_partnerWindow.TradeItems[0] : TradeItems[0]);
                    if (itemToRepair != null)
                    {
                        crafter.RepairItem(itemToRepair);
                    }
                }
                else
                {
                    //Calculate the count of items
                    int myTradeItemsCount      = m_tradeItems.Count;
                    int partnerTradeItemsCount = m_partnerWindow.TradeItems.Count;

                    //Test if we and our partner have enough space in inventory
                    int  mySpaceNeeded      = Math.Max(0, partnerTradeItemsCount - myTradeItemsCount);
                    int  partnerSpaceNeeded = Math.Max(0, myTradeItemsCount - partnerTradeItemsCount);
                    bool enoughSpace        = m_owner.Inventory.IsSlotsFree(mySpaceNeeded, eInventorySlot.FirstBackpack, eInventorySlot.LastBackpack);
                    bool partnerEnoughSpace = partner.Inventory.IsSlotsFree(partnerSpaceNeeded, eInventorySlot.FirstBackpack, eInventorySlot.LastBackpack);

                    //Check the preconditions
                    if (!enoughSpace || !partnerEnoughSpace)
                    {
                        if (!enoughSpace)
                        {
                            m_owner.Out.SendMessage("You don't have enough space in your inventory.", eChatType.CT_Merchant, eChatLoc.CL_SystemWindow);
                            partner.Out.SendMessage(m_owner.Name + " doesn't have enough space in his inventory.", eChatType.CT_Merchant, eChatLoc.CL_SystemWindow);
                        }
                        if (!partnerEnoughSpace)
                        {
                            partner.Out.SendMessage("You don't have enough space in your inventory.", eChatType.CT_Merchant, eChatLoc.CL_SystemWindow);
                            m_owner.Out.SendMessage(partner.Name + " doesn't have enough space in his inventory.", eChatType.CT_Merchant, eChatLoc.CL_SystemWindow);
                        }

                        //Update our tradewindow and return
                        TradeUpdate();

                        //This was already removed above, needs to be returned to the players on trade failure.
                        m_owner.AddMoney(TradeMoney);
                        partner.AddMoney(m_partnerWindow.TradeMoney);

                        return(false);
                    }

                    //Now transfer everything
                    m_owner.Inventory.BeginChanges();
                    partner.Inventory.BeginChanges();
                    m_changesCount++;
                    m_partnerWindow.m_changesCount++;

                    // must be cloned because Inventory.RemoveItem removes it from trade window
                    ArrayList ownerTradeItems   = (ArrayList)TradeItems.Clone();
                    ArrayList partnerTradeItems = (ArrayList)m_partnerWindow.TradeItems.Clone();

                    // remove all items first to make sure there is enough space
                    // if inventory is full but removed items count >= received count
                    foreach (InventoryItem item in ownerTradeItems)
                    {
                        lock (m_owner.Inventory)
                        {
                            if (!m_owner.Inventory.RemoveTradeItem(item))
                            {
                                if (logTrade)
                                {
                                    GameServer.Instance.LogGMAction("   NOTItem: " + m_owner.Name + "(" + m_owner.Client.Account.Name + ") -> " + partner.Name + "(" + partner.Client.Account.Name + ") : " + item.Name + "(" + item.Id_nb + ")");
                                }

                                //BOT.Ban(m_owner, "Trade Hack");
                                //BOT.Ban(partner, "Trade Hack");

                                return(false);
                            }
                        }
                    }
                    foreach (InventoryItem item in partnerTradeItems)
                    {
                        lock (partner.Inventory)
                        {
                            if (!partner.Inventory.RemoveTradeItem(item))
                            {
                                if (logTrade)
                                {
                                    GameServer.Instance.LogGMAction("   NOTItem: " + m_owner.Name + "(" + m_owner.Client.Account.Name + ") -> " + partner.Name + "(" + partner.Client.Account.Name + ") : " + item.Name + "(" + item.Id_nb + ")");
                                }

                                //BOT.Ban(m_owner, "Trade Hack");
                                //BOT.Ban(partner, "Trade Hack");

                                return(false);
                            }
                        }
                    }

                    foreach (InventoryItem item in ownerTradeItems)
                    {
                        if (m_owner.Guild != partner.Guild)
                        {
                            item.Emblem = 0;
                        }

                        bool tradeSuccess = false;

                        if (item.IsDeleted)
                        {
                            tradeSuccess = partner.Inventory.AddItem(eInventorySlot.FirstEmptyBackpack, item);
                        }
                        else
                        {
                            tradeSuccess = partner.Inventory.AddTradeItem(eInventorySlot.FirstEmptyBackpack, item);
                        }

                        if (!tradeSuccess)
                        {
                            log.Error("Trade item was not added to Partner first free slot.  Owner = " + m_owner.Name + ", Partner = " + partner.Name + "; Item = " + item.Id_nb);
                        }
                        else
                        {
                            InventoryLogging.LogInventoryAction(m_owner, partner, eInventoryActionType.Trade, item.Template, item.Count);
                            if (logTrade)
                            {
                                GameServer.Instance.LogGMAction("   Item: " + m_owner.Name + "(" + m_owner.Client.Account.Name + ") -> " + partner.Name + "(" + partner.Client.Account.Name + ") : " + item.Name + "(" + item.Id_nb + ")");
                            }
                        }
                    }

                    foreach (InventoryItem item in partnerTradeItems)
                    {
                        if (m_owner.Guild != partner.Guild)
                        {
                            item.Emblem = 0;
                        }

                        bool tradeSuccess = false;

                        if (item.IsDeleted)
                        {
                            tradeSuccess = m_owner.Inventory.AddItem(eInventorySlot.FirstEmptyBackpack, item);
                        }
                        else
                        {
                            tradeSuccess = m_owner.Inventory.AddTradeItem(eInventorySlot.FirstEmptyBackpack, item);
                        }

                        if (!tradeSuccess)
                        {
                            log.Error("Trade item was not added to Owner first free slot.  Owner = " + m_owner.Name + ", Partner = " + partner.Name + "; Item = " + item.Id_nb);
                        }
                        else
                        {
                            InventoryLogging.LogInventoryAction(partner, m_owner, eInventoryActionType.Trade, item.Template, item.Count);
                            if (logTrade)
                            {
                                GameServer.Instance.LogGMAction("   Item: " + partner.Name + "(" + partner.Client.Account.Name + ") -> " + m_owner.Name + "(" + m_owner.Client.Account.Name + ") : " + item.Name + "(" + item.Id_nb + ")");
                            }
                        }
                    }

                    m_owner.Inventory.CommitChanges();
                    partner.Inventory.CommitChanges();
                    m_changesCount--;
                    m_partnerWindow.m_changesCount--;

                    m_owner.Out.SendMessage("Trade Completed. " + myTradeItemsCount + " items for " + partnerTradeItemsCount + " items.", eChatType.CT_System, eChatLoc.CL_SystemWindow);
                    partner.Out.SendMessage("Trade Completed. " + partnerTradeItemsCount + " items for " + myTradeItemsCount + " items.", eChatType.CT_System, eChatLoc.CL_SystemWindow);

                    m_owner.Inventory.SaveIntoDatabase(m_owner.InternalID);
                    partner.Inventory.SaveIntoDatabase(partner.InternalID);
                }

                if (logTrade)
                {
                    if (m_partnerWindow.TradeMoney > 0)
                    {
                        GameServer.Instance.LogGMAction("  Money: " + partner.Name + "(" + partner.Client.Account.Name + ") -> " + m_owner.Name + "(" + m_owner.Client.Account.Name + ") : " + m_partnerWindow.TradeMoney + "coppers");
                    }
                    if (TradeMoney > 0)
                    {
                        GameServer.Instance.LogGMAction("  Money: " + m_owner.Name + "(" + m_owner.Client.Account.Name + ") -> " + partner.Name + "(" + partner.Client.Account.Name + ") : " + TradeMoney + "coppers");
                    }
                }

                if (TradeMoney > 0 || m_partnerWindow.TradeMoney > 0)
                {
                    //Now add the money
                    m_owner.AddMoney(m_partnerWindow.TradeMoney, "You get {0}.");
                    partner.AddMoney(TradeMoney, "You get {0}.");
                    InventoryLogging.LogInventoryAction(m_owner, partner, eInventoryActionType.Trade, TradeMoney);
                    InventoryLogging.LogInventoryAction(partner, m_owner, eInventoryActionType.Trade, m_partnerWindow.TradeMoney);
                    m_owner.SaveIntoDatabase();
                    partner.SaveIntoDatabase();
                }

                CloseTrade();                // Close the Trade Window

                return(true);
            }
        }