示例#1
0
        public async Task <IActionResult> Calculate(CalculateMapModel model)
        {
            if (string.IsNullOrEmpty(model.Map))
            {
                return(StatusCode(400, new { err = "Incorrect beatmap link!" }));
            }

            SentrySdk.ConfigureScope(scope => { scope.Contexts["Map"] = model; });

            if (!uint.TryParse(model.Map, out var mapId))
            {
                var parsedLink = BeatmapLinkParser.Parse(model.Map);
                if (parsedLink == null)
                {
                    return(StatusCode(400, new { err = "Incorrect beatmap link!" }));
                }

                if (parsedLink.IsBeatmapset)
                {
                    return(StatusCode(400, new { err = "Beatmap set links aren't supported" }));
                }

                mapId = parsedLink.Id;
            }

            var mapInfo = await calculationService.Calculate(mapId, model.Mods);

            if (!string.IsNullOrEmpty(mapInfo))
            {
                return(Ok(mapInfo));
            }

            return(StatusCode(500, new { err = "Failed to calculate!" }));
        }
示例#2
0
        #pragma warning disable IDE1006 //Disable warnings for naming styles

        public async Task osuLinkParser(SocketMessage s)
        {
            if (s != null)
            {
                BeatmapLinkParser parser = new BeatmapLinkParser();
                EmbedBuilder      embed  = new EmbedBuilder();
                if (s is SocketUserMessage msg)
                {
                    var context = new SocketCommandContext(_client, msg);
                    if (s.Content.Contains("https://osu.ppy.sh/beatmapsets/"))
                    {
                        await parser.LinkParserMethod(s, embed, context);
                    }
                    else if (s.Content.Contains("https://osu.ppy.sh/b/"))
                    {
                        await parser.LinkParserMethod(s, embed, context);
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
        }
示例#3
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));
        }