aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/libparser/node
diff options
context:
space:
mode:
authorRenaud Guezennec <renaud@rolisteam.org>2025-02-09 06:05:05 +0100
committerRenaud Guezennec <renaud@rolisteam.org>2025-02-09 06:05:05 +0100
commit6fcb5ca46927f7baab744e117af9eb1ce5b74838 (patch)
tree803b14b1315dfed095705d0c417b19c970541535 /src/libparser/node
parentb8486f92408afa1a0c71d3f62d93f49ac8bebc60 (diff)
downloadOneRoll-6fcb5ca46927f7baab744e117af9eb1ce5b74838.tar.gz
OneRoll-6fcb5ca46927f7baab744e117af9eb1ce5b74838.zip
[Dice] add functions: floor, ceil, round
Diffstat (limited to 'src/libparser/node')
-rw-r--r--src/libparser/node/roundnode.cpp71
-rw-r--r--src/libparser/node/roundnode.h31
2 files changed, 102 insertions, 0 deletions
diff --git a/src/libparser/node/roundnode.cpp b/src/libparser/node/roundnode.cpp
new file mode 100644
index 0000000..8f825f5
--- /dev/null
+++ b/src/libparser/node/roundnode.cpp
@@ -0,0 +1,71 @@
+#include "roundnode.h"
+
+#include <diceparser/parsingtoolbox.h>
+
+RoundNode::RoundNode(Mode mode) : m_scalarResult(new ScalarResult), m_mode(mode) {}
+
+void RoundNode::run(ExecutionNode* previous)
+{
+ m_previousNode= previous;
+
+ if(m_cmd == nullptr)
+ return;
+
+ m_cmd->execute(this);
+ auto internal= ParsingToolBox::getLeafNode(m_cmd);
+ if(!internal)
+ return;
+
+ auto endResult= internal->getResult();
+
+ auto scalar= endResult->getResult(Dice::RESULT_TYPE::SCALAR).toDouble();
+
+ int resVal;
+ switch(m_mode)
+ {
+ case FLOOR:
+ resVal= std::floor(scalar);
+ break;
+ case CEIL:
+ resVal= std::ceil(scalar);
+ break;
+ case ROUND:
+ resVal= std::round(scalar);
+ break;
+ }
+
+ m_scalarResult->setValue(resVal);
+ m_result= m_scalarResult.get();
+}
+
+QString RoundNode::toString(bool withLabel) const
+{
+
+ return withLabel ? QString("%1 [label=\"RoundNode\"]").arg(m_id) : m_id;
+}
+
+qint64 RoundNode::getPriority() const
+{
+ qint64 priority= 0;
+ if(nullptr != m_nextNode)
+ {
+ priority= m_nextNode->getPriority();
+ }
+
+ return priority;
+}
+
+ExecutionNode* RoundNode::getCopy() const
+{
+ RoundNode* node= new RoundNode(m_mode);
+ if(nullptr != m_nextNode)
+ {
+ node->setNextNode(m_nextNode->getCopy());
+ }
+ return node;
+}
+
+void RoundNode::setCommand(ExecutionNode* cmd)
+{
+ m_cmd= cmd;
+}
diff --git a/src/libparser/node/roundnode.h b/src/libparser/node/roundnode.h
new file mode 100644
index 0000000..0bf49b7
--- /dev/null
+++ b/src/libparser/node/roundnode.h
@@ -0,0 +1,31 @@
+#ifndef ROUNDNODE_H
+#define ROUNDNODE_H
+
+#include "executionnode.h"
+#include "scalarresult.h"
+
+class RoundNode : public ExecutionNode
+{
+public:
+ enum Mode {
+ FLOOR,
+ CEIL,
+ ROUND
+ };
+ RoundNode(Mode mode);
+
+ // ExecutionNode interface
+public:
+ void run(ExecutionNode *previous);
+ QString toString(bool withLabel) const;
+ qint64 getPriority() const;
+ ExecutionNode *getCopy() const;
+ void setCommand(ExecutionNode* cmd);
+
+private:
+ std::unique_ptr<ScalarResult> m_scalarResult;
+ ExecutionNode* m_cmd= nullptr;
+ Mode m_mode{ROUND};
+};
+
+#endif // ROUNDNODE_H