-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for computing performance of non-legacy scores #195
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea6ac6c
Add support for computing performance of non-legacy scores
smoogipoo f0de373
Merge branch 'master' into new-scores-command
smoogipoo ea86ddf
Actually add the subcommand
smoogipoo b2a9f49
Merge branch 'master' into new-scores-command
peppy 47b22dd
Link implementation to osu-queue-score-statistics
smoogipoo cb60951
Merge branch 'master' into new-scores-command
smoogipoo 408b127
Fix querying imported legacy scores having duplicate stats
smoogipoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Globalization; | ||
using System.Net.Http; | ||
|
@@ -33,11 +34,19 @@ public override void OnExecute(CommandLineApplication app, IConsole console) | |
base.OnExecute(app, console); | ||
} | ||
|
||
protected T GetJsonFromApi<T>(string request) | ||
protected T GetJsonFromApi<T>(string request, HttpMethod method = null, Dictionary<string, string> parameters = null) | ||
{ | ||
using var req = new JsonWebRequest<T>($"{Program.ENDPOINT_CONFIGURATION.APIEndpointUrl}/api/v2/{request}"); | ||
req.Method = method ?? HttpMethod.Get; | ||
req.AddHeader("x-api-version", api_version.ToString(CultureInfo.InvariantCulture)); | ||
req.AddHeader(System.Net.HttpRequestHeader.Authorization.ToString(), $"Bearer {apiAccessToken}"); | ||
|
||
if (parameters != null) | ||
{ | ||
foreach ((string key, string value) in parameters) | ||
req.AddParameter(key, value); | ||
} | ||
|
||
req.Perform(); | ||
|
||
return req.ResponseObject; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
PerformanceCalculator/Performance/LegacyScorePerformanceCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System.Linq; | ||
using McMaster.Extensions.CommandLineUtils; | ||
using osu.Game.Beatmaps; | ||
using osu.Game.Database; | ||
using osu.Game.Online.API.Requests.Responses; | ||
using osu.Game.Rulesets; | ||
using osu.Game.Rulesets.Mods; | ||
using osu.Game.Rulesets.Scoring.Legacy; | ||
using osu.Game.Scoring; | ||
using osu.Game.Scoring.Legacy; | ||
|
||
namespace PerformanceCalculator.Performance | ||
{ | ||
[Command(Name = "legacy-score", Description = "Computes the performance (pp) of an online score.")] | ||
public class LegacyScorePerformanceCommand : ScorePerformanceCommand | ||
{ | ||
[Argument(1, "ruleset-id", "The ID of the ruleset that the score was set on.")] | ||
public int RulesetId { get; set; } | ||
|
||
protected override SoloScoreInfo QueryScore() => GetJsonFromApi<SoloScoreInfo>($"scores/{LegacyHelper.GetRulesetShortNameFromId(RulesetId)}/{ScoreId}"); | ||
|
||
protected override ScoreInfo CreateScore(SoloScoreInfo apiScore, Ruleset ruleset, APIBeatmap apiBeatmap, WorkingBeatmap workingBeatmap) | ||
{ | ||
var score = base.CreateScore(apiScore, ruleset, apiBeatmap, workingBeatmap); | ||
|
||
score.Mods = score.Mods.Append(ruleset.CreateMod<ModClassic>()).ToArray(); | ||
score.IsLegacyScore = true; | ||
score.LegacyTotalScore = (int)score.TotalScore; | ||
LegacyScoreDecoder.PopulateMaximumStatistics(score, workingBeatmap); | ||
StandardisedScoreMigrationTools.UpdateFromLegacy( | ||
score, | ||
ruleset, | ||
LegacyBeatmapConversionDifficultyInfo.FromAPIBeatmap(apiBeatmap), | ||
((ILegacyRuleset)ruleset).CreateLegacyScoreSimulator().Simulate(workingBeatmap, workingBeatmap.GetPlayableBeatmap(ruleset.RulesetInfo, score.Mods))); | ||
|
||
return score; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,70 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using McMaster.Extensions.CommandLineUtils; | ||
using Newtonsoft.Json; | ||
using osu.Game.Beatmaps; | ||
using osu.Game.Database; | ||
using osu.Game.Beatmaps.Legacy; | ||
using osu.Game.Models; | ||
using osu.Game.Online.API.Requests.Responses; | ||
using osu.Game.Rulesets; | ||
using osu.Game.Rulesets.Mods; | ||
using osu.Game.Rulesets.Scoring.Legacy; | ||
using osu.Game.Scoring.Legacy; | ||
using osu.Game.Rulesets.Catch.Difficulty; | ||
using osu.Game.Rulesets.Difficulty; | ||
using osu.Game.Rulesets.Mania.Difficulty; | ||
using osu.Game.Rulesets.Osu.Difficulty; | ||
using osu.Game.Rulesets.Taiko.Difficulty; | ||
using osu.Game.Scoring; | ||
|
||
namespace PerformanceCalculator.Performance | ||
{ | ||
[Command(Name = "score", Description = "Computes the performance (pp) of an online score.")] | ||
public class ScorePerformanceCommand : ApiCommand | ||
{ | ||
[Argument(0, "ruleset-id", "The ID of the ruleset that the score was set on.")] | ||
public int RulesetId { get; set; } | ||
|
||
[Argument(1, "score-id", "The score's online ID.")] | ||
[Argument(0, "score-id", "The score's online ID.")] | ||
public ulong ScoreId { get; set; } | ||
|
||
[Option(CommandOptionType.NoValue, Template = "-a|--online-attributes", Description = "Whether to use the currently-live difficulty attributes for the beatmap.")] | ||
public bool OnlineAttributes { get; set; } | ||
|
||
public override void Execute() | ||
{ | ||
base.Execute(); | ||
|
||
SoloScoreInfo apiScore = GetJsonFromApi<SoloScoreInfo>($"scores/{LegacyHelper.GetRulesetShortNameFromId(RulesetId)}/{ScoreId}"); | ||
SoloScoreInfo apiScore = QueryScore(); | ||
APIBeatmap apiBeatmap = GetJsonFromApi<APIBeatmap>($"beatmaps/lookup?id={apiScore.BeatmapID}"); | ||
|
||
var ruleset = LegacyHelper.GetRulesetFromLegacyID(apiScore.RulesetID); | ||
var workingBeatmap = ProcessorWorkingBeatmap.FromFileOrId(apiScore.BeatmapID.ToString()); | ||
var score = CreateScore(apiScore, ruleset, apiBeatmap, workingBeatmap); | ||
|
||
DifficultyAttributes attributes; | ||
|
||
if (OnlineAttributes) | ||
{ | ||
LegacyMods legacyMods = LegacyHelper.ConvertToLegacyDifficultyAdjustmentMods(workingBeatmap.BeatmapInfo, ruleset, score.Mods); | ||
attributes = queryApiAttributes(apiScore.BeatmapID, apiScore.RulesetID, legacyMods); | ||
} | ||
else | ||
{ | ||
var difficultyCalculator = ruleset.CreateDifficultyCalculator(workingBeatmap); | ||
attributes = difficultyCalculator.Calculate(LegacyHelper.FilterDifficultyAdjustmentMods(workingBeatmap.BeatmapInfo, ruleset, score.Mods)); | ||
} | ||
|
||
var performanceCalculator = ruleset.CreatePerformanceCalculator(); | ||
var performanceAttributes = performanceCalculator?.Calculate(score, attributes); | ||
|
||
OutputPerformance(score, performanceAttributes, attributes); | ||
} | ||
|
||
protected virtual SoloScoreInfo QueryScore() => GetJsonFromApi<SoloScoreInfo>($"scores/{ScoreId}"); | ||
|
||
protected virtual ScoreInfo CreateScore(SoloScoreInfo apiScore, Ruleset ruleset, APIBeatmap apiBeatmap, WorkingBeatmap workingBeatmap) | ||
{ | ||
var score = apiScore.ToScoreInfo(apiScore.Mods.Select(m => m.ToMod(ruleset)).ToArray(), apiBeatmap); | ||
score.Ruleset = ruleset.RulesetInfo; | ||
score.BeatmapInfo!.Metadata = new BeatmapMetadata | ||
|
@@ -40,27 +74,41 @@ public override void Execute() | |
Author = new RealmUser { Username = apiBeatmap.Metadata.Author.Username }, | ||
}; | ||
|
||
var workingBeatmap = ProcessorWorkingBeatmap.FromFileOrId(score.BeatmapInfo!.OnlineID.ToString()); | ||
return score; | ||
} | ||
|
||
if (apiScore.BuildID == null) | ||
private DifficultyAttributes queryApiAttributes(int beatmapId, int rulesetId, LegacyMods mods) | ||
{ | ||
Dictionary<string, string> parameters = new Dictionary<string, string> | ||
{ | ||
score.Mods = score.Mods.Append(ruleset.CreateMod<ModClassic>()).ToArray(); | ||
score.IsLegacyScore = true; | ||
score.LegacyTotalScore = (int)score.TotalScore; | ||
LegacyScoreDecoder.PopulateMaximumStatistics(score, workingBeatmap); | ||
StandardisedScoreMigrationTools.UpdateFromLegacy( | ||
score, | ||
ruleset, | ||
LegacyBeatmapConversionDifficultyInfo.FromAPIBeatmap(apiBeatmap), | ||
((ILegacyRuleset)ruleset).CreateLegacyScoreSimulator().Simulate(workingBeatmap, workingBeatmap.GetPlayableBeatmap(ruleset.RulesetInfo, score.Mods))); | ||
} | ||
{ "mods", ((int)mods).ToString(CultureInfo.InvariantCulture) } | ||
}; | ||
|
||
var difficultyCalculator = ruleset.CreateDifficultyCalculator(workingBeatmap); | ||
var difficultyAttributes = difficultyCalculator.Calculate(LegacyHelper.ConvertToLegacyDifficultyAdjustmentMods(workingBeatmap.BeatmapInfo, ruleset, score.Mods)); | ||
var performanceCalculator = ruleset.CreatePerformanceCalculator(); | ||
var performanceAttributes = performanceCalculator?.Calculate(score, difficultyAttributes); | ||
switch (rulesetId) | ||
{ | ||
case 0: | ||
return GetJsonFromApi<AttributesResponse<OsuDifficultyAttributes>>($"beatmaps/{beatmapId}/attributes", HttpMethod.Post, parameters).Attributes; | ||
|
||
OutputPerformance(score, performanceAttributes, difficultyAttributes); | ||
case 1: | ||
return GetJsonFromApi<AttributesResponse<TaikoDifficultyAttributes>>($"beatmaps/{beatmapId}/attributes", HttpMethod.Post, parameters).Attributes; | ||
|
||
case 2: | ||
return GetJsonFromApi<AttributesResponse<CatchDifficultyAttributes>>($"beatmaps/{beatmapId}/attributes", HttpMethod.Post, parameters).Attributes; | ||
|
||
case 3: | ||
return GetJsonFromApi<AttributesResponse<ManiaDifficultyAttributes>>($"beatmaps/{beatmapId}/attributes", HttpMethod.Post, parameters).Attributes; | ||
|
||
default: | ||
throw new ArgumentOutOfRangeException(nameof(rulesetId)); | ||
} | ||
} | ||
|
||
[JsonObject(MemberSerialization.OptIn)] | ||
private class AttributesResponse<T> | ||
where T : DifficultyAttributes | ||
{ | ||
[JsonProperty("attributes")] | ||
public T Attributes { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could at least link back to the
osu-queue-score-statistics
version of this.