private async Task MonitorLogAsync(CachedClanWarLog cached) { if (_stopRequestedTokenSource.IsCancellationRequested || _clansClient.UpdatingClanWar.TryAdd(cached.Tag, new byte()) == false) { return; } try { CachedClanWarLog fetched = await CachedClanWarLog .FromClanWarLogResponseAsync(cached.Tag, _clansClient, _clansApi, _stopRequestedTokenSource.Token) .ConfigureAwait(false); if (fetched.Data != null && _clansClient.HasUpdated(cached, fetched)) { _clansClient.OnClanWarLogUpdated(cached.Data, fetched.Data); } cached.UpdateFrom(fetched); } finally { _clansClient.UpdatingClanWar.TryRemove(cached.Tag, out _); } }
private async Task MonitorClanAsync(CachedClan cached) { if (_stopRequestedTokenSource.IsCancellationRequested || UpdatingClan.TryAdd(cached.Tag, new byte()) == false) { return; } try { CachedClan fetched = await CachedClan.FromClanResponseAsync(cached.Tag, _clansClient, _clansApi, _stopRequestedTokenSource.Token); if (cached.Data != null && fetched.Data != null && _clansClient.HasUpdated(cached, fetched)) { _clansClient.OnClanUpdated(cached.Data, fetched.Data); } cached.UpdateFrom(fetched); } finally { UpdatingClan.TryRemove(cached.Tag, out _); } }
private async Task MonitorWarAsync(CachedWar cached) { if (_stopRequestedTokenSource.IsCancellationRequested || _clansClient.UpdatingWar.TryAdd(cached, new byte()) == false) { return; } try { using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); List <CachedClanWar>?cachedClanWars = null; if (cached.WarTag == null) { cachedClanWars = await dbContext.ClanWars .AsNoTracking() .Where(c => cached.ClanTags.Contains(c.Tag)) .OrderByDescending(c => c.ServerExpiration) .ToListAsync(_stopRequestedTokenSource.Token) .ConfigureAwait(false); if (cachedClanWars.Count == 2 && cachedClanWars.All(c => c.PreparationStartTime > cached.PreparationStartTime) || cached.EndTime.AddDays(8) < DateTime.UtcNow) { cached.IsFinal = true; return; } CachedClanWar?cachedClanWar = cachedClanWars .OrderByDescending(c => c.ServerExpiration) .FirstOrDefault(c => c.PreparationStartTime == cached.PreparationStartTime); if (cached.Data != null && cachedClanWar?.Data != null && _clansClient.HasUpdated(cached, cachedClanWar)) { _clansClient.OnClanWarUpdated(cached.Data, cachedClanWar.Data); } if (cachedClanWar != null) { cached.UpdateFrom(cachedClanWar); } } else { CachedWar?fetched = await CachedWar .FromClanWarLeagueWarResponseAsync( cached.WarTag, cached.Season.Value, _clansClient, _clansApi, _stopRequestedTokenSource.Token) .ConfigureAwait(false); if (cached.Data != null && fetched.Data != null && cached.Season == fetched.Season && _clansClient.HasUpdated(cached, fetched)) { _clansClient.OnClanWarUpdated(cached.Data, fetched.Data); } cached.UpdateFrom(fetched); } cached.IsFinal = cached.State == WarState.WarEnded; SendWarAnnouncements(cached, cachedClanWars); } finally { _clansClient.UpdatingWar.TryRemove(cached, out _); } }
private async Task MonitorCwlAsync(CachedClanWarLeagueGroup cached, CacheContext dbContext) { if (_stopRequestedTokenSource.IsCancellationRequested || _updatingGroup.TryAdd(cached.Tag, new byte()) == false) { return; } try { CachedClanWarLeagueGroup?fetched = await CachedClanWarLeagueGroup .FromClanWarLeagueGroupResponseAsync(cached.Tag, _clansClient, _clansApi, _stopRequestedTokenSource.Token) .ConfigureAwait(false); if (fetched.Data != null && _clansClient.HasUpdated(cached, fetched)) { _clansClient.OnClanWarLeagueGroupUpdated(cached.Data, fetched.Data); } cached.UpdateFrom(fetched); List <Task> tasks = new List <Task>(); HashSet <string> downloadWarTags = new HashSet <string>(); if (cached.Data == null || cached.Season.Value.Month < DateTime.Now.Month) { return; } try { foreach (var round in cached.Data.Rounds) { foreach (string warTag in round.WarTags.Where(w => w != "#0")) { if (_updatingWarTags.TryAdd(warTag, new byte())) { downloadWarTags.Add(warTag); tasks.Add(ReturnNewWarOrDefaultAsync(cached.Tag, cached.Season.Value, warTag)); } } } await Task.WhenAll(tasks).ConfigureAwait(false); foreach (Task <CachedWar?> cachedWarTask in tasks) { if (cachedWarTask.IsCompletedSuccessfully && cachedWarTask.Result is CachedWar cachedWar && cachedWar.Data != null) { dbContext.Wars.Add(cachedWar); _clansClient.OnClanWarAdded(cachedWar.Data); } } } finally { foreach (string warTag in downloadWarTags) { _updatingWarTags.TryRemove(warTag, out _); } } } catch (Exception e) { _clansClient.OnLog(this, new LogEventArgs(nameof(MonitorCwlAsync), LogLevel.Debug, $"Error updating group:{cached.Tag};\n{e.Message};\n{e.InnerException?.Message}")); } finally { _updatingGroup.TryRemove(cached.Tag, out _); } }