File indexing completed on 2024-05-12 05:39:27
0001 #include "dicerollernode.h" 0002 #include "die.h" 0003 0004 #include <QDebug> 0005 #include <QThread> 0006 #include <QThreadPool> 0007 #include <QTime> 0008 0009 DiceRollerNode::DiceRollerNode(qint64 max, qint64 min) 0010 : m_max(max), m_diceResult(new DiceResult()), m_min(min), m_operator(Dice::ArithmeticOperator::PLUS) 0011 { 0012 m_result= m_diceResult; 0013 } 0014 void DiceRollerNode::run(ExecutionNode* previous) 0015 { 0016 m_previousNode= previous; 0017 if(nullptr != previous) 0018 { 0019 Result* result= previous->getResult(); 0020 if(nullptr != result) 0021 { 0022 auto num= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); 0023 if(num <= 0) 0024 { 0025 m_errors.insert(Dice::ERROR_CODE::NO_DICE_TO_ROLL, QObject::tr("No dice to roll")); 0026 } 0027 m_diceCount= num > 0 ? static_cast<quint64>(num) : 0; 0028 m_result->setPrevious(result); 0029 0030 auto possibleValue= static_cast<quint64>(std::abs((m_max - m_min) + 1)); 0031 if(possibleValue < m_diceCount && m_unique) 0032 { 0033 m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, 0034 QObject::tr("More unique values asked than possible values (D operator)")); 0035 return; 0036 } 0037 0038 for(quint64 i= 0; i < m_diceCount; ++i) 0039 { 0040 Die* die= new Die(); 0041 die->setOp(m_operator); 0042 die->setBase(m_min); 0043 die->setMaxValue(m_max); 0044 die->roll(); 0045 if(m_unique) 0046 { 0047 const auto& equal= [](const Die* a, const Die* b) { return a->getValue() == b->getValue(); }; 0048 while(m_diceResult->contains(die, equal)) 0049 { 0050 die->roll(false); 0051 } 0052 } 0053 m_diceResult->insertResult(die); 0054 } 0055 if(nullptr != m_nextNode) 0056 { 0057 m_nextNode->run(this); 0058 } 0059 } 0060 } 0061 } 0062 0063 quint64 DiceRollerNode::getFaces() const 0064 { 0065 return static_cast<quint64>(std::abs(m_max - m_min) + 1); 0066 } 0067 0068 std::pair<qint64, qint64> DiceRollerNode::getRange() const 0069 { 0070 return std::make_pair(m_min, m_max); 0071 } 0072 QString DiceRollerNode::toString(bool wl) const 0073 { 0074 if(wl) 0075 { 0076 return QString("%1 [label=\"DiceRollerNode faces: %2\"]").arg(m_id).arg(getFaces()); 0077 } 0078 else 0079 { 0080 return m_id; 0081 } 0082 } 0083 qint64 DiceRollerNode::getPriority() const 0084 { 0085 qint64 priority= 4; 0086 // if(nullptr!=m_nextNode) 0087 // { 0088 // priority = m_nextNode->getPriority(); 0089 // } 0090 return priority; 0091 } 0092 ExecutionNode* DiceRollerNode::getCopy() const 0093 { 0094 DiceRollerNode* node= new DiceRollerNode(m_max, m_min); 0095 if(nullptr != m_nextNode) 0096 { 0097 node->setNextNode(m_nextNode->getCopy()); 0098 } 0099 return node; 0100 } 0101 0102 Dice::ArithmeticOperator DiceRollerNode::getOperator() const 0103 { 0104 return m_operator; 0105 } 0106 0107 void DiceRollerNode::setOperator(const Dice::ArithmeticOperator& dieOperator) 0108 { 0109 m_operator= dieOperator; 0110 m_diceResult->setOperator(dieOperator); 0111 } 0112 0113 bool DiceRollerNode::getUnique() const 0114 { 0115 return m_unique; 0116 } 0117 0118 void DiceRollerNode::setUnique(bool unique) 0119 { 0120 m_unique= unique; 0121 }