public async Task AddOrUpdateAsync(IEnumerable <string> tags, bool downloadWars = true, bool downloadCwl = true, bool downloadMembers = false) { List <string> formattedTags = new List <string>(); foreach (string tag in tags) { formattedTags.Add(Clash.FormatTag(tag)); } using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); List <CachedClan> cachedClans = await dbContext.Clans .Where(c => formattedTags.Contains(c.Tag)) .ToListAsync() .ConfigureAwait(false); foreach (string formattedTag in formattedTags) { CachedClan cachedClan = cachedClans.FirstOrDefault(c => c.Tag == formattedTag) ?? await PrepareNewCachedClanAsync(formattedTag, downloadWars, downloadCwl, downloadMembers, dbContext).ConfigureAwait(false); cachedClan.Tag = formattedTag; cachedClan.DownloadCurrentWar = downloadWars; cachedClan.DownloadCwl = downloadCwl; cachedClan.DownloadMembers = downloadMembers; } await dbContext.SaveChangesAsync().ConfigureAwait(false); }
public async Task DeleteAsync(string tag) { string formattedTag = Clash.FormatTag(tag); using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); while (!UpdatingVillage.TryAdd(formattedTag, new byte())) { await Task.Delay(500); } try { CachedPlayer cachedPlayer = await dbContext.Players.FirstOrDefaultAsync(c => c.Tag == formattedTag); if (cachedPlayer != null) { dbContext.Players.Remove(cachedPlayer); } await dbContext.SaveChangesAsync(); } finally { UpdatingVillage.TryRemove(formattedTag, out _); } }
public async Task <CachedPlayer> UpdateAsync(string tag, bool download = true) { string formattedTag = Clash.FormatTag(tag); using var scope = Services.CreateScope(); CacheContext cacheContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); CachedPlayer cachedPlayer = await cacheContext.Players .FirstOrDefaultAsync(v => v.Tag == formattedTag) .ConfigureAwait(false); if (cachedPlayer != null && cachedPlayer.Download == download) { return(cachedPlayer); } if (cachedPlayer == null) { cachedPlayer = new CachedPlayer(formattedTag); cacheContext.Players.Add(cachedPlayer); } cachedPlayer.Download = download; await cacheContext.SaveChangesAsync().ConfigureAwait(false); return(cachedPlayer); }
public async Task <ClanWarLeagueGroup?> GetOrFetchLeagueGroupOrDefaultAsync(string tag, CancellationToken?cancellationToken = default) { string formattedTag = Clash.FormatTag(tag); using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); return((await dbContext.Groups.FirstOrDefaultAsync(g => g.Tag == formattedTag))?.Data ?? await _clansApi.FetchClanWarLeagueGroupOrDefaultAsync(formattedTag, cancellationToken)); }
public async Task <CachedPlayer> GetCachedPlayerAsync(string tag, CancellationToken?cancellationToken = default) { string formattedTag = Clash.FormatTag(tag); using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); return(await dbContext.Players .Where(i => i.Tag == formattedTag) .FirstAsync(cancellationToken.GetValueOrDefault()) .ConfigureAwait(false)); }
public async Task <List <CachedWar> > GetClanWarsAsync(string tag, CancellationToken?cancellationToken = default) { string formattedTag = Clash.FormatTag(tag); using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); return(await dbContext.Wars .AsNoTracking() .Where(i => i.ClanTag == formattedTag || i.OpponentTag == formattedTag) .ToListAsync(cancellationToken.GetValueOrDefault(_stopRequestedTokenSource.Token)) .ConfigureAwait(false)); }
public async Task AddAsync(string tag, bool downloadWars = true, bool downloadCwl = true, bool downloadMembers = false) { string formattedTag = Clash.FormatTag(tag); using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); CachedClan cachedClan = await dbContext.Clans.Where(c => c.Tag == formattedTag).FirstOrDefaultAsync().ConfigureAwait(false); cachedClan ??= await PrepareNewCachedClanAsync(formattedTag, downloadWars, downloadCwl, downloadMembers, dbContext).ConfigureAwait(false); await dbContext.SaveChangesAsync().ConfigureAwait(false); return; }
public async Task <CachedWar?> GetLeagueWarOrDefaultAsync(string warTag, DateTime season, CancellationToken?cancellationToken = default) { string formattedTag = Clash.FormatTag(warTag); using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); CachedWar?war = await dbContext.Wars .AsNoTracking() .FirstOrDefaultAsync(w => w.WarTag == formattedTag && w.Season == season, cancellationToken.GetValueOrDefault()) .ConfigureAwait(false); war?.Data?.Initialize(war.ServerExpiration, formattedTag); return(war); }
public async Task <List <CachedClan> > GetCachedClansAsync(IEnumerable <string> tags, CancellationToken?cancellationToken = default) { List <string> formattedTags = new List <string>(); foreach (string tag in tags) { formattedTags.Add(Clash.FormatTag(tag)); } using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); return(await dbContext.Clans .AsNoTracking() .Where(i => formattedTags.Contains(i.Tag)) .ToListAsync(cancellationToken.GetValueOrDefault()) .ConfigureAwait(false)); }
public async Task DeleteAsync(string tag) { string formattedTag = Clash.FormatTag(tag); using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); while (!_clanMonitor.UpdatingClan.TryAdd(formattedTag, new byte())) { await Task.Delay(500); } while (!UpdatingClanWar.TryAdd(formattedTag, new byte())) { await Task.Delay(500); } try { CachedClan cachedClan = await dbContext.Clans.FirstOrDefaultAsync(c => c.Tag == formattedTag); CachedClanWarLog cachedClanWarLog = await dbContext.WarLogs.FirstOrDefaultAsync(c => c.Tag == formattedTag); if (cachedClan != null) { dbContext.Clans.Remove(cachedClan); } if (cachedClanWarLog != null) { dbContext.WarLogs.Remove(cachedClanWarLog); } await dbContext.SaveChangesAsync(); } finally { _clanMonitor.UpdatingClan.TryRemove(formattedTag, out _); UpdatingClanWar.TryRemove(formattedTag, out _); } }
/// <summary> /// Get player information Get information about a single player by player tag. Player tags can be found either in game or by from clan member lists. Note that player tags start with hash character '#' and that needs to be URL-encoded properly to work in URL, so for example player tag '#2ABC' would become '%232ABC' in the URL. /// </summary> /// <exception cref="CocApi.Client.ApiException">Thrown when fails to make API call</exception> /// <param name="playerTag">Tag of the player.</param> /// <param name="cancellationToken">Cancellation Token to cancel the request.</param> /// <returns>Task of ApiResponse (Player)</returns> public async System.Threading.Tasks.Task <CocApi.Client.ApiResponse <Player> > FetchPlayerResponseAsync(string playerTag, System.Threading.CancellationToken?cancellationToken = null) { if (playerTag == null) { throw new ArgumentNullException(nameof(playerTag)); } playerTag = Clash.FormatTag(playerTag); using System.Net.Http.HttpRequestMessage request = new System.Net.Http.HttpRequestMessage(); string path = "/players/{playerTag}"; path = path.Replace("{playerTag}", Uri.EscapeDataString(playerTag)); path = $"{path}?"; if (path.EndsWith("&")) { path = path[..^ 1];
public async Task <CachedWar?> GetActiveClanWarOrDefaultAsync(string tag, CancellationToken?cancellationToken = default) { string formattedTag = Clash.FormatTag(tag); using var scope = Services.CreateScope(); CacheContext dbContext = scope.ServiceProvider.GetRequiredService <CacheContext>(); List <CachedWar> cache = await dbContext.Wars .AsNoTracking() .Where(i => (i.ClanTag == formattedTag || i.OpponentTag == formattedTag)) .OrderByDescending(w => w.PreparationStartTime) .ToListAsync(cancellationToken.GetValueOrDefault(_stopRequestedTokenSource.Token)) .ConfigureAwait(false); if (cache.Count == 0) { return(null); } return(cache.FirstOrDefault(c => c.State == WarState.InWar && c.EndTime > DateTime.UtcNow) ?? cache.FirstOrDefault(c => c.State == WarState.Preparation && c.EndTime > DateTime.UtcNow) ?? cache.First()); }