using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using DominionBase.Cards; namespace DominionBase.Players.AI { public class OpeningProcessor { private static Boolean IsDownloadedNew = false; private static Boolean IsDownloading = false; private static System.Threading.Mutex _Mutex; private static OpeningProcessor _Instance = null; private OpeningProcessor () { } static OpeningProcessor() { _Instance = new OpeningProcessor(); _Mutex = new System.Threading.Mutex(); } private Boolean _IsReady = false; private OpeningCollection _Openings = new OpeningCollection(); public Boolean IsReady { get { return _IsReady; } } public OpeningCollection Openings { get { return _Openings; } } public static OpeningProcessor Acquire() { _Mutex.WaitOne(); return _Instance; } public static void Release() { _Mutex.ReleaseMutex(); } public void Run() { IsDownloading = true; if (!System.IO.File.Exists("output.txt") || DateTime.Now - System.IO.File.GetLastWriteTime("output.txt") > TimeSpan.FromDays(1)) { try { Utilities.DownloadFileSlowly.DownloadFromUrl("http://councilroom.com/openings?card=All+cards", "output.txt", 108096); //String data = Utilities.DownloadFileSlowly.DownloadFromUrl("http://councilroom.com/openings?card=All+cards", 8096); } catch (Exception ex) { IsDownloading = false; return; } IsDownloadedNew = true; _IsReady = false; } IsDownloading = false; if (!IsReady) { CardCollection allCards = CardCollection.GetAllCards(c => c.Location == Location.Kingdom || c.Location == Location.General); if (IsDownloadedNew || !System.IO.File.Exists(OpeningCollection.Filename)) { String data = System.IO.File.ReadAllText("output.txt"); Match tableObject = Regex.Match(data, "(.*)
", RegexOptions.Singleline); Regex regexRowMatch = new Regex(@"]*>.*?.*?(?-?\d\.\d+) ± (?-?\d\.\d+).*?.*?.*?(?.*?).*(?.*?)", RegexOptions.Singleline); if (tableObject != null && tableObject.Success && tableObject.Groups[1].Success) { foreach (Match opening in Regex.Matches(tableObject.Groups[1].Value, "]*>(.*?)", RegexOptions.Singleline)) { Match parsedMatch = regexRowMatch.Match(opening.Groups[1].Value); if (parsedMatch.Success) { Opening newOpening = new Opening( parsedMatch.Groups["skill"].Value, parsedMatch.Groups["accuracy"].Value, parsedMatch.Groups["card_names"].Value.Replace("'", "'"), parsedMatch.Groups["costs"].Value, allCards); if (newOpening.IsValid) _Openings.Add(newOpening); } } } _Openings.Save(); } else if (System.IO.File.Exists(OpeningCollection.Filename)) { _Openings.Load(allCards); } _IsReady = true; } } } }