2020-04-11 07:41:11 +00: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 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-04-11 07:41:11 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-12-22 05:12:02 +00:00
|
|
|
using JetBrains.Annotations;
|
2020-04-11 07:41:11 +00:00
|
|
|
using osu.Framework.Allocation;
|
2020-04-11 07:43:09 +00:00
|
|
|
using osu.Framework.Bindables;
|
2020-04-11 07:41:11 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
|
2020-12-22 05:28:26 +00:00
|
|
|
namespace osu.Game.Beatmaps
|
2020-04-11 07:41:11 +00:00
|
|
|
{
|
2020-12-22 05:28:26 +00: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>
|
2020-10-22 05:19:12 +00:00
|
|
|
public partial class DifficultyRecommender : Component
|
2020-04-11 07:41:11 +00:00
|
|
|
{
|
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
2020-04-11 07:43:09 +00:00
|
|
|
[Resolved]
|
|
|
|
private Bindable<RulesetInfo> ruleset { get; set; }
|
|
|
|
|
2022-06-08 14:44:57 +00:00
|
|
|
private readonly Dictionary<string, double> recommendedDifficultyMapping = new Dictionary<string, double>();
|
2020-10-22 05:19:12 +00:00
|
|
|
|
2020-04-11 07:41:11 +00:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2022-06-08 14:44:57 +00:00
|
|
|
api.LocalUser.BindValueChanged(_ => populateValues(), true);
|
2020-04-11 07:41:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 07:58:13 +00: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>
|
2020-12-22 05:12:02 +00:00
|
|
|
[CanBeNull]
|
2020-04-11 07:58:13 +00:00
|
|
|
public BeatmapInfo GetRecommendedBeatmap(IEnumerable<BeatmapInfo> beatmaps)
|
|
|
|
{
|
2022-06-08 14:44:57 +00:00
|
|
|
foreach (string r in orderedRulesets)
|
2020-04-11 07:58:13 +00:00
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
if (!recommendedDifficultyMapping.TryGetValue(r, out double recommendation))
|
2020-12-22 05:57:32 +00:00
|
|
|
continue;
|
2020-04-14 15:42:18 +00:00
|
|
|
|
2022-12-16 09:16:26 +00:00
|
|
|
BeatmapInfo beatmapInfo = beatmaps.Where(b => b.Ruleset.ShortName.Equals(r, StringComparison.Ordinal)).MinBy(b =>
|
2020-04-11 08:08:07 +00:00
|
|
|
{
|
2021-11-11 08:18:51 +00:00
|
|
|
double difference = b.StarRating - recommendation;
|
2020-04-11 08:08:07 +00:00
|
|
|
return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder
|
2022-12-16 09:16:26 +00:00
|
|
|
});
|
2020-04-14 15:42:18 +00:00
|
|
|
|
2021-10-02 15:55:29 +00:00
|
|
|
if (beatmapInfo != null)
|
|
|
|
return beatmapInfo;
|
2020-04-11 07:58:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 05:57:32 +00:00
|
|
|
return null;
|
2020-04-11 07:58:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 14:44:57 +00:00
|
|
|
private void populateValues()
|
2020-04-11 07:41:11 +00:00
|
|
|
{
|
2022-06-08 14:44:57 +00:00
|
|
|
if (api.LocalUser.Value.RulesetsStatistics == null)
|
2020-11-21 12:09:32 +00:00
|
|
|
return;
|
|
|
|
|
2022-06-08 18:10:27 +00:00
|
|
|
foreach (var kvp in api.LocalUser.Value.RulesetsStatistics)
|
2020-04-11 07:41:11 +00:00
|
|
|
{
|
2022-06-08 14:44:57 +00:00
|
|
|
// algorithm taken from https://github.com/ppy/osu-web/blob/e6e2825516449e3d0f3f5e1852c6bdd3428c3437/app/Models/User.php#L1505
|
2022-06-08 18:10:27 +00:00
|
|
|
recommendedDifficultyMapping[kvp.Key] = Math.Pow((double)(kvp.Value.PP ?? 0), 0.4) * 0.195;
|
2022-06-08 14:44:57 +00:00
|
|
|
}
|
2020-04-11 07:41:11 +00:00
|
|
|
}
|
2020-04-11 08:07:08 +00:00
|
|
|
|
2020-12-03 17:53:06 +00:00
|
|
|
/// <returns>
|
2020-12-22 05:57:32 +00:00
|
|
|
/// Rulesets ordered descending by their respective recommended difficulties.
|
|
|
|
/// The currently selected ruleset will always be first.
|
2020-12-03 17:53:06 +00:00
|
|
|
/// </returns>
|
2022-06-08 14:44:57 +00:00
|
|
|
private IEnumerable<string> orderedRulesets
|
2021-06-16 02:25:00 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (LoadState < LoadState.Ready || ruleset.Value == null)
|
2022-06-08 14:44:57 +00:00
|
|
|
return Enumerable.Empty<string>();
|
2021-06-16 02:25:00 +00:00
|
|
|
|
|
|
|
return recommendedDifficultyMapping
|
|
|
|
.OrderByDescending(pair => pair.Value)
|
|
|
|
.Select(pair => pair.Key)
|
2022-12-16 09:16:26 +00:00
|
|
|
.Where(r => !r.Equals(ruleset.Value.ShortName, StringComparison.Ordinal))
|
2022-06-08 14:44:57 +00:00
|
|
|
.Prepend(ruleset.Value.ShortName);
|
2021-06-16 02:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-11 07:41:11 +00:00
|
|
|
}
|
|
|
|
}
|