Esempio n. 1
0
        private async Task <CachedWar?> ReturnNewWarOrDefaultAsync(string tag, DateTime season, string warTag)
        {
            if (_stopRequestedTokenSource.IsCancellationRequested)
            {
                return(null);
            }

            try
            {
                using var scope = Services.CreateScope();

                CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>();

                CachedWar?cachedWar = await dbContext.Wars
                                      .FirstOrDefaultAsync(w => w.WarTag == warTag && w.Season == season)
                                      .ConfigureAwait(false);

                if (cachedWar != null)
                {
                    return(null);
                }

                CachedWar fetched = await CachedWar
                                    .FromClanWarLeagueWarResponseAsync(warTag, season, _clansClient, _clansApi, _stopRequestedTokenSource.Token)
                                    .ConfigureAwait(false);

                if (fetched.ClanTags.Any(c => c == tag) == false)
                {
                    return(null);
                }

                return(fetched);
            }
            catch (Exception e)
            {
                _clansClient.OnLog(this, new LogEventArgs(nameof(ReturnNewWarOrDefaultAsync), LogLevel.Debug, $"Error updating cwl war:{tag}\n{e.Message}\n{e.InnerException?.Message}"));

                return(null);
            }
        }
Esempio n. 2
0
        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 _);
            }
        }