1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 08:52:55 +08:00

Add and use separate extensions for historical and upcoming playlist items

This commit is contained in:
Bartłomiej Dach 2021-12-21 08:01:04 +01:00
parent 0975f570ba
commit bd1fb33ad6
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
2 changed files with 50 additions and 23 deletions

View File

@ -16,9 +16,12 @@ namespace osu.Game.Tests.OnlinePlay
// mostly an extreme edge case, i.e. during room creation.
var items = Array.Empty<PlaylistItem>();
var currentItem = items.GetCurrentItem();
Assert.That(currentItem, Is.Null);
Assert.Multiple(() =>
{
Assert.That(items.GetHistoricalItems(), Is.Empty);
Assert.That(items.GetCurrentItem(), Is.Null);
Assert.That(items.GetUpcomingItems(), Is.Empty);
});
}
[Test]
@ -31,9 +34,12 @@ namespace osu.Game.Tests.OnlinePlay
new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 },
};
var currentItem = items.GetCurrentItem();
Assert.That(currentItem, Is.EqualTo(items[0]));
Assert.Multiple(() =>
{
Assert.That(items.GetHistoricalItems(), Is.Empty);
Assert.That(items.GetCurrentItem(), Is.EqualTo(items[0]));
Assert.That(items.GetUpcomingItems(), Is.EquivalentTo(items));
});
}
[Test]
@ -46,9 +52,12 @@ namespace osu.Game.Tests.OnlinePlay
new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 },
};
var currentItem = items.GetCurrentItem();
Assert.That(currentItem, Is.EqualTo(items[1]));
Assert.Multiple(() =>
{
Assert.That(items.GetHistoricalItems(), Is.Empty);
Assert.That(items.GetCurrentItem(), Is.EqualTo(items[1]));
Assert.That(items.GetUpcomingItems(), Is.EquivalentTo(new[] { items[1], items[0], items[2] }));
});
}
[Test]
@ -56,14 +65,17 @@ namespace osu.Game.Tests.OnlinePlay
{
var items = new[]
{
new PlaylistItem { ID = 2, BeatmapID = 1002, PlaylistOrder = 2, Expired = true },
new PlaylistItem { ID = 1, BeatmapID = 1001, PlaylistOrder = 1, Expired = true },
new PlaylistItem { ID = 1, BeatmapID = 1001, Expired = true, PlayedAt = new DateTimeOffset(2021, 12, 21, 7, 55, 0, TimeSpan.Zero) },
new PlaylistItem { ID = 2, BeatmapID = 1002, Expired = true, PlayedAt = new DateTimeOffset(2021, 12, 21, 7, 53, 0, TimeSpan.Zero) },
new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 },
};
var currentItem = items.GetCurrentItem();
Assert.That(currentItem, Is.EqualTo(items[2]));
Assert.Multiple(() =>
{
Assert.That(items.GetHistoricalItems(), Is.EquivalentTo(new[] { items[1], items[0] }));
Assert.That(items.GetCurrentItem(), Is.EqualTo(items[2]));
Assert.That(items.GetUpcomingItems(), Is.EquivalentTo(new[] { items[2] }));
});
}
[Test]
@ -71,15 +83,18 @@ namespace osu.Game.Tests.OnlinePlay
{
var items = new[]
{
new PlaylistItem { ID = 2, BeatmapID = 1002, PlaylistOrder = 2, Expired = true },
new PlaylistItem { ID = 1, BeatmapID = 1001, PlaylistOrder = 1, Expired = true },
new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3, Expired = true },
new PlaylistItem { ID = 1, BeatmapID = 1001, Expired = true, PlayedAt = new DateTimeOffset(2021, 12, 21, 7, 55, 0, TimeSpan.Zero) },
new PlaylistItem { ID = 2, BeatmapID = 1002, Expired = true, PlayedAt = new DateTimeOffset(2021, 12, 21, 7, 53, 0, TimeSpan.Zero) },
new PlaylistItem { ID = 3, BeatmapID = 1002, Expired = true, PlayedAt = new DateTimeOffset(2021, 12, 21, 7, 57, 0, TimeSpan.Zero) },
};
var currentItem = items.GetCurrentItem();
// if all items are expired, the last-played item is expected to be returned.
Assert.That(currentItem, Is.EqualTo(items[2]));
Assert.Multiple(() =>
{
Assert.That(items.GetHistoricalItems(), Is.EquivalentTo(new[] { items[1], items[0], items[2] }));
// if all items are expired, the last-played item is expected to be returned.
Assert.That(items.GetCurrentItem(), Is.EqualTo(items[2]));
Assert.That(items.GetUpcomingItems(), Is.Empty);
});
}
}
}

View File

@ -13,6 +13,18 @@ namespace osu.Game.Online.Rooms
{
public static class PlaylistExtensions
{
/// <summary>
/// Returns all historical/expired items from the <paramref name="playlist"/>, in the order in which they were played.
/// </summary>
public static IEnumerable<PlaylistItem> GetHistoricalItems(this IEnumerable<PlaylistItem> playlist)
=> playlist.Where(item => item.Expired).OrderBy(item => item.PlayedAt);
/// <summary>
/// Returns all non-expired items from the <paramref name="playlist"/>, in the order in which they are to be played.
/// </summary>
public static IEnumerable<PlaylistItem> GetUpcomingItems(this IEnumerable<PlaylistItem> playlist)
=> playlist.Where(item => !item.Expired).OrderBy(item => item.PlaylistOrder);
/// <summary>
/// Returns the first non-expired <see cref="PlaylistItem"/> in playlist order from the supplied <paramref name="playlist"/>,
/// or the last-played <see cref="PlaylistItem"/> if all items are expired,
@ -24,8 +36,8 @@ namespace osu.Game.Online.Rooms
return null;
return playlist.All(item => item.Expired)
? playlist.OrderByDescending(item => item.PlaylistOrder).First()
: playlist.OrderBy(item => item.PlaylistOrder).First(item => !item.Expired);
? GetHistoricalItems(playlist).Last()
: GetUpcomingItems(playlist).First();
}
public static string GetTotalDuration(this BindableList<PlaylistItem> playlist) =>