From a139441265fb0f35a749952b345ff77cf95cb861 Mon Sep 17 00:00:00 2001 From: Joseph Perrino <107595901+sei-jperrino@users.noreply.github.com> Date: Tue, 20 Sep 2022 08:53:25 -0400 Subject: [PATCH] Certificate and board report bugfixes (#54) * Player Count fixed, Team Count implemented - Certificates with Player Count now include only those who have begun and ended a session - Certificates can use a Team Count variable to show the number of teams who participated * Players will only see a certificate displayed among others if they completed a session within it * Sponsor counting fixed in board reports - Sorting by count also added --- .../Features/Player/PlayerService.cs | 26 ++++++++++++++++--- .../Features/Report/ReportService.cs | 9 ++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Gameboard.Api/Features/Player/PlayerService.cs b/src/Gameboard.Api/Features/Player/PlayerService.cs index d0bbf5d7..214fb4a5 100644 --- a/src/Gameboard.Api/Features/Player/PlayerService.cs +++ b/src/Gameboard.Api/Features/Player/PlayerService.cs @@ -682,10 +682,17 @@ public async Task MakeCertificate(string id) .FirstOrDefaultAsync(p => p.Id == id); var playerCount = await Store.DbSet - .Where(p => p.GameId == player.GameId) + .Where(p => p.GameId == player.GameId && + p.SessionEnd > DateTimeOffset.MinValue) .CountAsync(); - return CertificateFromTemplate(player, playerCount); + var teamCount = await Store.DbSet + .Where(p => p.GameId == player.GameId && + p.SessionEnd > DateTimeOffset.MinValue) + .GroupBy(p => p.TeamId) + .CountAsync(); + + return CertificateFromTemplate(player, playerCount, teamCount); } public async Task MakeCertificates(string uid) @@ -696,16 +703,26 @@ public async Task MakeCertificates(string uid) .Include(p => p.Game) .Include(p => p.User) .Where(p => p.UserId == uid && + p.SessionEnd > DateTimeOffset.MinValue && p.Game.GameEnd < now && p.Game.CertificateTemplate != null && p.Game.CertificateTemplate.Length > 0) .OrderByDescending(p => p.Game.GameEnd) .ToArrayAsync(); - return completedSessions.Select(c => CertificateFromTemplate(c, Store.DbSet.Where(pl => pl.Game == c.Game).Count())).ToArray(); + return completedSessions.Select(c => CertificateFromTemplate(c, + Store.DbSet + .Where(p => p.Game == c.Game && + p.SessionEnd > DateTimeOffset.MinValue) + .Count(), + Store.DbSet + .Where(p => p.Game == c.Game && + p.SessionEnd > DateTimeOffset.MinValue) + .GroupBy(p => p.TeamId).Count() + )).ToArray(); } - private Api.PlayerCertificate CertificateFromTemplate(Data.Player player, int playerCount) { + private Api.PlayerCertificate CertificateFromTemplate(Data.Player player, int playerCount, int teamCount) { string certificateHTML = player.Game.CertificateTemplate; if (certificateHTML.IsEmpty()) @@ -720,6 +737,7 @@ private Api.PlayerCertificate CertificateFromTemplate(Data.Player player, int pl certificateHTML = certificateHTML.Replace("{{track}}", player.Game.Track); certificateHTML = certificateHTML.Replace("{{date}}", player.SessionEnd.ToString("MMMM dd, yyyy")); certificateHTML = certificateHTML.Replace("{{player_count}}", playerCount.ToString()); + certificateHTML = certificateHTML.Replace("{{team_count}}", teamCount.ToString()); return new Api.PlayerCertificate { Game = Mapper.Map(player.Game), diff --git a/src/Gameboard.Api/Features/Report/ReportService.cs b/src/Gameboard.Api/Features/Report/ReportService.cs index 305d7702..486de54a 100644 --- a/src/Gameboard.Api/Features/Report/ReportService.cs +++ b/src/Gameboard.Api/Features/Report/ReportService.cs @@ -75,9 +75,10 @@ internal Task GetSponsorStats() { var sp = (from sponsors in Store.Sponsors join u in Store.Users on - sponsors.Logo equals u.Sponsor + sponsors.Logo equals u.Sponsor into j + from allSponsors in j.DefaultIfEmpty() select new { sponsors.Id, sponsors.Name, sponsors.Logo }).GroupBy(s => new { s.Id, s.Name, s.Logo }) - .Select(g => new SponsorStat { Id = g.Key.Id, Name = g.Key.Name, Logo = g.Key.Logo, Count = g.Count() }).OrderByDescending(g => g.Count).ThenBy(g => g.Name); + .Select(g => new SponsorStat { Id = g.Key.Id, Name = g.Key.Name, Logo = g.Key.Logo, Count = g.Count(gr => Store.Users.Any(u => u.Sponsor == gr.Logo) ) }).OrderByDescending(g => g.Count).ThenBy(g => g.Name); SponsorReport sponsorReport = new SponsorReport { @@ -97,7 +98,7 @@ internal Task GetGameSponsorsStats(string gameId) throw new ArgumentNullException("Invalid game id"); } - var game = Store.Games.Where(g => g.Id == gameId).Select(g => new { g.Id, g.Name }).FirstOrDefault(); + var game = Store.Games.Where(g => g.Id == gameId).Select(g => new { g.Id, g.Name, g.MaxTeamSize }).FirstOrDefault(); if (game == null) { @@ -128,6 +129,8 @@ internal Task GetGameSponsorsStats(string gameId) }); } + sponsorStats = sponsorStats.OrderByDescending(g => game.MaxTeamSize > 0 ? g.TeamCount : g.Count).ToList(); + // Create row for multisponsor teams sponsorStats.Add(new SponsorStat {