using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using Zeta.CommonBot.Settings;
namespace Atom.Functions
{
class Randomizer
{
private static Random randGenerator = new Random(DateTime.Now.Ticks.GetHashCode());
private static List<string> profiles = Enumerable.Empty<string>().ToList<string>();
public static string SelectProfile(string profiles)
{
string[] profilesArray = profiles.Split(';');
string profile = profilesArray[randGenerator.Next(0, profilesArray.Length)];
return Path.Combine(Profilemaster.ProfilesPath, profile);
}
public static void RecurseSelect(XmlNodeList nodes)
{
foreach (XmlNode xmlNodeCiggarcChild in nodes)
{
if (xmlNodeCiggarcChild.Name == "Randomize")
{
XmlNodeList randChildren = xmlNodeCiggarcChild.SelectNodes("select");
List<XmlNode> randomizedList = new List<XmlNode>();
foreach (XmlNode n in randChildren)
randomizedList.Add(n);
//adapted from http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp
randomizedList = randomizedList.OrderBy<XmlNode, int>((item) => randGenerator.Next()).ToList();
foreach (XmlNode n in randomizedList)
{
profiles.Add(SelectProfile(n.Attributes["profiles"].Value));
if (n.HasChildNodes)
{
RecurseSelect(n.ChildNodes);
}
}
}
if (xmlNodeCiggarcChild.Name == "select")
{
profiles.Add(SelectProfile(xmlNodeCiggarcChild.Attributes["profiles"].Value));
}
}
}
public static void initProfiles(bool randomize = false)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(GlobalSettings.Instance.LastProfile);
if (profiles.Count > 0)
profiles = new List<string>();
XmlNode xmlNodeCiggarc = xmlDoc.SelectSingleNode("Profile/Ciggarc");
if (xmlNodeCiggarc != null)
{
RecurseSelect(xmlNodeCiggarc.ChildNodes);
}
Profilemaster.ProfilesOrder = profiles.ToArray();
Logger.Log("Profile order for this run: ");
outputRunProfilesList();
}
public static void outputRunProfilesList()
{
foreach (string s in profiles)
{
Logger.Log(string.Format(@"[{0} / {1}] ", profiles.FindIndex(r => r.Equals(s)) + 1, profiles.Count) + Path.GetFileName(s));
}
}
}
}