1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 07:20:24 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

183 lines
7.0 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2017-12-12 16:48:38 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2017-12-14 19:40:58 +08:00
using osu.Framework.Extensions.IEnumerableExtensions;
2017-12-12 16:48:38 +08:00
using osu.Game.Beatmaps;
2017-12-14 19:40:58 +08:00
using osu.Game.Screens.Select.Filter;
using osu.Game.Utils;
2018-04-13 17:19:50 +08:00
2017-12-12 16:48:38 +08:00
namespace osu.Game.Screens.Select.Carousel
{
public class CarouselBeatmapSet : CarouselGroupEagerSelect
{
public override float TotalHeight
{
get
{
switch (State.Value)
{
case CarouselItemState.Selected:
return DrawableCarouselBeatmapSet.HEIGHT + Items.Count(c => c.Visible) * DrawableCarouselBeatmap.HEIGHT;
default:
return DrawableCarouselBeatmapSet.HEIGHT;
}
}
}
public IEnumerable<CarouselBeatmap> Beatmaps => Items.OfType<CarouselBeatmap>();
2018-04-13 17:19:50 +08:00
2017-12-12 16:48:38 +08:00
public BeatmapSetInfo BeatmapSet;
2018-04-13 17:19:50 +08:00
2023-01-09 02:02:48 +08:00
public Func<IEnumerable<BeatmapInfo>, BeatmapInfo?>? GetRecommendedBeatmap;
public CarouselBeatmapSet(BeatmapSetInfo beatmapSet)
2017-12-12 16:48:38 +08:00
{
2017-12-14 19:40:58 +08:00
BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet));
2018-04-13 17:19:50 +08:00
2017-12-14 19:40:58 +08:00
beatmapSet.Beatmaps
.Where(b => !b.Hidden)
.OrderBy(b => b.Ruleset)
.ThenBy(b => b.StarRating)
2017-12-14 19:40:58 +08:00
.Select(b => new CarouselBeatmap(b))
.ForEach(AddItem);
2017-12-12 16:48:38 +08:00
}
2018-04-13 17:19:50 +08:00
public override CarouselItem? GetNextToSelect()
2020-03-26 06:19:54 +08:00
{
if (LastSelected == null || LastSelected.Filtered.Value)
2020-03-26 06:19:54 +08:00
{
if (GetRecommendedBeatmap?.Invoke(Items.OfType<CarouselBeatmap>().Where(b => !b.Filtered.Value).Select(b => b.BeatmapInfo)) is BeatmapInfo recommended)
return Items.OfType<CarouselBeatmap>().First(b => b.BeatmapInfo.Equals(recommended));
2020-03-26 06:19:54 +08:00
}
return base.GetNextToSelect();
}
2017-12-14 19:40:58 +08:00
public override int CompareTo(FilterCriteria criteria, CarouselItem other)
2017-12-12 16:48:38 +08:00
{
2017-12-14 19:40:58 +08:00
if (!(other is CarouselBeatmapSet otherSet))
return base.CompareTo(criteria, other);
2018-04-13 17:19:50 +08:00
int comparison;
2017-12-14 19:40:58 +08:00
switch (criteria.Sort)
2017-12-12 16:48:38 +08:00
{
default:
case SortMode.Artist:
comparison = OrdinalSortByCaseStringComparer.INSTANCE.Compare(BeatmapSet.Metadata.Artist, otherSet.BeatmapSet.Metadata.Artist);
break;
2019-04-01 11:44:46 +08:00
2017-12-12 16:48:38 +08:00
case SortMode.Title:
comparison = OrdinalSortByCaseStringComparer.INSTANCE.Compare(BeatmapSet.Metadata.Title, otherSet.BeatmapSet.Metadata.Title);
break;
2019-04-01 11:44:46 +08:00
2017-12-12 16:48:38 +08:00
case SortMode.Author:
comparison = OrdinalSortByCaseStringComparer.INSTANCE.Compare(BeatmapSet.Metadata.Author.Username, otherSet.BeatmapSet.Metadata.Author.Username);
break;
2019-04-01 11:44:46 +08:00
2021-05-10 05:26:45 +08:00
case SortMode.Source:
comparison = OrdinalSortByCaseStringComparer.INSTANCE.Compare(BeatmapSet.Metadata.Source, otherSet.BeatmapSet.Metadata.Source);
break;
2021-05-10 05:26:45 +08:00
case SortMode.DateAdded:
comparison = otherSet.BeatmapSet.DateAdded.CompareTo(BeatmapSet.DateAdded);
break;
case SortMode.DateRanked:
comparison = Nullable.Compare(otherSet.BeatmapSet.DateRanked, BeatmapSet.DateRanked);
break;
case SortMode.LastPlayed:
comparison = -compareUsingAggregateMax(otherSet, static b => (b.LastPlayed ?? DateTimeOffset.MinValue).ToUnixTimeSeconds());
break;
2019-07-07 23:14:23 +08:00
case SortMode.BPM:
comparison = compareUsingAggregateMax(otherSet, static b => b.BPM);
break;
2019-07-07 23:14:23 +08:00
2019-07-07 23:26:56 +08:00
case SortMode.Length:
comparison = compareUsingAggregateMax(otherSet, static b => b.Length);
break;
2019-07-07 23:26:56 +08:00
2017-12-12 16:48:38 +08:00
case SortMode.Difficulty:
comparison = compareUsingAggregateMax(otherSet, static b => b.StarRating);
break;
2022-08-22 11:09:33 +08:00
case SortMode.DateSubmitted:
comparison = Nullable.Compare(otherSet.BeatmapSet.DateSubmitted, BeatmapSet.DateSubmitted);
break;
2017-12-14 19:40:58 +08:00
}
if (comparison != 0) return comparison;
2023-01-18 18:13:26 +08:00
// If the initial sort could not differentiate, attempt to use DateAdded to order sets in a stable fashion.
// The directionality of this matches the current SortMode.DateAdded, but we may want to reconsider if that becomes a user decision (ie. asc / desc).
comparison = otherSet.BeatmapSet.DateAdded.CompareTo(BeatmapSet.DateAdded);
if (comparison != 0) return comparison;
2023-01-19 00:12:57 +08:00
// If DateAdded fails to break the tie, fallback to our internal GUID for stability.
// This basically means it's a stable random sort.
return otherSet.BeatmapSet.ID.CompareTo(BeatmapSet.ID);
}
/// <summary>
/// All beatmaps which are not filtered and valid for display.
/// </summary>
protected IEnumerable<BeatmapInfo> ValidBeatmaps
{
get
{
foreach (var item in Items) // iterating over Items directly to not allocate 2 enumerators
{
if (item is CarouselBeatmap b && (!b.Filtered.Value || b.State.Value == CarouselItemState.Selected))
yield return b.BeatmapInfo;
}
}
}
/// <summary>
/// Whether there are available beatmaps which are not filtered and valid for display.
/// Cheaper alternative to <see cref="ValidBeatmaps"/>.Any()
/// </summary>
public bool HasValidBeatmaps
{
get
{
foreach (var item in Items) // iterating over Items directly to not allocate 2 enumerators
{
if (item is CarouselBeatmap b && (!b.Filtered.Value || b.State.Value == CarouselItemState.Selected))
return true;
}
return false;
}
}
private int compareUsingAggregateMax(CarouselBeatmapSet other, Func<BeatmapInfo, double> func)
{
bool ourBeatmaps = HasValidBeatmaps;
bool otherBeatmaps = other.HasValidBeatmaps;
if (!ourBeatmaps && !otherBeatmaps) return 0;
if (!ourBeatmaps) return -1;
if (!otherBeatmaps) return 1;
return ValidBeatmaps.Max(func).CompareTo(other.ValidBeatmaps.Max(func));
}
2017-12-14 19:40:58 +08:00
public override void Filter(FilterCriteria criteria)
{
base.Filter(criteria);
2022-08-22 15:39:46 +08:00
Filtered.Value = Items.All(i => i.Filtered.Value);
2017-12-12 16:48:38 +08:00
}
2018-04-13 17:19:50 +08:00
public override string ToString() => BeatmapSet.ToString();
2017-12-12 16:48:38 +08:00
}
}