コード例 #1
0
 public async Task <StoredGuild> GetGuildAsync(string guildName, StoredRealm realm, int profileId)
 {
     return(await this.cache.GetOrCacheAsync(
                this.GetFromDatabase(guildName, profileId, realm),
                this.GetFromSource(guildName, profileId, realm),
                this.Store(),
                this.GetKey(guildName, profileId, realm.Id)));
 }
コード例 #2
0
 public async Task <StoredPlayer> GetPlayerAsync(string playerName, StoredRealm realm, StoredGuild guild, int profileId)
 {
     return(await this.cache.GetOrCacheAsync(
                this.GetFromDatabase(playerName, realm.Id, profileId),
                this.GetFromSource(playerName, realm, guild, profileId),
                this.Store(),
                this.GetKey(playerName, realm.Name, profileId)));
 }
コード例 #3
0
ファイル: CacheTests.cs プロジェクト: davew2040/GuildTools
        public async Task PlayerStoreTest()
        {
            const string         connectionString = "Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=GuildTools;Integrated Security=True";
            KeyedResourceManager manager          = new KeyedResourceManager();

            BlizzardApiSecrets blizzardSecrets = new BlizzardApiSecrets()
            {
                ClientId     = this.config.GetValue <string>("BlizzardApiSecrets:ClientId"),
                ClientSecret = this.config.GetValue <string>("BlizzardApiSecrets:ClientSecret")
            };

            IMemoryCache      memoryCache = new MemoryCache(new MemoryCacheOptions());
            GuildToolsContext context     = new GuildToolsContext(SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options as DbContextOptions);

            HttpClient              client          = new HttpClient();
            IDataRepository         repo            = new DataRepository(context);
            IBlizzardService        blizzardService = new BlizzardService(repo, this.config, client);
            PerRequestCallThrottler throttler       = new PerRequestCallThrottler(TimeSpan.FromSeconds(1));
            IGuildService           guildService    = new GuildService(blizzardService, throttler);

            int profileId = 1;

            GameRegion region = new GameRegion()
            {
                Id         = 1,
                RegionName = "US"
            };

            StoredRealm realm = context.StoredRealms.Include(a => a.Region).First();
            StoredGuild guild = context.StoredGuilds.First();

            PlayerStoreByValue playerStore = new PlayerStoreByValue(guildService, memoryCache, context, manager);

            DateTime initial = DateTime.Now;

            var realms = await playerStore.GetPlayerAsync("Kromp", realm, guild, profileId);

            DateTime second       = DateTime.Now;
            TimeSpan sinceInitial = second - initial;

            var realms2 = await playerStore.GetPlayerAsync("Kromp", realm, guild, profileId);

            DateTime third       = DateTime.Now;
            TimeSpan sinceSecond = third - second;

            var realms3 = await playerStore.GetPlayerAsync("Kromp", realm, guild, profileId);

            DateTime fourth     = DateTime.Now;
            TimeSpan sinceThird = fourth - third;

            int x = 42;
        }
コード例 #4
0
        private Func <Task <CacheResult <StoredGuild> > > GetFromSource(string guildName, int profileId, StoredRealm realm)
        {
            return(async() =>
            {
                var json = await this.blizzardService.GetGuildAsync(
                    guildName,
                    realm.Name,
                    BlizzardUtilities.GetBlizzardRegionFromEfRegion((EfEnums.GameRegionEnum)realm.Region.Id));

                if (BlizzardService.DidGetFail(json))
                {
                    return null;
                }

                var guild = ExternalServices.Blizzard.JsonParsing.GuildParsing.GetGuild(json);

                return new CacheResult <StoredGuild>()
                {
                    Found = true,
                    Result = new StoredGuild()
                    {
                        Name = guild.Name,
                        ProfileId = profileId,
                        RealmId = realm.Id
                    }
                };
            });
        }
コード例 #5
0
 private Func <Task <StoredGuild> > GetFromDatabase(string guildName, int profileId, StoredRealm realm)
 {
     return(async() =>
     {
         return await this.context.StoredGuilds.SingleOrDefaultAsync(
             x => x.Name == guildName &&
             x.ProfileId == profileId &&
             x.RealmId == realm.Id);
     });
 }
コード例 #6
0
        private Func <Task <CacheResult <StoredPlayer> > > GetFromSource(string name, StoredRealm realm, StoredGuild guild, int profileId)
        {
            return(async() =>
            {
                var result = await this.guildService.GetSinglePlayerAsync(
                    BlizzardUtilities.GetBlizzardRegionFromEfRegion((GameRegionEnum)realm.Region.Id),
                    realm.Slug,
                    name);

                if (result == null)
                {
                    return new CacheResult <StoredPlayer>()
                    {
                        Found = false
                    };
                }

                return new CacheResult <StoredPlayer>()
                {
                    Found = true,
                    Result = new StoredPlayer()
                    {
                        Name = result.PlayerName,
                        RealmId = realm.Id,
                        ProfileId = profileId,
                        GuildId = guild.Id,
                        Level = result.Level,
                        Class = result.Class
                    }
                };
            });
        }