aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/node
diff options
context:
space:
mode:
authorRenaud G <renaud@rolisteam.org>2021-02-07 02:43:54 +0100
committerRenaud G <renaud@rolisteam.org>2021-02-07 02:45:12 +0100
commitb9f44f8dffef37a182ed4b9fdba68c935a972026 (patch)
treec5dfc852cb7afcf0e70fe1b623d0d5253df01f7f /node
parent0bba02370a82719e47ba0566c3c56ea0b770c96c (diff)
downloadOneRoll-b9f44f8dffef37a182ed4b9fdba68c935a972026.tar.gz
OneRoll-b9f44f8dffef37a182ed4b9fdba68c935a972026.zip
Add SwitchCaseNode
Diffstat (limited to 'node')
-rw-r--r--node/switchcasenode.cpp146
-rw-r--r--node/switchcasenode.h54
2 files changed, 200 insertions, 0 deletions
diff --git a/node/switchcasenode.cpp b/node/switchcasenode.cpp
new file mode 100644
index 0000000..99450d2
--- /dev/null
+++ b/node/switchcasenode.cpp
@@ -0,0 +1,146 @@
+/***************************************************************************
+ * Copyright (C) 2021 by Renaud Guezennec *
+ * http://www.rolisteam.org/contact *
+ * *
+ * This software is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#include "switchcasenode.h"
+
+#include "parsingtoolbox.h"
+#include "stringresult.h"
+
+SwitchCaseNode::SwitchCaseNode() : m_stringResult(new StringResult)
+{
+ m_result= m_stringResult;
+}
+
+void SwitchCaseNode::setStopAtFirt(bool b)
+{
+ m_stopAtFirst= b;
+}
+
+void SwitchCaseNode::run(ExecutionNode* previous)
+{
+ m_previousNode= previous;
+ if(nullptr == previous)
+ {
+ m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR,
+ QStringLiteral("No previous node before Swith/Case operator"));
+ return;
+ }
+ auto previousResult= previous->getResult();
+ m_result->setPrevious(previousResult);
+
+ if(nullptr == previousResult
+ || (!previousResult->hasResultOfType(Dice::RESULT_TYPE::SCALAR)
+ && !previousResult->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)))
+ {
+ m_errors.insert(Dice::ERROR_CODE::NO_VALID_RESULT,
+ QStringLiteral("No scalar or dice result before Swith/Case operator"));
+ return;
+ }
+
+ QList<Die*> dieList;
+ 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);
+ }
+ else
+ {
+ auto diceResult= dynamic_cast<DiceResult*>(previousResult);
+ if(diceResult)
+ dieList.append(diceResult->getResultList());
+ }
+
+ for(auto die : dieList)
+ {
+ for(auto const& info : qAsConst(m_branchList))
+ {
+ if(info->validatorList)
+ {
+ auto res= info->validatorList->hasValid(die, true, true);
+ if(!res)
+ continue;
+ }
+
+ info->node->run(m_previousNode);
+ auto lastNode= ParsingToolBox::getLeafNode(info->node);
+ if(lastNode && lastNode->getResult())
+ {
+ m_stringResult->addText(lastNode->getResult()->getStringResult());
+ }
+ else
+ m_stringResult->addText(QString());
+
+ if(m_stopAtFirst)
+ break;
+ }
+ }
+
+ if(m_stringResult->getText().isEmpty())
+ m_errors.insert(Dice::ERROR_CODE::NO_VALID_RESULT, QStringLiteral("No value fits the Swith/Case operator"));
+
+ if(nullptr != m_nextNode)
+ {
+ m_nextNode->run(this);
+ }
+}
+
+QString SwitchCaseNode::toString(bool withLabel) const
+{
+ if(withLabel)
+ {
+ return QString("%1 [label=\"SwitchCaseNode\"]").arg(m_id);
+ }
+ else
+ {
+ return m_id;
+ }
+}
+
+qint64 SwitchCaseNode::getPriority() const
+{
+ qint64 priority= 0;
+ if(nullptr != m_previousNode)
+ {
+ priority= m_previousNode->getPriority();
+ }
+ return priority;
+}
+
+ExecutionNode* SwitchCaseNode::getCopy() const
+{
+ SwitchCaseNode* node= new SwitchCaseNode();
+ for(auto const& info : qAsConst(m_branchList))
+ {
+ node->insertCase(info->node, info->validatorList);
+ }
+
+ if(nullptr != m_nextNode)
+ {
+ node->setNextNode(m_nextNode->getCopy());
+ }
+ return node;
+}
+
+void SwitchCaseNode::insertCase(ExecutionNode* node, ValidatorList* validator)
+{
+ std::unique_ptr<CaseInfo> info(new CaseInfo{validator, node});
+ m_branchList.push_back(std::move(info));
+}
diff --git a/node/switchcasenode.h b/node/switchcasenode.h
new file mode 100644
index 0000000..719da90
--- /dev/null
+++ b/node/switchcasenode.h
@@ -0,0 +1,54 @@
+/***************************************************************************
+ * Copyright (C) 2021 by Renaud Guezennec *
+ * http://www.rolisteam.org/contact *
+ * *
+ * This software is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#ifndef SWITCHCASENODE_H
+#define SWITCHCASENODE_H
+
+#include <memory>
+
+#include "executionnode.h"
+#include "validatorlist.h"
+
+struct CaseInfo
+{
+ ValidatorList* validatorList;
+ ExecutionNode* node;
+};
+class StringResult;
+class SwitchCaseNode : public ExecutionNode
+{
+public:
+ SwitchCaseNode();
+ void setStopAtFirt(bool b);
+
+ void run(ExecutionNode* previous= nullptr) override;
+
+ QString toString(bool withLabel) const override;
+ qint64 getPriority() const override;
+ ExecutionNode* getCopy() const override;
+
+ void insertCase(ExecutionNode* node, ValidatorList* validator);
+
+private:
+ std::vector<std::unique_ptr<CaseInfo>> m_branchList;
+ StringResult* m_stringResult;
+ bool m_stopAtFirst= false;
+};
+
+#endif // SWITCHCASENODE_H