76 lines
2.2 KiB
Plaintext
76 lines
2.2 KiB
Plaintext
namespace ChatApi.Services.Bot.Structs
|
|
{
|
|
public class ChatBot
|
|
{
|
|
private readonly List<Question> _questions;
|
|
private readonly ChatPersistence _persistence;
|
|
private readonly Dictionary<int, string> _answers;
|
|
private readonly Guid _usuarioId;
|
|
private int _indiceAtual;
|
|
|
|
public ChatBot(ChatPersistence persistence, Guid usuarioId)
|
|
{
|
|
_questions = new List<Question>();
|
|
_answers = new Dictionary<int, string>();
|
|
_indiceAtual = 0;
|
|
_persistence = persistence;
|
|
_usuarioId = usuarioId;
|
|
|
|
var estado = _persistence.GetState(_usuarioId);
|
|
if (estado != null)
|
|
{
|
|
_indiceAtual = estado.IndicePerguntaAtual;
|
|
_answers = estado.Respostas;
|
|
}
|
|
else
|
|
{
|
|
_indiceAtual = 0;
|
|
}
|
|
}
|
|
|
|
public void AddQuestion(Question Question)
|
|
{
|
|
_questions.Add(Question);
|
|
}
|
|
|
|
public bool InformarResposta(string resposta)
|
|
{
|
|
var perguntaAtual = _questions[_indiceAtual];
|
|
if (perguntaAtual.Validator(resposta))
|
|
{
|
|
_answers[_indiceAtual] = resposta;
|
|
_indiceAtual++;
|
|
|
|
_persistence.SaveState(new ChatState
|
|
{
|
|
UsuarioId = _usuarioId,
|
|
IndicePerguntaAtual = _indiceAtual,
|
|
Respostas = new Dictionary<int, string>(_answers)
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
Console.WriteLine("Resposta inválida. Tente novamente.");
|
|
return false;
|
|
}
|
|
public void Iniciar()
|
|
{
|
|
while (_indiceAtual < _questions.Count)
|
|
{
|
|
var QuestionAtual = _questions[_indiceAtual];
|
|
if (QuestionAtual.TryToAnswer(out _))
|
|
{
|
|
_indiceAtual++;
|
|
}
|
|
else
|
|
{
|
|
_indiceAtual = 0; // Reinicia o fluxo
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("Obrigado por responder a todas as perguntas!");
|
|
}
|
|
}
|
|
}
|