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 Embed BuildParticipantStatsForCategoryEmbed(LeaderboardParticipant participant, List <DetailedActivity> activities, string type, DateTime start, DateTime end) { var categoryResult = _leaderboardResultService.GetTopResultsForCategory( new Dictionary <LeaderboardParticipant, List <DetailedActivity> > { { participant, activities } }, type, x => x.Type == type); var embedBuilder = new EmbedBuilder() .WithTitle( $"'{type}' stats for '{start:yyyy MMMM dd} - {end:yyyy MMMM dd}'") .WithCurrentTimestamp() .WithColor(Color.Gold); if (!categoryResult.ChallengeByChallengeResultDictionary.Any()) { embedBuilder.WithDescription("Something went wrong"); return(embedBuilder.Build()); } foreach (var(categoryName, participantResults) in categoryResult.ChallengeByChallengeResultDictionary) { foreach (var participantResult in participantResults) { embedBuilder.AddField(categoryName, OutputFormatters.ParticipantResultForChallenge(categoryName, participantResult.Value), true); } } return(embedBuilder.Build()); }
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}'"); } } } }