コード例 #1
0
        public static void HandleVendorPurchase(WorldSession session, ClientVendorPurchase vendorPurchase)
        {
            VendorInfo vendorInfo = session.Player.SelectedVendorInfo;

            if (vendorInfo == null)
            {
                return;
            }

            EntityVendorItemModel vendorItem = vendorInfo.GetItemAtIndex(vendorPurchase.VendorIndex);

            if (vendorItem == null)
            {
                return;
            }

            Item2Entry itemEntry      = GameTableManager.Instance.Item.GetEntry(vendorItem.ItemId);
            float      costMultiplier = vendorInfo.BuyPriceMultiplier * vendorPurchase.VendorItemQty;

            // do all sanity checks before modifying currency
            var currencyChanges = new List <(CurrencyType CurrencyTypeId, ulong CurrencyAmount)>();

            for (int i = 0; i < itemEntry.CurrencyTypeId.Length; i++)
            {
                CurrencyType currencyId = (CurrencyType)itemEntry.CurrencyTypeId[i];
                if (currencyId == CurrencyType.None)
                {
                    continue;
                }

                ulong currencyAmount = (ulong)(itemEntry.CurrencyAmount[i] * costMultiplier);
                if (!session.Player.CurrencyManager.CanAfford(currencyId, currencyAmount))
                {
                    return;
                }

                currencyChanges.Add((currencyId, currencyAmount));
            }

            foreach ((CurrencyType currencyTypeId, ulong currencyAmount) in currencyChanges)
            {
                session.Player.CurrencyManager.CurrencySubtractAmount(currencyTypeId, currencyAmount);
            }

            session.Player.Inventory.ItemCreate(InventoryLocation.Inventory, itemEntry.Id, vendorPurchase.VendorItemQty * itemEntry.BuyFromVendorStackCount);
        }