58 lines
1.5 KiB
C#
58 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using MfGames.Gallium;
|
|
|
|
namespace MfGames.Nitride.Temporal.Schedules;
|
|
|
|
/// <summary>
|
|
/// An indexed-based schedule that uses the regular expression to get a
|
|
/// numerical value and then uses that to determine the schedule.
|
|
/// </summary>
|
|
public partial class IndexedPathRegexSchedule<TSchedule> : PathRegexScheduleBase
|
|
where TSchedule : IndexedSchedule
|
|
{
|
|
public IndexedPathRegexSchedule()
|
|
{
|
|
this.Indexes = new Dictionary<int, TSchedule>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the dictionary that indexes the various numerical indexes
|
|
/// to provide the scheduling.
|
|
/// </summary>
|
|
public Dictionary<int, TSchedule> Indexes { get; set; }
|
|
|
|
public IndexedPathRegexSchedule<TSchedule> WithIndexes(
|
|
Dictionary<int, TSchedule> value)
|
|
{
|
|
this.Indexes = value;
|
|
return this;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override Entity Apply(
|
|
Entity entity,
|
|
int number,
|
|
TimeService timeService)
|
|
{
|
|
// Figure out the entry in the index.
|
|
var applicableKeys = this.Indexes.Keys
|
|
.Where(a => a <= number)
|
|
.ToList();
|
|
|
|
if (applicableKeys.Count == 0)
|
|
{
|
|
return entity;
|
|
}
|
|
|
|
int startIndex = applicableKeys.Max(a => a);
|
|
TSchedule schedule = this.Indexes[startIndex];
|
|
|
|
// Pass everything into the schedule to perform the applying.
|
|
int startOffset = number - startIndex;
|
|
|
|
return schedule.Apply(entity, startOffset, timeService);
|
|
}
|
|
}
|