Exemple #1
0
        /// <summary>
        /// Refreshes the cache with the specified league.
        /// </summary>
        public async Task OnAfterInit()
        {
            logger.Information($"Populating PoeNinja cache.");

            var itemsTasks = Enum.GetValues(typeof(ItemType))
                             .Cast <ItemType>()
                             .Select(x => new { itemType = x, request = client.QueryItem(configuration.LeagueId, x) })
                             .ToList();
            var currenciesTasks = Enum.GetValues(typeof(CurrencyType))
                                  .Cast <CurrencyType>()
                                  .Select(x => new { currencyType = x, request = client.QueryItem(configuration.LeagueId, x) })
                                  .ToList();

            await Task.WhenAll(itemsTasks.Select(x => x.request).Cast <Task>().Concat(currenciesTasks.Select(x => x.request).Cast <Task>()));

            Items.AddRange(itemsTasks.Select(x => new PoeNinjaCacheItem <PoeNinjaItem> {
                Type = x.itemType.ToString(), Items = x.request.Result.Lines
            }).SelectMany(x => x.Items));
            Currencies.AddRange(currenciesTasks.Select(x => new PoeNinjaCacheItem <PoeNinjaCurrency> {
                Type = x.currencyType.ToString(), Items = x.request.Result.Lines
            }).SelectMany(x => x.Items));

            LastRefreshTimestamp = DateTime.Now;

            logger.Information($"PoeNinja cache populated.");
        }
Exemple #2
0
        public async Task RefreshData()
        {
            Items        = new List <PoeNinjaItem>();
            Currencies   = new List <PoeNinjaCurrency>();
            Translations = new Dictionary <string, string>();

            if (!client.IsSupportingCurrentLanguage)
            {
                logger.Information($"PoeNinja doesn't support this language.");
                return;
            }

            logger.Information($"Populating PoeNinja cache.");

            var itemsTasks = Enum.GetValues(typeof(ItemType))
                             .Cast <ItemType>()
                             .Select(x => new { itemType = x, request = client.QueryItem(configuration.LeagueId, x) })
                             .ToList();
            var currenciesTasks = Enum.GetValues(typeof(CurrencyType))
                                  .Cast <CurrencyType>()
                                  .Select(x => new { currencyType = x, request = client.QueryItem(configuration.LeagueId, x) })
                                  .ToList();

            await Task.WhenAll(itemsTasks.Select(x => x.request).Cast <Task>().Concat(currenciesTasks.Select(x => x.request).Cast <Task>()));

            Items = itemsTasks.Select(x => new PoeNinjaCacheItem <PoeNinjaItem> {
                Type = x.itemType.ToString(), Items = x.request.Result.Lines
            }).SelectMany(x => x.Items).ToList();
            Currencies = currenciesTasks.Select(x => new PoeNinjaCacheItem <PoeNinjaCurrency> {
                Type = x.currencyType.ToString(), Items = x.request.Result.Lines
            }).SelectMany(x => x.Items).ToList();

            // PoeNinja also includes translations of an item's description,
            // we will strip those by supposing that they always end with a dot (end of sentence).
            var flattenedTranslations = itemsTasks.Select(x => x.request.Result.Language?.Translations?.Where(y => !y.Value.Contains(".")))
                                        .Where(x => x != null)
                                        .SelectMany(x => x)
                                        .Distinct()
                                        .ToDictionary(x => x.Key, x => x.Value);

            if (flattenedTranslations.Any())
            {
                // We flip the dictionary to use the value instead of the key and ignore duplicates.
                Translations = flattenedTranslations.GroupBy(x => x.Value).Select(x => x.First()).ToDictionary(x => x.Value, x => x.Key);
            }

            LastRefreshTimestamp = DateTime.Now;

            logger.Information($"PoeNinja cache populated.");

            return;
        }