Пример #1
0
        public async Task Ranks(int count, int position)
        {
            const int elementsPerList = 20;

            using (var uow = _db.UnitOfWork)
            {
                var levelModels = uow.LevelModel.GetAll().Where(p => p.TotalXP != 0 && p.GuildId == Context.Guild.Id)
                                  .OrderByDescending(p => p.TotalXP).Skip(position - 1 <= 0 ? 0 : position - 1).Take(count).ToList();

                if (!levelModels.Any())
                {
                    return;
                }

                var groupedLevelModels =
                    levelModels.GroupBy(lm => (int)Math.Floor(levelModels.IndexOf(lm) * 1d / elementsPerList));
                var rankStrings = new List <string>();
                var sb          = new StringBuilder();
                sb.AppendLine(GetText("ranks_header"));
                foreach (var glm in groupedLevelModels)
                {
                    var listNumber = glm.Key + 1;
                    if (!glm.Any())
                    {
                        continue;
                    }
                    sb.Append($"```{GetText("ranks_list_header", listNumber)}");
                    foreach (var lm in glm)
                    {
                        var user = await Context.Guild.GetUserAsync(lm.UserId).ConfigureAwait(false);

                        var level     = uow.LevelModel.GetLevel(lm.GuildId, lm.UserId);
                        var currentXp = uow.LevelModel.GetCurrentXp(lm.GuildId, lm.UserId);
                        sb.Append("\n" + GetText("ranks_list_row", $"{position + levelModels.IndexOf(lm),3}",
                                                 $"{user?.ToString() ?? lm.UserId.ToString(),-37}", $"{level,3}", $"{currentXp,6}",
                                                 $"{LevelModelRepository.GetXpToNextLevel(level),6}", $"{lm.TotalXP,8}"));
                    }

                    sb.Append("```");
                    rankStrings.Add(sb.ToString());
                    sb.Clear();
                }

                var channel = count <= 20
                    ? Context.Channel
                    : await Context.User.GetOrCreateDMChannelAsync().ConfigureAwait(false);

                foreach (var s in rankStrings)
                {
                    await channel.SendMessageAsync(s).ConfigureAwait(false);

                    Thread.Sleep(250);
                }
            }
        }
Пример #2
0
        public async Task Rank(ulong userId = 0)
        {
            userId = userId != 0 ? userId : Context.User.Id;

            int totalRanks, rank, totalXp, level, currentXp;

            using (var uow = _db.UnitOfWork)
            {
                var lm = uow.LevelModel.Get(Context.Guild.Id, userId);
                totalXp    = uow.LevelModel.GetTotalXp(Context.Guild.Id, userId);
                level      = uow.LevelModel.GetLevel(Context.Guild.Id, userId);
                currentXp  = uow.LevelModel.GetCurrentXp(Context.Guild.Id, userId);
                totalRanks = uow.LevelModel.GetAll().Count(m => m.TotalXP > 0 && m.GuildId == Context.Guild.Id);
                rank       = lm == null
                    ? -1
                    : uow.LevelModel.GetAll().Where(p => p.GuildId == Context.Guild.Id)
                             .OrderByDescending(p => p.TotalXP).ToList().IndexOf(lm) + 1;
                await uow.CompleteAsync().ConfigureAwait(false);
            }

            if (userId == Context.User.Id)
            {
                await Context.Channel.SendMessageAsync(GetText("rank_self", Context.User.Mention, level, currentXp,
                                                               LevelModelRepository.GetXpToNextLevel(level), totalXp, totalXp > 0 ? rank.ToString() : "-",
                                                               totalRanks)).ConfigureAwait(false);
            }
            else
            {
                var user = await Context.Guild.GetUserAsync(userId).ConfigureAwait(false);

                var namestring = user?.Nickname ?? (user?.Username ?? userId.ToString());
                await Context.Channel.SendMessageAsync(GetText("rank_other", Context.User.Mention, namestring, level,
                                                               currentXp, LevelModelRepository.GetXpToNextLevel(level), totalXp,
                                                               totalXp > 0 ? rank.ToString() : "-", totalRanks)).ConfigureAwait(false);
            }
        }