ActivEarth.DAO.ChallengeDAO.GenerateRandomChallenges C# (CSharp) Метод

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

Creates a number of random challenges by reading in from the Challenge_Definitions SQL table.
public static GenerateRandomChallenges ( ChallengeType type, int count ) : List
type ChallengeType Type of challenges desired (Daily/Weekly/Monthly).
count int Number of challenges needing to be created.
Результат List
        public static List<Challenge> GenerateRandomChallenges(ChallengeType type, int count)
        {
            try
            {
                using (SqlConnection connection = ConnectionManager.GetConnection())
                {
                    List<Challenge> challenges = new List<Challenge>();

                    var data = new ActivEarthDataProvidersDataContext(connection);
                    List<Challenge> templates = (from c in data.ChallengeDefinitionDataProviders
                                               where (c.challenge_type == (byte)type && c.persistent == false)
                                               select new Challenge()
                                               {
                                                   IsPersistent = c.persistent,
                                                   StatisticBinding = (Statistic)c.statistic,
                                                   Requirement = (float)c.requirement,
                                                   Reward = c.reward,
                                                   Description = c.description,
                                                   Name = (c.name == null ? String.Empty : c.name),
                                                   ImagePath = c.image_path
                                               }).ToList();

                    Random rand = new Random();

                    while (challenges.Count < count)
                    {
                        int i = rand.Next(templates.Count);

                        if (!challenges.Contains(templates[i]))
                        {
                            string formatString = StatisticInfoDAO.GetStatisticFormatString(templates[i].StatisticBinding);
                            string requirementString = templates[i].Requirement.ToString(formatString);

                            templates[i].Description = String.Format(templates[i].Description, requirementString);

                            challenges.Add(templates[i]);
                        }
                    }

                    return challenges;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }