Esempio n. 1
0
        /// <summary>
        /// returns how much was sold de facto
        /// new version of buy-old,
        /// real deal. If not enough money to buy (including deposits) then buys some part of desired
        /// </summary>
        internal Storage Sell(Consumer buyer, Storage whatWantedToBuy)
        {
            if (whatWantedToBuy.isNotZero())
            {
                Storage sale;
                if (whatWantedToBuy.Product.isAbstract())
                {
                    sale = marketPrice.ConvertToRandomCheapestExistingSubstitute(whatWantedToBuy);
                    if (sale == null)//no substitution available on market
                    {
                        return(new Storage(whatWantedToBuy.Product));
                    }
                    else if (sale.isZero())
                    {
                        return(sale);
                    }
                }
                else
                {
                    sale = whatWantedToBuy;
                }

                Storage   howMuchCanConsume;
                MoneyView price = getCost(sale.Product);
                MoneyView cost;

                if (World.market.sentToMarket.has(sale))
                {
                    cost = getCost(sale);

                    if (buyer.CanPay(cost))
                    {
                        Ssssel(buyer, cost, sale);
                        return(sale);
                    }
                    else
                    {
                        float val = (float)(buyer.getMoneyAvailable().Get() / price.Get());
                        howMuchCanConsume = new Storage(sale.Product, val);
                        howMuchCanConsume.Subtract(0.001f, false); // to fix percision bug
                        if (howMuchCanConsume.isZero())
                        {
                            return(howMuchCanConsume);
                        }
                        else
                        {
                            Ssssel(buyer, getCost(howMuchCanConsume), howMuchCanConsume);
                            return(howMuchCanConsume);
                        }
                    }
                }
                else
                {
                    // assuming available < buying
                    Storage howMuchAvailable = new Storage(World.market.HowMuchAvailable(sale));
                    if (howMuchAvailable.isNotZero())
                    {
                        cost = getCost(howMuchAvailable);
                        if (buyer.CanPay(cost))
                        {
                            Ssssel(buyer, cost, howMuchAvailable);
                            return(howMuchAvailable);
                        }
                        else
                        {
                            howMuchCanConsume = new Storage(howMuchAvailable.Product, (float)(buyer.getMoneyAvailable().Get() / price.Get()));

                            if (howMuchCanConsume.get() > howMuchAvailable.get())
                            {
                                howMuchCanConsume.Set(howMuchAvailable.get()); // you don't buy more than there is
                            }
                            howMuchCanConsume.Subtract(0.001f, false);         // to fix percision bug
                            if (howMuchCanConsume.isNotZero())
                            {
                                Ssssel(buyer, buyer.getMoneyAvailable(), howMuchCanConsume);//pay all money cause you don't have more
                                return(howMuchCanConsume);
                            }
                            else
                            {
                                return(howMuchCanConsume);
                            }
                        }
                    }
                    else
                    {
                        return(howMuchAvailable);
                    }
                }
            }
            else
            {
                return(whatWantedToBuy); // assuming buying is empty here
            }
        }