Exemple #1
0
        private void CheckInfos(IDictionary <string, string> infos)
        {
            if (!infos.ContainsKey(NameKey))
            {
                throw new ParserException("No name found");
            }

            if (!infos.ContainsKey(TypeKey))
            {
                throw new ParserException("No type found");
            }

            if (!infos.ContainsKey(RarityKey))
            {
                throw new ParserException("No rarity found");
            }

            string type        = infos.GetOrDefault(TypeKey);
            string castingcost = infos.GetOrDefault(ManaCostKey);

            //Add check on casting cost because of second face of Garruk, the Veil-Cursed which has no loyalty counter
            if ((MagicRules.IsCreature(type) || MagicRules.IsPlaneswalker(type) || MagicRules.IsVehicle(type)) && !infos.ContainsKey(PTKey) && !string.IsNullOrWhiteSpace(castingcost))
            {
                throw new ParserException("No PT/Loyalty found");
            }
        }
Exemple #2
0
        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);
        }