2019-01-24 17:43:03 +09:00
|
|
|
// 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 18:19:50 +09:00
|
|
|
|
2017-12-12 17:48:38 +09:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2017-12-14 20:40:58 +09:00
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2017-12-12 17:48:38 +09:00
|
|
|
using osu.Game.Beatmaps;
|
2017-12-14 20:40:58 +09:00
|
|
|
using osu.Game.Screens.Select.Filter;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2017-12-12 17:48:38 +09:00
|
|
|
namespace osu.Game.Screens.Select.Carousel
|
|
|
|
{
|
|
|
|
public class CarouselBeatmapSet : CarouselGroupEagerSelect
|
|
|
|
{
|
2020-10-12 14:23:18 +09:00
|
|
|
public override float TotalHeight
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
switch (State.Value)
|
|
|
|
{
|
|
|
|
case CarouselItemState.Selected:
|
2022-07-21 16:06:06 +09:00
|
|
|
return DrawableCarouselBeatmapSet.HEIGHT + Items.Count(c => c.Visible) * DrawableCarouselBeatmap.HEIGHT;
|
2020-10-12 14:23:18 +09:00
|
|
|
|
|
|
|
default:
|
|
|
|
return DrawableCarouselBeatmapSet.HEIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 12:37:41 +09:00
|
|
|
|
2022-07-21 16:06:06 +09:00
|
|
|
public IEnumerable<CarouselBeatmap> Beatmaps => Items.OfType<CarouselBeatmap>();
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2017-12-12 17:48:38 +09:00
|
|
|
public BeatmapSetInfo BeatmapSet;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2023-01-08 19:02:48 +01:00
|
|
|
public Func<IEnumerable<BeatmapInfo>, BeatmapInfo?>? GetRecommendedBeatmap;
|
2020-04-11 16:58:13 +09:00
|
|
|
|
|
|
|
public CarouselBeatmapSet(BeatmapSetInfo beatmapSet)
|
2017-12-12 17:48:38 +09:00
|
|
|
{
|
2017-12-14 20:40:58 +09:00
|
|
|
BeatmapSet = beatmapSet ?? throw new ArgumentNullException(nameof(beatmapSet));
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2017-12-14 20:40:58 +09:00
|
|
|
beatmapSet.Beatmaps
|
|
|
|
.Where(b => !b.Hidden)
|
2022-01-27 15:59:20 +09:00
|
|
|
.OrderBy(b => b.Ruleset)
|
2022-01-07 22:57:22 +09:00
|
|
|
.ThenBy(b => b.StarRating)
|
2017-12-14 20:40:58 +09:00
|
|
|
.Select(b => new CarouselBeatmap(b))
|
2022-07-21 16:06:06 +09:00
|
|
|
.ForEach(AddItem);
|
2017-12-12 17:48:38 +09:00
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2023-01-08 19:02:48 +01:00
|
|
|
protected override CarouselItem? GetNextToSelect()
|
2020-03-26 00:19:54 +02:00
|
|
|
{
|
2020-12-03 23:13:14 +02:00
|
|
|
if (LastSelected == null || LastSelected.Filtered.Value)
|
2020-03-26 00:19:54 +02:00
|
|
|
{
|
2022-07-21 16:06:06 +09: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 00:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return base.GetNextToSelect();
|
|
|
|
}
|
|
|
|
|
2017-12-14 20:40:58 +09:00
|
|
|
public override int CompareTo(FilterCriteria criteria, CarouselItem other)
|
2017-12-12 17:48:38 +09:00
|
|
|
{
|
2017-12-14 20:40:58 +09:00
|
|
|
if (!(other is CarouselBeatmapSet otherSet))
|
|
|
|
return base.CompareTo(criteria, other);
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2023-03-03 16:28:09 +09:00
|
|
|
int comparison;
|
2023-01-17 16:36:47 +09:00
|
|
|
|
2017-12-14 20:40:58 +09:00
|
|
|
switch (criteria.Sort)
|
2017-12-12 17:48:38 +09:00
|
|
|
{
|
|
|
|
default:
|
|
|
|
case SortMode.Artist:
|
2023-01-17 16:36:47 +09:00
|
|
|
comparison = string.Compare(BeatmapSet.Metadata.Artist, otherSet.BeatmapSet.Metadata.Artist, StringComparison.OrdinalIgnoreCase);
|
|
|
|
break;
|
2019-04-01 12:44:46 +09:00
|
|
|
|
2017-12-12 17:48:38 +09:00
|
|
|
case SortMode.Title:
|
2023-01-17 16:36:47 +09:00
|
|
|
comparison = string.Compare(BeatmapSet.Metadata.Title, otherSet.BeatmapSet.Metadata.Title, StringComparison.OrdinalIgnoreCase);
|
|
|
|
break;
|
2019-04-01 12:44:46 +09:00
|
|
|
|
2017-12-12 17:48:38 +09:00
|
|
|
case SortMode.Author:
|
2023-01-17 16:36:47 +09:00
|
|
|
comparison = string.Compare(BeatmapSet.Metadata.Author.Username, otherSet.BeatmapSet.Metadata.Author.Username, StringComparison.OrdinalIgnoreCase);
|
|
|
|
break;
|
2019-04-01 12:44:46 +09:00
|
|
|
|
2021-05-09 14:26:45 -07:00
|
|
|
case SortMode.Source:
|
2023-01-17 16:36:47 +09:00
|
|
|
comparison = string.Compare(BeatmapSet.Metadata.Source, otherSet.BeatmapSet.Metadata.Source, StringComparison.OrdinalIgnoreCase);
|
|
|
|
break;
|
2021-05-09 14:26:45 -07:00
|
|
|
|
2019-06-05 18:17:43 +09:00
|
|
|
case SortMode.DateAdded:
|
2023-01-17 16:36:47 +09:00
|
|
|
comparison = otherSet.BeatmapSet.DateAdded.CompareTo(BeatmapSet.DateAdded);
|
|
|
|
break;
|
2019-06-05 18:17:43 +09:00
|
|
|
|
2022-07-19 19:37:04 +09:00
|
|
|
case SortMode.DateRanked:
|
2023-03-06 15:16:31 +09:00
|
|
|
comparison = Nullable.Compare(otherSet.BeatmapSet.DateRanked, BeatmapSet.DateRanked);
|
2023-01-17 16:36:47 +09:00
|
|
|
break;
|
2022-07-19 19:37:04 +09:00
|
|
|
|
2022-07-13 16:37:05 +09:00
|
|
|
case SortMode.LastPlayed:
|
2023-01-17 16:36:47 +09:00
|
|
|
comparison = -compareUsingAggregateMax(otherSet, b => (b.LastPlayed ?? DateTimeOffset.MinValue).ToUnixTimeSeconds());
|
|
|
|
break;
|
2022-07-13 16:37:05 +09:00
|
|
|
|
2019-07-07 18:14:23 +03:00
|
|
|
case SortMode.BPM:
|
2023-01-17 16:36:47 +09:00
|
|
|
comparison = compareUsingAggregateMax(otherSet, b => b.BPM);
|
|
|
|
break;
|
2019-07-07 18:14:23 +03:00
|
|
|
|
2019-07-07 18:26:56 +03:00
|
|
|
case SortMode.Length:
|
2023-01-17 16:36:47 +09:00
|
|
|
comparison = compareUsingAggregateMax(otherSet, b => b.Length);
|
|
|
|
break;
|
2019-07-07 18:26:56 +03:00
|
|
|
|
2017-12-12 17:48:38 +09:00
|
|
|
case SortMode.Difficulty:
|
2023-01-17 16:36:47 +09:00
|
|
|
comparison = compareUsingAggregateMax(otherSet, b => b.StarRating);
|
|
|
|
break;
|
2022-08-21 23:09:33 -04:00
|
|
|
|
|
|
|
case SortMode.DateSubmitted:
|
2023-03-06 15:16:31 +09:00
|
|
|
comparison = Nullable.Compare(otherSet.BeatmapSet.DateSubmitted, BeatmapSet.DateSubmitted);
|
2023-01-17 16:36:47 +09:00
|
|
|
break;
|
2017-12-14 20:40:58 +09:00
|
|
|
}
|
2023-01-17 16:36:47 +09:00
|
|
|
|
|
|
|
if (comparison != 0) return comparison;
|
|
|
|
|
2023-01-18 19:13:26 +09: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).
|
2023-01-17 16:36:47 +09:00
|
|
|
comparison = otherSet.BeatmapSet.DateAdded.CompareTo(BeatmapSet.DateAdded);
|
|
|
|
|
|
|
|
if (comparison != 0) return comparison;
|
|
|
|
|
2023-01-18 17:12:57 +01:00
|
|
|
// If DateAdded fails to break the tie, fallback to our internal GUID for stability.
|
2023-01-17 16:36:47 +09:00
|
|
|
// This basically means it's a stable random sort.
|
|
|
|
return otherSet.BeatmapSet.ID.CompareTo(BeatmapSet.ID);
|
2023-03-03 16:28:09 +09:00
|
|
|
}
|
|
|
|
|
2019-10-07 15:16:26 +09:00
|
|
|
/// <summary>
|
|
|
|
/// All beatmaps which are not filtered and valid for display.
|
|
|
|
/// </summary>
|
2021-10-02 12:44:22 +09:00
|
|
|
protected IEnumerable<BeatmapInfo> ValidBeatmaps => Beatmaps.Where(b => !b.Filtered.Value || b.State.Value == CarouselItemState.Selected).Select(b => b.BeatmapInfo);
|
2019-10-07 15:16:26 +09:00
|
|
|
|
|
|
|
private int compareUsingAggregateMax(CarouselBeatmapSet other, Func<BeatmapInfo, double> func)
|
|
|
|
{
|
2021-10-27 13:04:41 +09:00
|
|
|
bool ourBeatmaps = ValidBeatmaps.Any();
|
|
|
|
bool otherBeatmaps = other.ValidBeatmaps.Any();
|
2019-10-07 15:16:26 +09:00
|
|
|
|
|
|
|
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 20:40:58 +09:00
|
|
|
public override void Filter(FilterCriteria criteria)
|
|
|
|
{
|
|
|
|
base.Filter(criteria);
|
2022-08-22 03:39:46 -04:00
|
|
|
|
2023-03-03 16:28:09 +09:00
|
|
|
Filtered.Value = Items.All(i => i.Filtered.Value);
|
2017-12-12 17:48:38 +09:00
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2017-12-16 16:14:37 +09:00
|
|
|
public override string ToString() => BeatmapSet.ToString();
|
2017-12-12 17:48:38 +09:00
|
|
|
}
|
|
|
|
}
|