make yaml playout count an expression (#2102)

This commit is contained in:
Jason Dove
2025-07-01 16:24:11 +00:00
committed by GitHub
parent cd4a9c1d16
commit 5a9d27e196
5 changed files with 41 additions and 4 deletions
+8
View File
@@ -54,6 +54,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `count / 2` will start in the middle of the content
- `random` will start at a random point in the content
- `2` (similar to before this change) will skip the first two items in the content
- YAML playout: make `count` an expression
- The following parameters can be used:
- `count`: the total number of items in the content
- `random`: a random number between zero and (count - 1)
- For example:
- `count / 2` will play half of the items in the content
- `random % 4 + 1` will play between 1 and 4 items
- `2` (similar to before this change) will play exactly two items
### Changed
- Allow `Other Video` libraries and `Image` libraries to use the same folders
@@ -46,6 +46,8 @@ public class PlaylistEnumerator : IMediaCollectionEnumerator
public int Count => throw new NotSupportedException("Count isn't used for playlist enumeration");
public int CountForRandom => _allMediaItemIds.Count;
public Option<TimeSpan> MinimumDuration { get; private set; }
public void MoveNext()
@@ -28,7 +28,30 @@ public class YamlPlayoutCountHandler(EnumeratorCache enumeratorCache) : YamlPlay
foreach (IMediaCollectionEnumerator enumerator in maybeEnumerator)
{
for (var i = 0; i < count.Count; i++)
var random = new Random(context.Playout.Seed + context.InstructionIndex);
int enumeratorCount = enumerator is PlaylistEnumerator playlistEnumerator
? playlistEnumerator.CountForRandom
: enumerator.Count;
var expression = new NCalc.Expression(count.Count);
expression.EvaluateParameter += (name, e) =>
{
e.Result = name switch
{
"count" => enumeratorCount,
"random" => random.Next() % enumeratorCount,
_ => e.Result
};
};
object expressionResult = expression.Evaluate();
int countValue = expressionResult switch
{
double doubleResult => (int)Math.Floor(doubleResult),
int intResult => intResult,
_ => 0
};
for (var i = 0; i < countValue; i++)
{
foreach (MediaItem mediaItem in enumerator.Current)
{
@@ -27,13 +27,17 @@ public class YamlPlayoutSkipItemsHandler(EnumeratorCache enumeratorCache) : IYam
foreach (IMediaCollectionEnumerator enumerator in maybeEnumerator)
{
var random = new Random(context.Playout.Seed + context.InstructionIndex);
int enumeratorCount = enumerator is PlaylistEnumerator playlistEnumerator
? playlistEnumerator.CountForRandom
: enumerator.Count;
var expression = new NCalc.Expression(skipItems.SkipItems);
expression.EvaluateParameter += (name, e) =>
{
e.Result = name switch
{
"count" => enumerator.Count,
"random" => new Random().Next() % enumerator.Count,
"count" => enumeratorCount,
"random" => random.Next() % enumeratorCount,
_ => e.Result
};
};
@@ -2,5 +2,5 @@ namespace ErsatzTV.Core.Scheduling.YamlScheduling.Models;
public class YamlPlayoutCountInstruction : YamlPlayoutInstruction
{
public int Count { get; set; }
public string Count { get; set; }
}