[scheduling] Add a new mode RandomRotation that randomly picks an item from a randomly choosen group (show/artist) for block schedule (#1885)

* init

* minor naming change

* address to comments round 1

* update dependencies

* formatting

* make sure it rotates

* update changelog

---------

Co-authored-by: Jason Dove <1695733+jasongdove@users.noreply.github.com>
This commit is contained in:
embolon
2024-09-10 13:36:20 -05:00
committed by GitHub
co-authored by Jason Dove
parent 91e85cc9c1
commit f40eaef898
16 changed files with 152 additions and 16 deletions
@@ -43,7 +43,8 @@ public class BlockPlayoutBuilder(
PlaybackOrder.Chronological,
PlaybackOrder.SeasonEpisode,
PlaybackOrder.Shuffle,
PlaybackOrder.Random
PlaybackOrder.Random,
PlaybackOrder.RandomRotation
];
DateTimeOffset start = DateTimeOffset.Now;
@@ -264,6 +265,12 @@ public class BlockPlayoutBuilder(
playout,
blockItem,
historyKey),
PlaybackOrder.RandomRotation => BlockPlayoutEnumerator.RandomRotation(
collectionItems,
currentTime,
playout,
blockItem,
historyKey),
_ => new RandomizedMediaCollectionEnumerator(
collectionItems,
new CollectionEnumeratorState { Seed = new Random().Next(), Index = 0 })
@@ -137,4 +137,31 @@ public static class BlockPlayoutEnumerator
// as long as already-played items are not included
return new BlockPlayoutShuffledMediaCollectionEnumerator(mediaItems, state);
}
public static IMediaCollectionEnumerator RandomRotation(
List<MediaItem> collectionItems,
DateTimeOffset currentTime,
Playout playout,
BlockItem blockItem,
string historyKey)
{
DateTime historyTime = currentTime.UtcDateTime;
Option<PlayoutHistory> maybeHistory = playout.PlayoutHistory
.Filter(h => h.BlockId == blockItem.BlockId)
.Filter(h => h.Key == historyKey)
.Filter(h => h.When < historyTime)
.OrderByDescending(h => h.When)
.HeadOrNone();
var state = new CollectionEnumeratorState { Seed = playout.Seed + blockItem.BlockId, Index = 0 };
foreach (PlayoutHistory h in maybeHistory)
{
// Make sure to only increase the index by 1 since we can only
// guarantee the next one is a different show. h.Index comes from
// the previous play item which already increased by 1.
state.Index = h.Index;
}
return new RandomizedRotatingMediaCollectionEnumerator(collectionItems, state);
}
}