Browse Source

prevent repeated playout items when reshuffling (#80)

pull/81/head
Jason Dove 5 years ago committed by GitHub
parent
commit
76446e0d69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 48
      ErsatzTV.Core.Tests/Scheduling/ShuffledContentTests.cs
  2. 18
      ErsatzTV.Core/Scheduling/ShuffledMediaCollectionEnumerator.cs

48
ErsatzTV.Core.Tests/Scheduling/ShuffledContentTests.cs

@ -15,6 +15,54 @@ namespace ErsatzTV.Core.Tests.Scheduling
// this seed will produce (shuffle) 1-10 in order // this seed will produce (shuffle) 1-10 in order
private const int MagicSeed = 670596; private const int MagicSeed = 670596;
[Test]
public void Episodes_Should_Not_Duplicate_When_Reshuffling()
{
List<MediaItem> contents = Episodes(10);
// normally returns 10 5 7 4 3 6 2 8 9 1 1 (note duplicate 1 at end)
var state = new CollectionEnumeratorState { Seed = 8 };
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state);
var list = new List<int>();
for (var i = 1; i <= 1000; i++)
{
shuffledContent.Current.IsSome.Should().BeTrue();
shuffledContent.Current.Do(x => list.Add(x.Id));
shuffledContent.MoveNext();
}
for (var i = 0; i < list.Count - 1; i++)
{
if (list[i] == list[i + 1])
{
Assert.Fail("List contains duplicate items");
}
}
}
[Test]
[Timeout(2000)]
public void Duplicate_Check_Should_Ignore_Single_Item()
{
List<MediaItem> contents = Episodes(1);
var state = new CollectionEnumeratorState();
var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state);
var list = new List<int>();
for (var i = 1; i <= 10; i++)
{
shuffledContent.Current.IsSome.Should().BeTrue();
shuffledContent.Current.Do(x => list.Add(x.Id));
shuffledContent.MoveNext();
}
list.Should().Equal(1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
}
[Test] [Test]
public void Episodes_Should_Shuffle() public void Episodes_Should_Shuffle()
{ {

18
ErsatzTV.Core/Scheduling/ShuffledMediaCollectionEnumerator.cs

@ -34,13 +34,21 @@ namespace ErsatzTV.Core.Scheduling
public void MoveNext() public void MoveNext()
{ {
State.Index++; if ((State.Index + 1) % _shuffled.Count == 0)
if (State.Index % _shuffled.Count == 0)
{ {
Option<MediaItem> tail = Current;
State.Index = 0; State.Index = 0;
State.Seed = _random.Next(); do
_random = new Random(State.Seed); {
_shuffled = Shuffle(_mediaItems, _random); State.Seed = _random.Next();
_random = new Random(State.Seed);
_shuffled = Shuffle(_mediaItems, _random);
} while (_mediaItems.Count > 1 && Current == tail);
}
else
{
State.Index++;
} }
State.Index %= _shuffled.Count; State.Index %= _shuffled.Count;

Loading…
Cancel
Save