public async Task <PlayerInfo> GetGames(string userVanityUrlName) { EventSource.SetCurrentThreadActivityId(Guid.NewGuid()); SiteEventSource.Log.HandleGetGamesRequestStart(userVanityUrlName); long steamId; var cachedSteamId = MemoryCache.Default[userVanityUrlName]; if (cachedSteamId != null) { steamId = (long)cachedSteamId; } else { try { steamId = await SiteUtil.GetFirstResult( ct => ResolveVanityUrl(userVanityUrlName, ct), s_vanitUrlResolutionParallelization, e => { }).ConfigureAwait(true); } catch (HttpResponseException) //Steam API rejected vanity URL name { //if the userVanityUrlName is numerical, we'll try assuming it's actually the Steam64 ID if (!Int64.TryParse(userVanityUrlName, out steamId)) { throw; } } MemoryCache.Default.Set(userVanityUrlName, steamId, DateTimeOffset.Now.AddMinutes(VanityUrlToSteamIdCacheExpirationMinutes)); } var ownedGamesInfo = await GetGamesCore(ct => GetOwnedGames(steamId, ct), ct => GetPersonaInfo(steamId, ct)).ConfigureAwait(true); SiteEventSource.Log.HandleGetGamesRequestStop(userVanityUrlName); return(ownedGamesInfo); }
private static async Task <PlayerInfo> GetGamesCore(Func <CancellationToken, Task <OwnedGame[]> > gamesGetter, Func <CancellationToken, Task <PersonaInfo> > personaGetter) { var ownedGamesTask = SiteUtil.GetFirstResult(gamesGetter, s_ownedGamesRetrievalParallelization, e => { }).ConfigureAwait(false); var personaInfoTask = SiteUtil.GetFirstResult(personaGetter, s_playerSummaryRetrievalParallelization, e => { }).ConfigureAwait(false); var ownedGames = await ownedGamesTask; var personaInfo = await personaInfoTask; SiteEventSource.Log.PrepareResponseStart(); var games = new List <SteamAppUserData>(); bool partialCache = false; int playtime = 0; int mainTtb = 0; int extrasTtb = 0; int completionistTtb = 0; int mainRemaining = 0; int extrasRemaining = 0; int completionistRemaining = 0; int mainCompleted = 0; int extrasCompleted = 0; int completionistCompleted = 0; var playtimesByGenre = new Dictionary <string, int>(); var playtimesByMetacritic = new Dictionary <int, int>(); var playtimesByAppType = new Dictionary <string, int>(); var playtimesByReleaseYear = new Dictionary <int, int>(); foreach (var game in ownedGames) { var inCache = Cache.TryGetValue(game.appid, out SteamAppData cachedGameData); if (!inCache) { SiteEventSource.Log.SkipNonCachedApp(game.appid, game.name); partialCache = true; continue; } if (cachedGameData.HltbInfo == null) { SiteEventSource.Log.SkipNonGame(game.appid, game.name); continue; } playtime += game.playtime_forever; mainTtb += cachedGameData.HltbInfo.MainTtb; extrasTtb += cachedGameData.HltbInfo.ExtrasTtb; completionistTtb += cachedGameData.HltbInfo.CompletionistTtb; mainRemaining += Math.Max(0, cachedGameData.HltbInfo.MainTtb - game.playtime_forever); extrasRemaining += Math.Max(0, cachedGameData.HltbInfo.ExtrasTtb - game.playtime_forever); completionistRemaining += Math.Max(0, cachedGameData.HltbInfo.CompletionistTtb - game.playtime_forever); mainCompleted += Math.Min(game.playtime_forever, cachedGameData.HltbInfo.MainTtb); extrasCompleted += Math.Min(game.playtime_forever, cachedGameData.HltbInfo.ExtrasTtb); completionistCompleted += Math.Min(game.playtime_forever, cachedGameData.HltbInfo.CompletionistTtb); IReadOnlyList <string> genres = cachedGameData.Genres.Except(NonGenres).ToArray(); if (genres.Count == 0) { genres = cachedGameData.Genres; } IncrementDictionaryEntryFromZero(playtimesByGenre, String.Join("/", genres), cachedGameData.HltbInfo.MainTtb); IncrementDictionaryEntryFromZero(playtimesByMetacritic, cachedGameData.MetacriticScore, cachedGameData.HltbInfo.MainTtb); IncrementDictionaryEntryFromZero(playtimesByReleaseYear, cachedGameData.ReleaseYear, cachedGameData.HltbInfo.MainTtb); IncrementDictionaryEntryFromZero(playtimesByAppType, cachedGameData.AppType, cachedGameData.HltbInfo.MainTtb); games.Add(new SteamAppUserData(cachedGameData, game.playtime_forever)); } SiteEventSource.Log.PrepareResponseStop(); return(new PlayerInfo(partialCache, games.ToArray(), ownedGames.Length - games.Count, new Totals(playtime, mainTtb, extrasTtb, completionistTtb, mainRemaining, extrasRemaining, completionistRemaining, mainCompleted, extrasCompleted, completionistCompleted, playtimesByGenre, playtimesByMetacritic, playtimesByAppType, playtimesByReleaseYear), personaInfo)); }