예제 #1
0
        private async Task <WeissSchwarzDeck> Parse(Uri uri)
        {
            var encoreDecksDeckAPIURL = "https://www.encoredecks.com/api/deck";

            var localPath = uri.LocalPath;
            var deckID    = localPath.Substring(localPath.LastIndexOf('/') + 1);

            Log.Information("Deck ID: {deckID}", deckID);

            dynamic deckJSON = await GetDeckJSON(encoreDecksDeckAPIURL, deckID);

            WeissSchwarzDeck res = new WeissSchwarzDeck();

            res.Name = deckJSON.name;

            using (var db = _database())
            {
                await db.Database.MigrateAsync();

                foreach (dynamic card in deckJSON.cards)
                {
                    string           serial = WeissSchwarzCard.GetSerial(card.set.ToString(), card.side.ToString(), card.lang.ToString(), card.release.ToString(), card.sid.ToString());
                    WeissSchwarzCard wscard = await db.WeissSchwarzCards.FindAsync(serial);

                    if (wscard == null)
                    {
                        string setID = card.series;
                        await _parse($"https://www.encoredecks.com/api/series/{setID}/cards");

                        wscard = await db.WeissSchwarzCards.FindAsync(serial);
                    }

                    if (res.Ratios.TryGetValue(wscard, out int quantity))
                    {
                        res.Ratios[wscard]++;
                    }
                    else
                    {
                        res.Ratios[wscard] = 1;
                    }

                    //Log.Information("Parsed: {@wscard}", wscard);
                }
            }
            var simpleRatios = res.AsSimpleDictionary();

            Log.Information("Deck Parsed: {@simpleRatios}", simpleRatios);
            Log.Information("Cards in Deck: {@count}", simpleRatios.Values.Sum());
            return(res);
        }
예제 #2
0
        public async IAsyncEnumerable <WeissSchwarzCard> Parse(string urlOrFile)
        {
            if (encoreDecksSiteSetMatcher.IsMatch(urlOrFile))
            {
                urlOrFile = TransformIntoAPIFormat(urlOrFile);
            }

            IList <dynamic> setCards = null;

            do
            {
                try
                {
                    setCards = await urlOrFile.WithRESTHeaders().GetJsonListAsync();
                }
                catch (FlurlHttpException)
                {
                    // Do nothing
                }
            } while (setCards == null);
            foreach (var setCard in setCards)
            {
                WeissSchwarzCard result = new WeissSchwarzCard();
                result.Name = new MultiLanguageString();
                var enOptional = DynamicExtensions.AsOptional(setCard.locale.EN);
                var jpOptional = DynamicExtensions.AsOptional(setCard.locale.NP);
                result.Name.EN = enOptional.name;
                result.Name.JP = jpOptional.name;
                (List <object>, List <object>)attributes = (enOptional.attributes, jpOptional.attributes);
                result.Traits   = TranslateTraits(attributes).ToList();
                result.Effect   = ((List <object>)enOptional.ability)?.Cast <string>().ToArray();
                result.Rarity   = setCard.rarity;
                result.Side     = TranslateSide(setCard.side);
                result.Level    = (int?)setCard.level;
                result.Cost     = (int?)setCard.cost;
                result.Power    = (int?)setCard.power;
                result.Soul     = (int?)setCard.soul;
                result.Triggers = TranslateTriggers(setCard.trigger);

                result.Serial = WeissSchwarzCard.GetSerial(setCard.set.ToString(), setCard.side.ToString(), setCard.lang.ToString(), setCard.release.ToString(), setCard.sid.ToString());

                result.Type    = TranslateType(setCard.cardtype);
                result.Color   = TranslateColor(setCard.colour);
                result.Remarks = $"Parsed: {this.GetType().Name}";
                yield return(result);
            }
            // Get
            yield break;
        }