// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Gauntlet
{
///
/// Object that allow to execute several callbacks until they return true.
///
public class Checker
{
public Dictionary Validations { get; }
public Checker()
{
Validations = new();
}
///
/// Register a callback to validate
///
///
///
public void AddValidation(string ActionKey, Func Action)
{
Validations.Add(ActionKey, new Validation(ActionKey, Action));
}
///
/// Check all callbacks, return true if all passed.
///
///
public bool PerformValidations()
{
if (!Validations.Any())
{
return false;
}
IEnumerable PassedValidations = Validations.Values.Where(I => !I.IsValidated && I.Validate());
foreach (var Item in PassedValidations)
{
Item.IsValidated = true;
Log.Info($"Validated: {Item.ActionKey}");
}
return Validations.Values.All(I => I.IsValidated);
}
///
/// Check the specific callback, return true if it passed and false otherwise.
///
///
public bool HasValidated(string ActionKey)
{
return Validations.TryGetValue(ActionKey, out var Validation) && Validation.IsValidated;
}
}
///
/// The class combining a key, a validation callback and its result
///
public class Validation
{
public string ActionKey { get; }
public Func Validate { get; }
public bool IsValidated { get; set; }
public Validation(string InActionKey, Func InValidate)
{
ActionKey = InActionKey;
Validate = InValidate;
}
}
}