diff options
| author | 2024-12-29 14:43:33 +0100 | |
|---|---|---|
| committer | 2024-12-29 15:18:51 +0100 | |
| commit | 3f56b91ca67c6fa5a407bd5df39808dc742cd693 (patch) | |
| tree | c621b6d558943b00b764cda3e31dcb5c1e73f745 /src/libparser/node | |
| parent | 5c508b351a95f416e4a599f76902b888369de1b4 (diff) | |
| download | OneRoll-3f56b91ca67c6fa5a407bd5df39808dc742cd693.tar.gz OneRoll-3f56b91ca67c6fa5a407bd5df39808dc742cd693.zip | |
Fix #62: endless loop with list of values
Diffstat (limited to 'src/libparser/node')
| -rw-r--r-- | src/libparser/node/explodedicenode.cpp | 43 | ||||
| -rw-r--r-- | src/libparser/node/rerolldicenode.cpp | 37 |
2 files changed, 78 insertions, 2 deletions
diff --git a/src/libparser/node/explodedicenode.cpp b/src/libparser/node/explodedicenode.cpp index 136611a..f4c699d 100644 --- a/src/libparser/node/explodedicenode.cpp +++ b/src/libparser/node/explodedicenode.cpp @@ -1,5 +1,7 @@ #include "explodedicenode.h" +#include "booleancondition.h" #include "diceparser/parsingtoolbox.h" +#include "validator.h" #include "validatorlist.h" ExplodeDiceNode::ExplodeDiceNode() : m_diceResult(new DiceResult()) @@ -21,11 +23,43 @@ void ExplodeDiceNode::run(ExecutionNode* previous) if(!previous_result) return; - for(auto& die : previous_result->getResultList()) + auto diceList= previous_result->getResultList(); + + auto allInvalid= std::all_of(std::begin(diceList), std::end(diceList), + [](const Die* die) { return die->getMaxValue() < die->getBase(); }); + + qint64 maxVal= 0; + if(allInvalid) + { + + auto const& list= m_validatorList->validators(); + auto max= std::max_element(std::begin(list), std::end(list), + [](Validator* a, Validator* b) + { + auto aa= dynamic_cast<BooleanCondition*>(a); + auto bb= dynamic_cast<BooleanCondition*>(b); + + if(bb && aa) + return aa->valueToScalar() < bb->valueToScalar(); + else + return (!bb && aa); + }); + + auto maxCondition= dynamic_cast<BooleanCondition*>(*max); + if(maxCondition) + maxVal= maxCondition->valueToScalar(); + } + + for(auto& die : diceList) { Die* tmpdie= new Die(*die); m_diceResult->insertResult(tmpdie); die->displayed(); + if(allInvalid && maxVal != tmpdie->getMaxValue() && maxVal > tmpdie->getBase()) + { + qInfo() << "Invalid range for explosing dice, set " << maxVal << " as maximum" << tmpdie->getMaxValue(); + tmpdie->setMaxValue(maxVal); + } } qint64 limit= -1; @@ -51,6 +85,13 @@ void ExplodeDiceNode::run(ExecutionNode* previous) .arg(static_cast<int>(die->getBase())) .arg(static_cast<int>(die->getMaxValue())))); + isValid(die->getBase() > die->getMaxValue(), Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, + QObject::tr("Condition (%1) cause an endless loop with dice: %2") + .arg(toString(true)) + .arg(QStringLiteral("d[%1,%2]") + .arg(static_cast<int>(die->getBase())) + .arg(static_cast<int>(die->getMaxValue())))); + hasExploded= true; if(limit >= 0) { diff --git a/src/libparser/node/rerolldicenode.cpp b/src/libparser/node/rerolldicenode.cpp index 25143ac..7e5bda3 100644 --- a/src/libparser/node/rerolldicenode.cpp +++ b/src/libparser/node/rerolldicenode.cpp @@ -1,5 +1,6 @@ #include <diceparser/parsingtoolbox.h> +#include "booleancondition.h" #include "rerolldicenode.h" #include "validatorlist.h" #include <utility> @@ -37,7 +38,34 @@ void RerollDiceNode::run(ExecutionNode* previous) m_result->setPrevious(previous_result); - for(auto const& die : previous_result->getResultList()) + QList<Die*>& list1= previous_result->getResultList(); + + auto allInvalid= std::all_of(std::begin(list1), std::end(list1), + [](const Die* die) { return die->getMaxValue() < die->getBase(); }); + + qint64 maxVal= 0; + if(allInvalid) + { + + auto const& list1= m_validatorList->validators(); + auto max= std::max_element(std::begin(list1), std::end(list1), + [](Validator* a, Validator* b) + { + auto aa= dynamic_cast<BooleanCondition*>(a); + auto bb= dynamic_cast<BooleanCondition*>(b); + + if(bb && aa) + return aa->valueToScalar() < bb->valueToScalar(); + else + return (!bb && aa); + }); + + auto maxCondition= dynamic_cast<BooleanCondition*>(*max); + if(maxCondition) + maxVal= maxCondition->valueToScalar(); + } + + for(auto const& die : list1) { if(!die) return; @@ -45,6 +73,13 @@ void RerollDiceNode::run(ExecutionNode* previous) Die* tmpdie= new Die(*die); m_diceResult->insertResult(tmpdie); die->displayed(); + + if(allInvalid && maxVal != tmpdie->getMaxValue() && maxVal > tmpdie->getBase()) + { + qInfo() << "Invalid range for explosing dice, set " << maxVal << " as maximum" << tmpdie->getMaxValue() + << tmpdie->getBase(); + tmpdie->setMaxValue(maxVal); + } } QList<Die*>& list= m_diceResult->getResultList(); |