ActivEarth.Server.Service.Competition.BadgeManager.UpdateBadge C# (CSharp) Метод

UpdateBadge() публичный статический Метод

Updates the badge to reflect a change in statistics.
public static UpdateBadge ( int userId, Statistic statistic ) : int
userId int User to update.
statistic Statistic Statistic tracked by the badge to be updated.
Результат int
        public static int UpdateBadge(int userId, Statistic statistic)
        {
            int pointsEarned = 0;

            Badge badge = BadgeDAO.GetBadgeFromUserIdAndStatistic(userId, statistic);

            if (badge != null)
            {
                int oldLevel = badge.Level;
                int newLevel = oldLevel;

                UserStatistic userStat = UserStatisticDAO.GetStatisticFromUserIdAndStatType(userId, statistic);

                if (userStat == null)
                {
                    UserStatisticDAO.CreateNewStatisticForUser(userId, statistic, 0);
                    userStat = UserStatisticDAO.GetStatisticFromUserIdAndStatType(userId, statistic);

                    if (userStat == null) { return 0; }
                }

                float stat = userStat.Value;

                while ((newLevel < BadgeLevels.Max) &&
                    (stat >= badge.LevelRequirements[(int)newLevel + 1]))
                {
                    newLevel++;
                }

                for (int i = oldLevel + 1; i <= newLevel; i++)
                {
                    pointsEarned += badge.LevelRewards[i];
                }

                badge.Level = newLevel;

                if (badge.Level == BadgeLevels.Max)
                {
                    badge.Progress = 100;
                }
                else
                {
                    badge.Progress = (int)(100 * (stat - badge.LevelRequirements[newLevel]) /
                        (badge.LevelRequirements[newLevel + 1] - badge.LevelRequirements[newLevel]));
                }

                BadgeDAO.UpdateBadge(badge);
            }

            return pointsEarned;
        }