public async Task ShowLeaderboard() { using (Context.Channel.EnterTypingState()) { try { _logger.LogInformation("Executing leaderboard command"); var start = DateTime.Now.AddDays(-7); var groupedActivitiesByParticipant = new Dictionary <LeaderboardParticipant, List <DetailedActivity> >(); var participants = await _participantService.GetAllParticipantsForServerAsync(Context.Guild.Id.ToString()); foreach (var participant in participants) { try { groupedActivitiesByParticipant.Add(participant, await _stravaService.FetchActivitiesForParticipant(participant, start)); } catch (StravaException e) when(e.Error == StravaException.StravaErrorType.RefreshFailed) { await AskToRelogin(participant.DiscordUserId); } } var realRideCategoryResult = _leaderboardResultService.GetTopResultsForCategory( groupedActivitiesByParticipant, Constants.LeaderboardRideType.RealRide, x => x.Type == Constants.LeaderboardRideType.RealRide); await ReplyAsync(embed : _embedBuilderService .BuildLeaderboardEmbed( realRideCategoryResult, start, DateTime.Now ) ); var virtualRideCategoryResult = _leaderboardResultService.GetTopResultsForCategory( groupedActivitiesByParticipant, Constants.LeaderboardRideType.VirtualRide, x => x.Type == Constants.LeaderboardRideType.VirtualRide); await ReplyAsync(embed : _embedBuilderService .BuildLeaderboardEmbed( virtualRideCategoryResult, start, DateTime.Now ) ); } catch (Exception e) { _logger.LogError(e, "leaderboard failed"); } } }
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 override async Task DoWork(CancellationToken cancellationToken) { _logger.LogInformation("Executing leaderboard hosted service"); foreach (var leaderboard in await _leaderboardService.GetAllLeaderboards()) { try { _logger.LogInformation("Removing role from all users"); await _roleService.RemoveRoleFromAllParticipantsInServer(leaderboard.ServerId, Constants.LeaderboardWinnerRoleName); } catch (Exception e) { _logger.LogError(e, $"Failed to remove leaderboard role from all users in '{leaderboard.ServerId}'"); } var start = DateTime.Now.AddDays(-7); var groupedActivitiesByParticipant = new Dictionary <LeaderboardParticipant, List <DetailedActivity> >(); var participants = await _participantService.GetAllParticipantsForServerAsync(leaderboard.ServerId); foreach (var participant in participants) { try { groupedActivitiesByParticipant.Add(participant, await _stravaService.FetchActivitiesForParticipant(participant, start)); } catch (StravaException e) when(e.Error == StravaException.StravaErrorType.RefreshFailed) { await AskToRelogin(participant.DiscordUserId); } } var channel = _discordSocketClient.GetChannel(ulong.Parse(leaderboard.ChannelId)) as SocketTextChannel; var realRideCategoryResult = _leaderboardResultService.GetTopResultsForCategory( groupedActivitiesByParticipant, Constants.LeaderboardRideType.RealRide, x => x.Type == Constants.LeaderboardRideType.RealRide); await channel.SendMessageAsync(embed : _embedBuilderService .BuildLeaderboardEmbed(realRideCategoryResult, start, DateTime.Now)); var virtualRideCategoryResult = _leaderboardResultService.GetTopResultsForCategory( groupedActivitiesByParticipant, Constants.LeaderboardRideType.VirtualRide, x => x.Type == Constants.LeaderboardRideType.VirtualRide); await channel.SendMessageAsync(embed : _embedBuilderService .BuildLeaderboardEmbed( virtualRideCategoryResult, start, DateTime.Now ) ); var allParticipantResults = realRideCategoryResult.ChallengeByChallengeResultDictionary.SelectMany(x => x.Value).Union( virtualRideCategoryResult .ChallengeByChallengeResultDictionary.SelectMany(x => x.Value)); foreach (var participantResult in allParticipantResults) { try { await _roleService.GrantUserRoleIfExists(leaderboard.ServerId, participantResult.Participant.DiscordUserId, Constants.LeaderboardWinnerRoleName); } catch (Exception e) { _logger.LogError(e, $"Failed to grant leaderboard role for '{participantResult.Participant.DiscordUserId}'"); } } } }