optionally shuffle marathon groups (#1850)
This commit is contained in:
@@ -12,7 +12,9 @@ public class PlaylistEnumerator : IMediaCollectionEnumerator
|
||||
private int _enumeratorIndex;
|
||||
private System.Collections.Generic.HashSet<int> _idsToIncludeInEPG;
|
||||
private IList<bool> _playAll;
|
||||
private IList<IMediaCollectionEnumerator> _sortedEnumerators;
|
||||
private List<IMediaCollectionEnumerator> _sortedEnumerators;
|
||||
private bool _shufflePlaylistItems;
|
||||
private CloneableRandom _random;
|
||||
|
||||
private PlaylistEnumerator()
|
||||
{
|
||||
@@ -65,6 +67,13 @@ public class PlaylistEnumerator : IMediaCollectionEnumerator
|
||||
{
|
||||
State.Index = 0;
|
||||
_remainingMediaItemIds.UnionWith(_allMediaItemIds);
|
||||
|
||||
if (_shufflePlaylistItems)
|
||||
{
|
||||
State.Seed = _random.Next();
|
||||
_random = new CloneableRandom(State.Seed);
|
||||
_sortedEnumerators = ShufflePlaylistItems();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,13 +81,15 @@ public class PlaylistEnumerator : IMediaCollectionEnumerator
|
||||
IMediaCollectionRepository mediaCollectionRepository,
|
||||
Dictionary<PlaylistItem, List<MediaItem>> playlistItemMap,
|
||||
CollectionEnumeratorState state,
|
||||
bool shufflePlaylistItems,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var result = new PlaylistEnumerator
|
||||
{
|
||||
_sortedEnumerators = [],
|
||||
_playAll = [],
|
||||
_idsToIncludeInEPG = []
|
||||
_idsToIncludeInEPG = [],
|
||||
_shufflePlaylistItems = shufflePlaylistItems
|
||||
};
|
||||
|
||||
// collections should share enumerators
|
||||
@@ -165,6 +176,13 @@ public class PlaylistEnumerator : IMediaCollectionEnumerator
|
||||
.OrderBy(identity)
|
||||
.HeadOrNone();
|
||||
|
||||
result._random = new CloneableRandom(state.Seed);
|
||||
|
||||
if (shufflePlaylistItems)
|
||||
{
|
||||
result._sortedEnumerators = result.ShufflePlaylistItems();
|
||||
}
|
||||
|
||||
result.State = new CollectionEnumeratorState { Seed = state.Seed };
|
||||
result._enumeratorIndex = 0;
|
||||
|
||||
@@ -181,4 +199,28 @@ public class PlaylistEnumerator : IMediaCollectionEnumerator
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<IMediaCollectionEnumerator> ShufflePlaylistItems()
|
||||
{
|
||||
if (_sortedEnumerators.Count < 3)
|
||||
{
|
||||
return _sortedEnumerators;
|
||||
}
|
||||
|
||||
IMediaCollectionEnumerator[] copy = _sortedEnumerators.ToArray();
|
||||
IMediaCollectionEnumerator last = _sortedEnumerators.Last();
|
||||
|
||||
do
|
||||
{
|
||||
int n = copy.Length;
|
||||
while (n > 1)
|
||||
{
|
||||
n--;
|
||||
int k = _random.Next(n + 1);
|
||||
(copy[k], copy[n]) = (copy[n], copy[k]);
|
||||
}
|
||||
} while (copy.First() == last);
|
||||
|
||||
return copy.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1020,6 +1020,7 @@ public class PlayoutBuilder : IPlayoutBuilder
|
||||
_mediaCollectionRepository,
|
||||
playlistItemMap,
|
||||
state,
|
||||
shufflePlaylistItems: false,
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,6 +112,7 @@ public class EnumeratorCache(IMediaCollectionRepository mediaCollectionRepositor
|
||||
mediaCollectionRepository,
|
||||
itemMap,
|
||||
state,
|
||||
shufflePlaylistItems: false,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,9 @@ public class YamlPlayoutContentMarathonItem : YamlPlayoutContentItem
|
||||
[YamlMember(Alias = "group_by", ApplyNamingConventions = false)]
|
||||
public string GroupBy { get; set; }
|
||||
|
||||
[YamlMember(Alias = "shuffle_groups", ApplyNamingConventions = false)]
|
||||
public bool ShuffleGroups { get; set; }
|
||||
|
||||
[YamlMember(Alias = "item_order", ApplyNamingConventions = false)]
|
||||
public string ItemOrder { get; set; }
|
||||
|
||||
|
||||
@@ -68,6 +68,7 @@ public class YamlPlayoutMarathonHelper(IMediaCollectionRepository mediaCollectio
|
||||
mediaCollectionRepository,
|
||||
itemMap,
|
||||
state,
|
||||
marathon.ShuffleGroups,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user