public async Task GenerateForServer(IMessageChannel replyChannel, string serverId, DateTime start, bool grantWinnerRole, params ICategory[] categories) { _logger.LogInformation("Executing leaderboard command"); if (grantWinnerRole) { await _roleService.RemoveRoleFromAllInServer(serverId, Constants.LeaderboardWinnerRoleName); } var participantsWithActivities = new List <ParticipantWithActivities>(); var participants = _participantService.GetAllParticipantsForServerAsync(serverId); foreach (var participant in participants) { var(policy, context) = _stravaAuthenticationService.GetUnauthorizedPolicy(participant.StravaId); try { var activities = await policy.ExecuteAsync(x => _activitiesService.GetForStravaUser(participant.StravaId, start), context); participantsWithActivities.Add(new ParticipantWithActivities { Participant = participant, Activities = activities }); } catch (Exception e) { _logger.LogWarning(e, $"Failed to fetch activities for {participant.DiscordUserId}, excluding participant from leaderboard"); } } var categoryResults = new List <CategoryResult>(); foreach (var category in categories) { var categoryResult = GetTopResultsForCategory(participantsWithActivities, category); await replyChannel.SendMessageAsync(embed : _embedBuilderService .BuildLeaderboardEmbed( categoryResult, start, DateTime.Now ) ); categoryResults.Add(categoryResult); } if (grantWinnerRole) { var winners = new List <ParticipantResult>(); foreach (var subCategoryResult in categoryResults.SelectMany(categoryResult => categoryResult.SubCategoryResults)) { winners.AddRange(subCategoryResult.OrderedParticipantResults.Take(3)); } await GrantWinnerRoles(serverId, winners); } }
public async Task ShowParticipantStats() { using (Context.Channel.EnterTypingState()) { try { var participant = _participantService.GetParticipantOrDefault(Context.Guild.Id.ToString(), Context.User.Id.ToString()); if (participant == null) { await ReplyAsync("It seems like you're not part of the leaderboard. Try joining it."); return; } var start = DateTime.Now.AddDays(-7); var(policy, context) = _stravaAuthenticationService.GetUnauthorizedPolicy(participant.StravaId); var activities = await policy.ExecuteAsync(x => _activityService.GetForStravaUser(participant.StravaId, start), context); var participantWithActivities = new ParticipantWithActivities { Participant = participant, Activities = activities }; var realRideCategoryResult = _leaderboardService.GetTopResultsForCategory( new List <ParticipantWithActivities> { participantWithActivities }, new RealRideCategory()); await ReplyAsync(embed : _embedBuilderService.BuildParticipantStatsForCategoryEmbed(realRideCategoryResult, $"'{new RealRideCategory().Name}' leaderboard for '{start:yyyy MMMM dd} - {DateTime.Now:yyyy MMMM dd}'")); var virtualRideCategoryResult = _leaderboardService.GetTopResultsForCategory( new List <ParticipantWithActivities> { participantWithActivities }, new VirtualRideCategory()); await ReplyAsync(embed : _embedBuilderService.BuildParticipantStatsForCategoryEmbed(virtualRideCategoryResult, $"'{new VirtualRideCategory().Name}' leaderboard for '{start:yyyy MMMM dd} - {DateTime.Now:yyyy MMMM dd}'")); } catch (Exception e) { _logger.LogError(e, "stats failed"); } } }