Skip to content

Commit

Permalink
added debugging lines
Browse files Browse the repository at this point in the history
  • Loading branch information
devhl-labs committed Mar 11, 2024
1 parent 8a2f7cd commit 6f8da6e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 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.5</Version>
<Version>2.11.6-debug</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.0.0</FileVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
46 changes: 44 additions & 2 deletions src/CocApi.Cache/Services/WarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ public WarService(

protected override async Task ExecuteScheduledTaskAsync(CancellationToken cancellationToken)
{
Logger.LogInformation("Executing war service");

SetDateVariables();

Logger.LogInformation("Setting variables");

WarServiceOptions options = Options.Value.Wars;

using var scope = ScopeFactory.CreateScope();

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

Logger.LogInformation("Querying wars");

List<CachedWar> cachedWars = await dbContext.Wars
.Where(w =>
string.IsNullOrWhiteSpace(w.WarTag) &&
Expand All @@ -85,6 +91,8 @@ protected override async Task ExecuteScheduledTaskAsync(CancellationToken cancel
tags.Add(cachedWar.OpponentTag);
}

Logger.LogInformation("Querying clans");

List<CachedClan> allCachedClans = await dbContext.Clans
.Where(c => tags.Contains(c.Tag))
.AsNoTracking()
Expand All @@ -97,6 +105,8 @@ protected override async Task ExecuteScheduledTaskAsync(CancellationToken cancel

IClansApi clansApi = ApiFactory.Create<IClansApi>();

Logger.LogInformation("Iterating wars");

try
{
foreach (CachedWar cachedWar in cachedWars)
Expand All @@ -117,8 +127,12 @@ protected override async Task ExecuteScheduledTaskAsync(CancellationToken cancel
tasks.Add(SendWarAnnouncementsAsync(cachedWar, cachedClans.Select(c => c.CurrentWar).ToArray(), cancellationToken));
}

Logger.LogInformation("Waiting for tasks to complete");

await Task.WhenAll(tasks).WaitAsync(cancellationToken).ConfigureAwait(false);

Logger.LogInformation("Tasks are complete");

await dbContext.SaveChangesAsync(CancellationToken.None).ConfigureAwait(false);
}
finally
Expand All @@ -139,6 +153,8 @@ private async Task UpdateWarAsync(
CachedClan? cachedOpponent,
CancellationToken cancellationToken)
{
Logger.LogInformation("Creating cached clans");

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

Expand All @@ -158,17 +174,21 @@ private async Task UpdateWarAsync(
.OrderByDescending(c => c?.CurrentWar.ExpiresAt)
.FirstOrDefault(c => c?.CurrentWar.PreparationStartTime == cachedWar.PreparationStartTime);

if (cachedWar.Content != null &&
clan?.CurrentWar.Content != null &&
if (cachedWar.Content != null &&
clan?.CurrentWar.Content != null &&
CachedWar.HasUpdated(cachedWar, clan.CurrentWar) &&
ClanWarUpdated != null)
{
Logger.LogInformation("sending the war updated event");

await ClanWarUpdated.Invoke(this, new ClanWarUpdatedEventArgs(
cachedWar.Content,
clan.CurrentWar.Content,
cachedClan?.Content,
cachedOpponent?.Content,
cancellationToken))
.ConfigureAwait(false);
}

if (clan != null)
{
Expand All @@ -183,6 +203,9 @@ private async Task UpdateWarAsync(
c.CurrentWar.PreparationStartTime > cachedWar.PreparationStartTime
)))
cachedWar.IsFinal = true;


Logger.LogInformation("Done sending war updated event");
}

private async Task<CachedClan?> CreateCachedClan(IClansApi clansApi, string tag, CacheDbContext dbContext, CancellationToken cancellationToken)
Expand All @@ -194,6 +217,8 @@ private async Task UpdateWarAsync(
{
_unmonitoredClans.Add(tag);

Logger.LogInformation("Creating cached clan war from current war");

CachedClanWar cachedClanWar = await CachedClanWar
.FromCurrentWarResponseAsync(tag, Options.Value.RealTime == null ? default : new(Options.Value.RealTime.Value), TimeToLiveProvider, clansApi, cancellationToken)
.ConfigureAwait(false);
Expand Down Expand Up @@ -240,7 +265,11 @@ private async Task SendWarAnnouncementsAsync(CachedWar cachedWar, CachedClanWar[
cachedWar.Announcements |= Announcements.WarStartingSoon;

if (ClanWarStartingSoon != null)
{
Logger.LogInformation("Clan war starting soon");

await ClanWarStartingSoon.Invoke(this, new WarEventArgs(cachedWar.Content, cancellationToken)).ConfigureAwait(false);
}
}

if (cachedWar.Announcements.HasFlag(Announcements.WarEndingSoon) == false &&
Expand All @@ -250,7 +279,10 @@ private async Task SendWarAnnouncementsAsync(CachedWar cachedWar, CachedClanWar[
cachedWar.Announcements |= Announcements.WarEndingSoon;

if (ClanWarEndingSoon != null)
{
Logger.LogInformation("Clan war ending soon");
await ClanWarEndingSoon.Invoke(this, new WarEventArgs(cachedWar.Content, cancellationToken)).ConfigureAwait(false);
}
}

if (cachedWar.Announcements.HasFlag(Announcements.WarEndNotSeen) == false &&
Expand All @@ -264,7 +296,11 @@ private async Task SendWarAnnouncementsAsync(CachedWar cachedWar, CachedClanWar[
cachedWar.Announcements |= Announcements.WarEndNotSeen;

if (ClanWarEndNotSeen != null)
{
Logger.LogInformation("Clan war end not seen");

await ClanWarEndNotSeen.Invoke(this, new WarEventArgs(cachedWar.Content, cancellationToken)).ConfigureAwait(false);
}
}

if (cachedWar.Announcements.HasFlag(Announcements.WarEnded) == false &&
Expand All @@ -274,8 +310,14 @@ private async Task SendWarAnnouncementsAsync(CachedWar cachedWar, CachedClanWar[
cachedWar.Announcements |= Announcements.WarEnded;

if (ClanWarEnded != null)
{
Logger.LogInformation("Clan war ended");

await ClanWarEnded.Invoke(this, new WarEventArgs(cachedWar.Content, cancellationToken)).ConfigureAwait(false);
}
}

Logger.LogInformation("done sending war announcement");
}
catch (Exception e)
{
Expand Down

0 comments on commit 6f8da6e

Please sign in to comment.