Exemplo n.º 1
0
        public async Task ShowParticipantStats()
        {
            using (Context.Channel.EnterTypingState())
            {
                try
                {
                    var participant =
                        await _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.");
                    }

                    var start      = DateTime.Now.AddDays(-7);
                    var activities = await _stravaService.FetchActivitiesForParticipant(participant, start);

                    await ReplyAsync(embed : _embedBuilderService.BuildParticipantStatsForCategoryEmbed(participant,
                                                                                                        activities, Constants.LeaderboardRideType.RealRide, start, DateTime.Now));

                    await ReplyAsync(embed : _embedBuilderService.BuildParticipantStatsForCategoryEmbed(participant,
                                                                                                        activities, Constants.LeaderboardRideType.VirtualRide, start, DateTime.Now));
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "stats failed");
                }
            }
        }
Exemplo n.º 2
0
        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 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}'");
                    }
                }
            }
        }