Tidy up function passing, naming, ordering etc.

This commit is contained in:
Dean Herbert
2020-04-11 16:58:13 +09:00
parent 7f753f6b4d
commit abea7b5299
4 changed files with 42 additions and 36 deletions

View File

@ -49,6 +49,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>
@ -120,8 +125,6 @@ namespace osu.Game.Screens.Select
private CarouselRoot root; private CarouselRoot root;
public DifficultyRecommender DifficultyRecommender;
public BeatmapCarousel() public BeatmapCarousel()
{ {
root = new CarouselRoot(this); root = new CarouselRoot(this);
@ -586,12 +589,10 @@ namespace osu.Game.Screens.Select
b.Metadata = beatmapSet.Metadata; b.Metadata = beatmapSet.Metadata;
} }
BeatmapInfo recommender(IEnumerable<BeatmapInfo> beatmaps) var set = new CarouselBeatmapSet(beatmapSet)
{ {
return DifficultyRecommender?.GetRecommendedBeatmap(beatmaps); GetRecommendedBeatmap = beatmaps => GetRecommendedBeatmap?.Invoke(beatmaps)
} };
var set = new CarouselBeatmapSet(beatmapSet, recommender);
foreach (var c in set.Beatmaps) foreach (var c in set.Beatmaps)
{ {

View File

@ -12,13 +12,13 @@ namespace osu.Game.Screens.Select.Carousel
{ {
public class CarouselBeatmapSet : CarouselGroupEagerSelect public class CarouselBeatmapSet : CarouselGroupEagerSelect
{ {
private readonly 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, Func<IEnumerable<BeatmapInfo>, BeatmapInfo> getRecommendedBeatmap) public Func<IEnumerable<BeatmapInfo>, BeatmapInfo> GetRecommendedBeatmap;
public CarouselBeatmapSet(BeatmapSetInfo beatmapSet)
{ {
BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet)); BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet));
@ -26,8 +26,6 @@ namespace osu.Game.Screens.Select.Carousel
.Where(b => !b.Hidden) .Where(b => !b.Hidden)
.Select(b => new CarouselBeatmap(b)) .Select(b => new CarouselBeatmap(b))
.ForEach(AddChild); .ForEach(AddChild);
this.getRecommendedBeatmap = getRecommendedBeatmap;
} }
protected override DrawableCarouselItem CreateDrawableRepresentation() => new DrawableCarouselBeatmapSet(this); protected override DrawableCarouselItem CreateDrawableRepresentation() => new DrawableCarouselBeatmapSet(this);
@ -36,9 +34,8 @@ namespace osu.Game.Screens.Select.Carousel
{ {
if (LastSelected == null) if (LastSelected == null)
{ {
var recommendedBeatmapInfo = getRecommendedBeatmap(Children.OfType<CarouselBeatmap>().Where(b => !b.Filtered.Value).Select(b => b.Beatmap)); if (GetRecommendedBeatmap?.Invoke(Children.OfType<CarouselBeatmap>().Where(b => !b.Filtered.Value).Select(b => b.Beatmap)) is BeatmapInfo recommended)
if (recommendedBeatmapInfo != null) return Children.OfType<CarouselBeatmap>().First(b => b.Beatmap == recommended);
return Children.OfType<CarouselBeatmap>().First(b => b.Beatmap == recommendedBeatmapInfo);
} }
return base.GetNextToSelect(); return base.GetNextToSelect();

View File

@ -33,10 +33,33 @@ namespace osu.Game.Screens.Select
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
updateRecommended(); calculateRecommendedDifficulties();
} }
private void updateRecommended() /// <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.ContainsKey(ruleset.Value))
{
calculateRecommendedDifficulties();
return null;
}
return beatmaps.OrderBy(b =>
{
var difference = b.StarDifficulty - recommendedStarDifficulty[ruleset.Value];
return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder
}).FirstOrDefault();
}
private void calculateRecommendedDifficulties()
{ {
if (pendingAPIRequests > 0) if (pendingAPIRequests > 0)
return; return;
@ -60,20 +83,5 @@ namespace osu.Game.Screens.Select
api.Queue(req); api.Queue(req);
}); });
} }
public BeatmapInfo GetRecommendedBeatmap(IEnumerable<BeatmapInfo> beatmaps)
{
if (!recommendedStarDifficulty.ContainsKey(ruleset.Value))
{
updateRecommended();
return null;
}
return beatmaps.OrderBy(b =>
{
var difference = b.StarDifficulty - recommendedStarDifficulty[ruleset.Value];
return difference >= 0 ? difference * 2 : difference * -1; // prefer easier over harder
}).FirstOrDefault();
}
} }
} }

View File

@ -80,7 +80,8 @@ 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 DifficultyRecommender recommender;
private BeatmapInfoWedge beatmapInfoWedge; private BeatmapInfoWedge beatmapInfoWedge;
private DialogOverlay dialogOverlay; private DialogOverlay dialogOverlay;
@ -108,10 +109,9 @@ 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[]
{ {
recommender = new DifficultyRecommender(),
new ResetScrollContainer(() => Carousel.ScrollToSelected()) new ResetScrollContainer(() => Carousel.ScrollToSelected())
{ {
RelativeSizeAxes = Axes.Y, RelativeSizeAxes = Axes.Y,
@ -159,7 +159,7 @@ namespace osu.Game.Screens.Select
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
SelectionChanged = updateSelectedBeatmap, SelectionChanged = updateSelectedBeatmap,
BeatmapSetsChanged = carouselBeatmapsLoaded, BeatmapSetsChanged = carouselBeatmapsLoaded,
DifficultyRecommender = difficultyRecommender, GetRecommendedBeatmap = recommender.GetRecommendedBeatmap,
}, },
} }
}, },