Esempio n. 1
0
        public async Task <GlobalLeaderboard> GetGlobalLeaderboard()
        {
            try
            {
                using (var soraContext = new SoraContext())
                {
                    var users  = soraContext.Users.ToList();
                    var sorted = users.OrderByDescending(x => x.Exp).ToList();
                    var resp   = new GlobalLeaderboard()
                    {
                        ShardId = _client.ShardId
                    };
                    for (int i = 0; i < (sorted.Count > 150 ? 150 : sorted.Count); i++)
                    {
                        var   guser = sorted[i];
                        IUser user  = _client.GetUser(guser.UserId) ?? await _restClient.GetUserAsync(guser.UserId) as IUser;

                        if (user == null)
                        {
                            continue;
                        }
                        resp.Ranks.Add(new GuildRank()
                        {
                            Rank      = i + 1,
                            AvatarUrl = user.GetAvatarUrl() ?? "https://i.imgur.com/PvYs6dc.png",
                            Discrim   = user.Discriminator,
                            Exp       = (int)guser.Exp,
                            Name      = user.Username,
                            NextExp   = ExpService.CalculateNeededExp(ExpService.CalculateLevel(guser.Exp) + 1),
                            UserId    = user.Id + ""
                        });
                        if (resp.Ranks.Count >= 100)
                        {
                            break;
                        }
                    }
                    return(resp);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(null);
        }
Esempio n. 2
0
        public async Task GenerateImage(
            [Summary("@User or leave blank to get your own")]
            DiscordGuildUser userT = null)
        {
            var user = userT?.GuildUser ?? (IGuildUser)Context.User;

            var userStatsM = await _profileRepo.GetProfileStatistics(user.Id, Context.Guild.Id).ConfigureAwait(false);

            if (!userStatsM.HasValue)
            {
                await ReplyFailureEmbed(
                    $"{Formatter.UsernameDiscrim(user)} is not in my Database :/ Make sure he used or chatted with Sora at least once.");

                return;
            }

            try
            {
                // First get his avatar
                await _hch.DownloadAndSaveFile(
                    new Uri(user.GetAvatarUrl() ?? user.GetDefaultAvatarUrl()),
                    Path.Combine(_imgGen.ImageGenPath, ImageGenerator.AVATAR_CACHE, $"{user.Id.ToString()}.png"))
                .ConfigureAwait(false);

                // Now generate image
                string filePath = Path.Combine(_imgGen.ImageGenPath, ImageGenerator.PROFILE_CARDS,
                                               $"{user.Id.ToString()}.png");

                var us          = ~userStatsM;
                var globalLevel = ExpService.CalculateLevel(us.GlobalExp);
                var localLevel  = ExpService.CalculateLevel(us.LocalExp);
                _imgGen.GenerateProfileImage(new ProfileImageGenDto()
                {
                    UserId             = user.Id,
                    Name               = user.Username,
                    GlobalExp          = us.GlobalExp,
                    GlobalLevel        = globalLevel,
                    GlobalRank         = us.GlobalRank,
                    GlobalNextLevelExp = ExpService.CalculateNeededExp(globalLevel + 1),
                    HasCustomBg        = us.HasCustomBg,
                    LocalExp           = us.LocalExp,
                    LocalRank          = us.LocalRank,
                    LocalLevel         = localLevel,
                    LocalNextLevelExp  = ExpService.CalculateNeededExp(localLevel + 1),
                    ClanName           = us.ClanName
                }, filePath);
                await Context.Channel.SendFileAsync(filePath);
            }
            catch (Exception e)
            {
                await ReplyFailureEmbedExtended(
                    "Failed to generate image. Something went wrong sorry :/",
                    "This could have multiple reasons. One of them could be that your username has characters that " +
                    "are currently not supported. This is any weird character that you wouldn't naturally find on your standard " +
                    "keyboard.");

                if (e.InnerException is NotImplementedException)
                {
                    return; // Since we dont care about exceptions about not supported text and shit. Can't do anything about that
                }
                _log.LogError(e, $"Failed to generate image for {user.Id.ToString()} ({user.Username})");
            }
            finally
            {
                // Remove avatar
                string avatar = Path.Combine(_imgGen.ImageGenPath, ImageGenerator.AVATAR_CACHE,
                                             $"{user.Id.ToString()}.png");
                if (File.Exists(avatar))
                {
                    File.Delete(avatar);
                }

                // Remove profile image
                string profileImg = Path.Combine(_imgGen.ImageGenPath, ImageGenerator.PROFILE_CARDS,
                                                 $"{user.Id.ToString()}.png");
                if (File.Exists(profileImg))
                {
                    File.Delete(profileImg);
                }
            }
        }
Esempio n. 3
0
        public async Task <GuildLeaderboard> GetGuildLeaderboard(ulong guildId)
        {
            try
            {
                using (var soraContext = new SoraContext())
                {
                    var guild = _client.GetGuild(guildId);
                    if (guild == null)
                    {
                        return(new GuildLeaderboard()
                        {
                            Success = false
                        });
                    }
                    var resp = new GuildLeaderboard()
                    {
                        Success   = true,
                        AvatarUrl = guild.IconUrl ?? "https://i.imgur.com/PvYs6dc.png",
                        GuildName = guild.Name
                    };
                    var levelRoles = soraContext.GuildLevelRoles.Where(x => x.GuildId == guildId).ToList();
                    var guildUsers = soraContext.GuildUsers.Where(x => x.GuildId == guildId).ToList();
                    var sorted     = guildUsers.OrderByDescending(x => x.Exp).ToList();
                    var sortedLvls = levelRoles.OrderBy(x => x.RequiredLevel).ToList();
                    for (int i = 0; i < sortedLvls.Count; i++)
                    {
                        var role = sortedLvls[i];
                        var r    = guild.GetRole(role.RoleId);
                        if (r == null || role.Banned)
                        {
                            continue;
                        }
                        resp.RoleRewards.Add(new RoleReward()
                        {
                            Name     = r.Name,
                            Color    = $"rgb({r.Color.R}, {r.Color.G}, {r.Color.B})",
                            LevelReq = role.RequiredLevel
                        });
                    }
                    int rank = 1;
                    for (int i = 0; i < sorted.Count; i++)
                    {
                        var user = sorted[i];
                        if (rank > 100)
                        {
                            break;
                        }
                        IUser u = _client.GetUser(user.UserId) ?? await _restClient.GetUserAsync(user.UserId) as IUser;

                        if (u == null)
                        {
                            continue;
                        }
                        resp.Ranks.Add(new GuildRank()
                        {
                            AvatarUrl = u.GetAvatarUrl() ?? "https://i.imgur.com/PvYs6dc.png",
                            Discrim   = u.Discriminator,
                            Exp       = (int)user.Exp,
                            Name      = u.Username,
                            NextExp   = ExpService.CalculateNeededExp(ExpService.CalculateLevel(user.Exp) + 1),
                            Rank      = rank
                        });
                        rank++;
                    }
                    return(resp);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(new GuildLeaderboard()
            {
                Success = false
            });
        }