ChatApi/Services/Bot/Structs/Question.cs
2024-12-22 15:49:43 -03:00

45 lines
1.3 KiB
C#

namespace ChatApi.Services.Bot.Structs
{
public class Question
{
public string Text { get; }
public Func<string, bool> Validator { get; }
public int CountTry { get; set; }
public Func<Dictionary<int, string>, bool> Condition { get; }
public Func<string, string> ConvertToSave { get; }
private const int MaxTentativas = 2;
public Question(string texto, Func<string, bool> validador, Func<Dictionary<int, string>, bool> condition=null, Func<string,string> convertToSave = null)
{
Text = texto;
Validator = validador;
CountTry = 0;
Condition = condition;
ConvertToSave = convertToSave;
}
public bool MustShow(Dictionary<int, string> respostasAnteriores)
{
return Condition == null || Condition(respostasAnteriores);
}
public string TryToAnswer(string resposta)
{
if (Validator(resposta))
{
return "";
}
CountTry++;
if (CountTry >= MaxTentativas)
{
CountTry = 0;
return "Não entendi sua resposta. Vamos reiniciar.";
}
return "Resposta inválida. Tente novamente.";
}
}
}