Exemplo n.º 1
0
        private async Task <ICommandAnswer> GetRebalanceProfile(Message msg)
        {
            if (!string.IsNullOrEmpty(msg.Text))
            {
                await using var db = new DatabaseOsu();

                string player;
                if (msg.Text.Length > 11)
                {
                    player = ProfileLinkParser.Parse(msg.Text)?.Id;
                }
                else
                {
                    player = (await db.Players.FirstOrDefaultAsync(x => x.TelegramID == msg.From.Id))?.ToString();
                }

                if (!string.IsNullOrEmpty(player))
                {
                    var result = await FormatRebalanceProfile(player);

                    if (!string.IsNullOrEmpty(result))
                    {
                        return(new TextCommandAnswer(result));
                    }
                }
            }

            return(Localization.GetAnswer("generic_badrequest", msg.Chat.Id));
        }
Exemplo n.º 2
0
        private async Task <ICommandAnswer> AddMe(Telegram.Bot.Types.Message message)
        {
            if (message.Text.Length > 6)
            {
                await using var db = new DatabaseOsu();

                string player = ProfileLinkParser.Parse(message.Text)?.Id;
                if (string.IsNullOrEmpty(player))
                {
                    player = message.Text.Substring(7);
                }

                if (!string.IsNullOrEmpty(player))
                {
                    if (db.Players.Any(x => x.TelegramID == message.From.Id))
                    {
                        return(Localization.GetAnswer($"annoy_{RNG.NextNoMemory(1, 10)}", message.Chat.Id));
                    }

                    if (!uint.TryParse(player, out var osuID))
                    {
                        // if they used /u/cookiezi instead of /u/124493 we ask osu API for an ID
                        var info = await WebApiHandler.MakeApiRequest(new GetUser(player));

                        if (info == null)
                        {
                            return(Localization.GetAnswer("recentscores_player_add_failed", message.Chat.Id));
                        }
                        else
                        {
                            osuID = info.Id;
                        }
                    }

                    if (osuID != 0)
                    {
                        await db.Players.AddAsync(new DatabaseOsu.Player
                        {
                            OsuID      = osuID,
                            TelegramID = message.From.Id
                        });

                        await db.SaveChangesAsync();

                        return(Localization.GetAnswer("recentscores_player_add_success", message.Chat.Id));
                    }
                }
            }

            return(Localization.GetAnswer("recentscores_player_add_failed", message.Chat.Id));
        }
Exemplo n.º 3
0
        private async Task <ICommandAnswer> GetPass(Telegram.Bot.Types.Message message)
        {
            uint playerId = 0;

            List <string> msgSplit = message.Text.Split(' ').ToList();

            msgSplit.RemoveAt(0);

            if (msgSplit.Count > 0)
            {
                var playerName = string.Join(" ", msgSplit);
                var player     = await WebApiHandler.MakeApiRequest(new GetUser(playerName));

                if (player != null)
                {
                    playerId = player.Id;
                }
            }
            else
            {
                await using var db = new DatabaseOsu();
                var id = db.Players.FirstOrDefault(x => x.TelegramID == message.From.Id)?.OsuID;
                if (id == null || id == 0)
                {
                    return(Localization.GetAnswer("recentscores_unknown_player", message.Chat.Id));
                }

                playerId = id.Value;
            }

            if (playerId == 0)
            {
                return(Localization.GetAnswer("generic_fail", message.Chat.Id));
            }

            var lastScores = await WebApiHandler.MakeApiRequest(new GetUserScores(playerId, ScoreType.Recent, false));

            if (lastScores.Count > 0)
            {
                var score = lastScores[0];
                ChatBeatmapCache.StoreLastMap(message.Chat.Id, new ChatBeatmapCache.CachedBeatmap {
                    BeatmapId = score.BeatmapShort.Id, BeatmapSetId = score.BeatmapShort.BeatmapSetId
                });

                score.Beatmap = await WebApiHandler.MakeApiRequest(new GetBeatmap(score.BeatmapShort.Id));

                return(new TextCommandAnswer(FormatScore(score, true)));
            }

            return(Localization.GetAnswer("generic_fail", message.Chat.Id));
        }
Exemplo n.º 4
0
        private async Task <ICommandAnswer> RemoveMe(Telegram.Bot.Types.Message message)
        {
            await using (var db = new DatabaseOsu())
            {
                var player = db.Players.FirstOrDefault(x => x.TelegramID == message.From.Id);
                if (player != null)
                {
                    db.Players.Remove(player);
                    await db.SaveChangesAsync();

                    return(Localization.GetAnswer("recentscores_player_remove_success", message.Chat.Id));
                }

                return(Localization.GetAnswer("generic_fail", message.Chat.Id));
            }
        }
Exemplo n.º 5
0
        private async Task <ICommandAnswer> GetProfile(Message msg)
        {
            if (!string.IsNullOrEmpty(msg.Text))
            {
                await using var dbOsu = new DatabaseOsu();

                var userId = msg.From.Id;

                if (msg.ReplyToMessage != null)
                {
                    userId = msg.ReplyToMessage.From.Id;
                }

                // username in profile has priority over replies
                if (msg.Text.Length > 9)
                {
                    var username = msg.Text.Substring(9).Trim();
                    if (username.StartsWith('@'))
                    {
                        username = username[1..];
Exemplo n.º 6
0
        private async Task <ICommandAnswer> GetLastScores(Telegram.Bot.Types.Message message)
        {
            uint playerId = 0;
            int  amount   = 1;

            List <string> msgSplit = message.Text.Split(' ').ToList();

            msgSplit.RemoveAt(0);

            if (msgSplit.Count > 0 && int.TryParse(msgSplit.Last(), out amount))
            {
                msgSplit.Remove(msgSplit.Last());
            }

            if (amount > recent_amount)
            {
                amount = recent_amount;
            }
            else if (amount <= 0)
            {
                amount = 1;
            }

            if (msgSplit.Count > 0)
            {
                var playerName = string.Join(" ", msgSplit);
                var player     = await WebApiHandler.MakeApiRequest(new GetUser(playerName));

                if (player != null)
                {
                    playerId = player.Id;
                }
            }
            else
            {
                await using var db = new DatabaseOsu();
                var id = db.Players.FirstOrDefault(x => x.TelegramID == message.From.Id)?.OsuID;
                if (id == null || id == 0)
                {
                    return(Localization.GetAnswer("recentscores_unknown_player", message.Chat.Id));
                }

                playerId = id.Value;
            }

            if (playerId == 0)
            {
                return(Localization.GetAnswer("generic_fail", message.Chat.Id));
            }

            List <Score> lastScores = await WebApiHandler.MakeApiRequest(new GetUserScores(playerId, ScoreType.Recent, true));

            if (lastScores != null)
            {
                if (lastScores.Count == 0)
                {
                    return(Localization.GetAnswer("recentscores_no_scores", message.Chat.Id));
                }

                string result = string.Empty;
                foreach (var score in lastScores.Take(amount))
                {
                    if (amount == 1)
                    {
                        ChatBeatmapCache.StoreLastMap(message.Chat.Id, new ChatBeatmapCache.CachedBeatmap {
                            BeatmapId = score.BeatmapShort.Id, BeatmapSetId = score.BeatmapShort.BeatmapSetId
                        });
                    }

                    score.Beatmap = await WebApiHandler.MakeApiRequest(new GetBeatmap(score.BeatmapShort.Id));

                    result += FormatScore(score, true);
                }

                return(new TextCommandAnswer(result));
            }

            return(Localization.GetAnswer("generic_fail", message.Chat.Id));
        }
Exemplo n.º 7
0
        private async Task <ICommandAnswer> GetMapScores(Telegram.Bot.Types.Message message)
        {
            uint mapId = 0;

            // beatmap id regex can parse link as part of a complex message so we dont need to clean it up beforehand
            var msgText = message.Text;

            if (message.ReplyToMessage != null)
            {
                var sentMap = ChatBeatmapCache.GetSentMap(message.ReplyToMessage.MessageId);
                if (sentMap?.BeatmapId is not null)
                {
                    mapId = sentMap.BeatmapId;
                }
                else if (message.ReplyToMessage.Text.Contains(".ppy.sh"))
                {
                    msgText = message.ReplyToMessage.Text;
                }
            }

            var msgSplit = msgText.Split(' ').ToList();
            var mods     = LegacyMods.NM;

            if (msgSplit.Count > 1)
            {
                var data = BeatmapLinkParser.Parse(msgText);
                if (data != null)
                {
                    mapId = data.ID;
                    mods  = data.Mods;
                    if (data.IsBeatmapset)
                    {
                        BeatmapSet set = await WebApiHandler.MakeApiRequest(new GetBeatmapSet(data.ID));

                        if (set?.Beatmaps?.Count > 0)
                        {
                            mapId = set.Beatmaps.OrderBy(x => x.StarRating).Last().Id;
                        }
                    }
                }
            }
            else if (mapId == 0)
            {
                mapId = ChatBeatmapCache.GetLastMap(message.Chat.Id)?.BeatmapId ?? 0;
            }

            if (mapId == 0)
            {
                return(Localization.GetAnswer("generic_fail", message.Chat.Id));
            }

            await using var db = new DatabaseOsu();

            var playerId = db.Players.FirstOrDefault(x => x.TelegramID == message.From.Id)?.OsuID;

            if (playerId == null || playerId == 0)
            {
                return(Localization.GetAnswer("recentscores_unknown_player", message.Chat.Id));
            }

            var result = string.Empty;

            if (mods == 0)
            {
                // no mods specified - use apiv1 to get all scores on a map and then get score data from apiv2
                var scores = await WebApiHandler.MakeApiRequest(new WebAPI.Requests.V1.GetScores(playerId.Value.ToString(), mapId, mods, score_amount));

                if (scores == null || scores.Count <= 0)
                {
                    return(Localization.GetAnswer("recentscores_no_scores", message.Chat.Id));
                }

                var map = await WebApiHandler.MakeApiRequest(new GetBeatmap(mapId));

                foreach (var v1Score in scores)
                {
                    var score = await WebApiHandler.MakeApiRequest(new GetUserBeatmapScore(mapId, playerId.Value, v1Score.LegacyMods));

                    if (score != null)
                    {
                        score.Beatmap = map;
                        result       += FormatScore(score, false);
                    }
                }
            }
            else
            {
                // mods specified - get data straight from apiv2
                var score = await WebApiHandler.MakeApiRequest(new GetUserBeatmapScore(mapId, playerId.Value, mods));

                if (score == null)
                {
                    return(Localization.GetAnswer("recentscores_no_scores", message.Chat.Id));
                }

                score.Beatmap = await WebApiHandler.MakeApiRequest(new GetBeatmap(mapId));

                result += FormatScore(score, false);
            }

            if (!string.IsNullOrEmpty(result))
            {
                ChatBeatmapCache.StoreLastMap(message.Chat.Id, new ChatBeatmapCache.CachedBeatmap {
                    BeatmapId = mapId
                });
                return(new TextCommandAnswer(result));
            }

            return(Localization.GetAnswer("generic_fail", message.Chat.Id));
        }