Ejemplo n.º 1
0
        public async Task GetBeatmap(string ID, string mode = null, string action = null)
        {
            SocketUserMessage msg = await Context.Channel.SendMessageAsync("Fetching data...", attachID : true);

            OsuGameModes gameMode = (string.IsNullOrEmpty(mode) ? OsuGameModes.None : OsuGameModesConverter.FromOfficialNumeration(byte.Parse(mode)));

            OsuBeatmap beatmap = await OsuNetwork.DownloadBeatmap(int.Parse(ID), gameMode, msg);

            gameMode = beatmap.GameMode;

            OsuUser creator = await OsuNetwork.DownloadUser(beatmap.Creator, OsuGameModes.STD, tolerateNull : true, maxAttempts : 3);

            Task <OsuScore[]> bestPlayDownloader = OsuNetwork.DownloadBeatmapBest(beatmap, gameMode, scoreCount: 3, logger: msg, tolerateNull: true, maxAttempts: 2);

            OsuBoundUserDB bound = await OsuDB.GetBoundUserBy_DiscordID(Context.Message.Author.ID);

            Task <OsuScore[]> boundBestScoreDownloader = OsuNetwork.DownloadBeatmapBest(beatmap, gameMode, user: bound.UserID, logger: msg, tolerateNull: true, maxAttempts: 3);

            EmbedBuilder eb = beatmap.ToEmbedBuilder(gameMode, creator);

            OsuScore[] bestPlays = await bestPlayDownloader;

            await msg.EditAsync("Fetching best plays...", null);

            if (bestPlays[0] != null)
            {
                EmbedFieldBuilder rankingsField = new EmbedFieldBuilder
                {
                    Name  = $"{CustomEmoji.Osu.Gamemode.GetGamemodeEmoji(beatmap.GameMode)}",
                    Value = "report to LtLi0n"
                };

                OsuUser[] bestPlayUsers = new OsuUser[bestPlays.Length];

                int longestName = int.MinValue;

                for (int i = 0; i < bestPlays.Length; i++)
                {
                    bestPlayUsers[i] = await OsuNetwork.DownloadUser(bestPlays[i].Username, beatmap.GameMode, logger : msg, maxAttempts : 2);

                    if (bestPlayUsers[i].Username.Length > longestName)
                    {
                        longestName = bestPlayUsers[i].Username.Length;
                    }
                }

                for (int i = 0; i < bestPlayUsers.Length; i++)
                {
                    string toAdd = bestPlays[i].ToScoreString(bestPlayUsers[i].Country, gameMode, i + 1, nameFormat: '\u200b');

                    if (i == 0)
                    {
                        rankingsField.Value = toAdd;
                    }
                    else
                    {
                        rankingsField.Value += toAdd;
                    }
                }

                rankingsField.IsInline = true;

                eb.AddField(rankingsField);

                if (bound != null)
                {
                    try
                    {
                        OsuScore boundBestScore = (await boundBestScoreDownloader)[0];

                        if (boundBestScore != null)
                        {
                            eb.AddField(x =>
                            {
                                x.Name     = $"Your best";
                                x.Value    = boundBestScore.ToScoreString(bound.Country, gameMode, includeReplay: boundBestScore.HasReplay, nameFormat: '\u200b');
                                x.IsInline = true;
                            });
                        }
                    }
                    catch (Exception e) { }
                }
            }
            else
            {
                string[] lines_f1 = eb.Fields[0].Value.ToString().Split('\n');
                lines_f1[0] += "\t\t\u200b";

                string convertBack = "";
                Tool.ForEach(lines_f1, x => convertBack += (x + '\n'));

                eb.Fields[0].Value = convertBack;

                eb.Fields[1].Value = '\u200b';
            }

            string additional = string.Empty;

            if (action != null)
            {
                additional = action.ToLower() == "json" ? $"```json\n{JValue.Parse(JsonConvert.SerializeObject(beatmap)).ToString(Formatting.Indented)}```" : string.Empty;
            }

            await msg.EditAsync((additional), embed : eb.Build());
        }
Ejemplo n.º 2
0
        public async Task GetRecent([Remainder] string target = null)
        {
            SocketUserMessage msg = await Context.Channel.SendMessageAsync("Fetching data...", attachID : true);

            ulong?targetID = null;

            string username = string.Empty;
            string country  = string.Empty;

            OsuGameModes gameMode = OsuGameModes.STD;

            if (string.IsNullOrEmpty(target))
            {
                OsuBoundUserDB bound = await OsuDB.GetBoundUserBy_DiscordID(Context.Message.Author.ID);

                if (bound != null)
                {
                    targetID = bound.UserID;
                    country  = bound.Country;
                    username = bound.UserName;
                    gameMode = bound.MainMode;
                }
                else
                {
                    await msg.EditAsync($"You were not found in the database.\n" +
                                        $"To use this command without parameters, proceed to bind your profile with `$osubind [username]`", null);
                }
            }
            else
            {
                if (target.Where(x => x < '0' || x > '9').Count() > 0)
                {
                    //Not ID
                }
                else
                {
                    targetID = ulong.Parse(target);
                }
            }

            OsuUserRecent recent = await OsuNetwork.DownloadUserRecent(targetID.Value, gameMode);

            OsuBeatmap beatmap = await OsuNetwork.DownloadBeatmap(recent.BeatmapID, gameMode);

            if (beatmap == null)
            {
                await msg.EditAsync("", null);
            }
            else
            {
                OsuUser beatmapCreator = await OsuNetwork.DownloadUser(beatmap.Creator, gameMode);

                EmbedBuilder eb = beatmap.ToEmbedBuilder(gameMode, beatmapCreator, true);

                eb.Fields.Insert(0, new EmbedFieldBuilder
                {
                    Name  = $"Recent {CustomEmoji.Osu.Gamemode.GetGamemodeEmoji(gameMode)}",
                    Value = recent.ToScoreString(country, gameMode, username, nameFormat: '\u200b')
                });

                await msg.EditAsync("", eb.Build());
            }
        }