refactor yaml playout builder (#1820)
* update changelog * refactor some handlers * refactor skip items instruction * more refactoring
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using ErsatzTV.Core.Domain;
|
||||
using ErsatzTV.Core.Interfaces.Repositories;
|
||||
using ErsatzTV.Core.Interfaces.Scheduling;
|
||||
using ErsatzTV.Core.Scheduling.YamlScheduling.Models;
|
||||
|
||||
namespace ErsatzTV.Core.Scheduling.YamlScheduling;
|
||||
|
||||
public class EnumeratorCache(IMediaCollectionRepository mediaCollectionRepository)
|
||||
{
|
||||
private readonly Dictionary<string, IMediaCollectionEnumerator> _enumerators = new();
|
||||
|
||||
public System.Collections.Generic.HashSet<string> MissingContentKeys { get; } = [];
|
||||
|
||||
public async Task<Option<IMediaCollectionEnumerator>> GetCachedEnumeratorForContent(
|
||||
YamlPlayoutContext context,
|
||||
string contentKey,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(contentKey))
|
||||
{
|
||||
return Option<IMediaCollectionEnumerator>.None;
|
||||
}
|
||||
|
||||
if (!_enumerators.TryGetValue(contentKey, out IMediaCollectionEnumerator enumerator))
|
||||
{
|
||||
Option<IMediaCollectionEnumerator> maybeEnumerator =
|
||||
await GetEnumeratorForContent(context, contentKey, cancellationToken);
|
||||
|
||||
if (maybeEnumerator.IsNone)
|
||||
{
|
||||
return Option<IMediaCollectionEnumerator>.None;
|
||||
}
|
||||
|
||||
foreach (IMediaCollectionEnumerator e in maybeEnumerator)
|
||||
{
|
||||
enumerator = e;
|
||||
_enumerators.Add(contentKey, enumerator);
|
||||
}
|
||||
}
|
||||
|
||||
return Some(enumerator);
|
||||
}
|
||||
|
||||
private async Task<Option<IMediaCollectionEnumerator>> GetEnumeratorForContent(
|
||||
YamlPlayoutContext context,
|
||||
string contentKey,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
int index = context.Definition.Content.FindIndex(c => c.Key == contentKey);
|
||||
if (index < 0)
|
||||
{
|
||||
return Option<IMediaCollectionEnumerator>.None;
|
||||
}
|
||||
|
||||
List<MediaItem> items = [];
|
||||
|
||||
YamlPlayoutContentItem content = context.Definition.Content[index];
|
||||
switch (content)
|
||||
{
|
||||
case YamlPlayoutContentSearchItem search:
|
||||
items = await mediaCollectionRepository.GetSmartCollectionItems(search.Query);
|
||||
break;
|
||||
case YamlPlayoutContentShowItem show:
|
||||
items = await mediaCollectionRepository.GetShowItemsByShowGuids(
|
||||
show.Guids.Map(g => $"{g.Source}://{g.Value}").ToList());
|
||||
break;
|
||||
}
|
||||
|
||||
// start at the appropriate place in the enumerator
|
||||
context.ContentIndex.TryGetValue(contentKey, out int enumeratorIndex);
|
||||
|
||||
var state = new CollectionEnumeratorState { Seed = context.Playout.Seed + index, Index = enumeratorIndex };
|
||||
switch (Enum.Parse<PlaybackOrder>(content.Order, true))
|
||||
{
|
||||
case PlaybackOrder.Chronological:
|
||||
return new ChronologicalMediaCollectionEnumerator(items, state);
|
||||
case PlaybackOrder.Shuffle:
|
||||
// TODO: fix this
|
||||
var groupedMediaItems = items.Map(mi => new GroupedMediaItem(mi, null)).ToList();
|
||||
return new ShuffledMediaCollectionEnumerator(groupedMediaItems, state, cancellationToken);
|
||||
}
|
||||
|
||||
return Option<IMediaCollectionEnumerator>.None;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user