148 lines
4.6 KiB
C++
Executable File
148 lines
4.6 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* The FiringUnitValidator regulates the rules for firing units
|
|
* $Id: participant_dis_helper.h 37747 2017-12-08 08:07:01Z ehrlicher $
|
|
* $HeadURL: svn://10.196.0.54/TDS/branches/product/features/24_vulcano/Lts/Plugins/ParticipantManagement/Gui/Common/participant_dis_helper.h $
|
|
* $Author: ehrlicher $
|
|
* Created by Hannes Fritsche, ESZS, 2024-03-07
|
|
*/
|
|
|
|
#include "firing_unit_validator.h"
|
|
|
|
#include <LtsUtilsSettings/settings.h>
|
|
#include <ParticipantManagement/ppm.hpp>
|
|
#include "ParticipantManagement/settings.h"
|
|
#include "TypeHandling/type_handler.h"
|
|
#include "participant_handler.h"
|
|
#include "participant.h"
|
|
|
|
namespace ParticipantManagement
|
|
{
|
|
namespace ParticipantHandling
|
|
{
|
|
|
|
FiringUnitValidator::FiringUnitValidator(const std::shared_ptr<ParticipantHandler> &participantHandler,
|
|
const std::shared_ptr<TypeHandling::TypeHandler> &typeHandler,
|
|
QObject *parent)
|
|
: QObject(parent)
|
|
, m_participantHandler(participantHandler)
|
|
, m_typeHandler(typeHandler)
|
|
{
|
|
}
|
|
|
|
Core::FunctionReplyObject FiringUnitValidator::firingUnitError(const QHash<int, int> &modIdToParticipantId, const QVector<ParticipantData> &participantData)
|
|
{
|
|
QString error;
|
|
|
|
for (const ParticipantData ¤tData : participantData)
|
|
{
|
|
if (!m_typeHandler->isFiringUnitType(currentData.typeId()))
|
|
continue;
|
|
|
|
int unitModId = currentData.modId();
|
|
|
|
error = getError(modIdToParticipantId, participantData, unitModId);
|
|
|
|
if (!error.isEmpty())
|
|
break;
|
|
}
|
|
|
|
return Core::FunctionReplyObject(error.toStdString().c_str());
|
|
}
|
|
|
|
QString FiringUnitValidator::getError(const QHash<int, int> &modIdToParticipantId, const QVector<ParticipantData> &participantData, int unitModId) const
|
|
{
|
|
bool hasCapacityError = false;
|
|
bool hasFalseTypeError = false;
|
|
bool hasMixedTypeError = false;
|
|
uint maxParticipantsPerFiringUnit = Settings::maxParticipantsPerFiringUnit();
|
|
uint participantsInFiringUnit = nonModdedInferiorsCount(unitModId, modIdToParticipantId);
|
|
int typeId = participantTypeInFiringUnit(unitModId, modIdToParticipantId);
|
|
|
|
for (const ParticipantData ¤tData : participantData)
|
|
{
|
|
if (currentData.superiorModId() != unitModId)
|
|
continue;
|
|
|
|
// capacity error
|
|
hasCapacityError = ++participantsInFiringUnit > maxParticipantsPerFiringUnit;
|
|
|
|
if (hasCapacityError)
|
|
return tr("There are only %1 participants allowed per firing unit.").arg(maxParticipantsPerFiringUnit);
|
|
|
|
// false type error
|
|
hasFalseTypeError = !m_typeHandler->isArtilleryParticipantType(currentData.typeId());
|
|
|
|
if (hasFalseTypeError)
|
|
return tr("There are only artillery participants allowed in firing units.");
|
|
|
|
// mixed type error
|
|
if (typeId == -1)
|
|
typeId = currentData.typeId();
|
|
|
|
hasMixedTypeError = currentData.typeId() != typeId;
|
|
|
|
if (hasMixedTypeError)
|
|
return tr("There are no mixed types allowed in firing units.");
|
|
}
|
|
|
|
return QString();
|
|
}
|
|
|
|
int FiringUnitValidator::nonModdedInferiorsCount(int unitModId, const QHash<int, int> &modIdToParticipantId) const
|
|
{
|
|
if (!modIdToParticipantId.contains(unitModId))
|
|
return 0;
|
|
|
|
const int unitParticipantId = modIdToParticipantId.value(unitModId);
|
|
const Participant *unitParticipant = m_participantHandler->participantById(unitParticipantId);
|
|
|
|
if (!unitParticipant)
|
|
return 0;
|
|
|
|
const QList<int> moddedParticipantIds = modIdToParticipantId.keys();
|
|
int nonModdedInferiorsCountRet = 0;
|
|
|
|
for (int inferiorId : unitParticipant->inferiors())
|
|
{
|
|
if (moddedParticipantIds.contains(inferiorId)) // only non modded participants
|
|
continue;
|
|
|
|
++nonModdedInferiorsCountRet;
|
|
}
|
|
|
|
return nonModdedInferiorsCountRet;
|
|
}
|
|
|
|
int FiringUnitValidator::participantTypeInFiringUnit(int unitModId, const QHash<int, int> &modIdToParticipantId) const
|
|
{
|
|
if (!modIdToParticipantId.contains(unitModId))
|
|
return -1;
|
|
|
|
const int unitParticipantId = modIdToParticipantId.value(unitModId);
|
|
const Participant *unitParticipant = m_participantHandler->participantById(unitParticipantId);
|
|
|
|
if (!unitParticipant)
|
|
return -1;
|
|
|
|
const QList<int> moddedParticipantIds = modIdToParticipantId.keys();
|
|
|
|
for (int inferiorId : unitParticipant->inferiors())
|
|
{
|
|
if (moddedParticipantIds.contains(inferiorId)) // only non modded participants
|
|
continue;
|
|
|
|
const Participant *inferior = m_participantHandler->participantById(inferiorId);
|
|
|
|
if (!inferior)
|
|
continue;
|
|
|
|
return inferior->typeId();
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
} // namespace ParticipantHandling
|
|
} // namespace ParticipantManagement
|