/// <summary>
        ///     Validates all troop upgrade levels.
        /// </summary>
        public void ValidateTroopUpgradeLevels()
        {
            LogicAvatar homeOwnerAvatar = this._level.GetHomeOwnerAvatar();

            if (homeOwnerAvatar != null)
            {
                if (homeOwnerAvatar.IsClientAvatar())
                {
                    int[] laboratoryLevels = new int[2];

                    for (int i = 0; i < 2; i++)
                    {
                        LogicBuilding laboratory = this._level.GetGameObjectManagerAt(i).GetLaboratory();

                        if (laboratory != null)
                        {
                            laboratoryLevels[i] = laboratory.GetUpgradeLevel();
                        }
                    }

                    LogicDataTable characterTable = LogicDataTables.GetTable(3);

                    if (characterTable.GetItemCount() > 0)
                    {
                        int idx = 0;

                        do
                        {
                            LogicCharacterData characterData = (LogicCharacterData)characterTable.GetItemAt(idx);

                            int upgradeLevel    = homeOwnerAvatar.GetUnitUpgradeLevel(characterData);
                            int villageType     = characterData.GetVillageType();
                            int newUpgradeLevel = upgradeLevel;

                            if (upgradeLevel >= characterData.GetUpgradeLevelCount())
                            {
                                newUpgradeLevel = characterData.GetUpgradeLevelCount() - 1;
                            }

                            int laboratoryLevel = laboratoryLevels[villageType];
                            int requireLaboratoryLevel;

                            do
                            {
                                requireLaboratoryLevel = characterData.GetRequiredLaboratoryLevel(newUpgradeLevel--);
                            } while (newUpgradeLevel >= 0 && requireLaboratoryLevel > laboratoryLevel);

                            newUpgradeLevel += 1;

                            if (upgradeLevel > newUpgradeLevel)
                            {
                                homeOwnerAvatar.SetUnitUpgradeLevel(characterData, newUpgradeLevel);
                                homeOwnerAvatar.GetChangeListener().CommodityCountChanged(1, characterData, newUpgradeLevel);
                            }
                        } while (++idx != characterTable.GetItemCount());
                    }

                    LogicDataTable spellTable = LogicDataTables.GetTable(25);

                    if (spellTable.GetItemCount() > 0)
                    {
                        int idx = 0;

                        do
                        {
                            LogicSpellData spellData = (LogicSpellData)spellTable.GetItemAt(idx);

                            int upgradeLevel    = homeOwnerAvatar.GetUnitUpgradeLevel(spellData);
                            int villageType     = spellData.GetVillageType();
                            int newUpgradeLevel = upgradeLevel;

                            if (upgradeLevel >= spellData.GetUpgradeLevelCount())
                            {
                                newUpgradeLevel = spellData.GetUpgradeLevelCount() - 1;
                            }

                            int laboratoryLevel = laboratoryLevels[villageType];
                            int requireLaboratoryLevel;

                            do
                            {
                                requireLaboratoryLevel = spellData.GetRequiredLaboratoryLevel(newUpgradeLevel--);
                            } while (newUpgradeLevel >= 0 && requireLaboratoryLevel > laboratoryLevel);

                            newUpgradeLevel += 1;

                            if (upgradeLevel > newUpgradeLevel)
                            {
                                homeOwnerAvatar.SetUnitUpgradeLevel(spellData, newUpgradeLevel);
                                homeOwnerAvatar.GetChangeListener().CommodityCountChanged(1, spellData, newUpgradeLevel);
                            }
                        } while (++idx != spellTable.GetItemCount());
                    }
                }
            }
        }
        public static void UpdateLeagueRank(LogicClientAvatar clientAvatar, int score, bool defender)
        {
            if (LogicDataTables.GetGlobals().EnableLeagues())
            {
                LogicDataTable  leagueTable = LogicDataTables.GetTable(LogicDataType.LEAGUE);
                LogicLeagueData data        = clientAvatar.GetLeagueTypeData();

                int leagueType = clientAvatar.GetLeagueType();

                if (leagueType != 0)
                {
                    int leagueItemCount = leagueTable.GetItemCount();

                    if (data.GetPromoteLimit() <= score)
                    {
                        if (data.IsPromoteEnabled())
                        {
                            while (leagueType + 1 < leagueItemCount)
                            {
                                LogicLeagueData leagueData = (LogicLeagueData)leagueTable.GetItemAt(++leagueType);

                                if (leagueData.GetPromoteLimit() > score || !leagueData.IsPromoteEnabled())
                                {
                                    break;
                                }
                            }

                            if (leagueType != clientAvatar.GetLeagueType())
                            {
                                clientAvatar.SetLeagueType(leagueType);
                            }
                        }
                    }
                    else if (data.GetDemoteLimit() >= score)
                    {
                        if (data.IsDemoteEnabled())
                        {
                            while (leagueType > 0)
                            {
                                LogicLeagueData leagueData = (LogicLeagueData)leagueTable.GetItemAt(--leagueType);

                                if (leagueData.GetDemoteLimit() < score || !leagueData.IsDemoteEnabled())
                                {
                                    break;
                                }
                            }

                            if (leagueType != clientAvatar.GetLeagueType())
                            {
                                clientAvatar.SetLeagueType(leagueType);
                            }
                        }
                    }
                }
                else if (!defender)
                {
                    for (int i = 0; i < leagueTable.GetItemCount(); i++)
                    {
                        LogicLeagueData leagueData = (LogicLeagueData)leagueTable.GetItemAt(i);

                        if (leagueData.GetPlacementLimitLow() <= score && leagueData.GetPlacementLimitHigh() >= score)
                        {
                            if (i != 0)
                            {
                                clientAvatar.SetLeagueType(i);
                            }

                            break;
                        }
                    }
                }
            }
        }