private void stopUpgrading()
 {
     currentInvestor = null;
     building        = false;
     upgrading       = false;
     constructionNeeds.setZero();
     daysInConstruction = 0;
 }
 private void onConstructionComplete(bool freshlyBuild)
 {
     level++;
     building  = false;
     upgrading = false;
     constructionNeeds.setZero();
     daysInConstruction = 0;
     if (freshlyBuild)
     {
         open(currentInvestor, false);
     }
     currentInvestor = null;
 }
Example #3
0
        /// <summary>
        /// Readonly !!
        /// </summary>
        public ReadOnlyValue HowMuchSelling(IShareOwner owner)
        {
            Share record;

            if (ownership.TryGetValue(owner, out record))
            {
                return(new Procent(record.GetShareForSale(), GetAssetsValue()));
            }
            else
            {
                return(Procent.ZeroProcent);
            }
        }
Example #4
0
        /// <summary>
        /// Readonly !!
        /// </summary>
        internal Procent HowMuchOwns(IShareOwner owner)
        {
            Share record;

            if (ownership.TryGetValue(owner, out record))
            {
                return(new Procent(record.GetShare(), GetAllAssetsValue()));
            }
            else
            {
                return(Procent.ZeroProcent.Copy());
            }
        }
Example #5
0
 public Factory BuildFactory(IShareOwner investor, ProductionType type, MoneyView cost, bool instantBuild = false)
 {
     //if (getAllFactories().Any(x => x.Type == type)) //todo temporally
     //{
     //    throw new Exception("Can't have 2 same factory types");
     //}
     //else
     {
         var res = new Factory(this, investor, type, cost, instantBuild);
         allFactories.Add(res);
         return(res);
     }
 }
Example #6
0
        /// <summary>
        /// Buy that share (or less). Assumes that there is something on sale. Assumes that buyer has enough money
        /// </summary>
        public void BuyStandardShare(IShareOwner buyer)
        {
            // what if buys from itself?
            if (HowMuchSelling(buyer).isNotZero())
            {
                CancelBuyOrder(buyer, Options.PopBuyAssetsAtTime);
            }
            else
            {
                //var purchaseValue = GetShareMarketValue(Options.PopBuyAssetsAtTime);
                var purchaseValue = GetInvestmentCost();
                var sharesToBuy   = ownership.Where(x => x.Value.GetShareForSale().IsEqual(purchaseValue));

                if (sharesToBuy.Count() == 0)
                {
                    //if no equal sharesToBuy find smaller one
                    sharesToBuy = ownership.Where(x => x.Value.GetShareForSale().isSmallerThan(purchaseValue) &&
                                                  x.Value.GetShareForSale().isNotZero());
                }
                if (sharesToBuy.Count() == 0)
                {
                    //if no smaller sharesToBuy find bigger one
                    sharesToBuy = ownership.Where(x => x.Value.GetShareForSale().isBiggerThan(purchaseValue) &&
                                                  x.Value.GetShareForSale().isNotZero());
                }
                if (sharesToBuy.Count() != 0)
                {
                    var shareToBuy = sharesToBuy.Random();
                    var cost       = shareToBuy.Value.GetShareForSale();
                    if (cost.isBiggerThan(purchaseValue))
                    {
                        cost = purchaseValue;
                    }
                    var buyingAgent = buyer as Agent;

                    if (buyingAgent.Pay(shareToBuy.Key as Agent, cost))
                    {
                        if (Game.logInvestments)
                        {
                            var boughtProcent = new Procent(cost, parent.ownership.totallyInvested);
                            Debug.Log(buyer + " bough " + boughtProcent + " shares (" + cost + ") of " + parent + " from " + shareToBuy.Key + " awaiting " + GetMargin() + " margin");
                        }
                        Transfer(shareToBuy.Key, buyer, cost);
                        //reduce onSale amount on successful deal

                        shareToBuy.Value.ReduceSale(cost);
                    }
                }
            }
        }
Example #7
0
        public void CancelBuyOrder(IShareOwner owner, Procent share)
        {
            Share record;

            if (ownership.TryGetValue(owner, out record))
            {
                var value = GetShareAssetsValue(share);
                record.CancelBuyOrder(value);
            }
            else
            {
                Debug.Log("No such owner");
            }
        }
Example #8
0
        public void SetToSell(IShareOwner owner, Procent share, bool showMessageAboutOperationFails = true)
        {
            Share record;

            if (ownership.TryGetValue(owner, out record))
            {
                var value = GetShareAssetsValue(share);
                record.SetToSell(value);
            }
            else if (showMessageAboutOperationFails)
            {
                Debug.Log("No such owner");
            }
        }
 internal void upgrade(IShareOwner byWhom)
 {
     currentInvestor = byWhom;
     upgrading       = true;
     constructionNeeds.Add(getUpgradeNeeds());
     if ((byWhom as Agent).GetCountry().economy.getValue() != Economy.PlannedEconomy)
     {
         var cost = Game.market.getCost(getUpgradeNeeds());
         (byWhom as Agent).payWithoutRecord(this, cost);
         ownership.Add(byWhom, cost);
         //Debug.Log(byWhom + " invested " + cost + " in upgrading " + this);
     }
     //else
     //    Debug.Log(byWhom + " invested in upgrading " + this);
 }
Example #10
0
 // temporally
 public static IEnumerable <KeyValuePair <IShareable, Procent> > GetAllShares(IShareOwner owner)
 {
     foreach (var item in AllExistingCountries())
     {
         foreach (var factory in item.Provinces.AllFactories)
         {
             foreach (var record in factory.ownership.GetAllShares())
             {
                 if (record.Key == owner)
                 {
                     yield return(new KeyValuePair <IShareable, Procent>(factory, record.Value));
                 }
             }
         }
     }
 }
Example #11
0
 public void Add(IShareOwner owner, ReadOnlyValue value)
 {
     //if (IsCorrectData(value.))
     {
         Share record;
         if (ownership.TryGetValue(owner, out record))
         {
             record.Increase(value);
         }
         else
         {
             ownership.Add(owner, new Share(value));
         }
         totallyInvested.Add(value);
         //owner.GetOwnership().Add(parent, value);
     }
 }
        internal void open(IShareOwner byWhom, bool payMoney)
        {
            var agent = byWhom as Agent;

            if (agent.GetCountry().economy.getValue() != Economy.PlannedEconomy)
            {
                salary.set(GetProvince().getLocalMinSalary());
            }
            if (payMoney)
            {
                agent.payWithoutRecord(this, getReopenCost());
                ownership.Add(byWhom, getReopenCost());
                //Debug.Log(byWhom + " invested " + getReopenCost() + " in reopening " + this);
            }
            _isOpen          = true;
            daysUnprofitable = 0;
            daysClosed       = 0;
        }
Example #13
0
        /// <summary>
        /// amount is Market value share, while data stored in assets value
        /// </summary>
        public bool Transfer(IShareOwner oldOwner, IShareOwner newOwner, MoneyView amount)
        {
            var share      = new Procent(amount, GetMarketValue());
            var toTransfer = GetAssetsValue().Copy().Multiply(share); // ownership is stored in money

            Share oldOwnerAsset;

            if (ownership.TryGetValue(oldOwner, out oldOwnerAsset))
            {
                if (oldOwnerAsset.GetShare().isBiggerOrEqual(toTransfer)) // has enough to transfer
                {
                    Share newOwnerAsset;
                    if (ownership.TryGetValue(newOwner, out newOwnerAsset))
                    {
                        newOwnerAsset.Increase(toTransfer);
                    }
                    else
                    {
                        ownership.Add(newOwner, new Share(toTransfer));
                    }

                    Remove(oldOwner, toTransfer);
                    return(true);
                }
                else
                {
                    TransferAll(oldOwner, newOwner);
                    if (Game.logInvestments)
                    {
                        Debug.Log("Not enough property to transfer"); // reduce payment in that case, transfer data in non-market value
                    }
                    return(false);
                }
            }
            else
            {
                Debug.Log("No such owner");
                return(false);
            }
            //}
            //else return false;
        }
Example #14
0
        internal bool Has(IShareOwner owner, Procent share)
        {
            Share found;

            if (ownership.TryGetValue(owner, out found))
            {
                if (found.GetShare().isBiggerOrEqual(GetShareAssetsValue(share)))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Don't call it directly
        /// </summary>

        public Factory(Province province, IShareOwner investor, FactoryType type, Value cost)
            : base(type, province)
        {
            //this.buildByPlannedEconomy = buildByPlannedEconomy;
            ownership = new Owners(this);
            if (investor != null) // that mean that factory is a fake
            {
                currentInvestor = investor;
                //assuming this is level 0 building
                constructionNeeds = new StorageSet(getType().GetBuildNeeds());

                ownership.Add(investor, cost);

                salary.set(province.getLocalMinSalary());
                if (GetCountry().economy.getValue() == Economy.PlannedEconomy)
                {
                    setPriorityAutoWithPlannedEconomy();
                }
                //else
                //    Debug.Log(investor + " invested " + cost + " in building new " + this);
            }
        }
Example #16
0
        /// <summary>
        /// Test it!!
        /// </summary>
        public bool Transfer(IShareOwner oldOwner, IShareOwner newOwner, Value amount)
        {
            //if (IsCorrectData(share.get()))
            //{
            Share oldOwnerAsset;

            if (ownership.TryGetValue(oldOwner, out oldOwnerAsset))
            {
                if (oldOwnerAsset.GetShare().isBiggerOrEqual(amount))
                {
                    Share newOwnerAsset;
                    if (ownership.TryGetValue(newOwner, out newOwnerAsset))
                    {
                        newOwnerAsset.Increase(amount);
                    }
                    else
                    {
                        ownership.Add(newOwner, new Share(amount));
                    }

                    Remove(oldOwner, amount);
                    return(true);
                }
                else
                {
                    TransferAll(oldOwner, newOwner);
                    Debug.Log("Not enough property to transfer");
                    return(false);
                }
            }
            else
            {
                Debug.Log("No such owner");
                return(false);
            }
            //}
            //else return false;
        }
Example #17
0
        internal void TransferAll(IShareOwner oldOwner, IShareOwner newOwner, bool showMessageAboutOperationFails = true)
        {
            Share oldOwnerAsset;

            if (ownership.TryGetValue(oldOwner, out oldOwnerAsset))
            {
                Share newOwnerAsset;
                if (ownership.TryGetValue(newOwner, out newOwnerAsset))
                {
                    newOwnerAsset.Increase(oldOwnerAsset.GetShare());
                }
                else
                {
                    ownership.Add(newOwner, new Share(oldOwnerAsset.GetShare()));
                }

                ownership.Remove(oldOwner);
            }
            else
            if (showMessageAboutOperationFails)
            {
                Debug.Log("No such owner");
            }
        }
Example #18
0
        private void Remove(IShareOwner fromWho, Value howMuchRemove)
        {
            Share record;

            if (ownership.TryGetValue(fromWho, out record))
            {
                if (record.GetShare().isBiggerThan(howMuchRemove))
                {
                    record.Decrease(howMuchRemove);
                }
                else if (record.GetShare().IsEqual(howMuchRemove))
                {
                    ownership.Remove(fromWho);
                }
                else
                {
                    Debug.Log("Doesn't have that much");
                }
            }
            else
            {
                Debug.Log("No such owner");
            }
        }
        /// <summary>
        /// Now includes workforce/efficiency. Also buying for upgrading\building are happening here
        /// </summary>
        override public void consumeNeeds()
        {
            // consume resource needs
            if (IsOpen && !getType().isResourceGathering())
            {
                List <Storage> shoppingList = getHowMuchInputProductsReservesWants();
                if (shoppingList.Count > 0)
                {
                    if (GetCountry().economy.getValue() == Economy.PlannedEconomy)
                    {
                        var realNeed = GetCountry().countryStorageSet.hasAllOfConvertToBiggest(shoppingList);
                        if (realNeed != null)
                        {
                            //getCountry().countryStorageSet.send(this.getInputProductsReserve(), shoppingList);
                            consumeFromCountryStorage(realNeed, GetCountry());
                            getInputProductsReserve().Add(realNeed);
                        }
                    }
                    else
                    {
                        if (isSubsidized())
                        {
                            Game.market.buy(this, new StorageSet(shoppingList), GetCountry());
                        }
                        else
                        {
                            Game.market.buy(this, new StorageSet(shoppingList), null);
                        }
                    }
                }
            }
            if (isUpgrading() || isBuilding())
            {
                daysInConstruction++;
                bool isBuyingComplete = false;

                //if (buildByPlannedEconomy)
                if (GetCountry().economy.getValue() == Economy.PlannedEconomy)
                {
                    if (daysInConstruction >= Options.fabricConstructionTimeWithoutCapitalism)
                    {
                        if (GetCountry().countryStorageSet.has(constructionNeeds))
                        {
                            isBuyingComplete      = true; //getCountry().countryStorageSet.send(this.getInputProductsReserve(), constructionNeeds);
                            buildByPlannedEconomy = false;
                            //getCountry().countryStorageSet.send(this, )
                        }
                    }
                }
                else
                {
                    if (isBuilding())
                    {
                        isBuyingComplete = Game.market.buy(this, constructionNeeds, Options.BuyInTimeFactoryUpgradeNeeds, getType().GetBuildNeeds());
                    }
                    else if (isUpgrading())
                    {
                        isBuyingComplete = Game.market.buy(this, constructionNeeds, Options.BuyInTimeFactoryUpgradeNeeds, getUpgradeNeeds());
                    }

                    // get money from current investor, not owner
                    Value needExtraFonds = new Value(wantsMinMoneyReserv() - cash.get(), false);
                    if (needExtraFonds.isNotZero())
                    {
                        var investor = currentInvestor as Agent;
                        if (investor.canPay(needExtraFonds))
                        {
                            investor.payWithoutRecord(this, needExtraFonds);
                            ownership.Add(currentInvestor, needExtraFonds);
                        }

                        else
                        {
                            investor.getBank().giveLackingMoney(investor, needExtraFonds);
                            if (investor.canPay(needExtraFonds))
                            {
                                investor.payWithoutRecord(this, needExtraFonds);
                                ownership.Add(currentInvestor, needExtraFonds);
                            }
                        }
                    }
                }
                if (isBuyingComplete ||
                    (GetCountry().economy.getValue() == Economy.NaturalEconomy && daysInConstruction == Options.fabricConstructionTimeWithoutCapitalism))

                {
                    //todo avoid extra subtraction and redo whole method
                    if (isBuilding())
                    {
                        onConstructionComplete(true);
                        getInputProductsReserve().subtract(getType().GetBuildNeeds(), false);
                    }
                    else // assuming isUpgrading()
                    {
                        onConstructionComplete(false);
                        getInputProductsReserve().subtract(getUpgradeNeeds(), false);
                    }
                }
                else
                {
                    if (daysInConstruction == Options.maxDaysBuildingBeforeRemoving)
                    {
                        if (isBuilding())
                        {
                            currentInvestor = null;
                            markToDestroy();
                        }
                        else // upgrading
                        {
                            stopUpgrading();
                        }
                    }
                }
            }
        }
Example #20
0
 public bool HasOwner(IShareOwner owner)
 {
     return(ownership.ContainsKey(owner));
 }
Example #21
0
 internal bool HasOwner(IShareOwner owner)
 {
     return(ownership.ContainsKey(owner));
 }
Example #22
0
 internal bool IsOnlyOwner(IShareOwner owner)
 {
     return(ownership.ContainsKey(owner) && ownership.Count == 1);
 }