Skip to content

Commit

Permalink
Certificate and board report bugfixes (#54)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sei-jperrino authored Sep 20, 2022
1 parent b7ee413 commit a139441
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
26 changes: 22 additions & 4 deletions src/Gameboard.Api/Features/Player/PlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -682,10 +682,17 @@ public async Task<PlayerCertificate> 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<PlayerCertificate[]> MakeCertificates(string uid)
Expand All @@ -696,16 +703,26 @@ public async Task<PlayerCertificate[]> 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())
Expand All @@ -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<Game>(player.Game),
Expand Down
9 changes: 6 additions & 3 deletions src/Gameboard.Api/Features/Report/ReportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ internal Task<SponsorReport> 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
{
Expand All @@ -97,7 +98,7 @@ internal Task<GameSponsorReport> 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)
{
Expand Down Expand Up @@ -128,6 +129,8 @@ internal Task<GameSponsorReport> GetGameSponsorsStats(string gameId)
});
}

sponsorStats = sponsorStats.OrderByDescending(g => game.MaxTeamSize > 0 ? g.TeamCount : g.Count).ToList();

// Create row for multisponsor teams
sponsorStats.Add(new SponsorStat
{
Expand Down

0 comments on commit a139441

Please sign in to comment.