Esempio n. 1
0
        private void Ssssel(Consumer buyer, MoneyView cost, Storage sale)
        {
            buyer.Pay(World.market, cost);
            buyer.consumeFromMarket(sale);
            var isSP = buyer as SimpleProduction;

            if (isSP != null)
            {
                isSP.getInputProductsReserve().Add(sale);
            }
        }
Esempio n. 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
     }
 }