2020-04-11 16:41:11 +09: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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
2020-04-11 16:43:09 +09:00
|
|
|
using osu.Framework.Bindables;
|
2024-11-18 06:40:14 -05:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2020-04-11 16:41:11 +09:00
|
|
|
using osu.Framework.Graphics;
|
2024-11-17 18:15:38 -05:00
|
|
|
using osu.Game.Online;
|
2020-04-11 16:41:11 +09:00
|
|
|
using osu.Game.Rulesets;
|
2024-11-17 20:35:59 -05:00
|
|
|
using osu.Game.Users;
|
2020-04-11 16:41:11 +09:00
|
|
|
|
2020-12-22 14:28:26 +09:00
|
|
|
namespace osu.Game.Beatmaps
|
2020-04-11 16:41:11 +09:00
|
|
|
{
|
2020-12-22 14:28:26 +09: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 14:19:12 +09:00
|
|
|
public partial class DifficultyRecommender : Component
|
2020-04-11 16:41:11 +09:00
|
|
|
{
|
2024-12-10 15:09:11 -08:00
|
|
|
public event Action? StarRatingUpdated;
|
|
|
|
|
2024-11-17 18:15:38 -05:00
|
|
|
private readonly LocalUserStatisticsProvider statisticsProvider;
|
2020-04-11 16:41:11 +09:00
|
|
|
|
2020-04-11 16:43:09 +09:00
|
|
|
[Resolved]
|
2024-12-10 15:02:41 -08:00
|
|
|
private Bindable<RulesetInfo> gameRuleset { get; set; } = null!;
|
2024-11-17 20:35:59 -05:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private RulesetStore rulesets { get; set; } = null!;
|
2020-04-11 16:43:09 +09:00
|
|
|
|
2022-06-08 17:44:57 +03:00
|
|
|
private readonly Dictionary<string, double> recommendedDifficultyMapping = new Dictionary<string, double>();
|
2020-10-22 14:19:12 +09:00
|
|
|
|
2024-11-17 18:15:38 -05:00
|
|
|
/// <returns>
|
|
|
|
/// Rulesets ordered descending by their respective recommended difficulties.
|
|
|
|
/// The currently selected ruleset will always be first.
|
|
|
|
/// </returns>
|
|
|
|
private IEnumerable<string> orderedRulesets
|
2020-04-11 16:41:11 +09:00
|
|
|
{
|
2024-11-17 18:15:38 -05:00
|
|
|
get
|
|
|
|
{
|
2024-11-17 20:35:59 -05:00
|
|
|
if (LoadState < LoadState.Ready || gameRuleset.Value == null)
|
2024-11-17 18:15:38 -05:00
|
|
|
return Enumerable.Empty<string>();
|
|
|
|
|
|
|
|
return recommendedDifficultyMapping
|
|
|
|
.OrderByDescending(pair => pair.Value)
|
|
|
|
.Select(pair => pair.Key)
|
2024-11-17 20:35:59 -05:00
|
|
|
.Where(r => !r.Equals(gameRuleset.Value.ShortName, StringComparison.Ordinal))
|
|
|
|
.Prepend(gameRuleset.Value.ShortName);
|
2024-11-17 18:15:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public DifficultyRecommender(LocalUserStatisticsProvider statisticsProvider)
|
|
|
|
{
|
|
|
|
this.statisticsProvider = statisticsProvider;
|
|
|
|
}
|
|
|
|
|
2024-11-17 20:35:59 -05:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
foreach (var ruleset in rulesets.AvailableRulesets)
|
|
|
|
{
|
|
|
|
if (statisticsProvider.GetStatisticsFor(ruleset) is UserStatistics statistics)
|
|
|
|
updateMapping(ruleset, statistics);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 18:15:38 -05:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2024-11-18 06:40:14 -05:00
|
|
|
statisticsProvider.StatisticsUpdated += onStatisticsUpdated;
|
2024-11-17 20:35:59 -05:00
|
|
|
}
|
2024-11-17 19:13:23 -05:00
|
|
|
|
2024-11-18 06:40:14 -05:00
|
|
|
private void onStatisticsUpdated(UserStatisticsUpdate update) => updateMapping(update.Ruleset, update.NewStatistics);
|
|
|
|
|
2024-11-17 20:35:59 -05:00
|
|
|
private void updateMapping(RulesetInfo ruleset, UserStatistics statistics)
|
|
|
|
{
|
2024-12-12 16:17:57 -08:00
|
|
|
// algorithm taken from https://github.com/ppy/osu-web/blob/027026fccc91525e39cee5d2f369f1b343eb1bf1/app/Models/UserStatistics/Model.php#L93-L94
|
|
|
|
recommendedDifficultyMapping[ruleset.ShortName] =
|
|
|
|
ruleset.ShortName == @"taiko"
|
|
|
|
? Math.Pow((double)(statistics.PP ?? 0), 0.35) * 0.27
|
|
|
|
: Math.Pow((double)(statistics.PP ?? 0), 0.4) * 0.195;
|
2024-12-10 15:09:11 -08:00
|
|
|
|
|
|
|
StarRatingUpdated?.Invoke();
|
2020-04-11 16:41:11 +09:00
|
|
|
}
|
|
|
|
|
2024-12-10 18:18:34 -08:00
|
|
|
public double? GetRecommendedStarRatingFor(RulesetInfo ruleset)
|
|
|
|
=> recommendedDifficultyMapping.TryGetValue(ruleset.ShortName, out double starRating) ? starRating : null;
|
2024-12-10 15:09:11 -08:00
|
|
|
|
2020-04-11 16:58:13 +09: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>
|
2024-12-10 15:02:41 -08:00
|
|
|
public BeatmapInfo? GetRecommendedBeatmap(IEnumerable<BeatmapInfo> beatmaps)
|
2020-04-11 16:58:13 +09:00
|
|
|
{
|
2022-06-08 17:44:57 +03:00
|
|
|
foreach (string r in orderedRulesets)
|
2020-04-11 16:58:13 +09:00
|
|
|
{
|
2021-10-27 13:04:41 +09:00
|
|
|
if (!recommendedDifficultyMapping.TryGetValue(r, out double recommendation))
|
2020-12-22 14:57:32 +09:00
|
|
|
continue;
|
2020-04-14 18:42:18 +03:00
|
|
|
|
2024-12-10 15:02:41 -08:00
|
|
|
BeatmapInfo? beatmapInfo = beatmaps.Where(b => b.Ruleset.ShortName.Equals(r, StringComparison.Ordinal)).MinBy(b =>
|
2020-04-11 17:08:07 +09:00
|
|
|
{
|
2021-11-11 17:18:51 +09:00
|
|
|
double difference = b.StarRating - recommendation;
|
2020-04-11 17:08:07 +09:00
|
|
|
return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder
|
2022-12-16 18:16:26 +09:00
|
|
|
});
|
2020-04-14 18:42:18 +03:00
|
|
|
|
2021-10-03 00:55:29 +09:00
|
|
|
if (beatmapInfo != null)
|
|
|
|
return beatmapInfo;
|
2020-04-11 16:58:13 +09:00
|
|
|
}
|
|
|
|
|
2020-12-22 14:57:32 +09:00
|
|
|
return null;
|
2020-04-11 16:58:13 +09:00
|
|
|
}
|
2024-11-18 06:40:14 -05:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
if (statisticsProvider.IsNotNull())
|
|
|
|
statisticsProvider.StatisticsUpdated -= onStatisticsUpdated;
|
|
|
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
}
|
2020-04-11 16:41:11 +09:00
|
|
|
}
|
|
|
|
}
|