This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-nitride-cil/src/MfGames.Nitride.Temporal.Schedules/ApplySchedules.cs
D. Moonfire d5b975c179
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
feat(schedules): implemented the basic schedule
2023-01-16 22:10:24 -06:00

86 lines
2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using FluentValidation;
using MfGames.Gallium;
using MfGames.Nitride.Generators;
namespace MfGames.Nitride.Temporal.Schedules;
public enum SchedulePeriod
{
/// <summary>
/// Indicates that all the matching schedule periods are instant (zero time).
/// </summary>
Instant,
/// <summary>
/// Indicates that the entities will be scheduled a day apart.
/// </summary>
Day,
/// <summary>
/// Indicates that the entities will be scheduled seven days apart.
/// </summary>
Week,
}
/// <summary>
/// Applies schedules against the list of entities.
/// </summary>
[WithProperties]
public partial class ApplySchedules : OperationBase
{
private readonly IValidator<ApplySchedules> validator;
public ApplySchedules(
IValidator<ApplySchedules> validator,
Timekeeper timekeeper)
{
this.Timekeeper = timekeeper;
this.validator = validator;
this.Schedules = new List<ISchedule>();
}
/// <summary>
/// Gets or sets the ordered list of schedules to apply to the entities.
/// </summary>
public IList<ISchedule> Schedules { get; set; }
/// <summary>
/// Gets or sets the timekeeper associated with this operation.
/// </summary>
public Timekeeper Timekeeper { get; set; }
/// <inheritdoc />
public override IEnumerable<Entity> Run(IEnumerable<Entity> input)
{
this.validator.ValidateAndThrow(this);
return input.Select(this.Apply);
}
public ApplySchedules WithSchedules<TItem>(IEnumerable<TItem> items)
where TItem : ISchedule
{
this.Schedules = items.OfType<ISchedule>().ToList();
return this;
}
private Entity Apply(Entity entity)
{
foreach (ISchedule schedule in this.Schedules)
{
if (!schedule.CanApply(entity))
{
continue;
}
entity = schedule.Apply(entity, this.Timekeeper);
}
return entity;
}
}