Skip to content

Commit

Permalink
added debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
devhl-labs committed Oct 16, 2024
1 parent 0ebd9f9 commit 0dfa8b7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/CocApi.Cache/CocApi.Cache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Authors>devhl</Authors>
<Product />
<Description>Caches response from the Clash of Clans API.</Description>
<Version>2.11.9</Version>
<Version>2.11.10</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.0.0</FileVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
86 changes: 51 additions & 35 deletions src/CocApi.Cache/Services/WarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,50 +139,60 @@ private async Task UpdateWarAsync(
CachedClan? cachedOpponent,
CancellationToken cancellationToken)
{
if (cachedClan == null && cachedOpponent?.CurrentWar.IsExpired == true)
cachedClan = await CreateCachedClan(clansApi, cachedWar.ClanTag, dbContext, cancellationToken).ConfigureAwait(false);
try
{
if (cachedClan == null && cachedOpponent?.CurrentWar.IsExpired == true)
cachedClan = await CreateCachedClan(clansApi, cachedWar.ClanTag, dbContext, cancellationToken).ConfigureAwait(false);

if (cachedOpponent == null && cachedClan?.CurrentWar.IsExpired == true)
cachedOpponent = await CreateCachedClan(clansApi, cachedWar.OpponentTag, dbContext, cancellationToken).ConfigureAwait(false);
if (cachedOpponent == null && cachedClan?.CurrentWar.IsExpired == true)
cachedOpponent = await CreateCachedClan(clansApi, cachedWar.OpponentTag, dbContext, cancellationToken).ConfigureAwait(false);

List<CachedClan?> cachedClans = new() { cachedClan, cachedOpponent };
List<CachedClan?> cachedClans = new() { cachedClan, cachedOpponent };

if (cachedClans.All(c => c?.CurrentWar.PreparationStartTime > cachedWar.PreparationStartTime) || cachedWar.EndTime.AddDays(8) < now)
{
cachedWar.IsFinal = true;
if (cachedClans.All(c => c?.CurrentWar.PreparationStartTime > cachedWar.PreparationStartTime) || cachedWar.EndTime.AddDays(8) < now)
{
cachedWar.IsFinal = true;

return;
}
return;
}

CachedClan? clan = cachedClans
.OrderByDescending(c => c?.CurrentWar.ExpiresAt)
.FirstOrDefault(c => c?.CurrentWar.PreparationStartTime == cachedWar.PreparationStartTime);

if (cachedWar.Content != null &&
clan?.CurrentWar.Content != null &&
CachedWar.HasUpdated(cachedWar, clan.CurrentWar) &&
ClanWarUpdated != null)
await ClanWarUpdated.Invoke(this, new ClanWarUpdatedEventArgs(
cachedWar.Content,
clan.CurrentWar.Content,
cachedClan?.Content,
cachedOpponent?.Content,
cancellationToken))
.ConfigureAwait(false);
CachedClan? clan = cachedClans
.OrderByDescending(c => c?.CurrentWar.ExpiresAt)
.FirstOrDefault(c => c?.CurrentWar.PreparationStartTime == cachedWar.PreparationStartTime);

if (cachedWar.Content != null &&
clan?.CurrentWar.Content != null &&
CachedWar.HasUpdated(cachedWar, clan.CurrentWar) &&
ClanWarUpdated != null)
await ClanWarUpdated.Invoke(this, new ClanWarUpdatedEventArgs(
cachedWar.Content,
clan.CurrentWar.Content,
cachedClan?.Content,
cachedOpponent?.Content,
cancellationToken))
.ConfigureAwait(false);

if (clan != null)
{
cachedWar.UpdateFrom(clan.CurrentWar);

if (clan != null)
cachedWar.IsFinal = clan.CurrentWar.State == Rest.Models.WarState.WarEnded;
}
else if (cachedClans.All(c =>
c != null &&
(
c.CurrentWar.PreparationStartTime == DateTime.MinValue ||
c.CurrentWar.PreparationStartTime > cachedWar.PreparationStartTime
)))
cachedWar.IsFinal = true;
}
catch (Exception e)
{
cachedWar.UpdateFrom(clan.CurrentWar);

cachedWar.IsFinal = clan.CurrentWar.State == Rest.Models.WarState.WarEnded;
Logger.LogError(e, "UpdateWarAsync war: {} clan: {}", cachedWar.PreparationStartTime, cachedClan?.Tag ?? cachedOpponent?.Tag);

throw;
}
else if (cachedClans.All(c =>
c != null &&
(
c.CurrentWar.PreparationStartTime == DateTime.MinValue ||
c.CurrentWar.PreparationStartTime > cachedWar.PreparationStartTime
)))
cachedWar.IsFinal = true;
}

private async Task<CachedClan?> CreateCachedClan(IClansApi clansApi, string tag, CacheDbContext dbContext, CancellationToken cancellationToken)
Expand Down Expand Up @@ -220,6 +230,12 @@ private async Task UpdateWarAsync(

return cachedClan;
}
catch(Exception e)
{
Logger.LogError(e, "CreateCachedClan: {tag}", tag);

throw;
}
finally
{
Synchronizer.UpdatingClan.TryRemove(tag, out _);
Expand Down

0 comments on commit 0dfa8b7

Please sign in to comment.