From a59583ee0901c3086059cd38b44ffcb86c7729b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Mon, 20 Dec 2021 13:18:02 +0100 Subject: [PATCH] Add extension method for returning next playlist item --- .../OnlinePlay/PlaylistExtensionsTest.cs | 72 +++++++++++++++++++ osu.Game/Online/Rooms/PlaylistExtensions.cs | 10 +++ 2 files changed, 82 insertions(+) create mode 100644 osu.Game.Tests/OnlinePlay/PlaylistExtensionsTest.cs diff --git a/osu.Game.Tests/OnlinePlay/PlaylistExtensionsTest.cs b/osu.Game.Tests/OnlinePlay/PlaylistExtensionsTest.cs new file mode 100644 index 0000000000..d2ee47bc23 --- /dev/null +++ b/osu.Game.Tests/OnlinePlay/PlaylistExtensionsTest.cs @@ -0,0 +1,72 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using NUnit.Framework; +using osu.Game.Online.Rooms; + +namespace osu.Game.Tests.OnlinePlay +{ + [TestFixture] + public class PlaylistExtensionsTest + { + [Test] + public void TestPlaylistItemsInOrder() + { + var items = new[] + { + new PlaylistItem { ID = 1, BeatmapID = 1001, PlaylistOrder = 1 }, + new PlaylistItem { ID = 2, BeatmapID = 1002, PlaylistOrder = 2 }, + new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 }, + }; + + var nextItem = items.GetNextItem(); + + Assert.That(nextItem, Is.EqualTo(items[0])); + } + + [Test] + public void TestPlaylistItemsOutOfOrder() + { + var items = new[] + { + new PlaylistItem { ID = 2, BeatmapID = 1002, PlaylistOrder = 2 }, + new PlaylistItem { ID = 1, BeatmapID = 1001, PlaylistOrder = 1 }, + new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 }, + }; + + var nextItem = items.GetNextItem(); + + Assert.That(nextItem, Is.EqualTo(items[1])); + } + + [Test] + public void TestExpiredPlaylistItemsSkipped() + { + 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 }, + }; + + var nextItem = items.GetNextItem(); + + Assert.That(nextItem, Is.EqualTo(items[2])); + } + + [Test] + public void TestAllItemsExpired() + { + 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 }, + }; + + var nextItem = items.GetNextItem(); + + Assert.That(nextItem, Is.Null); + } + } +} diff --git a/osu.Game/Online/Rooms/PlaylistExtensions.cs b/osu.Game/Online/Rooms/PlaylistExtensions.cs index 992011da3c..e900dc53ef 100644 --- a/osu.Game/Online/Rooms/PlaylistExtensions.cs +++ b/osu.Game/Online/Rooms/PlaylistExtensions.cs @@ -1,6 +1,9 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +#nullable enable + +using System.Collections.Generic; using System.Linq; using Humanizer; using Humanizer.Localisation; @@ -10,6 +13,13 @@ namespace osu.Game.Online.Rooms { public static class PlaylistExtensions { + /// + /// Returns the next to be played from the supplied , + /// or if all items are expired. + /// + public static PlaylistItem? GetNextItem(this IEnumerable playlist) => + playlist.OrderBy(item => item.PlaylistOrder).FirstOrDefault(item => !item.Expired); + public static string GetTotalDuration(this BindableList playlist) => playlist.Select(p => p.Beatmap.Value.Length).Sum().Milliseconds().Humanize(minUnit: TimeUnit.Second, maxUnit: TimeUnit.Hour, precision: 2); }