Пример #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
            }
        }
Пример #2
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 buy(Consumer buyer, Storage whatWantedToBuy)
 {
     if (whatWantedToBuy.isNotZero())
     {
         Storage buying;
         if (whatWantedToBuy.Product.isAbstract())
         {
             buying = marketPrice.ConvertToRandomCheapestExistingSubstitute(whatWantedToBuy);
             if (buying == null)//no substitution available on market
             {
                 return(new Storage(whatWantedToBuy.Product));
             }
         }
         else
         {
             buying = whatWantedToBuy;
         }
         Storage howMuchCanConsume;
         Value   price = getPrice(buying.Product);
         Value   cost;
         if (Game.market.sentToMarket.has(buying))
         {
             cost = buying.Copy().Multiply(price);
             //if (cost.isNotZero())
             //{
             if (buyer.CanPay(cost))
             {
                 buyer.Pay(Game.market, cost);
                 buyer.consumeFromMarket(buying);
                 if (buyer is SimpleProduction)
                 {
                     (buyer as SimpleProduction).getInputProductsReserve().Add(buying);
                 }
                 howMuchCanConsume = buying;
             }
             else
             {
                 float val = buyer.Cash.get() / price.get();
                 val = Mathf.Floor(val * Value.Precision) / Value.Precision;
                 howMuchCanConsume = new Storage(buying.Product, val);
                 buyer.Pay(Game.market, howMuchCanConsume.Copy().Multiply(price));
                 buyer.consumeFromMarket(howMuchCanConsume);
                 if (buyer is SimpleProduction)
                 {
                     (buyer as SimpleProduction).getInputProductsReserve().Add(howMuchCanConsume);
                 }
             }
             //}
             //else
             //    return new Storage(buying.Product, 0f);
         }
         else
         {
             // assuming available < buying
             Storage howMuchAvailable = new Storage(Game.market.HowMuchAvailable(buying));
             if (howMuchAvailable.get() > 0f)
             {
                 cost = howMuchAvailable.Copy().Multiply(price);
                 if (buyer.CanPay(cost))
                 {
                     buyer.Pay(Game.market, cost);
                     buyer.consumeFromMarket(howMuchAvailable);
                     if (buyer is SimpleProduction)
                     {
                         (buyer as SimpleProduction).getInputProductsReserve().Add(howMuchAvailable);
                     }
                     howMuchCanConsume = howMuchAvailable;
                 }
                 else
                 {
                     howMuchCanConsume = new Storage(howMuchAvailable.Product, buyer.Cash.get() / price.get());
                     if (howMuchCanConsume.get() > howMuchAvailable.get())
                     {
                         howMuchCanConsume.Set(howMuchAvailable.get()); // you don't buy more than there is
                     }
                     if (howMuchCanConsume.isNotZero())
                     {
                         buyer.PayAllAvailableMoney(Game.market); //pay all money cause you don't have more
                         buyer.consumeFromMarket(howMuchCanConsume);
                         if (buyer is SimpleProduction)
                         {
                             (buyer as SimpleProduction).getInputProductsReserve().Add(howMuchCanConsume);
                         }
                     }
                 }
             }
             else
             {
                 howMuchCanConsume = new Storage(buying.Product, 0f);
             }
         }
         return(howMuchCanConsume);
     }
     else
     {
         return(whatWantedToBuy); // assuming buying is empty here
     }
 }