Exemple #1
0
        public virtual Result <SellToVendorResult <T> > SellToVendor(ICustomer <T> customer, IVendorProduct <T> product, int amount = 1)
        {
            var canSell = CanSellToVendor(customer, product, amount);

            if (canSell.result == false)
            {
                return(new Result <SellToVendorResult <T> >(null, canSell.error));
            }

            var originalAmount = product.collectionEntry?.amount;

            customer.RemoveItem(product.item, amount);

            var soldProduct = product;

            if (config.addSoldProductToVendor)
            {
                // NOTE: If we're removing the entire stack from the customer's collection it's safe the move everything over.
                // NOTE: If we're moving a part of the stack, duplicate it and move the duplicates.
                if (amount < originalAmount)
                {
                    // Not selling the entire stack; Duplicate the item for the next slot.
                    soldProduct = (IVendorProduct <T>)product.Clone();
                }

                vendorCollection.Add(soldProduct, amount);
            }

            foreach (var c in product.sellPrice)
            {
                vendorCurrencies.Remove(c.currency, c.amount * amount * config.sellToVendorPriceMultiplier);
                customer.AddCurrency(c.currency, c.amount * amount * config.sellToVendorPriceMultiplier);
            }

            var sellResult = new SellToVendorResult <T>(customer, soldProduct, amount, soldProduct.sellPrice);

            OnSoldToVendor?.Invoke(this, sellResult);
            return(sellResult);
        }