diff options
| author | 2022-04-29 10:48:09 +0200 | |
|---|---|---|
| committer | 2022-04-29 10:48:09 +0200 | |
| commit | 07c5f6ec23fcf9237a24e71adcfacabce677f818 (patch) | |
| tree | 588e8c5f82b9163181fad3581f610e6f1d88cba4 /src/libparser/node/dicerollernode.cpp | |
| parent | a9153f1615a842cfb9e9bcda4d9071e202618569 (diff) | |
| download | OneRoll-07c5f6ec23fcf9237a24e71adcfacabce677f818.tar.gz OneRoll-07c5f6ec23fcf9237a24e71adcfacabce677f818.zip | |
Change file organization.
Diffstat (limited to 'src/libparser/node/dicerollernode.cpp')
| -rw-r--r-- | src/libparser/node/dicerollernode.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/libparser/node/dicerollernode.cpp b/src/libparser/node/dicerollernode.cpp new file mode 100644 index 0000000..2b00c0a --- /dev/null +++ b/src/libparser/node/dicerollernode.cpp @@ -0,0 +1,121 @@ +#include "dicerollernode.h" +#include "die.h" + +#include <QDebug> +#include <QThread> +#include <QThreadPool> +#include <QTime> + +DiceRollerNode::DiceRollerNode(qint64 max, qint64 min) + : m_max(max), m_diceResult(new DiceResult()), m_min(min), m_operator(Dice::ArithmeticOperator::PLUS) +{ + m_result= m_diceResult; +} +void DiceRollerNode::run(ExecutionNode* previous) +{ + m_previousNode= previous; + if(nullptr != previous) + { + Result* result= previous->getResult(); + if(nullptr != result) + { + auto num= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); + if(num <= 0) + { + m_errors.insert(Dice::ERROR_CODE::NO_DICE_TO_ROLL, QObject::tr("No dice to roll")); + } + m_diceCount= num > 0 ? static_cast<quint64>(num) : 0; + m_result->setPrevious(result); + + auto possibleValue= static_cast<quint64>(std::abs((m_max - m_min) + 1)); + if(possibleValue < m_diceCount && m_unique) + { + m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, + QObject::tr("More unique values asked than possible values (D operator)")); + return; + } + + for(quint64 i= 0; i < m_diceCount; ++i) + { + Die* die= new Die(); + die->setOp(m_operator); + die->setBase(m_min); + die->setMaxValue(m_max); + die->roll(); + if(m_unique) + { + const auto& equal= [](const Die* a, const Die* b) { return a->getValue() == b->getValue(); }; + while(m_diceResult->contains(die, equal)) + { + die->roll(false); + } + } + m_diceResult->insertResult(die); + } + if(nullptr != m_nextNode) + { + m_nextNode->run(this); + } + } + } +} + +quint64 DiceRollerNode::getFaces() const +{ + return static_cast<quint64>(std::abs(m_max - m_min) + 1); +} + +std::pair<qint64, qint64> DiceRollerNode::getRange() const +{ + return std::make_pair(m_min, m_max); +} +QString DiceRollerNode::toString(bool wl) const +{ + if(wl) + { + return QString("%1 [label=\"DiceRollerNode faces: %2\"]").arg(m_id).arg(getFaces()); + } + else + { + return m_id; + } +} +qint64 DiceRollerNode::getPriority() const +{ + qint64 priority= 4; + // if(nullptr!=m_nextNode) + // { + // priority = m_nextNode->getPriority(); + // } + return priority; +} +ExecutionNode* DiceRollerNode::getCopy() const +{ + DiceRollerNode* node= new DiceRollerNode(m_max, m_min); + if(nullptr != m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; +} + +Dice::ArithmeticOperator DiceRollerNode::getOperator() const +{ + return m_operator; +} + +void DiceRollerNode::setOperator(const Dice::ArithmeticOperator& dieOperator) +{ + m_operator= dieOperator; + m_diceResult->setOperator(dieOperator); +} + +bool DiceRollerNode::getUnique() const +{ + return m_unique; +} + +void DiceRollerNode::setUnique(bool unique) +{ + m_unique= unique; +} |