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);
            }
        }
Пример #2
0
        public async Task ListLeaderboardParticipants()
        {
            using (Context.Channel.EnterTypingState())
            {
                try
                {
                    _logger.LogInformation("Executing list");
                    var embeds       = new List <Embed>();
                    var participants = _participantService.GetAllParticipantsForServerAsync(Context.Guild.Id.ToString());
                    foreach (var participant in participants)
                    {
                        var(policy, context) = _stravaAuthenticationService.GetUnauthorizedPolicy(participant.StravaId);
                        try
                        {
                            var athlete = await policy.ExecuteAsync(x => _athleteService.Get(participant.StravaId), context);

                            embeds.Add(_embedBuilderService.BuildAthleteInfoEmbed(participant, athlete));
                        }
                        catch (ApiException e)
                        {
                            _logger.LogWarning(e, $"Failed to fetch athlete info for {participant.DiscordUserId}");
                        }
                    }

                    if (!embeds.Any())
                    {
                        embeds.Add(
                            new EmbedBuilder()
                            .WithTitle("No participants found")
                            .WithCurrentTimestamp()
                            .Build()
                            );
                    }

                    foreach (var embed in embeds)
                    {
                        await ReplyAsync(embed : embed);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "list failed");
                }
            }
        }
Пример #3
0
        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");
                }
            }
        }