Initial rewrite, moving API logic to SongSelect

This commit is contained in:
Endrik Tombak
2020-04-09 18:47:28 +03:00
parent 260de77304
commit f40bdcd34e
3 changed files with 79 additions and 37 deletions

View File

@ -34,8 +34,6 @@ namespace osu.Game.Screens.Select
private const float bleed_top = FilterControl.HEIGHT; private const float bleed_top = FilterControl.HEIGHT;
private const float bleed_bottom = Footer.HEIGHT; private const float bleed_bottom = Footer.HEIGHT;
protected readonly Bindable<double> RecommendedStarDifficulty = new Bindable<double>();
/// <summary> /// <summary>
/// Triggered when the <see cref="BeatmapSets"/> loaded change and are completely loaded. /// Triggered when the <see cref="BeatmapSets"/> loaded change and are completely loaded.
/// </summary> /// </summary>
@ -122,6 +120,7 @@ namespace osu.Game.Screens.Select
protected List<DrawableCarouselItem> Items = new List<DrawableCarouselItem>(); protected List<DrawableCarouselItem> Items = new List<DrawableCarouselItem>();
private CarouselRoot root; private CarouselRoot root;
public SongSelect.DifficultyRecommender DifficultyRecommender;
public BeatmapCarousel() public BeatmapCarousel()
{ {
@ -145,10 +144,10 @@ namespace osu.Game.Screens.Select
private BeatmapManager beatmaps { get; set; } private BeatmapManager beatmaps { get; set; }
[Resolved] [Resolved]
private IAPIProvider api { get; set; } private Bindable<RulesetInfo> decoupledRuleset { get; set; }
[BackgroundDependencyLoader(permitNulls: true)] [BackgroundDependencyLoader(permitNulls: true)]
private void load(OsuConfigManager config, Bindable<RulesetInfo> decoupledRuleset) private void load(OsuConfigManager config)
{ {
config.BindWith(OsuSetting.RandomSelectAlgorithm, RandomAlgorithm); config.BindWith(OsuSetting.RandomSelectAlgorithm, RandomAlgorithm);
config.BindWith(OsuSetting.SongSelectRightMouseScroll, RightClickScrollingEnabled); config.BindWith(OsuSetting.SongSelectRightMouseScroll, RightClickScrollingEnabled);
@ -162,27 +161,6 @@ namespace osu.Game.Screens.Select
beatmaps.BeatmapRestored += beatmapRestored; beatmaps.BeatmapRestored += beatmapRestored;
loadBeatmapSets(GetLoadableBeatmaps()); loadBeatmapSets(GetLoadableBeatmaps());
decoupledRuleset.BindValueChanged(UpdateRecommendedStarDifficulty, true);
}
protected void UpdateRecommendedStarDifficulty(ValueChangedEvent<RulesetInfo> ruleset)
{
if (api.LocalUser.Value is GuestUser)
{
RecommendedStarDifficulty.Value = 0;
return;
}
var req = new GetUserRequest(api.LocalUser.Value.Id, ruleset.NewValue);
req.Success += result =>
{
// algorithm taken from https://github.com/ppy/osu-web/blob/e6e2825516449e3d0f3f5e1852c6bdd3428c3437/app/Models/User.php#L1505
RecommendedStarDifficulty.Value = Math.Pow((double)(result.Statistics.PP ?? 0), 0.4) * 0.195;
};
api.PerformAsync(req);
} }
protected virtual IEnumerable<BeatmapSetInfo> GetLoadableBeatmaps() => beatmaps.GetAllUsableBeatmapSetsEnumerable(); protected virtual IEnumerable<BeatmapSetInfo> GetLoadableBeatmaps() => beatmaps.GetAllUsableBeatmapSetsEnumerable();
@ -608,7 +586,12 @@ namespace osu.Game.Screens.Select
b.Metadata = beatmapSet.Metadata; b.Metadata = beatmapSet.Metadata;
} }
var set = new CarouselBeatmapSet(beatmapSet, RecommendedStarDifficulty); BeatmapInfo recommender(IEnumerable<BeatmapInfo> beatmaps)
{
return DifficultyRecommender?.GetRecommendedBeatmap(beatmaps, decoupledRuleset.Value);
}
var set = new CarouselBeatmapSet(beatmapSet, recommender);
foreach (var c in set.Beatmaps) foreach (var c in set.Beatmaps)
{ {

View File

@ -13,13 +13,13 @@ namespace osu.Game.Screens.Select.Carousel
{ {
public class CarouselBeatmapSet : CarouselGroupEagerSelect public class CarouselBeatmapSet : CarouselGroupEagerSelect
{ {
private readonly Bindable<double> recommendedStarDifficulty = new Bindable<double>(); private Func<IEnumerable<BeatmapInfo>, BeatmapInfo> getRecommendedBeatmap;
public IEnumerable<CarouselBeatmap> Beatmaps => InternalChildren.OfType<CarouselBeatmap>(); public IEnumerable<CarouselBeatmap> Beatmaps => InternalChildren.OfType<CarouselBeatmap>();
public BeatmapSetInfo BeatmapSet; public BeatmapSetInfo BeatmapSet;
public CarouselBeatmapSet(BeatmapSetInfo beatmapSet, Bindable<double> recommendedStarDifficulty) public CarouselBeatmapSet(BeatmapSetInfo beatmapSet, Func<IEnumerable<BeatmapInfo>, BeatmapInfo> getRecommendedBeatmap)
{ {
BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet)); BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet));
@ -28,7 +28,7 @@ namespace osu.Game.Screens.Select.Carousel
.Select(b => new CarouselBeatmap(b)) .Select(b => new CarouselBeatmap(b))
.ForEach(AddChild); .ForEach(AddChild);
this.recommendedStarDifficulty.BindTo(recommendedStarDifficulty); this.getRecommendedBeatmap = getRecommendedBeatmap;
} }
protected override DrawableCarouselItem CreateDrawableRepresentation() => new DrawableCarouselBeatmapSet(this); protected override DrawableCarouselItem CreateDrawableRepresentation() => new DrawableCarouselBeatmapSet(this);
@ -37,14 +37,8 @@ namespace osu.Game.Screens.Select.Carousel
{ {
if (LastSelected == null) if (LastSelected == null)
{ {
return Children.OfType<CarouselBeatmap>() var recommendedBeatmapInfo = getRecommendedBeatmap(Children.OfType<CarouselBeatmap>().Where(b => !b.Filtered.Value).Select(b => b.Beatmap));
.Where(b => !b.Filtered.Value) return Children.OfType<CarouselBeatmap>().Where(b => b.Beatmap == recommendedBeatmapInfo).First();
.OrderBy(b =>
{
var difference = b.Beatmap.StarDifficulty - recommendedStarDifficulty.Value;
return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder
})
.FirstOrDefault();
} }
return base.GetNextToSelect(); return base.GetNextToSelect();

View File

@ -36,6 +36,9 @@ using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Bindings; using osu.Framework.Input.Bindings;
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
using osu.Game.Scoring; using osu.Game.Scoring;
using osu.Game.Online.API;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Game.Online.API.Requests;
namespace osu.Game.Screens.Select namespace osu.Game.Screens.Select
{ {
@ -80,6 +83,7 @@ namespace osu.Game.Screens.Select
protected override BackgroundScreen CreateBackground() => new BackgroundScreenBeatmap(Beatmap.Value); protected override BackgroundScreen CreateBackground() => new BackgroundScreenBeatmap(Beatmap.Value);
protected BeatmapCarousel Carousel { get; private set; } protected BeatmapCarousel Carousel { get; private set; }
private DifficultyRecommender difficultyRecommender;
private BeatmapInfoWedge beatmapInfoWedge; private BeatmapInfoWedge beatmapInfoWedge;
private DialogOverlay dialogOverlay; private DialogOverlay dialogOverlay;
@ -107,6 +111,8 @@ namespace osu.Game.Screens.Select
// initial value transfer is required for FilterControl (it uses our re-cached bindables in its async load for the initial filter). // initial value transfer is required for FilterControl (it uses our re-cached bindables in its async load for the initial filter).
transferRulesetValue(); transferRulesetValue();
AddInternal(difficultyRecommender = new DifficultyRecommender());
AddRangeInternal(new Drawable[] AddRangeInternal(new Drawable[]
{ {
new ResetScrollContainer(() => Carousel.ScrollToSelected()) new ResetScrollContainer(() => Carousel.ScrollToSelected())
@ -156,6 +162,7 @@ namespace osu.Game.Screens.Select
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
SelectionChanged = updateSelectedBeatmap, SelectionChanged = updateSelectedBeatmap,
BeatmapSetsChanged = carouselBeatmapsLoaded, BeatmapSetsChanged = carouselBeatmapsLoaded,
DifficultyRecommender = difficultyRecommender,
}, },
} }
}, },
@ -780,6 +787,64 @@ namespace osu.Game.Screens.Select
return base.OnKeyDown(e); return base.OnKeyDown(e);
} }
public class DifficultyRecommender : Component
{
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private RulesetStore rulesets { get; set; }
private Dictionary<RulesetInfo, double> recommendedStarDifficulty = new Dictionary<RulesetInfo, double>();
private int pendingAPIRequests = 0;
[BackgroundDependencyLoader]
private void load()
{
updateRecommended();
}
private void updateRecommended()
{
if (pendingAPIRequests > 0)
return;
if (api.LocalUser.Value is GuestUser)
return;
rulesets.AvailableRulesets.ForEach(rulesetInfo =>
{
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;
pendingAPIRequests--;
};
req.Failure += _ => pendingAPIRequests--;
pendingAPIRequests++;
api.Queue(req);
});
}
public BeatmapInfo GetRecommendedBeatmap(IEnumerable<BeatmapInfo> beatmaps, RulesetInfo currentRuleset)
{
if (!recommendedStarDifficulty.ContainsKey(currentRuleset))
{
updateRecommended();
return null;
}
return beatmaps.OrderBy(b =>
{
var difference = b.StarDifficulty - recommendedStarDifficulty[currentRuleset];
return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder
}).FirstOrDefault();
}
}
private class VerticalMaskingContainer : Container private class VerticalMaskingContainer : Container
{ {
private const float panel_overflow = 1.2f; private const float panel_overflow = 1.2f;