aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--node/switchcasenode.cpp52
-rw-r--r--tests/dice/tst_dice.cpp9
-rw-r--r--validatorlist.cpp29
3 files changed, 47 insertions, 43 deletions
diff --git a/node/switchcasenode.cpp b/node/switchcasenode.cpp
index 0928dd2..b118243 100644
--- a/node/switchcasenode.cpp
+++ b/node/switchcasenode.cpp
@@ -54,35 +54,26 @@ void SwitchCaseNode::run(ExecutionNode* previous)
return;
}
- QList<Die*> dieList;
- if(previousResult->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST))
- {
- auto diceResult= dynamic_cast<DiceResult*>(previousResult);
- if(diceResult)
- dieList.append(diceResult->getResultList());
- }
- else if(previousResult->hasResultOfType(Dice::RESULT_TYPE::SCALAR))
- {
- auto resultScalar= previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal();
- auto d= new Die();
- d->insertRollValue(resultScalar);
- dieList.append(d);
- }
-
- for(auto die : dieList)
+ QStringList resultList;
+ for(auto const& info : qAsConst(m_branchList))
{
- QStringList resultList;
- for(auto const& info : qAsConst(m_branchList))
+ if(m_stopAtFirst && !resultList.isEmpty())
+ break;
+ if(info->validatorList)
+ {
+ info->validatorList->validResult(previousResult, true, false, [&info, this, &resultList](Die*, qint64) {
+ info->node->run(m_previousNode);
+ auto lastNode= ParsingToolBox::getLeafNode(info->node);
+ if(lastNode && lastNode->getResult())
+ {
+ resultList << lastNode->getResult()->getStringResult();
+ }
+ else
+ resultList << QString();
+ });
+ }
+ else if(resultList.isEmpty())
{
- if(info->validatorList)
- {
- auto res= info->validatorList->hasValid(die, true, true);
- if(!res)
- continue;
- }
- else if(!resultList.isEmpty())
- break;
-
info->node->run(m_previousNode);
auto lastNode= ParsingToolBox::getLeafNode(info->node);
if(lastNode && lastNode->getResult())
@@ -91,13 +82,10 @@ void SwitchCaseNode::run(ExecutionNode* previous)
}
else
resultList << QString();
-
- if(m_stopAtFirst)
- break;
}
- for(auto const& str : qAsConst(resultList))
- m_stringResult->addText(str);
}
+ for(auto const& str : qAsConst(resultList))
+ m_stringResult->addText(str);
if(m_stringResult->getText().isEmpty())
m_errors.insert(Dice::ERROR_CODE::NO_VALID_RESULT, QStringLiteral("No value fits the Switch/Case operator"));
diff --git a/tests/dice/tst_dice.cpp b/tests/dice/tst_dice.cpp
index a3f0980..150dddb 100644
--- a/tests/dice/tst_dice.cpp
+++ b/tests/dice/tst_dice.cpp
@@ -44,6 +44,7 @@
#include "node/stringnode.h"
#include "node/switchcasenode.h"
#include "node/uniquenode.h"
+#include "node/valueslistnode.h"
#include "operationcondition.h"
#include "parsingtoolbox.h"
#include "result/stringresult.h"
@@ -1233,8 +1234,10 @@ void TestDice::switchCaseTest()
QFETCH(QString, expected);
QFETCH(bool, stopatfirt);
- NumberNode* node1= new NumberNode();
- node1->setNumber(value);
+ NumberNode* valNode= new NumberNode();
+ ValuesListNode* node1= new ValuesListNode();
+ valNode->setNumber(value);
+ node1->insertValue(valNode);
SwitchCaseNode* node2= new SwitchCaseNode();
node2->setStopAtFirt(stopatfirt);
@@ -1297,7 +1300,7 @@ void TestDice::switchCaseTest_data()
<< QList<int>{1, 2, 3, 4} << QStringList{"a", "b", "c", "d"} << QStringLiteral("a,b,c")
<< false;
- QTest::addRow("cmd6") << 3
+ QTest::addRow("cmd7") << 3
<< QList<BC::LogicOperator>{BC::LesserOrEqual, BC::LesserOrEqual, BC::LesserOrEqual,
BC::LesserOrEqual}
<< QList<int>{1, 2, 3, 4} << QStringList{"a", "b", "c", "d"} << QStringLiteral("c") << true;
diff --git a/validatorlist.cpp b/validatorlist.cpp
index b665328..cb650df 100644
--- a/validatorlist.cpp
+++ b/validatorlist.cpp
@@ -56,7 +56,12 @@ DiceResult* getDiceResult(Result* result)
auto dice= dynamic_cast<DiceResult*>(result);
if(nullptr == dice)
{
- qFatal("Error, no dice result");
+ auto value= result->getResult(Dice::RESULT_TYPE::SCALAR).toInt();
+ dice= new DiceResult();
+ auto die= new Die();
+ die->setValue(value);
+ dice->insertResult(die);
+ qWarning("Error, no dice result");
// TODO: manage error here.
}
return dice;
@@ -297,9 +302,17 @@ void ValidatorList::validResult(Result* result, bool recursive, bool unlight,
DiceResult* diceResult= getDiceResult(result);
if(nullptr == diceResult)
break;
- for(auto die : diceResult->getResultList())
+
+ if(m_validatorList.size() > 1)
{
- validResult.appendValidDice(die, die->getValue());
+ for(auto const& die : qAsConst(diceResult->getResultList()))
+ {
+ validResult.appendValidDice(die, die->getValue());
+ }
+ }
+ else
+ {
+ validResult.appendValidDice(new Die(die), die.getValue());
}
}
}
@@ -309,7 +322,7 @@ void ValidatorList::validResult(Result* result, bool recursive, bool unlight,
DiceResult* diceResult= getDiceResult(result);
if(nullptr == diceResult)
break;
- for(auto die : diceResult->getResultList())
+ for(auto const& die : qAsConst(diceResult->getResultList()))
{
auto score= validator->hasValid(die, recursive, unlight);
if(score)
@@ -324,7 +337,7 @@ void ValidatorList::validResult(Result* result, bool recursive, bool unlight,
DiceResult* diceResult= getDiceResult(result);
if(nullptr == diceResult)
break;
- for(auto die : diceResult->getResultList())
+ for(auto const& die : qAsConst(diceResult->getResultList()))
{
auto score= validator->hasValid(die, recursive, unlight);
if(score)
@@ -346,7 +359,7 @@ void ValidatorList::validResult(Result* result, bool recursive, bool unlight,
if(all)
{
validResult.setAllTrue(true);
- for(auto die : diceResult->getResultList())
+ for(auto die : qAsConst(diceResult->getResultList()))
{
validResult.appendValidDice(die, die->getValue());
}
@@ -365,7 +378,7 @@ void ValidatorList::validResult(Result* result, bool recursive, bool unlight,
if(any)
{
validResult.setAllTrue(true);
- for(auto die : diceResult->getResultList())
+ for(auto die : qAsConst(diceResult->getResultList()))
{
validResult.appendValidDice(die, die->getValue());
}
@@ -380,7 +393,7 @@ void ValidatorList::validResult(Result* result, bool recursive, bool unlight,
int i= 0;
ValidatorResult finalResult;
- for(auto vec : validityData)
+ for(const auto& vec : validityData)
{
auto diceList= vec.validDice();
if(i == 0)