1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 13:15:08 +08:00

Refactor recommendation iteration code to read better

This commit is contained in:
Dean Herbert 2020-12-22 14:57:32 +09:00
parent c6be969e33
commit 807c1ecd1f

View File

@ -35,7 +35,7 @@ namespace osu.Game.Beatmaps
/// </summary> /// </summary>
private int? requestedUserId; private int? requestedUserId;
private readonly Dictionary<RulesetInfo, double> recommendedStarDifficulty = new Dictionary<RulesetInfo, double>(); private readonly Dictionary<RulesetInfo, double> recommendedDifficultyMapping = new Dictionary<RulesetInfo, double>();
private readonly IBindable<APIState> apiState = new Bindable<APIState>(); private readonly IBindable<APIState> apiState = new Bindable<APIState>();
@ -57,31 +57,27 @@ namespace osu.Game.Beatmaps
[CanBeNull] [CanBeNull]
public BeatmapInfo GetRecommendedBeatmap(IEnumerable<BeatmapInfo> beatmaps) public BeatmapInfo GetRecommendedBeatmap(IEnumerable<BeatmapInfo> beatmaps)
{ {
if (!recommendedStarDifficulty.Any()) foreach (var r in orderedRulesets)
return null;
BeatmapInfo beatmap = null;
foreach (var r in getBestRulesetOrder())
{ {
recommendedStarDifficulty.TryGetValue(r, out var stars); if (!recommendedDifficultyMapping.TryGetValue(r, out var recommendation))
continue;
beatmap = beatmaps.Where(b => b.Ruleset.Equals(r)).OrderBy(b => BeatmapInfo beatmap = beatmaps.Where(b => b.Ruleset.Equals(r)).OrderBy(b =>
{ {
var difference = b.StarDifficulty - stars; var difference = b.StarDifficulty - recommendation;
return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder
}).FirstOrDefault(); }).FirstOrDefault();
if (beatmap != null) if (beatmap != null)
break; return beatmap;
} }
return beatmap; return null;
} }
private void fetchRecommendedValues() private void fetchRecommendedValues()
{ {
if (recommendedStarDifficulty.Count > 0 && api.LocalUser.Value.Id == requestedUserId) if (recommendedDifficultyMapping.Count > 0 && api.LocalUser.Value.Id == requestedUserId)
return; return;
requestedUserId = api.LocalUser.Value.Id; requestedUserId = api.LocalUser.Value.Id;
@ -94,7 +90,7 @@ namespace osu.Game.Beatmaps
req.Success += result => req.Success += result =>
{ {
// algorithm taken from https://github.com/ppy/osu-web/blob/e6e2825516449e3d0f3f5e1852c6bdd3428c3437/app/Models/User.php#L1505 // algorithm taken from https://github.com/ppy/osu-web/blob/e6e2825516449e3d0f3f5e1852c6bdd3428c3437/app/Models/User.php#L1505
recommendedStarDifficulty[rulesetInfo] = Math.Pow((double)(result.Statistics.PP ?? 0), 0.4) * 0.195; recommendedDifficultyMapping[rulesetInfo] = Math.Pow((double)(result.Statistics.PP ?? 0), 0.4) * 0.195;
}; };
api.Queue(req); api.Queue(req);
@ -102,29 +98,13 @@ namespace osu.Game.Beatmaps
} }
/// <returns> /// <returns>
/// Rulesets ordered by highest recommended star difficulty, except currently selected ruleset first /// Rulesets ordered descending by their respective recommended difficulties.
/// The currently selected ruleset will always be first.
/// </returns> /// </returns>
private IEnumerable<RulesetInfo> getBestRulesetOrder() private IEnumerable<RulesetInfo> orderedRulesets =>
{ recommendedDifficultyMapping
IEnumerable<RulesetInfo> bestRulesetOrder = recommendedStarDifficulty.OrderByDescending(pair => pair.Value) .OrderByDescending(pair => pair.Value).Select(pair => pair.Key).Where(r => !r.Equals(ruleset.Value))
.Select(pair => pair.Key); .Prepend(ruleset.Value);
List<RulesetInfo> orderedRulesets;
if (bestRulesetOrder.Contains(ruleset.Value))
{
orderedRulesets = bestRulesetOrder.ToList();
orderedRulesets.Remove(ruleset.Value);
orderedRulesets.Insert(0, ruleset.Value);
}
else
{
orderedRulesets = new List<RulesetInfo> { ruleset.Value };
orderedRulesets.AddRange(bestRulesetOrder);
}
return orderedRulesets;
}
private void onlineStateChanged(ValueChangedEvent<APIState> state) => Schedule(() => private void onlineStateChanged(ValueChangedEvent<APIState> state) => Schedule(() =>
{ {