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;
}
}