* rename collection type * split collections into separate pages * add rerun collection types, migration, editor * add rerun to classic schedule items * rerun plumbing in classic playout builder * start to implement rerun enumerator * add scheduledAt to enumerator movenext * maintain rerun history in db * fix shuffle * fix rerun allowed playback orders * fix updating rerun collections * update changelog; fix editing * update changelog
64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Extensions;
|
|
using ErsatzTV.Core.Interfaces.Scheduling;
|
|
|
|
namespace ErsatzTV.Core.Scheduling;
|
|
|
|
public class RandomizedMediaCollectionEnumerator : IMediaCollectionEnumerator
|
|
{
|
|
private readonly Lazy<Option<TimeSpan>> _lazyMinimumDuration;
|
|
private readonly IList<MediaItem> _mediaItems;
|
|
private readonly Random _random;
|
|
private int _index;
|
|
|
|
public RandomizedMediaCollectionEnumerator(IList<MediaItem> mediaItems, CollectionEnumeratorState state)
|
|
{
|
|
CurrentIncludeInProgramGuide = Option<bool>.None;
|
|
|
|
_mediaItems = mediaItems;
|
|
_lazyMinimumDuration =
|
|
new Lazy<Option<TimeSpan>>(() =>
|
|
_mediaItems.Bind(i => i.GetNonZeroDuration()).OrderBy(identity).HeadOrNone());
|
|
_random = new Random(state.Seed);
|
|
|
|
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
|
|
if (State.Index == state.Index)
|
|
{
|
|
MoveNext(Option<DateTimeOffset>.None);
|
|
}
|
|
else
|
|
{
|
|
while (State.Index <= state.Index)
|
|
{
|
|
MoveNext(Option<DateTimeOffset>.None);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ResetState(CollectionEnumeratorState state) =>
|
|
// seed never changes here, no need to reset
|
|
State.Index = state.Index;
|
|
|
|
public CollectionEnumeratorState State { get; }
|
|
|
|
public Option<MediaItem> Current => _mediaItems.Any() ? _mediaItems[_index] : None;
|
|
public Option<bool> CurrentIncludeInProgramGuide { get; }
|
|
|
|
public void MoveNext(Option<DateTimeOffset> scheduledAt)
|
|
{
|
|
if (_mediaItems.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_index = _random.Next() % _mediaItems.Count;
|
|
State.Index++;
|
|
}
|
|
|
|
public Option<TimeSpan> MinimumDuration => _lazyMinimumDuration.Value;
|
|
|
|
public int Count => _mediaItems.Count;
|
|
}
|