diff --git a/ReplayBrowser/Services/LeaderboardService.cs b/ReplayBrowser/Services/LeaderboardService.cs index 072df93..954d131 100644 --- a/ReplayBrowser/Services/LeaderboardService.cs +++ b/ReplayBrowser/Services/LeaderboardService.cs @@ -93,18 +93,15 @@ public class LeaderboardService : IHostedService, IDisposable private readonly Ss14ApiHelper _apiHelper; private readonly IServiceScopeFactory _scopeFactory; private readonly AccountService _accountService; - private readonly ProfilePregeneratorService _profilePregeneratorService; - public LeaderboardService(IMemoryCache cache, Ss14ApiHelper apiHelper, IServiceScopeFactory factory, AccountService accountService, ProfilePregeneratorService profilePregeneratorService) + public LeaderboardService(IMemoryCache cache, Ss14ApiHelper apiHelper, IServiceScopeFactory factory, AccountService accountService) { _cache = cache; _apiHelper = apiHelper; _scopeFactory = factory; _accountService = accountService; - _profilePregeneratorService = profilePregeneratorService; } - public Task StartAsync(CancellationToken cancellationToken) { _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromHours(6)); @@ -398,24 +395,6 @@ public async Task GetLeaderboard(RangeOption rangeOption, strin stopwatch.Stop(); Log.Information("Calculating leaderboard took {Time}ms", stopwatch.ElapsedMilliseconds); - // We loop through every leaderboard and add the player to the permanent pregenerator list if they are not already on it. - foreach (var leaderboard in leaderboards) - { - foreach (var player in leaderboard.Value.Data) - { - if (player.Value.Player == null) - continue; - - if (player.Value.Player.PlayerGuid == null) - continue; - - if (_profilePregeneratorService.AlwaysGenerateProfiles.Contains((Guid)player.Value.Player.PlayerGuid)) - continue; - - _profilePregeneratorService.AlwaysGenerateProfiles.Add((Guid)player.Value.Player.PlayerGuid); - } - } - // Save leaderboard to cache (its expensive as fuck to calculate) var cacheEntryOptions = new MemoryCacheEntryOptions() .SetAbsoluteExpiration(TimeSpan.FromHours(5)); diff --git a/ReplayBrowser/Services/ProfilePregeneratorService.cs b/ReplayBrowser/Services/ProfilePregeneratorService.cs index 56d9a3f..f276a1f 100644 --- a/ReplayBrowser/Services/ProfilePregeneratorService.cs +++ b/ReplayBrowser/Services/ProfilePregeneratorService.cs @@ -24,11 +24,6 @@ public class ProfilePregeneratorService : IHostedService /// private readonly List _generatedProfiles = new(); - /// - /// List of profiles which get generated even if they are not on a watched profile list. (example being leaderboards) - /// - public List AlwaysGenerateProfiles = new List(); - /// /// A var which tracks the progress of the pregeneration. /// @@ -45,9 +40,7 @@ public ProfilePregeneratorService(IServiceScopeFactory scopeFactory, ReplayParse public Task StartAsync(CancellationToken cancellationToken) { _replayParserService.OnReplaysFinishedParsing += OnReplaysParsed; - - // In one minute, start the pregeneration. - Task.Delay(TimeSpan.FromMinutes(1), cancellationToken).ContinueWith(t => QueuePregeneration(), cancellationToken); + QueuePregeneration(); return Task.CompletedTask; } @@ -102,17 +95,12 @@ private async void PregenerateProfiles() using var scope = _scopeFactory.CreateScope(); var dbContext = scope.ServiceProvider.GetRequiredService(); var replayHelper = scope.ServiceProvider.GetRequiredService(); - var profilesToGenerate = new List(); - profilesToGenerate.AddRange(AlwaysGenerateProfiles); - await foreach (var account in dbContext.Accounts) - { - foreach (var profile in account.SavedProfiles) - { - if (profilesToGenerate.Contains(profile)) continue; - profilesToGenerate.Add(profile); - } - } + var profilesToGenerate = dbContext.Players + .Select(x => x.PlayerGuid) + .Where(x => !_generatedProfiles.Contains(x)) + .Distinct() + .ToList(); // Remove every profile already found in _generatedProfiles profilesToGenerate = profilesToGenerate.Except(_generatedProfiles).ToList();