1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 22:07:25 +08:00
osu-lazer/osu.Game/Beatmaps/DifficultyRecommender.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
3.6 KiB
C#
Raw Normal View History

2020-04-11 15:41:11 +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.
2022-06-17 15:37:17 +08:00
#nullable disable
2020-04-11 15:41:11 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
2020-04-11 15:41:11 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2020-04-11 15:41:11 +08:00
using osu.Framework.Graphics;
using osu.Game.Online.API;
using osu.Game.Rulesets;
namespace osu.Game.Beatmaps
2020-04-11 15:41:11 +08:00
{
/// <summary>
/// A class which will recommend the most suitable difficulty for the local user from a beatmap set.
/// This requires the user to be logged in, as it sources from the user's online profile.
/// </summary>
public partial class DifficultyRecommender : Component
2020-04-11 15:41:11 +08:00
{
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private Bindable<RulesetInfo> ruleset { get; set; }
private readonly Dictionary<string, double> recommendedDifficultyMapping = new Dictionary<string, double>();
2020-04-11 15:41:11 +08:00
[BackgroundDependencyLoader]
private void load()
{
api.LocalUser.BindValueChanged(_ => populateValues(), true);
2020-04-11 15:41:11 +08:00
}
/// <summary>
/// Find the recommended difficulty from a selection of available difficulties for the current local user.
/// </summary>
/// <remarks>
/// This requires the user to be online for now.
/// </remarks>
/// <param name="beatmaps">A collection of beatmaps to select a difficulty from.</param>
/// <returns>The recommended difficulty, or null if a recommendation could not be provided.</returns>
[CanBeNull]
public BeatmapInfo GetRecommendedBeatmap(IEnumerable<BeatmapInfo> beatmaps)
{
foreach (string r in orderedRulesets)
{
if (!recommendedDifficultyMapping.TryGetValue(r, out double recommendation))
continue;
2020-04-14 23:42:18 +08:00
BeatmapInfo beatmapInfo = beatmaps.Where(b => b.Ruleset.ShortName.Equals(r, StringComparison.Ordinal)).MinBy(b =>
2020-04-11 16:08:07 +08:00
{
double difference = b.StarRating - recommendation;
2020-04-11 16:08:07 +08:00
return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder
});
2020-04-14 23:42:18 +08:00
2021-10-02 23:55:29 +08:00
if (beatmapInfo != null)
return beatmapInfo;
}
return null;
}
private void populateValues()
2020-04-11 15:41:11 +08:00
{
if (api.LocalUser.Value.RulesetsStatistics == null)
2020-11-21 20:09:32 +08:00
return;
2022-06-09 02:10:27 +08:00
foreach (var kvp in api.LocalUser.Value.RulesetsStatistics)
2020-04-11 15:41:11 +08:00
{
// algorithm taken from https://github.com/ppy/osu-web/blob/e6e2825516449e3d0f3f5e1852c6bdd3428c3437/app/Models/User.php#L1505
2022-06-09 02:10:27 +08:00
recommendedDifficultyMapping[kvp.Key] = Math.Pow((double)(kvp.Value.PP ?? 0), 0.4) * 0.195;
}
2020-04-11 15:41:11 +08:00
}
2020-04-11 16:07:08 +08:00
2020-12-04 01:53:06 +08:00
/// <returns>
/// Rulesets ordered descending by their respective recommended difficulties.
/// The currently selected ruleset will always be first.
2020-12-04 01:53:06 +08:00
/// </returns>
private IEnumerable<string> orderedRulesets
{
get
{
if (LoadState < LoadState.Ready || ruleset.Value == null)
return Enumerable.Empty<string>();
return recommendedDifficultyMapping
.OrderByDescending(pair => pair.Value)
.Select(pair => pair.Key)
.Where(r => !r.Equals(ruleset.Value.ShortName, StringComparison.Ordinal))
.Prepend(ruleset.Value.ShortName);
}
}
2020-04-11 15:41:11 +08:00
}
}