1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 16:32:54 +08:00

Add extension method for returning next playlist item

This commit is contained in:
Bartłomiej Dach 2021-12-20 13:18:02 +01:00
parent 6bbc9ccf97
commit a59583ee09
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
2 changed files with 82 additions and 0 deletions

View 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);
}
}
}

View File

@ -1,6 +1,9 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable enable
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Humanizer; using Humanizer;
using Humanizer.Localisation; using Humanizer.Localisation;
@ -10,6 +13,13 @@ namespace osu.Game.Online.Rooms
{ {
public static class PlaylistExtensions 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) => 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); playlist.Select(p => p.Beatmap.Value.Length).Sum().Milliseconds().Humanize(minUnit: TimeUnit.Second, maxUnit: TimeUnit.Hour, precision: 2);
} }