43 lines
1.2 KiB
C#
43 lines
1.2 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; }
|
|
|
|
private const int MaxTentativas = 2;
|
|
|
|
public Question(string texto, Func<string, bool> validador, Func<Dictionary<int, string>, bool> condition=null)
|
|
{
|
|
Text = texto;
|
|
Validator = validador;
|
|
CountTry = 0;
|
|
Condition = condition;
|
|
}
|
|
|
|
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.";
|
|
}
|
|
}
|
|
}
|