mirror of
https://github.com/ppy/osu.git
synced 2025-01-13 13:32:54 +08:00
Add extension method for returning next playlist item
This commit is contained in:
parent
6bbc9ccf97
commit
a59583ee09
72
osu.Game.Tests/OnlinePlay/PlaylistExtensionsTest.cs
Normal file
72
osu.Game.Tests/OnlinePlay/PlaylistExtensionsTest.cs
Normal file
@ -0,0 +1,72 @@
|
||||
// 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.
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
// 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.
|
||||
|
||||
#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
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the next <see cref="PlaylistItem"/> to be played from the supplied <paramref name="playlist"/>,
|
||||
/// or <see langword="null"/> if all items are expired.
|
||||
/// </summary>
|
||||
public static PlaylistItem? GetNextItem(this IEnumerable<PlaylistItem> playlist) =>
|
||||
playlist.OrderBy(item => item.PlaylistOrder).FirstOrDefault(item => !item.Expired);
|
||||
|
||||
public static string GetTotalDuration(this BindableList<PlaylistItem> playlist) =>
|
||||
playlist.Select(p => p.Beatmap.Value.Length).Sum().Milliseconds().Humanize(minUnit: TimeUnit.Second, maxUnit: TimeUnit.Hour, precision: 2);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user