Return last playlist item if all expired

This commit is contained in:
Bartłomiej Dach
2021-12-20 13:42:49 +01:00
parent a5a9922f81
commit 0975f570ba
2 changed files with 32 additions and 11 deletions

View File

@ -1,6 +1,7 @@
// 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.
using System;
using NUnit.Framework; using NUnit.Framework;
using osu.Game.Online.Rooms; using osu.Game.Online.Rooms;
@ -9,6 +10,17 @@ namespace osu.Game.Tests.OnlinePlay
[TestFixture] [TestFixture]
public class PlaylistExtensionsTest public class PlaylistExtensionsTest
{ {
[Test]
public void TestEmpty()
{
// mostly an extreme edge case, i.e. during room creation.
var items = Array.Empty<PlaylistItem>();
var currentItem = items.GetCurrentItem();
Assert.That(currentItem, Is.Null);
}
[Test] [Test]
public void TestPlaylistItemsInOrder() public void TestPlaylistItemsInOrder()
{ {
@ -19,9 +31,9 @@ namespace osu.Game.Tests.OnlinePlay
new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 }, new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 },
}; };
var nextItem = items.GetCurrentItem(); var currentItem = items.GetCurrentItem();
Assert.That(nextItem, Is.EqualTo(items[0])); Assert.That(currentItem, Is.EqualTo(items[0]));
} }
[Test] [Test]
@ -34,9 +46,9 @@ namespace osu.Game.Tests.OnlinePlay
new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 }, new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 },
}; };
var nextItem = items.GetCurrentItem(); var currentItem = items.GetCurrentItem();
Assert.That(nextItem, Is.EqualTo(items[1])); Assert.That(currentItem, Is.EqualTo(items[1]));
} }
[Test] [Test]
@ -49,9 +61,9 @@ namespace osu.Game.Tests.OnlinePlay
new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 }, new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3 },
}; };
var nextItem = items.GetCurrentItem(); var currentItem = items.GetCurrentItem();
Assert.That(nextItem, Is.EqualTo(items[2])); Assert.That(currentItem, Is.EqualTo(items[2]));
} }
[Test] [Test]
@ -64,9 +76,10 @@ namespace osu.Game.Tests.OnlinePlay
new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3, Expired = true }, new PlaylistItem { ID = 3, BeatmapID = 1003, PlaylistOrder = 3, Expired = true },
}; };
var nextItem = items.GetCurrentItem(); var currentItem = items.GetCurrentItem();
Assert.That(nextItem, Is.Null); // if all items are expired, the last-played item is expected to be returned.
Assert.That(currentItem, Is.EqualTo(items[2]));
} }
} }
} }

View File

@ -15,10 +15,18 @@ namespace osu.Game.Online.Rooms
{ {
/// <summary> /// <summary>
/// Returns the first non-expired <see cref="PlaylistItem"/> in playlist order from the supplied <paramref name="playlist"/>, /// Returns the first non-expired <see cref="PlaylistItem"/> in playlist order from the supplied <paramref name="playlist"/>,
/// or <see langword="null"/> if all items are expired. /// or the last-played <see cref="PlaylistItem"/> if all items are expired,
/// or <see langword="null"/> if <paramref name="playlist"/> was empty.
/// </summary> /// </summary>
public static PlaylistItem? GetCurrentItem(this IEnumerable<PlaylistItem> playlist) => public static PlaylistItem? GetCurrentItem(this ICollection<PlaylistItem> playlist)
playlist.OrderBy(item => item.PlaylistOrder).FirstOrDefault(item => !item.Expired); {
if (playlist.Count == 0)
return null;
return playlist.All(item => item.Expired)
? playlist.OrderByDescending(item => item.PlaylistOrder).First()
: playlist.OrderBy(item => item.PlaylistOrder).First(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);