示例#1
0
        public void BuyBackItem(Character chr, Item item)
        {
            var inv       = chr.Inventory;
            var newSlotId = inv.FindFreeSlot(item, item.Amount, false);

            var err = newSlotId.Container.CheckAdd(newSlotId.Slot, item, item.Amount);

            if (err == InventoryError.OK)
            {
                var amount = item.Amount;
                inv.CheckUniqueness(item, ref amount, ref err, true);
                if (err == InventoryError.OK && amount != item.Amount)
                {
                    err = InventoryError.CANT_CARRY_MORE_OF_THIS;
                }
                else
                {
                    if (newSlotId.Slot != BaseInventory.INVALID_SLOT)
                    {
                        var price = ((uint)item.Amount * item.Template.SellPrice);
                        if (chr.Money < price)
                        {
                            NPCHandler.SendBuyError(chr.Client, NPC, (ItemId)item.EntryId, BuyItemError.NotEnoughMoney);
                            return;
                        }
                        else
                        {
                            item.Remove(false);
                            newSlotId.Container.AddUnchecked(newSlotId.Slot, item, true);
                            chr.Money -= price;

                            NPCHandler.SendBuyItem(chr.Client, NPC, item.Template.ItemId, amount);
                            return;
                        }
                    }
                    else
                    {
                        err = InventoryError.INVENTORY_FULL;
                    }
                }
            }

            ItemHandler.SendInventoryError(chr.Client, err);
        }
示例#2
0
        public void BuyItem(Character chr, uint itemEntryId, BaseInventory bag, int amount, int slot)
        {
            if (!CheckVendorInteraction(chr))
            {
                return;
            }

            var item = GetVendorItem(itemEntryId);

            if (item == null)
            {
                return;
            }

            amount = Math.Max(amount, 1);
            if (item.BuyStackSize > 0)
            {
                amount = amount * item.BuyStackSize;
            }

            uint price;
            var  buyErr = CanPlayerBuyItem(chr, item, amount, out price);

            if (buyErr != BuyItemError.Ok)
            {
                NPCHandler.SendBuyError(chr, NPC, item.Template.ItemId, buyErr);
                return;
            }

            BaseInventory  inv = chr.Inventory;
            InventoryError err;

            if (inv.IsValidSlot(slot))
            {
                err = inv.TryAdd(item.Template, ref amount, slot);
            }
            else
            {
                // count will be set to the actual amount of items that found space in the inventory
                // if not all could be added, err contains the reason why
                err = inv.TryAdd(item.Template, ref amount);
            }

            if (err != InventoryError.OK)
            {
                ItemHandler.SendInventoryError(chr.Client, null, null, err);
            }

            if (amount <= 0)
            {
                // Nothing was purchased
                // Should usually never happen, but just to make sure
                ItemHandler.SendInventoryError(chr.Client, null, null, InventoryError.INVENTORY_FULL);
                return;
            }

            chr.Money -= (price * (uint)amount);             // we already checked that our money is sufficient
            if (item.ExtendedCostEntry != null)
            {
                var exCost = item.ExtendedCostEntry;

                chr.HonorPoints -= exCost.HonorCost;
                chr.ArenaPoints -= exCost.ArenaPointCost;

                foreach (var reqItem in exCost.RequiredItems)
                {
                    if (reqItem.Id == ItemId.None)
                    {
                        break;
                    }

                    if (!chr.Inventory.RemoveByItemId(reqItem.Id, reqItem.Cost, false))
                    {
                        // should not happen
                        LogManager.GetCurrentClassLogger().Warn("Unable to remove required item \"{0}\" from player \"{1}\" when purchasing item: {2}",
                                                                reqItem.Template, chr, item.Template);
                    }
                }
            }

            // manage stock
            int remainingAmount;

            if (item.RemainingStockAmount != UnlimitedSupply)
            {
                // The vendor had a limited supply of this item, update the chr.Client with the new inventory
                remainingAmount = item.RemainingStockAmount - amount;
                //NPCHandler.SendVendorInventoryList(chr.Client, NPC.EntityId, ConstructVendorItemList(chr));
            }
            else
            {
                remainingAmount = UnlimitedSupply;
            }

            // send packet
            NPCHandler.SendBuyItem(chr.Client, NPC, item.Template.ItemId, amount, remainingAmount);
        }