Beispiel #1
0
        protected override void Trade(ITradeInventory shop, ITradeInventory customer, ITradeableItem item, int desiredQuantity, float unitPrice)
        {
            int availableQuantity = 0;

            if (shop.Items.Contains(item.InventoryItem))//Only do the trade if the item is in the shop's inventory
            {
                //1. Get the actual available qunatity
                if (item.InventoryItem.Stock >= desiredQuantity)
                {
                    availableQuantity = desiredQuantity;
                }
                else
                {
                    availableQuantity = item.InventoryItem.Stock;
                }
                //2. From the qunatity available, get the amount the buyer can carry
                int quantityBuyerCanCarry = customer.QuantityCanAdd(item.InventoryItem, availableQuantity);

                float totalBuyPrice = unitPrice * quantityBuyerCanCarry;

                int quantityBuyerCanAfford = 0;
                //3. From the amount the buyer can carry, get the amount the buyer can afford
                if (customer.Currency >= totalBuyPrice)
                {
                    quantityBuyerCanAfford = quantityBuyerCanCarry;
                }
                else
                {
                    quantityBuyerCanAfford = (int)(customer.Currency / unitPrice);
                    if (customer.Currency <= 0)
                    {
                        quantityBuyerCanAfford = 0;
                    }
                }

                float actualBuyPrice = unitPrice * quantityBuyerCanAfford;

                if (customer.Currency >= actualBuyPrice && quantityBuyerCanCarry > 0)
                {
                    customer.Currency -= actualBuyPrice;
                    shop.Currency     += actualBuyPrice;
                    InventoryItem buyerItem = item.InventoryItem.SplitStack(quantityBuyerCanAfford);
                    customer.AddItem(buyerItem);
                }
            }
        }