Ejemplo n.º 1
0
        public async Task Export(R4UDeck deck, IExportInfo info, Path jsonFilename)
        {
            var simplifiedDeck = new
            {
                Name    = deck.Name,
                Remarks = deck.Remarks,
                Ratios  = deck.AsSimpleDictionary()
            };

            jsonFilename.Open(
                async s => await JsonSerializer.SerializeAsync(s, simplifiedDeck, options: _defaultOptions),
                System.IO.FileMode.Create,
                System.IO.FileAccess.Write,
                System.IO.FileShare.ReadWrite
                );
            Log.Information($"Done: {jsonFilename.FullPath}");

            if (!String.IsNullOrWhiteSpace(info?.OutCommand))
            {
                await ExecuteCommandAsync(info.OutCommand, jsonFilename);
            }
        }
Ejemplo n.º 2
0
        public async Task <R4UDeck> Parse(string sourceUrlOrFile)
        {
            var document = await sourceUrlOrFile.WithHTMLHeaders().GetHTMLAsync();

            //var deckView = document.QuerySelector(".deckview");
            //var cardControllers = deckView.QuerySelectorAll(".card-controller-inner");
            var deckID = urlMatcher.Match(sourceUrlOrFile).Groups[2];

            Log.Information("Parsing ID: {deckID}", deckID);
            var response = await $"{deckLogApiUrlPrefix}{deckID}" //
                           .WithReferrer(sourceUrlOrFile)         //
                           .PostJsonAsync(null);
            var json = JsonConvert.DeserializeObject <dynamic>(await response.Content.ReadAsStringAsync());
            //var json = JsonConverter.CreateDefault().Deserialize<dynamic>(new JsonReader(await response.Content.ReadAsStreamAsync()));
            var newDeck        = new R4UDeck();
            var missingSerials = new List <string>();

            newDeck.Name    = json.title.ToString();
            newDeck.Remarks = json.memo.ToString();
            using (var db = _database())
            {
                List <dynamic> items = new List <dynamic>();
                items.AddRange(json.list);
                items.AddRange(json.sub_list);
                foreach (var cardJSON in items)
                {
                    string serial = cardJSON.card_number.ToString();
                    serial = serial.Replace('+', '+');
                    if (serial == null)
                    {
                        Log.Warning("serial is null for some reason!");
                    }
                    var card = await db.R4UCards.FindAsync(serial);

                    int quantity = cardJSON.num;
                    if (card != null)
                    {
                        Log.Debug("Adding: {card} [{quantity}]", card?.Serial, quantity);
                        if (newDeck.Ratios.TryGetValue(card, out int oldVal))
                        {
                            newDeck.Ratios[card] = oldVal + quantity;
                        }
                        else
                        {
                            newDeck.Ratios.Add(card, quantity);
                        }
                    }
                    else
                    {
                        missingSerials.Add(serial);
                        //throw new DeckParsingException($"MISSING_SERIAL_{serial}");
                        Log.Warning("Serial has been effectively skipped because it's not found on the local db: [{serial}]", serial);
                    }
                }
            }
            if (missingSerials.Count > 0)
            {
                throw new DeckParsingException($"The following serials are missing from the DB:\n{missingSerials.ConcatAsString("\n")}");
            }
            else
            {
                Log.Debug($"Result Deck: {JsonConvert.SerializeObject(newDeck.AsSimpleDictionary())}");
                return(newDeck);
            }
        }