2019-01-24 16:43:03 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
|
using osu.Game.Screens.Select.Leaderboards;
|
2018-06-08 10:41:54 +08:00
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2019-07-03 19:34:20 +08:00
|
|
|
|
using osu.Game.Rulesets.Mods;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Collections.Generic;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online.API.Requests
|
|
|
|
|
{
|
2019-03-27 16:08:40 +08:00
|
|
|
|
public class GetScoresRequest : APIRequest<APILegacyScores>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly BeatmapInfo beatmap;
|
2018-12-14 18:51:27 +08:00
|
|
|
|
private readonly BeatmapLeaderboardScope scope;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
private readonly RulesetInfo ruleset;
|
2019-07-03 19:34:20 +08:00
|
|
|
|
private readonly IEnumerable<Mod> mods;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-07-03 19:34:20 +08:00
|
|
|
|
public GetScoresRequest(BeatmapInfo beatmap, RulesetInfo ruleset, BeatmapLeaderboardScope scope = BeatmapLeaderboardScope.Global, IEnumerable<Mod> mods = null)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
if (!beatmap.OnlineBeatmapID.HasValue)
|
|
|
|
|
throw new InvalidOperationException($"Cannot lookup a beatmap's scores without having a populated {nameof(BeatmapInfo.OnlineBeatmapID)}.");
|
|
|
|
|
|
2018-12-14 18:51:27 +08:00
|
|
|
|
if (scope == BeatmapLeaderboardScope.Local)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
throw new InvalidOperationException("Should not attempt to request online scores for a local scoped leaderboard");
|
|
|
|
|
|
|
|
|
|
this.beatmap = beatmap;
|
|
|
|
|
this.scope = scope;
|
|
|
|
|
this.ruleset = ruleset ?? throw new ArgumentNullException(nameof(ruleset));
|
2019-07-03 19:34:20 +08:00
|
|
|
|
this.mods = mods ?? Array.Empty<Mod>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
Success += onSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 16:08:40 +08:00
|
|
|
|
private void onSuccess(APILegacyScores r)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-03-27 16:08:40 +08:00
|
|
|
|
foreach (APILegacyScoreInfo score in r.Scores)
|
2019-07-03 16:55:59 +08:00
|
|
|
|
{
|
2018-11-30 15:11:09 +08:00
|
|
|
|
score.Beatmap = beatmap;
|
2019-07-03 16:55:59 +08:00
|
|
|
|
score.Ruleset = ruleset;
|
|
|
|
|
}
|
2019-07-08 16:25:25 +08:00
|
|
|
|
|
|
|
|
|
var userScore = r.UserScore;
|
|
|
|
|
|
|
|
|
|
if (userScore != null)
|
|
|
|
|
{
|
|
|
|
|
userScore.Score.Beatmap = beatmap;
|
|
|
|
|
userScore.Score.Ruleset = ruleset;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 19:34:20 +08:00
|
|
|
|
protected override string Target => $@"beatmaps/{beatmap.OnlineBeatmapID}/scores{createQueryParameters()}";
|
|
|
|
|
|
|
|
|
|
private string createQueryParameters()
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-07-03 19:34:20 +08:00
|
|
|
|
StringBuilder query = new StringBuilder(@"?");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-07-05 11:16:17 +08:00
|
|
|
|
query.Append($@"type={scope.ToString().ToLowerInvariant()}");
|
|
|
|
|
query.Append($@"&mode={ruleset.ShortName}");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-07-03 19:34:20 +08:00
|
|
|
|
foreach (var mod in mods)
|
2019-07-05 11:16:17 +08:00
|
|
|
|
query.Append($@"&mods[]={mod.Acronym}");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-07-05 11:16:17 +08:00
|
|
|
|
return query.ToString();
|
2019-07-03 19:34:20 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|