Ejemplo n.º 1
0
        private async Task <IDictionary <int, HsReplayArchetype> > GetHsReplayArchetypes()
        {
            var json = await ImportingHelper.JsonRequest($"https://hsreplay.net/api/v1/archetypes/");

            var archetypes = JsonConvert.DeserializeObject <List <HsReplayArchetype> >(json);

            return(archetypes.ToDictionary(a => a.Id, a => a));
        }
        public static async Task <Deck> Import(string url)
        {
            try
            {
                // check url looks correct
                var match = Regex.Match(url, "/decks/([^/]+)$");
                // get deck name from url, and post the json request
                if (match.Success && match.Groups.Count == 2)
                {
                    var slug  = match.Groups[1].ToString();
                    var param = "{\"where\":{\"slug\":\""
                                + slug
                                + "\"},\"fields\":{},\"include\":[{\"relation\":\"cards\",\"scope\":{\"include\":[\"card\"]}}]}";
                    var data = await ImportingHelper.JsonRequest("https://tempostorm.com/api/decks/findOne?filter=" + param);

                    //parse json
                    var jsonObject = JsonConvert.DeserializeObject <dynamic>(data);
                    if (jsonObject.error == null)
                    {
                        var deck = new Deck();

                        deck.Name = jsonObject.name.ToString();
                        var cards = jsonObject.cards;

                        foreach (var item in cards)
                        {
                            var card = Database.GetCardFromName(item.card.name.ToString());
                            card.Count = item.cardQuantity.ToString().Equals("2") ? 2 : 1;
                            deck.Cards.Add(card);
                            if (string.IsNullOrEmpty(deck.Class) && card.PlayerClass != "Neutral")
                            {
                                deck.Class = card.PlayerClass;
                            }
                        }

                        return(deck);
                    }
                    throw new Exception("JSON request failed for '" + slug + "'.");
                }
                throw new Exception("The url (" + url + ") is not a vaild TempoStorm deck.");
            }
            catch (Exception e)
            {
                Log.Error(e);
                return(null);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Gets all decks from HSReplay.
        /// </summary>
        /// <param name="archetypes">The HSReplay archetypes</param>
        /// <param name="progress">Tuple of two integers holding the progress information for the UI</param>
        /// <returns>The list of all parsed decks</returns>
        private async Task <IList <Deck> > GetHsreplayDecks(IDictionary <int, HsReplayArchetype> archetypes, string gameType, IProgress <Tuple <int, int> > progress)
        {
            var pattern = @"(\[\d+,[12]{1}\])";

            var json = await ImportingHelper.JsonRequest($"https://hsreplay.net/analytics/query/list_decks_by_win_rate_v2/?GameType={gameType}");

            var decks = JsonConvert.DeserializeObject <HsReplayDecks>(json).Series.Data;

            return(decks.SelectMany(x => decks[x.Key].Select(d =>
            {
                // Count found decks thread-safe
                Interlocked.Increment(ref _decksFound);

                // Get the archetype or default
                HsReplayArchetype archetype;
                if (!archetypes.ContainsKey(d.ArchetypeId))
                {
                    archetype = new HsReplayArchetype();
                    archetype.Name = "Other";
                    archetype.Url = $"/archetypes/{d.ArchetypeId}";
                    archetype.Class = HsReplayNameToClassId[x.Key];
                }
                else
                {
                    archetype = archetypes[d.ArchetypeId];
                }

                // Create new deck
                var deck = new Deck();

                deck.Name = archetype.Name;
                deck.Url = archetype.Url;
                deck.Class = HsReplayClassIdToName[archetype.Class];

                // Insert deck note for statistics
                deck.Note = $"#Games: {d.TotalGames}, #Win Rate: {d.WinRate}%";

                // Set import datetime as LastEdited
                deck.LastEdited = DateTime.Now;

                var matches = Regex.Matches(d.DeckList, pattern);
                foreach (Match match in matches)
                {
                    var matchText = match.Value.Trim('[', ']');

                    // Get card from database with dbf id
                    var card = Database.GetCardFromDbfId(int.Parse(matchText.Split(',')[0]));
                    card.Count = int.Parse(matchText.Split(',')[1]);

                    deck.Cards.Add(card);
                }

                // Count imported decks thread-safe
                Interlocked.Increment(ref _decksImported);

                // Report progress for UI
                progress.Report(new Tuple <int, int>(_decksImported, _decksFound));

                return deck;
            })).ToList());
        }