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

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
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
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.
// See the LICENCE file in the repository root for full licence text.
using System;
using NUnit.Framework;
using osu.Game.Online.Rooms;
@ -9,6 +10,17 @@ namespace osu.Game.Tests.OnlinePlay
[TestFixture]
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]
public void TestPlaylistItemsInOrder()
{
@ -19,9 +31,9 @@ namespace osu.Game.Tests.OnlinePlay
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]
@ -34,9 +46,9 @@ namespace osu.Game.Tests.OnlinePlay
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]
@ -49,9 +61,9 @@ namespace osu.Game.Tests.OnlinePlay
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]
@ -64,9 +76,10 @@ namespace osu.Game.Tests.OnlinePlay
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>
/// 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>
public static PlaylistItem? GetCurrentItem(this IEnumerable<PlaylistItem> playlist) =>
playlist.OrderBy(item => item.PlaylistOrder).FirstOrDefault(item => !item.Expired);
public static PlaylistItem? GetCurrentItem(this ICollection<PlaylistItem> playlist)
{
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) =>
playlist.Select(p => p.Beatmap.Value.Length).Sum().Milliseconds().Humanize(minUnit: TimeUnit.Second, maxUnit: TimeUnit.Hour, precision: 2);