public void SetToSell(MoneyView sum) { if (howMuchOwns.Get() - howMuchWantsToSell.Get() - sum.Get() < 0m) { howMuchWantsToSell.Set(howMuchOwns); } else { howMuchWantsToSell.Add(sum); } }
/// <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 } }
/// <summary> /// Buys, returns actually bought, subsidizations allowed, uses deposits if available /// </summary> //public Storage Buy(Storage need)// old market.Sell //{ // if (this.CanAfford(need)) // { // return Buy(need); // } // //todo fix that - return subsidies // //else if (subsidizer.GiveFactorySubsidies(toWhom, toWhom.HowMuchLacksMoneyIncludingDeposits(getCost(need)))) // //{ // // return Sell(toWhom, need); // //} // else // return new Storage(need.Product, 0f); //} /// <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> protected Storage Buy(Storage need) // old Market.Sell { if (need.isNotZero()) { Market market = Market.GetCheapestMarket(need); Storage sale; if (need.Product.isAbstract()) { sale = market.prices.ConvertToRandomCheapestExistingSubstitute(need, Country.market); if (sale == null)//no substitution available on market { return(new Storage(need.Product)); } else if (sale.isZero()) { return(sale); } } else { sale = need; } Storage howMuchCanConsume; MoneyView price = Country.market.getCost(sale.Product); MoneyView cost; if (market.HasAvailable(sale)) { cost = Country.market.getCost(sale); if (this.CanPay(cost)) { this.Buy_utility(market, cost, sale); return(sale); } else { float val = (float)(this.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 { this.Buy_utility(market, Country.market.getCost(howMuchCanConsume), howMuchCanConsume); return(howMuchCanConsume); } } } else { // assuming available < buying Storage howMuchAvailable = new Storage(market.HowMuchAvailable(sale)); if (howMuchAvailable.isNotZero()) { cost = Country.market.getCost(howMuchAvailable); if (this.CanPay(cost)) { this.Buy_utility(market, cost, howMuchAvailable); return(howMuchAvailable); } else { howMuchCanConsume = new Storage(howMuchAvailable.Product, (float)(this.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 precision bug if (howMuchCanConsume.isNotZero()) { this.Buy_utility(market, this.getMoneyAvailable(), howMuchCanConsume);//pay all money cause you don't have more return(howMuchCanConsume); } else { return(howMuchCanConsume); } } } else { return(howMuchAvailable); } } } else { return(need); // assuming buying is empty here } }