refactor
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
#include "RandomResponses.h"
|
||||
#include <QDebug>
|
||||
|
||||
RandomResponses::RandomResponses(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_randomGenerator(QRandomGenerator::global()->generate())
|
||||
{
|
||||
}
|
||||
|
||||
QString RandomResponses::getResponse(const QString &groupName)
|
||||
{
|
||||
// Проверяем существование группы
|
||||
if (!m_groups.contains(groupName)) {
|
||||
qWarning() << "Группа" << groupName << "не найдена";
|
||||
return QString();
|
||||
}
|
||||
|
||||
const ResponseGroup &group = m_groups[groupName];
|
||||
|
||||
// Проверяем наличие ответов в группе
|
||||
if (group.responses.isEmpty()) {
|
||||
qWarning() << "Группа" << groupName << "не содержит ответов";
|
||||
return QString();
|
||||
}
|
||||
|
||||
// Генерируем случайный индекс
|
||||
int randomIndex = m_randomGenerator.bounded(group.responses.size());
|
||||
|
||||
// Возвращаем случайный ответ
|
||||
return group.responses.at(randomIndex);
|
||||
}
|
||||
|
||||
bool RandomResponses::addResponse(const QString &groupName, const QString &response)
|
||||
{
|
||||
if (response.isEmpty()) {
|
||||
qWarning() << "Пустой ответ не может быть добавлен";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Если группа существует, добавляем ответ в существующую
|
||||
if (m_groups.contains(groupName)) {
|
||||
m_groups[groupName].responses.append(response);
|
||||
}
|
||||
// Иначе создаем новую группу
|
||||
else {
|
||||
ResponseGroup newGroup;
|
||||
newGroup.name = groupName;
|
||||
newGroup.responses.append(response);
|
||||
m_groups.insert(groupName, newGroup);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RandomResponses::removeResponse(const QString &groupName, const QString &response)
|
||||
{
|
||||
// Проверяем существование группы
|
||||
if (!m_groups.contains(groupName)) {
|
||||
qWarning() << "Группа" << groupName << "не найдена";
|
||||
return false;
|
||||
}
|
||||
|
||||
ResponseGroup &group = m_groups[groupName];
|
||||
|
||||
// Удаляем все вхождения ответа
|
||||
int removedCount = group.responses.removeAll(response);
|
||||
|
||||
// Если группа стала пустой, удаляем ее
|
||||
if (group.responses.isEmpty()) {
|
||||
m_groups.remove(groupName);
|
||||
}
|
||||
|
||||
return removedCount > 0;
|
||||
}
|
||||
|
||||
bool RandomResponses::removeGroup(const QString &groupName)
|
||||
{
|
||||
return m_groups.remove(groupName) > 0;
|
||||
}
|
||||
|
||||
QStringList RandomResponses::getGroupNames() const
|
||||
{
|
||||
return m_groups.keys();
|
||||
}
|
||||
|
||||
QStringList RandomResponses::getGroupResponses(const QString &groupName) const
|
||||
{
|
||||
if (m_groups.contains(groupName)) {
|
||||
return m_groups[groupName].responses;
|
||||
}
|
||||
|
||||
return QStringList();
|
||||
}
|
||||
|
||||
bool RandomResponses::hasGroup(const QString &groupName) const
|
||||
{
|
||||
return m_groups.contains(groupName);
|
||||
}
|
||||
|
||||
void RandomResponses::clear()
|
||||
{
|
||||
m_groups.clear();
|
||||
}
|
||||
Reference in New Issue
Block a user