Merge pull request #8444 from LittleEndu/select-recommended

Make beatmap carousel select recommended difficulties
This commit is contained in:
Dan Balasescu 2020-04-14 14:25:17 +09:00 committed by GitHub
commit 33f7e429a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 155 additions and 4 deletions

View File

@ -54,6 +54,35 @@ private void load(RulesetStore rulesets)
this.rulesets = rulesets;
}
[Test]
public void TestRecommendedSelection()
{
loadBeatmaps();
AddStep("set recommendation function", () => carousel.GetRecommendedBeatmap = beatmaps => beatmaps.LastOrDefault());
// check recommended was selected
advanceSelection(direction: 1, diff: false);
waitForSelection(1, 3);
// change away from recommended
advanceSelection(direction: -1, diff: true);
waitForSelection(1, 2);
// next set, check recommended
advanceSelection(direction: 1, diff: false);
waitForSelection(2, 3);
// next set, check recommended
advanceSelection(direction: 1, diff: false);
waitForSelection(3, 3);
// go back to first set and ensure user selection was retained
advanceSelection(direction: -1, diff: false);
advanceSelection(direction: -1, diff: false);
waitForSelection(1, 2);
}
/// <summary>
/// Test keyboard traversal
/// </summary>

View File

@ -48,6 +48,11 @@ public class BeatmapCarousel : CompositeDrawable, IKeyBindingHandler<GlobalActio
/// </summary>
public BeatmapSetInfo SelectedBeatmapSet => selectedBeatmapSet?.BeatmapSet;
/// <summary>
/// A function to optionally decide on a recommended difficulty from a beatmap set.
/// </summary>
public Func<IEnumerable<BeatmapInfo>, BeatmapInfo> GetRecommendedBeatmap;
private CarouselBeatmapSet selectedBeatmapSet;
/// <summary>
@ -116,6 +121,7 @@ private void loadBeatmapSets(IEnumerable<BeatmapSetInfo> beatmapSets)
private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>();
protected List<DrawableCarouselItem> Items = new List<DrawableCarouselItem>();
private CarouselRoot root;
public BeatmapCarousel()
@ -579,7 +585,10 @@ private CarouselBeatmapSet createCarouselSet(BeatmapSetInfo beatmapSet)
b.Metadata = beatmapSet.Metadata;
}
var set = new CarouselBeatmapSet(beatmapSet);
var set = new CarouselBeatmapSet(beatmapSet)
{
GetRecommendedBeatmap = beatmaps => GetRecommendedBeatmap?.Invoke(beatmaps)
};
foreach (var c in set.Beatmaps)
{

View File

@ -16,6 +16,8 @@ public class CarouselBeatmapSet : CarouselGroupEagerSelect
public BeatmapSetInfo BeatmapSet;
public Func<IEnumerable<BeatmapInfo>, BeatmapInfo> GetRecommendedBeatmap;
public CarouselBeatmapSet(BeatmapSetInfo beatmapSet)
{
BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet));
@ -28,6 +30,17 @@ public CarouselBeatmapSet(BeatmapSetInfo beatmapSet)
protected override DrawableCarouselItem CreateDrawableRepresentation() => new DrawableCarouselBeatmapSet(this);
protected override CarouselItem GetNextToSelect()
{
if (LastSelected == null)
{
if (GetRecommendedBeatmap?.Invoke(Children.OfType<CarouselBeatmap>().Where(b => !b.Filtered.Value).Select(b => b.Beatmap)) is BeatmapInfo recommended)
return Children.OfType<CarouselBeatmap>().First(b => b.Beatmap == recommended);
}
return base.GetNextToSelect();
}
public override int CompareTo(FilterCriteria criteria, CarouselItem other)
{
if (!(other is CarouselBeatmapSet otherSet))

View File

@ -90,11 +90,15 @@ private void attemptSelection()
PerformSelection();
}
protected virtual CarouselItem GetNextToSelect()
{
return Children.Skip(lastSelectedIndex).FirstOrDefault(i => !i.Filtered.Value) ??
Children.Reverse().Skip(InternalChildren.Count - lastSelectedIndex).FirstOrDefault(i => !i.Filtered.Value);
}
protected virtual void PerformSelection()
{
CarouselItem nextToSelect =
Children.Skip(lastSelectedIndex).FirstOrDefault(i => !i.Filtered.Value) ??
Children.Reverse().Skip(InternalChildren.Count - lastSelectedIndex).FirstOrDefault(i => !i.Filtered.Value);
CarouselItem nextToSelect = GetNextToSelect();
if (nextToSelect != null)
nextToSelect.State.Value = CarouselItemState.Selected;

View File

@ -0,0 +1,92 @@
// 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;
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Rulesets;
namespace osu.Game.Screens.Select
{
public class DifficultyRecommender : Component, IOnlineComponent
{
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private RulesetStore rulesets { get; set; }
[Resolved]
private Bindable<RulesetInfo> ruleset { get; set; }
private readonly Dictionary<RulesetInfo, double> recommendedStarDifficulty = new Dictionary<RulesetInfo, double>();
[BackgroundDependencyLoader]
private void load()
{
api.Register(this);
}
/// <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>
public BeatmapInfo GetRecommendedBeatmap(IEnumerable<BeatmapInfo> beatmaps)
{
if (recommendedStarDifficulty.TryGetValue(ruleset.Value, out var stars))
{
return beatmaps.OrderBy(b =>
{
var difference = b.StarDifficulty - stars;
return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder
}).FirstOrDefault();
}
return null;
}
private void calculateRecommendedDifficulties()
{
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;
};
api.Queue(req);
});
}
public void APIStateChanged(IAPIProvider api, APIState state)
{
switch (state)
{
case APIState.Online:
calculateRecommendedDifficulties();
break;
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
api.Unregister(this);
}
}
}

View File

@ -81,6 +81,8 @@ public abstract class SongSelect : OsuScreen, IKeyBindingHandler<GlobalAction>
protected BeatmapCarousel Carousel { get; private set; }
private DifficultyRecommender recommender;
private BeatmapInfoWedge beatmapInfoWedge;
private DialogOverlay dialogOverlay;
@ -109,6 +111,7 @@ private void load(AudioManager audio, DialogOverlay dialog, OsuColour colours, S
AddRangeInternal(new Drawable[]
{
recommender = new DifficultyRecommender(),
new ResetScrollContainer(() => Carousel.ScrollToSelected())
{
RelativeSizeAxes = Axes.Y,
@ -156,6 +159,7 @@ private void load(AudioManager audio, DialogOverlay dialog, OsuColour colours, S
RelativeSizeAxes = Axes.Both,
SelectionChanged = updateSelectedBeatmap,
BeatmapSetsChanged = carouselBeatmapsLoaded,
GetRecommendedBeatmap = recommender.GetRecommendedBeatmap,
},
}
},