public async Task <ActionResult <IEnumerable <Models.HotOpScoreboard> > > GetHotOpScoreboard(ulong channelId) { try { var utcNow = DateTime.UtcNow; var channel = await Context.Channels .AsNoTracking() .Include(c => c.DiscordUserChannels) .ThenInclude(duc => duc.DiscordUser) .SingleOrDefaultAsync(c => c.ChannelId == channelId); var activeHotOps = await Context.HotOps .AsNoTracking() .Include(ho => ho.Owner) .ThenInclude(du => du.DiscordUserChannels) .Include(ho => ho.Sessions) .ThenInclude(hos => hos.DiscordUser) .ThenInclude(du => du.DiscordUserChannels) .Where(ho => ho.StartDateTimeUtc <= utcNow && ho.EndDateTimeUtc > utcNow) .ToListAsync(); var scoreboards = new List <Models.HotOpScoreboard>(); foreach (var hotOp in activeHotOps) { if (!hotOp.Owner.DiscordUserChannels.Any(duc => duc.ChannelId == channelId)) { continue; } var scores = new List <Models.HotOpScore>(); var minuteMultiplier = 10; foreach (var discordUserChannel in channel.DiscordUserChannels.Where(duc => duc.DiscordUserId != hotOp.OwnerId)) { var score = new Models.HotOpScore { DiscordUserId = discordUserChannel.DiscordUserId, DiscordUserName = discordUserChannel.DiscordUser.Username, Score = 0 }; foreach (var userSession in hotOp.Sessions.Where(s => s.DiscordUserId == discordUserChannel.DiscordUserId)) { if (userSession.EndDateTimeUtc.HasValue) { score.Score += (int)(Math.Round((userSession.EndDateTimeUtc.Value - userSession.StartDateTimeUtc).TotalMinutes, 0) * minuteMultiplier); } else { score.Score += (int)(Math.Round((utcNow - userSession.StartDateTimeUtc).TotalMinutes, 0) * minuteMultiplier); } } scores.Add(score); } var scoreboard = new Models.HotOpScoreboard { HotOpOwnerId = hotOp.OwnerId, HotOpOwnerName = hotOp.Owner.Username, Scores = scores }; scoreboards.Add(scoreboard); } return(Ok(scoreboards)); } catch (Exception ex) { return(InternalServerError($"Failed to get scoreboards for channel {channelId}", ex)); } }
public async Task <ActionResult <Models.HotOpScoreboard> > GetScoreboard(int id, [FromQuery] ulong?channelId = null) { try { var hotOp = await Context.HotOps .AsNoTracking() .Include(ho => ho.Owner) .Include(ho => ho.Sessions) .ThenInclude(hos => hos.DiscordUser) .ThenInclude(du => du.DiscordUserChannels) .SingleOrDefaultAsync(ho => ho.Id == id); if (hotOp == null) { return(NotFound($"Hot op {id} not found")); } var scores = new Dictionary <ulong, Models.HotOpScore>(); var minuteMultiplier = 10; var utcNow = DateTime.UtcNow; foreach (var session in hotOp.Sessions) { if (channelId.HasValue && !session.DiscordUser.DiscordUserChannels.Any(duc => duc.ChannelId == channelId.Value)) { continue; } if (!scores.ContainsKey(session.DiscordUserId)) { scores.Add(session.DiscordUserId, new Models.HotOpScore { DiscordUserId = session.DiscordUserId, DiscordUserName = session.DiscordUser.Username, Score = 0 }); } if (session.EndDateTimeUtc.HasValue) { scores[session.DiscordUserId].Score += (int)(Math.Round((session.EndDateTimeUtc.Value - session.StartDateTimeUtc).TotalMinutes, 0) * minuteMultiplier); } else { scores[session.DiscordUserId].Score += (int)(Math.Round((utcNow - session.StartDateTimeUtc).TotalMinutes, 0) * minuteMultiplier); } } var scoreboard = new Models.HotOpScoreboard { HotOpOwnerId = hotOp.OwnerId, HotOpOwnerName = hotOp.Owner.Username, Scores = scores.Values }; return(Ok(scoreboard)); } catch (Exception ex) { return(InternalServerError($"Failed to get hot op {id}", ex)); } }