Skip to content

Commit

Permalink
Fix enrollment report unstarted teams calc. Fix challenge launch fail…
Browse files Browse the repository at this point in the history
…ure after an attempt that is canceled
  • Loading branch information
sei-bstein committed Jan 15, 2025
1 parent 0f3ea4f commit 9848b05
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
43 changes: 22 additions & 21 deletions src/Gameboard.Api/Features/Challenge/Services/ChallengeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,33 @@ public async Task<Challenge> Create(NewChallenge model, string actorId, string g
TeamId = player.TeamId,
Specs = []
});

_launchCache.TryGetValue(player.TeamId, out var entry);

if (entry.Specs.Any(s => s.SpecId == model.SpecId))
{
throw new ChallengeStartPending();
}
else
try
{
entry.Specs.Add(new ChallengeLaunchCacheEntrySpec { GameId = player.GameId, SpecId = model.SpecId });
}
cancellationToken.ThrowIfCancellationRequested();

if (entry.Specs.Any(s => s.SpecId == model.SpecId))
{
throw new ChallengeStartPending();
}
else
{
entry.Specs.Add(new ChallengeLaunchCacheEntrySpec { GameId = player.GameId, SpecId = model.SpecId });
}

var spec = await _store
.WithNoTracking<Data.ChallengeSpec>()
.SingleAsync(s => s.Id == model.SpecId, cancellationToken);
var spec = await _store
.WithNoTracking<Data.ChallengeSpec>()
.SingleAsync(s => s.Id == model.SpecId, cancellationToken);

var playerCount = 1;
if (player.Game.AllowTeam)
{
playerCount = await _store
.WithNoTracking<Data.Player>()
.CountAsync(p => p.TeamId == player.TeamId, cancellationToken);
}
var playerCount = 1;
if (player.Game.AllowTeam)
{
playerCount = await _store
.WithNoTracking<Data.Player>()
.CountAsync(p => p.TeamId == player.TeamId, cancellationToken);
}

try
{
var challenge = await BuildAndRegisterChallenge(model, spec, player.Game, player, actorId, graderUrl, playerCount, model.Variant);

await _store.Create(challenge, cancellationToken);
Expand All @@ -171,7 +172,7 @@ public async Task<Challenge> Create(NewChallenge model, string actorId, string g
}
catch (Exception ex)
{
Logger.LogWarning(message: "Challenge registration failure: {exName} -- {exMessage}", ex.GetType().Name, ex.Message);
Logger.LogWarning("Challenge registration failure: {exName} -- {exMessage}", ex.GetType().Name, ex.Message);
throw;
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,23 @@ public async Task<EnrollmentReportStatSummary> GetSummaryStats(EnrollmentReportP
}
}

var distinctTeamIds = rawResults.Select(r => r.Player.TeamId).Distinct().ToArray();
var noChallengeStartedPlayers = rawResults.Where(r => r.ChallengesStarted == 0);
var noSessionPlayers = rawResults.Where(r => r.Player.SessionBegin.IsEmpty());
var teamsWithSession = rawResults.Where(r => r.Player.SessionBegin.IsNotEmpty()).Select(p => p.Player.TeamId).Distinct().Count();
var startedChallengeTeamsCount = distinctTeamIds.Length - rawResults.Where(p => p.ChallengesStarted > 0).Select(r => r.Player.TeamId).Distinct().Count();

return new EnrollmentReportStatSummary
{
DistinctGameCount = rawResults.Select(r => r.Player.GameId).Distinct().Count(),
DistinctPlayerCount = rawResults.Select(r => r.Player.UserId).Distinct().Count(),
DistinctSponsorCount = rawResults.Select(r => r.Player.SponsorId).Distinct().Count(),
DistinctTeamCount = rawResults.Select(r => r.Player.TeamId).Distinct().Count(),
DistinctTeamCount = distinctTeamIds.Length,
SponsorWithMostPlayers = sponsorWithMostPlayers,
PlayersWithNoSessionCount = noSessionPlayers.Select(r => r.Player.UserId).Distinct().Count(),
PlayersWithNoStartedChallengeCount = noChallengeStartedPlayers.Select(r => r.Player.UserId).Distinct().Count(),
TeamsWithNoSessionCount = noSessionPlayers.Select(r => r.Player.TeamId).Distinct().Count(),
TeamsWithNoStartedChallengeCount = noChallengeStartedPlayers.Select(r => r.Player.TeamId).Distinct().Count()
TeamsWithNoSessionCount = distinctTeamIds.Length - teamsWithSession,
TeamsWithNoStartedChallengeCount = distinctTeamIds.Length - startedChallengeTeamsCount
};
}

Expand Down

0 comments on commit 9848b05

Please sign in to comment.