public void UpgradeBuilding(BuildingType buildingType)
    {
        Debug.Log("Upgrade: " + buildingType);

        int cost;
        int time;
        int nextLevel;

        this.GetUpgradePriceAndTime(buildingType, out cost, out time, out nextLevel);

        if (cost > this.GoldCount)
        {
            Debug.Log("Not enough gold");
            return;
        }

        if (this.CurrentUpgradeJob != null)
        {
            Debug.Log("Already building smth");
            return;
        }

        AudioPlayer.Instance.PlayBuilding();

        Debug.Log("Upgrade started");

        this.CurrentUpgradeJob = new BuildingUpgradeJob()
        {
            BuildingType           = buildingType,
            LevelAfterUpgrade      = nextLevel,
            UpgradeFinishesByBlock = this.CurrentBlockNumber + time
        };

        Object.FindObjectOfType <GameManager>().Commitments.CommitBuildingUpgradeAction(buildingType);

        this.GoldCount -= cost;
    }
    /// <remarks>No reorgs expected.</remarks>
    public void UpdateState(int newBlockNumber)
    {
        int blocksPassed = newBlockNumber - this.CurrentBlockNumber;

        if (blocksPassed <= 0)
        {
            return;
        }

        int oldBlockNumber = this.CurrentBlockNumber;

        this.CurrentBlockNumber = newBlockNumber;

        var goldMineWasUpgraded       = false;
        var goldMineUpgradedBlocksAgo = 0;

        // Upgrades.
        if (this.CurrentUpgradeJob != null && this.CurrentBlockNumber >= this.CurrentUpgradeJob.UpgradeFinishesByBlock)
        {
            // Upgrade level.
            if (this.CurrentUpgradeJob.BuildingType == BuildingType.Barracks)
            {
                this.BarracksLevel = this.CurrentUpgradeJob.LevelAfterUpgrade;
            }
            else
            {
                this.GoldMineLevel        = this.CurrentUpgradeJob.LevelAfterUpgrade;
                goldMineWasUpgraded       = true;
                goldMineUpgradedBlocksAgo = newBlockNumber - oldBlockNumber;
            }

            this.CurrentUpgradeJob = null;
        }

        // Update gold.
        if (!goldMineWasUpgraded)
        {
            int goldMined = blocksPassed * this.goldMine.GoldProductionSpeedPerBlock[this.GoldMineLevel];
            this.GoldCount += goldMined;
        }
        else
        {
            int goldMined = (blocksPassed - goldMineUpgradedBlocksAgo) * this.goldMine.GoldProductionSpeedPerBlock[this.GoldMineLevel - 1];
            goldMined += goldMineUpgradedBlocksAgo * this.goldMine.GoldProductionSpeedPerBlock[this.GoldMineLevel];

            this.GoldCount += goldMined;
        }

        // Update units.
        if (this.UnitsBuildJob != null)
        {
            int maxCreated = this.UnitsBuildJob.UnitsPerBlockBuildTime * blocksPassed;

            if (maxCreated >= this.UnitsBuildJob.UnitsLeftToBuild)
            {
                int unitsBuilt = this.UnitsBuildJob.UnitsLeftToBuild;
                this.UnitsBuildJob = null;

                this.UnitsCount += unitsBuilt;
            }
            else
            {
                this.UnitsBuildJob.UnitsLeftToBuild -= maxCreated;
                this.UnitsCount += maxCreated;
            }
        }
    }