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.
|
|
|
|
|
|
|
|
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.Extensions.IEnumerableExtensions;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
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 class DifficultyRecommender : Component
|
2020-04-11 07:41:11 +00:00
|
|
|
{
|
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private RulesetStore rulesets { get; set; }
|
|
|
|
|
2020-04-11 07:43:09 +00:00
|
|
|
[Resolved]
|
|
|
|
private Bindable<RulesetInfo> ruleset { get; set; }
|
|
|
|
|
2020-12-22 05:23:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The user for which the last requests were run.
|
|
|
|
/// </summary>
|
|
|
|
private int? requestedUserId;
|
2020-12-03 17:38:45 +00:00
|
|
|
|
2020-04-11 07:41:11 +00:00
|
|
|
private readonly Dictionary<RulesetInfo, double> recommendedStarDifficulty = new Dictionary<RulesetInfo, double>();
|
|
|
|
|
2020-10-22 05:19:12 +00:00
|
|
|
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
|
|
|
|
|
2020-04-11 07:41:11 +00:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2020-10-22 05:19:12 +00:00
|
|
|
apiState.BindTo(api.State);
|
|
|
|
apiState.BindValueChanged(onlineStateChanged, 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)
|
|
|
|
{
|
2020-04-14 15:42:18 +00:00
|
|
|
if (!recommendedStarDifficulty.Any())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
BeatmapInfo beatmap = null;
|
|
|
|
|
|
|
|
foreach (var r in getBestRulesetOrder())
|
2020-04-11 07:58:13 +00:00
|
|
|
{
|
2020-05-01 16:44:35 +00:00
|
|
|
recommendedStarDifficulty.TryGetValue(r, out var stars);
|
2020-04-14 15:42:18 +00:00
|
|
|
|
|
|
|
beatmap = beatmaps.Where(b => b.Ruleset.Equals(r)).OrderBy(b =>
|
2020-04-11 08:08:07 +00:00
|
|
|
{
|
|
|
|
var difference = b.StarDifficulty - stars;
|
|
|
|
return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder
|
|
|
|
}).FirstOrDefault();
|
2020-04-14 15:42:18 +00:00
|
|
|
|
|
|
|
if (beatmap != null)
|
|
|
|
break;
|
2020-04-11 07:58:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 15:42:18 +00:00
|
|
|
return beatmap;
|
2020-04-11 07:58:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 05:23:33 +00:00
|
|
|
private void fetchRecommendedValues()
|
2020-04-11 07:41:11 +00:00
|
|
|
{
|
2020-12-22 05:23:33 +00:00
|
|
|
if (recommendedStarDifficulty.Count > 0 && api.LocalUser.Value.Id == requestedUserId)
|
2020-11-21 12:09:32 +00:00
|
|
|
return;
|
|
|
|
|
2020-12-22 05:23:33 +00:00
|
|
|
requestedUserId = api.LocalUser.Value.Id;
|
2020-12-03 17:38:45 +00:00
|
|
|
|
2020-05-01 15:02:28 +00:00
|
|
|
// only query API for built-in rulesets
|
2020-12-03 17:38:45 +00:00
|
|
|
rulesets.AvailableRulesets.Where(ruleset => ruleset.ID <= ILegacyRuleset.MAX_LEGACY_RULESET_ID).ForEach(rulesetInfo =>
|
2020-04-11 07:41:11 +00:00
|
|
|
{
|
|
|
|
var req = new GetUserRequest(api.LocalUser.Value.Id, rulesetInfo);
|
|
|
|
|
|
|
|
req.Success += result =>
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
api.Queue(req);
|
|
|
|
});
|
|
|
|
}
|
2020-04-11 08:07:08 +00:00
|
|
|
|
2020-12-03 17:53:06 +00:00
|
|
|
/// <returns>
|
|
|
|
/// Rulesets ordered by highest recommended star difficulty, except currently selected ruleset first
|
|
|
|
/// </returns>
|
2020-04-14 15:42:18 +00:00
|
|
|
private IEnumerable<RulesetInfo> getBestRulesetOrder()
|
|
|
|
{
|
2020-11-21 12:26:09 +00:00
|
|
|
IEnumerable<RulesetInfo> bestRulesetOrder = recommendedStarDifficulty.OrderByDescending(pair => pair.Value)
|
2020-12-03 17:53:41 +00:00
|
|
|
.Select(pair => pair.Key);
|
2020-04-14 15:42:18 +00:00
|
|
|
|
2020-04-17 17:33:12 +00:00
|
|
|
List<RulesetInfo> orderedRulesets;
|
2020-04-17 15:15:11 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:14:51 +00:00
|
|
|
return orderedRulesets;
|
2020-04-14 15:42:18 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 05:19:12 +00:00
|
|
|
private void onlineStateChanged(ValueChangedEvent<APIState> state) => Schedule(() =>
|
2020-04-11 08:07:08 +00:00
|
|
|
{
|
2020-10-22 05:19:12 +00:00
|
|
|
switch (state.NewValue)
|
2020-04-11 08:07:08 +00:00
|
|
|
{
|
|
|
|
case APIState.Online:
|
2020-12-22 05:23:33 +00:00
|
|
|
fetchRecommendedValues();
|
2020-04-11 08:07:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-10-22 05:19:12 +00:00
|
|
|
});
|
2020-04-11 07:41:11 +00:00
|
|
|
}
|
|
|
|
}
|