Esempio n. 1
0
        private async Task <R4UCard> ParseOriginalCard(WikiCardContext context, WikiSetContext setContext)
        {
            var card         = new R4UCard();
            var mainInfoBox  = context.MainInfoBox;
            var extraInfoBox = context.ExtraInfoBox;

            card.Name    = new MultiLanguageString();
            card.Name.JP = mainInfoBox.GetValueOrDefault("kanji", mainInfoBox["name"]);
            card.Name.EN = mainInfoBox["name"];
            card.Type    = TranslateType(mainInfoBox["card type"]);
            card.Set     = setContext.Set;
            if (card.Type == CardType.Character)
            {
                card.Cost   = int.Parse(mainInfoBox["cost"]);
                card.ATK    = int.Parse(mainInfoBox["atk"]);
                card.DEF    = int.Parse(mainInfoBox["def"]);
                card.Traits = await ParseTraits(context.RawMainInfoBox["trait"], setContext);
            }
            if (extraInfoBox.TryGetValue("card flavor(s)", out var flavor))
            {
                card.Flavor    = new MultiLanguageString();
                card.Flavor.EN = flavor;
            }
            if (extraInfoBox.TryGetValue("card abilities", out var abilities))
            {
                card.Effect = TranslateEffect(abilities);
                card.Color  = CardUtils.InferFromEffect(card.Effect);
            }
            else
            {
                // Assumed. All Partner cards do not have effects, and vanilla cards have [Relaxing]
                card.Color = CardColor.Red;
            }

            return(card);
        }
        private IEnumerable <R4UCard> CreateBaseCards(IHtmlParagraphElement paragraph, Dictionary <string, R4UReleaseSet> setMap)
        {
            List <R4UCard> cards = new List <R4UCard>();

            cards.Add(new R4UCard());

            var card    = cards.First();
            var content = paragraph.InnerHtml;
            var text    = paragraph.GetInnerText();
            var cursor  = text.AsSpanCursor();

            // var space = " ";

            // Format: <Serial> <Rarity> <JP Name with Spaces> <strong>English Name with Spaces</strong><br>
            if (TryGetExceptionalSerialRarityName(cursor.CurrentLine.ToString(), out var exceptionalResults))
            {
                cards = exceptionalResults.Select(res =>
                {
                    var card    = new R4UCard();
                    card.Serial = res.Serial;
                    card.Rarity = res.Rarity;
                    card.Name   = res.Name;
                    return(card);
                }).ToList();
                if (cards.Count < 1)
                {
                    yield break;
                }
                card = cards.First();
            }
            else if (serialRarityJPNameMatcher.IsMatch(content))
            {
                var firstLineMatch = serialRarityJPNameMatcher.Match(content);
                card.Serial  = firstLineMatch.Groups[1].Value.Trim();
                card.Rarity  = firstLineMatch.Groups[2].Value.Trim();
                card.Name    = new MultiLanguageString();
                card.Name.JP = rubyMatcher.Replace(firstLineMatch.Groups[3].Value, "").Trim(); // TODO: Resolve <ruby>永<rt>えい</rt>遠<rt>えん</rt></ruby>の<ruby>巫<rt>み</rt>女<rt>こ</rt></ruby> <ruby>霊<rt>れい</rt>夢<rt>む</rt></ruby>
                card.Name.EN = firstLineMatch.Groups[4].Value.Trim();
            }
            else
            {
                throw new NotImplementedException($"The serial/rarity/JP Name line cannot be parsed. Here's the offending line: {cursor.CurrentLine.ToString()}");
            }

            var releaseID = releaseIDMatcher.Match(card.Serial).Groups[1].Value;

            card.Set = setMap.GetValueOrDefault(releaseID, null) ?? CreateTemporarySet(releaseID);

            // Format: Cost <Cost> / <Series Name> / <Traits>
            cursor.Next();
            var secondLine = cursor.CurrentLine.ToString();

            if (costSeriesTraitMatcher.IsMatch(secondLine))
            {
                card.Type = CardType.Character;
                var secondLineMatch = costSeriesTraitMatcher.Match(cursor.CurrentLine.ToString());
                card.Cost   = int.Parse(secondLineMatch.Groups[1].Value);
                card.Traits = secondLineMatch.Groups[3].Value //
                              .Split(" – ")                   //
                              .Select(str => str.Trim())      //
                              .Select(t => new MultiLanguageString()
                {
                    EN = t
                })                                                     //
                              .ToList();

                cursor.Next();
                card.ATK = cursor.CurrentLine
                           .Slice("ATK ".Length)
                           .AsParsed <int>(int.TryParse);

                cursor.Next();
                string defLine = cursor.CurrentLine.ToString();
                card.DEF = cursor.CurrentLine
                           .Slice("DEF ".Length)
                           .AsParsed <int>(int.TryParse);

                Regex flavorTextMatcher = new Regex(@"" + defLine + @"<br><em>(.+)</em><br>");
                if (flavorTextMatcher.IsMatch(content))
                {
                    cursor.Next();
                    card.Flavor    = new MultiLanguageString();
                    card.Flavor.EN = cursor.CurrentLine.ToString(); // flavorTextMatcher.Match(content).Groups[1].Value;
                }
            }
            else if (seriesRebirthMatcher.IsMatch(secondLine))
            {
                card.Type = CardType.Rebirth;
                var rebirthLine = secondLine;

                Regex flavorTextMatcher = new Regex(@"" + rebirthLine + @"<br><em>(.+)</em><br>");
                if (flavorTextMatcher.IsMatch(content))
                {
                    cursor.Next();
                    card.Flavor    = new MultiLanguageString();
                    card.Flavor.EN = cursor.CurrentLine.ToString(); // flavorTextMatcher.Match(content).Groups[1].Value;
                }
            }
            else
            {
                card.Type  = CardType.Partner;
                card.Color = CardColor.Red;
            }

            if (card.Color != CardColor.Red)
            {
                List <MultiLanguageString> effects = new List <MultiLanguageString>();
                while (cursor.Next())
                {
                    Log.Information("Adding Effect: {eff}", cursor.CurrentLine.ToString());
                    effects.Add(new MultiLanguageString()
                    {
                        EN = cursor.CurrentLine.ToString()
                    });
                }
                effects     = Compress(effects);
                card.Effect = effects.ToArray();
                card.Color  = CardUtils.InferFromEffect(card.Effect);
            }

            yield return(card);

            foreach (var dupCard in cards.Skip(1))
            {
                var detailedDupCard = card.Clone();
                detailedDupCard.Serial = dupCard.Serial;
                detailedDupCard.Rarity = dupCard.Rarity;
                detailedDupCard.Name   = dupCard.Name;
                yield return(detailedDupCard);
            }
        }