mirror of
https://github.com/osukey/osukey.git
synced 2025-06-08 21:07:59 +09:00
Merge pull request #8444 from LittleEndu/select-recommended
Make beatmap carousel select recommended difficulties
This commit is contained in:
commit
33f7e429a8
@ -54,6 +54,35 @@ namespace osu.Game.Tests.Visual.SongSelect
|
|||||||
this.rulesets = 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>
|
/// <summary>
|
||||||
/// Test keyboard traversal
|
/// Test keyboard traversal
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -48,6 +48,11 @@ namespace osu.Game.Screens.Select
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public BeatmapSetInfo SelectedBeatmapSet => selectedBeatmapSet?.BeatmapSet;
|
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;
|
private CarouselBeatmapSet selectedBeatmapSet;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -116,6 +121,7 @@ namespace osu.Game.Screens.Select
|
|||||||
private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>();
|
private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>();
|
||||||
|
|
||||||
protected List<DrawableCarouselItem> Items = new List<DrawableCarouselItem>();
|
protected List<DrawableCarouselItem> Items = new List<DrawableCarouselItem>();
|
||||||
|
|
||||||
private CarouselRoot root;
|
private CarouselRoot root;
|
||||||
|
|
||||||
public BeatmapCarousel()
|
public BeatmapCarousel()
|
||||||
@ -579,7 +585,10 @@ namespace osu.Game.Screens.Select
|
|||||||
b.Metadata = beatmapSet.Metadata;
|
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)
|
foreach (var c in set.Beatmaps)
|
||||||
{
|
{
|
||||||
|
@ -16,6 +16,8 @@ namespace osu.Game.Screens.Select.Carousel
|
|||||||
|
|
||||||
public BeatmapSetInfo BeatmapSet;
|
public BeatmapSetInfo BeatmapSet;
|
||||||
|
|
||||||
|
public Func<IEnumerable<BeatmapInfo>, BeatmapInfo> GetRecommendedBeatmap;
|
||||||
|
|
||||||
public CarouselBeatmapSet(BeatmapSetInfo beatmapSet)
|
public CarouselBeatmapSet(BeatmapSetInfo beatmapSet)
|
||||||
{
|
{
|
||||||
BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet));
|
BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet));
|
||||||
@ -28,6 +30,17 @@ namespace osu.Game.Screens.Select.Carousel
|
|||||||
|
|
||||||
protected override DrawableCarouselItem CreateDrawableRepresentation() => new DrawableCarouselBeatmapSet(this);
|
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)
|
public override int CompareTo(FilterCriteria criteria, CarouselItem other)
|
||||||
{
|
{
|
||||||
if (!(other is CarouselBeatmapSet otherSet))
|
if (!(other is CarouselBeatmapSet otherSet))
|
||||||
|
@ -90,11 +90,15 @@ namespace osu.Game.Screens.Select.Carousel
|
|||||||
PerformSelection();
|
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()
|
protected virtual void PerformSelection()
|
||||||
{
|
{
|
||||||
CarouselItem nextToSelect =
|
CarouselItem nextToSelect = GetNextToSelect();
|
||||||
Children.Skip(lastSelectedIndex).FirstOrDefault(i => !i.Filtered.Value) ??
|
|
||||||
Children.Reverse().Skip(InternalChildren.Count - lastSelectedIndex).FirstOrDefault(i => !i.Filtered.Value);
|
|
||||||
|
|
||||||
if (nextToSelect != null)
|
if (nextToSelect != null)
|
||||||
nextToSelect.State.Value = CarouselItemState.Selected;
|
nextToSelect.State.Value = CarouselItemState.Selected;
|
||||||
|
92
osu.Game/Screens/Select/DifficultyRecommender.cs
Normal file
92
osu.Game/Screens/Select/DifficultyRecommender.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -81,6 +81,8 @@ namespace osu.Game.Screens.Select
|
|||||||
|
|
||||||
protected BeatmapCarousel Carousel { get; private set; }
|
protected BeatmapCarousel Carousel { get; private set; }
|
||||||
|
|
||||||
|
private DifficultyRecommender recommender;
|
||||||
|
|
||||||
private BeatmapInfoWedge beatmapInfoWedge;
|
private BeatmapInfoWedge beatmapInfoWedge;
|
||||||
private DialogOverlay dialogOverlay;
|
private DialogOverlay dialogOverlay;
|
||||||
|
|
||||||
@ -109,6 +111,7 @@ namespace osu.Game.Screens.Select
|
|||||||
|
|
||||||
AddRangeInternal(new Drawable[]
|
AddRangeInternal(new Drawable[]
|
||||||
{
|
{
|
||||||
|
recommender = new DifficultyRecommender(),
|
||||||
new ResetScrollContainer(() => Carousel.ScrollToSelected())
|
new ResetScrollContainer(() => Carousel.ScrollToSelected())
|
||||||
{
|
{
|
||||||
RelativeSizeAxes = Axes.Y,
|
RelativeSizeAxes = Axes.Y,
|
||||||
@ -156,6 +159,7 @@ namespace osu.Game.Screens.Select
|
|||||||
RelativeSizeAxes = Axes.Both,
|
RelativeSizeAxes = Axes.Both,
|
||||||
SelectionChanged = updateSelectedBeatmap,
|
SelectionChanged = updateSelectedBeatmap,
|
||||||
BeatmapSetsChanged = carouselBeatmapsLoaded,
|
BeatmapSetsChanged = carouselBeatmapsLoaded,
|
||||||
|
GetRecommendedBeatmap = recommender.GetRecommendedBeatmap,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user