using Microsoft.VisualBasic.CompilerServices;
using RhythmBase.Exceptions;
using System.Text.RegularExpressions;
namespace RhythmBase.Components
{
///
/// The conditions of the event.
///
public class Condition
{
///
/// Condition list.
///
public List<(bool Enabled, BaseConditional Conditional)> ConditionLists;
///
/// The time of effectiveness of the condition.
///
public float Duration { get; set; }
///
/// Initializes a new instance of the class.
///
public Condition()
{
ConditionLists = [];
}
///
/// Loads a condition from a string.
///
/// The text to load the condition from.
/// A new instance of the class.
/// Thrown when the condition is illegal.
internal static Condition Load(string text)
{
Condition @out = new();
MatchCollection Matches = Regex.Matches(text, "(~?\\d+)(?=[&d])");
if (Matches.Count > 0)
{
@out.Duration = float.Parse(Regex.Match(text, "[\\d\\.]").Value);
return @out;
}
throw new RhythmBaseException(string.Format("Illegal condition: {0}.", text));
}
///
/// Converts conditions to a string.
///
/// A string in the format supported by RDLevel.
public string Serialize() => $"{string.Join("&", ConditionLists.Select((i) => (i.Enabled ? "" : "~") + i.Conditional.Id.ToString()))}d{Duration}";
///
public override string ToString() => Serialize();
}
}