private IEnumerable <CardRuleInfo> GetRules(string text) { //Parsing using (XmlTextReader xmlReader = new XmlTextReader(new StringReader(SpecialXMLCorrection(text)))) { //Go to ruling div while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element) { string classValue = xmlReader.GetAttribute("class"); if (!string.IsNullOrEmpty(classValue) && classValue.ToLowerInvariant() == "postcontainer") { //Work on table IAwareXmlTextReader reader = new AwareXmlTextReader(xmlReader); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "tr") { string trClassValue = reader.GetAttribute("class"); if (!string.IsNullOrEmpty(trClassValue)) { trClassValue = trClassValue.ToLowerInvariant(); if (trClassValue == "post evenitem" || trClassValue == "post odditem") { CardRuleInfo cardRuleInfo = WorkOnRow(new AwareXmlTextReader(reader)); if (cardRuleInfo != null) { yield return(cardRuleInfo); } } } } } yield break; } } } } }
private CardWithExtraInfo GenerateCard(string text) { IDictionary <string, string> infos = new Dictionary <string, string>(); //Parsing using (XmlTextReader xmlReader = new XmlTextReader(new StringReader(SpecialXMLCorrection(text)))) { while (xmlReader.Read()) { if (xmlReader.NodeType != XmlNodeType.Element) { continue; } IAwareXmlTextReader reader = new AwareXmlTextReader(xmlReader); ICardInfoParserWorker worker = CardInfoParserWorkerFactory.Instance.CreateParserWorker(reader); if (worker == null) { continue; } infos.AddRange(ParseElement(reader, worker)); } } //Check parsing result CheckInfos(infos); //Generate result class CardWithExtraInfo cardWithExtraInfo = new CardWithExtraInfo { Name = RemoveParentheses(infos.GetOrDefault(NameKey)), CastingCost = infos.GetOrDefault(ManaCostKey), Text = infos.GetOrDefault(TextKey), Type = infos.GetOrDefault(TypeKey), PictureUrl = infos.GetOrDefault(ImageKey), Rarity = infos.GetOrDefault(RarityKey) }; if (MagicRules.IsCreature(cardWithExtraInfo.Type) || MagicRules.IsVehicle(cardWithExtraInfo.Type)) { string htmlTrim = infos.GetOrDefault(PTKey).HtmlTrim(); cardWithExtraInfo.Power = GetPower(htmlTrim); cardWithExtraInfo.Toughness = GetToughness(htmlTrim); } if (MagicRules.IsPlaneswalker(cardWithExtraInfo.Type)) { string htmlTrim = infos.GetOrDefault(PTKey).HtmlTrim(); //Possible see CheckInfos for more info if (!string.IsNullOrWhiteSpace(htmlTrim)) { htmlTrim = htmlTrim.ToUpper(); //Special case for: // Nissa, Steward of Elements with loyalty to X // B.O.B. (Bevy of Beebles) with loyalty to * if (htmlTrim == "X" || htmlTrim == "*") { cardWithExtraInfo.Loyalty = htmlTrim; } else { cardWithExtraInfo.Loyalty = int.Parse(htmlTrim).ToString(); } } } string variations = infos.GetOrDefault(VariationsKey); if (!string.IsNullOrWhiteSpace(variations)) { foreach (string variation in variations.Split(new[] { VariationsWorker.Separator }, System.StringSplitOptions.RemoveEmptyEntries)) { if (int.TryParse(variation, out int gatherid)) { cardWithExtraInfo.Add(gatherid); } } } cardWithExtraInfo.Type = infos.GetOrDefault(TypeKey); return(cardWithExtraInfo); }