File indexing completed on 2024-05-12 05:39:28

0001 #include <diceparser/parsingtoolbox.h>
0002 
0003 #include "rerolldicenode.h"
0004 #include "validatorlist.h"
0005 #include <utility>
0006 
0007 RerollDiceNode::RerollDiceNode(bool reroll, bool addingMode)
0008     : m_diceResult(new DiceResult()), m_validatorList(nullptr), m_reroll(reroll), m_adding(addingMode)
0009 {
0010     m_result= m_diceResult;
0011 }
0012 RerollDiceNode::~RerollDiceNode()
0013 {
0014     if(nullptr != m_validatorList)
0015     {
0016         delete m_validatorList;
0017         m_validatorList= nullptr;
0018     }
0019 }
0020 void RerollDiceNode::run(ExecutionNode* previous)
0021 {
0022     m_previousNode= previous;
0023     if((nullptr != previous) && (nullptr != previous->getResult()))
0024     {
0025         DiceResult* previous_result= dynamic_cast<DiceResult*>(previous->getResult());
0026         m_result->setPrevious(previous_result);
0027         if(nullptr != previous_result)
0028         {
0029             for(auto& die : previous_result->getResultList())
0030             {
0031                 Die* tmpdie= new Die(*die);
0032                 m_diceResult->insertResult(tmpdie);
0033                 die->displayed();
0034             }
0035             // m_diceResult->setResultList(list);
0036 
0037             QList<Die*>& list= m_diceResult->getResultList();
0038             QList<Die*> toRemove;
0039 
0040             for(auto& die : list)
0041             {
0042                 bool finished= false;
0043                 auto state= m_validatorList->isValidRangeSize(
0044                     std::make_pair<qint64, qint64>(die->getBase(), die->getMaxValue()));
0045                 if((Dice::CONDITION_STATE::ALWAYSTRUE == state && m_adding)
0046                    || (!m_reroll && !m_adding && state == Dice::CONDITION_STATE::UNREACHABLE))
0047                 {
0048                     m_errors.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR,
0049                                     QObject::tr("Condition (%1) cause an endless loop with this dice: %2")
0050                                         .arg(toString(true))
0051                                         .arg(QStringLiteral("d[%1,%2]")
0052                                                  .arg(static_cast<int>(die->getBase()))
0053                                                  .arg(static_cast<int>(die->getMaxValue()))));
0054                     continue;
0055                 }
0056                 while(m_validatorList->hasValid(die, false) && !finished)
0057                 {
0058                     if(m_instruction != nullptr)
0059                     {
0060                         m_instruction->run(this);
0061                         auto lastNode= ParsingToolBox::getLeafNode(m_instruction);
0062                         if(lastNode != nullptr)
0063                         {
0064                             auto lastResult= dynamic_cast<DiceResult*>(lastNode->getResult());
0065                             if(lastResult != nullptr)
0066                             {
0067                                 toRemove.append(die);
0068                                 list.append(lastResult->getResultList());
0069                                 lastResult->clear();
0070                             }
0071                         }
0072                     }
0073                     else
0074                     {
0075                         die->roll(m_adding);
0076                     }
0077                     if(m_reroll)
0078                     {
0079                         finished= true;
0080                     }
0081                 }
0082             }
0083 
0084             for(auto die : toRemove)
0085             {
0086                 list.removeOne(die);
0087             }
0088 
0089             if(nullptr != m_nextNode)
0090             {
0091                 m_nextNode->run(this);
0092             }
0093         }
0094         else
0095         {
0096             m_errors.insert(
0097                 Dice::ERROR_CODE::DIE_RESULT_EXPECTED,
0098                 QObject::tr(
0099                     " The a operator expects dice result. Please check the documentation and fix your command."));
0100         }
0101     }
0102 }
0103 void RerollDiceNode::setValidatorList(ValidatorList* val)
0104 {
0105     m_validatorList= val;
0106 }
0107 QString RerollDiceNode::toString(bool wl) const
0108 {
0109     if(wl)
0110     {
0111         return QString("%1 [label=\"RerollDiceNode validatior: %2\"]").arg(m_id, m_validatorList->toString());
0112     }
0113     else
0114     {
0115         return m_id;
0116     }
0117     // return QString("RerollDiceNode [label=\"RerollDiceNode validatior:%1\"");
0118 }
0119 qint64 RerollDiceNode::getPriority() const
0120 {
0121     qint64 priority= 0;
0122     if(nullptr != m_nextNode)
0123     {
0124         priority= m_nextNode->getPriority();
0125     }
0126 
0127     return priority;
0128 }
0129 ExecutionNode* RerollDiceNode::getCopy() const
0130 {
0131     RerollDiceNode* node= new RerollDiceNode(m_reroll, m_adding);
0132     node->setValidatorList(m_validatorList);
0133     if(nullptr != m_nextNode)
0134     {
0135         node->setNextNode(m_nextNode->getCopy());
0136     }
0137     return node;
0138 }
0139 
0140 ExecutionNode* RerollDiceNode::getInstruction() const
0141 {
0142     return m_instruction;
0143 }
0144 
0145 void RerollDiceNode::setInstruction(ExecutionNode* instruction)
0146 {
0147     m_instruction= instruction;
0148 }