Exemplo n.º 1
0
        private void AddCard(string setName, cockatrice_carddatabase cardDatabase, CardElement newCardElement)
        {
            IEnumerable <cockatrice_carddatabaseCard> nonDuplicateCards =
                cardDatabase.cards.Where(
                    card =>
                    !(string.Equals(card.name, newCardElement.CardName) &&
                      card.set.Any(set => string.Equals(set.Value, setName))))
                .Where(card => !card.set.Any(set => set.Value.ToLower().Contains("basal")));

            var cardList = new List <cockatrice_carddatabaseCard>(nonDuplicateCards);

            var cardSetList =
                new List <cockatrice_carddatabaseCardSet>(new[]
                                                          { new cockatrice_carddatabaseCardSet {
                                                                Value = setName, picURL = newCardElement.ImageUrl
                                                            } });

            foreach (
                var currentSets in cardDatabase.cards
                .Where(card => string.Equals(card.name, newCardElement.CardName))
                .Select(card => card.set))
            {
                cardSetList.AddRange(currentSets.Where(set => !string.Equals(set.Value, setName)));
            }

            cardList.RemoveAll(card => string.Equals(card.name, newCardElement.CardName));

            var newDatabaseCard = new cockatrice_carddatabaseCard(cardSetList, newCardElement);
            var oldImageUrl     = newCardElement.ImageUrl;

            if (string.IsNullOrWhiteSpace(newCardElement.ImageUrl))
            {
                foreach (var sourceCard in cardDatabase.cards.Where(card => string.Equals(card.name, newCardElement.CardName)))
                {
                    foreach (var picUrl in sourceCard.set
                             .Select(set => set.picURL)
                             .Where(picUrl => !string.IsNullOrEmpty(picUrl)))
                    {
                        newCardElement.ImageUrl = picUrl;
                        break;
                    }
                }
            }
            if (!oldImageUrl.Equals(newCardElement.ImageUrl))
            {
                newDatabaseCard = new cockatrice_carddatabaseCard(cardSetList, newCardElement);
            }

            cardList.Add(newDatabaseCard);
            cardDatabase.cards = cardList.ToArray();
        }
Exemplo n.º 2
0
        //Todo: Clean up this mess
        private static CardElement ExtractNewCardFromNode(HtmlNode spoilerRoot)
        {
            const string cardNameElementClassName = "t-spoiler";
            const string cardNameAttribute        = "id";

            const string cardRarityPrefix            = "t-spoiler-header";
            const string cardRarityAttributeName     = "class";
            const string imageUrlClassName           = "spoiler-image-link lightbox";
            const string imageUrlAttribute           = "href";
            const string cardTypeElementClassName    = "t-spoiler-type";
            const string cardStatElementClassName    = "t-spoiler-stat";
            const string manaElementRootClassName    = "t-spoiler-mana";
            const string manaElementPartialClassName = "mana-icon";
            const string oracleTextElementClassName  = "t-spoiler-ability";

            var spoilerElement =
                spoilerRoot.SearchChildNodeByAttributes(
                    attribute => attribute.Value.Equals(cardNameElementClassName));

            var cardName = spoilerElement.GetAttributeValue(cardNameAttribute, null);

            var cardRarityString =
                spoilerRoot.SearchChildNodeByAttributes(attribute => attribute.Value.StartsWith(cardRarityPrefix))
                .GetAttributeValue(cardRarityAttributeName, null);

            var cardRarity = CardRarityExt.FromString(cardRarityString);

            var imageUrlNode = spoilerRoot
                               .SearchChildNodeByAttributes(attribute => attribute.Value.StartsWith(cardRarityPrefix))
                               .Descendants()
                               .FirstOrDefault(node => node.Attributes.Any(attribute => attribute.Value.Equals(imageUrlClassName)));

            var imageUrlString = (imageUrlNode != null)
                ? imageUrlNode.GetAttributeValue(imageUrlAttribute, null)
                : string.Empty;

            var cardType =
                spoilerRoot.SearchChildNodeByAttributes(
                    attribute => attribute.Value.Equals(cardTypeElementClassName)).InnerText;
            var cardStatElement = spoilerRoot.SearchChildNodeByAttributes(
                attribute => attribute.Value.Equals(cardStatElementClassName));
            var cardStats = (cardStatElement != null)
                ? cardStatElement.InnerText
                : string.Empty;

            var manaCost         = string.Empty;
            var manaCostElements = spoilerRoot
                                   .SearchChildNodeByAttributes(attribute => attribute.Value.Equals(manaElementRootClassName))
                                   .SearchChildNodesByAttributes(attribute => attribute.Value.StartsWith(manaElementPartialClassName));

            if (manaCostElements != null)
            {
                var manaCostBuilder = new StringBuilder();
                foreach (var manaCostElement in manaCostElements)
                {
                    manaCostBuilder.Append(manaCostElement.InnerText);
                }
                manaCost = manaCostBuilder.ToString();
            }
            var oracleTextElement = spoilerRoot.SearchChildNodeByAttributes(
                attribute => attribute.Value.Equals(oracleTextElementClassName));
            var oracleText = string.Empty;

            if (oracleTextElement != null)
            {
                oracleText = oracleTextElement.InnerText;
            }
            var newCard = new CardElement
            {
                CardName   = cardName,
                ImageUrl   = imageUrlString,
                ManaCost   = manaCost,
                OracleText = oracleText,
                Rarity     = cardRarity,
                Stats      = cardStats,
                Type       = cardType
            };

            return(newCard);
        }