コード例 #1
0
        public void Execute(IJobExecutionContext context)
        {
            using (var wowLadderRepository = new WowLadderLiteDbRepository(SimpleLadderConfig.WowLadderLiteDbConnection))
                using (var trans = wowLadderRepository.BeginTransaction())
                {
                    try
                    {
                        if (wowLadderRepository.IsDownloadedToday)
                        {
                            return;
                        }

                        IReadOnlyList <PvpApiRowModel> ladderRows = _wowApiClient.GetAllPvpLadderRows();

                        if (!ladderRows.Any())
                        {
                            return;
                        }

                        foreach (BlizzardLocale locale in Enum.GetValues(typeof(BlizzardLocale)))
                        {
                            foreach (WowPvpBracket bracket in Enum.GetValues(typeof(WowPvpBracket)))
                            {
                                if (ladderRows.Any(lr => lr.Locale == (byte)locale && lr.Bracket == (byte)bracket))
                                {
                                    wowLadderRepository.RemoveRecords(locale, bracket);
                                }
                            }
                        }

                        wowLadderRepository.Create(ladderRows);
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        LogerManager.LogError(ex);
                        trans.Rollback();
                    }
                }
        }
コード例 #2
0
        /// <summary>
        /// Return pvp world of warcraft ladder data from official blizzard web api
        /// </summary>
        /// <param name="locale">BlizzardLocale.All is invalid param for this method</param>
        /// <param name="bracket">WowPvpBracket.All is invalid param for this method</param>
        /// <returns></returns>
        public async Task <IReadOnlyList <PvpApiRowModel> > GetPvpLadderRowsAsync(BlizzardLocale locale, WowPvpBracket bracket)
        {
            if (locale == BlizzardLocale.All)
            {
                throw new InvalidEnumArgumentException(nameof(BlizzardLocale.All));
            }

            if (bracket == WowPvpBracket.All)
            {
                throw new InvalidEnumArgumentException(nameof(WowPvpBracket.All));
            }

            var url = $"{BaseUrl}/{bracket.Stringify()}?locale={locale}&apikey={ApiKey}";

            try
            {
                using (var content = await HttpClient.GetStreamAsync(url))
                    using (var streamReader = new StreamReader(content))
                        using (var jsonTextReader = new JsonTextReader(streamReader))
                        {
                            var serializer = new JsonSerializer();
                            var rows       = serializer.Deserialize <PvpApiRowsModel>(jsonTextReader);

                            foreach (var row in rows.Rows)
                            {
                                row.Locale       = (byte)locale;
                                row.DownloadedOn = DateTime.Now;
                                row.Bracket      = (byte)bracket;
                            }

                            return(rows.Rows);
                        }
            }
            catch (Exception ex)
            {
                LogerManager.LogError(ex, $"url: {url}");
                return(null);
            }
        }