コード例 #1
0
        public virtual Result <bool> CanBuyFromVendor(ICustomer <T> customer, T item, int amount = 1)
        {
            //
            // Vendor
            //
            var index = vendorCollection.IndexOf(o => o?.item != null && o.item.Equals(item));

            if (index == -1)
            {
                return(new Result <bool>(false, Errors.VendorDoesNotContainItem));
            }

            var product = vendorCollection[index];

            if (product.buyPrice.Length == 0)
            {
                return(new Result <bool>(false, Errors.VendorProductHasNoPrice));
            }

            var canRemove = vendorCollection.CanRemove(product, amount);

            if (canRemove.result == false)
            {
                return(canRemove);
            }

            foreach (var c in product.buyPrice)
            {
                var vendorCanAddCurrency = vendorCurrencies.CanAdd(c.currency, c.amount * amount * config.buyFromVendorPriceMultiplier);
                if (vendorCanAddCurrency.result == false)
                {
                    return(vendorCanAddCurrency);
                }
            }


            //
            // Customer
            //
            var canAdd = customer.CanAddItem(product.item, amount);

            if (canAdd.result == false)
            {
                return(canAdd);
            }

            foreach (var c in product.buyPrice)
            {
                var canRemoveCurrency = customer.CanRemoveCurrency(c.currency, c.amount * amount * config.buyFromVendorPriceMultiplier);
                if (canRemoveCurrency.result == false)
                {
                    return(canRemoveCurrency);
                }
            }

            return(true);
        }