Browse Source

make yaml playout count an expression (#2102)

pull/2103/head
Jason Dove 1 year ago committed by GitHub
parent
commit
5a9d27e196
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 8
      CHANGELOG.md
  2. 2
      ErsatzTV.Core/Scheduling/PlaylistEnumerator.cs
  3. 25
      ErsatzTV.Core/Scheduling/YamlScheduling/Handlers/YamlPlayoutCountHandler.cs
  4. 8
      ErsatzTV.Core/Scheduling/YamlScheduling/Handlers/YamlPlayoutSkipItemsHandler.cs
  5. 2
      ErsatzTV.Core/Scheduling/YamlScheduling/Models/YamlPlayoutCountInstruction.cs

8
CHANGELOG.md

@ -54,6 +54,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). @@ -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

2
ErsatzTV.Core/Scheduling/PlaylistEnumerator.cs

@ -46,6 +46,8 @@ public class PlaylistEnumerator : IMediaCollectionEnumerator @@ -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()

25
ErsatzTV.Core/Scheduling/YamlScheduling/Handlers/YamlPlayoutCountHandler.cs

@ -28,7 +28,30 @@ public class YamlPlayoutCountHandler(EnumeratorCache enumeratorCache) : YamlPlay @@ -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)
{

8
ErsatzTV.Core/Scheduling/YamlScheduling/Handlers/YamlPlayoutSkipItemsHandler.cs

@ -27,13 +27,17 @@ public class YamlPlayoutSkipItemsHandler(EnumeratorCache enumeratorCache) : IYam @@ -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
ErsatzTV.Core/Scheduling/YamlScheduling/Models/YamlPlayoutCountInstruction.cs

@ -2,5 +2,5 @@ namespace ErsatzTV.Core.Scheduling.YamlScheduling.Models; @@ -2,5 +2,5 @@ namespace ErsatzTV.Core.Scheduling.YamlScheduling.Models;
public class YamlPlayoutCountInstruction : YamlPlayoutInstruction
{
public int Count { get; set; }
public string Count { get; set; }
}

Loading…
Cancel
Save