using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace RuriLib.Models
{
///
/// Class that allows to easily manage a list of CVar objects.
///
public class VariableList
{
/// The whole list of variables.
public List All { get; set; }
/// The list of all variables marked as Capture.
[JsonIgnore]
public List Captures { get { return All.Where(v => v.IsCapture && !v.Hidden).ToList(); } }
/// The list of all variables of type Single.
[JsonIgnore]
public List Singles { get { return All.Where(v => v.Type == CVar.VarType.Single).ToList(); } }
/// The list of all variables of type List.
[JsonIgnore]
public List Lists { get { return All.Where(v => v.Type == CVar.VarType.List).ToList(); } }
/// The list of all variables of type Dictionary.
[JsonIgnore]
public List Dictionaries { get { return All.Where(v => v.Type == CVar.VarType.Dictionary).ToList(); } }
///
/// Standard constructor that initializes an empty variable list.
///
public VariableList()
{
All = new List();
}
///
/// Initializes a variable list starting from an existing list of variables.
///
/// The list of variables.
public VariableList(List list)
{
All = list;
}
///
/// Gets a variable given its name.
///
/// The name of the variable
/// The variable or null if it wasn't found.
public CVar Get(string name)
{
return All.FirstOrDefault(v => v.Name == name);
}
///
/// Gets a variable given its name and type.
///
/// The name of the variable
/// The type of the variable
/// The variable or null if it wasn't found.
public CVar Get(string name, CVar.VarType type)
{
return All.FirstOrDefault(v => v.Name == name && v.Type == type);
}
///
/// Helper method that gets the value of a variable of type Single.
///
/// The name of the Single variable
/// The string value or null if it wasn't found.
public string GetSingle(string name)
{
try { return (string)Get(name, CVar.VarType.Single).Value; }
catch { return null; }
}
///
/// Helper method that gets the value of a variable of type List.
///
/// The name of the List variable
/// The list of strings value or null if it wasn't found.
public List GetList(string name)
{
try { return (List)Get(name, CVar.VarType.List).Value; }
catch { return null; }
}
///
/// Helper method that gets the value of a variable of type Dictionary.
///
/// The name of the Dictionary variable
/// The dictionary value or null if it wasn't found.
public Dictionary GetDictionary(string name)
{
try { return (Dictionary)Get(name, CVar.VarType.Dictionary).Value; }
catch { return null; }
}
///
/// Helper method that checks if a variable exists given its name.
///
/// The name of the variable
/// True if the variable exists
public bool VariableExists(string name)
{
return All.Any(v => v.Name == name);
}
///
/// Helper method that checks if a variable exists given its name and type.
///
/// The name of the variable
/// The type of the variable
/// True if the variable exists and matches the given type
public bool VariableExists(string name, CVar.VarType type)
{
return All.Any(v => v.Name == name && v.Type == type);
}
///
/// Adds a CVar object to the variable list.
///
/// The CVar object to add
public void Set(CVar variable)
{
// First of all remove any old variable with the same name
Remove(variable.Name);
// Then add the new one
All.Add(variable);
}
///
/// Adds a hidden CVar object to the variable list.
///
/// The name of the variable to add
/// The value of the variable to add
public void SetHidden(string name, dynamic value)
{
if (All.Any(v => v.Name == name && v.Hidden))
All.FirstOrDefault(v => v.Name == name && v.Hidden).Value = value;
else
All.Add(new CVar(name, value, false, true));
}
///
/// Adds a CVar object to the variable list only if no other variable with the same name exists.
///
/// The CVar object to add
public void SetNew(CVar variable)
{
if (!VariableExists(variable.Name)) Set(variable);
}
///
/// Removes a non-hidden variable given its name.
///
/// The name of the variable to remove
public void Remove(string name)
{
All.RemoveAll(v => v.Name == name && !v.Hidden);
}
///
/// Removes all non-hidden variables if their name matches a condition.
/// The name of the variables in the list are the left-hand term of the comparison.
///
/// The condition type
/// The right-hand term of the comparison
/// The BotData object used for variable replacement
public void Remove(Condition cond, string name, BotData data)
{
All.RemoveAll(v => ConditionChecker.Verify(v.Name, cond, name, data) && !v.Hidden);
}
///
/// Generates a string where all the variables marked as Capture are printed in an organized fashion.
///
/// The chained capture string.
public string ToCaptureString()
{
return string.Join(" | ", Captures.Select(c => c.Name + " = " + c.ToString()));
}
}
}