diff --git a/CHANGELOG.md b/CHANGELOG.md index 70a55bd76..07b0f8644 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - ETV will set this automatically when it has permission - When ETV does not have permission, startup will fail with logged instructions on how to configure MySql - Fix scaling anamorphic content in locales that don't use period as a decimal separator (e.g. `,`) +- Block schedules: fix playout build crash when empty collection uses random playback order ### Changed - **BREAKING CHANGE**: change how `Scripted Schedule` system works diff --git a/ErsatzTV.Core.Tests/Scheduling/RandomizedContentTests.cs b/ErsatzTV.Core.Tests/Scheduling/RandomizedContentTests.cs index 4cbdabbd0..c6e2d7eac 100644 --- a/ErsatzTV.Core.Tests/Scheduling/RandomizedContentTests.cs +++ b/ErsatzTV.Core.Tests/Scheduling/RandomizedContentTests.cs @@ -13,12 +13,12 @@ public class RandomizedContentTests private const int KnownSeed = 22295; - private readonly List _expected = new() - { + private readonly List _expected = + [ 5, 7, 7, 8, 6, 7, 8, 9, 10, 7, 5, 1, 7, 2, 5, 6, 1, 4, 5, 6, 4, 5, 1, 6, 5, 7, 1, 3, 9, 9, 9, 3, 3, 2, 3, 4, 5, 6, 9, 3, 6, 9, 7, 1, 2, 10, 3, 8, 3, 8, 8, 3, 1, 5, 4, 3, 6, 4, 6, 2, 9, 8, 3, 1, 8, 5, 1, 8, 2, 1, 1, 5, 5, 5, 3, 5, 8, 10, 4, 8, 7, 3, 3, 4, 4, 9, 2, 8, 8, 10, 8, 4, 3, 10, 7, 8, 9, 9 - }; + ]; private CancellationToken _cancellationToken; diff --git a/ErsatzTV.Core/Scheduling/RandomizedMediaCollectionEnumerator.cs b/ErsatzTV.Core/Scheduling/RandomizedMediaCollectionEnumerator.cs index d971d51ab..ee27ccfa1 100644 --- a/ErsatzTV.Core/Scheduling/RandomizedMediaCollectionEnumerator.cs +++ b/ErsatzTV.Core/Scheduling/RandomizedMediaCollectionEnumerator.cs @@ -24,10 +24,17 @@ public class RandomizedMediaCollectionEnumerator : IMediaCollectionEnumerator State = new CollectionEnumeratorState { Seed = state.Seed }; // we want to move at least once so we start with a random item and not the first // because _index defaults to 0 - while (State.Index <= state.Index) + if (State.Index == state.Index) { MoveNext(); } + else + { + while (State.Index <= state.Index) + { + MoveNext(); + } + } } public void ResetState(CollectionEnumeratorState state) => @@ -41,6 +48,11 @@ public class RandomizedMediaCollectionEnumerator : IMediaCollectionEnumerator public void MoveNext() { + if (_mediaItems.Count == 0) + { + return; + } + _index = _random.Next() % _mediaItems.Count; State.Index++; } diff --git a/ErsatzTV.Core/Scheduling/RandomizedRotatingMediaCollectionEnumerator.cs b/ErsatzTV.Core/Scheduling/RandomizedRotatingMediaCollectionEnumerator.cs index bb53bd66f..d6f38792b 100644 --- a/ErsatzTV.Core/Scheduling/RandomizedRotatingMediaCollectionEnumerator.cs +++ b/ErsatzTV.Core/Scheduling/RandomizedRotatingMediaCollectionEnumerator.cs @@ -48,10 +48,17 @@ public class RandomizedRotatingMediaCollectionEnumerator : IMediaCollectionEnume State = new CollectionEnumeratorState { Seed = state.Seed }; // we want to move at least once so we start with a random item and not the first // because _index defaults to 0 - while (State.Index <= state.Index) + if (State.Index == state.Index) { MoveNext(); } + else + { + while (State.Index <= state.Index) + { + MoveNext(); + } + } } public void ResetState(CollectionEnumeratorState state) => @@ -66,6 +73,11 @@ public class RandomizedRotatingMediaCollectionEnumerator : IMediaCollectionEnume public void MoveNext() { var groups = _groupMedia.Keys.ToList(); + if (groups.Count == 0) + { + return; + } + int nextRandom = _random.Next(); int groupNumber = nextRandom % groups.Count;