File indexing completed on 2024-05-12 05:39:28
0001 /*************************************************************************** 0002 * Copyright (C) 2021 by Renaud Guezennec * 0003 * http://www.rolisteam.org/contact * 0004 * * 0005 * This software is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "replacevaluenode.h" 0021 0022 #include "diceresult.h" 0023 #include <QDebug> 0024 #include <diceparser/parsingtoolbox.h> 0025 0026 ReplaceValueNode::ReplaceValueNode() : m_diceResult(new DiceResult) 0027 { 0028 m_result= m_diceResult; 0029 } 0030 0031 void ReplaceValueNode::setStopAtFirt(bool b) 0032 { 0033 m_stopAtFirst= b; 0034 } 0035 0036 void ReplaceValueNode::run(ExecutionNode* previous) 0037 { 0038 m_previousNode= previous; 0039 if(nullptr == previous) 0040 { 0041 m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, 0042 QStringLiteral("No previous node before Switch/Case operator")); 0043 return; 0044 } 0045 auto previousResult= previous->getResult(); 0046 m_result->setPrevious(previousResult); 0047 0048 if(nullptr == previousResult 0049 || (!previousResult->hasResultOfType(Dice::RESULT_TYPE::SCALAR) 0050 && !previousResult->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST))) 0051 { 0052 m_errors.insert(Dice::ERROR_CODE::NO_VALID_RESULT, 0053 QStringLiteral("No scalar or dice result before Switch/Case operator")); 0054 return; 0055 } 0056 0057 QList<Die*> dieList; 0058 if(previousResult->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) 0059 { 0060 auto diceResult= dynamic_cast<DiceResult*>(previousResult); 0061 if(diceResult) 0062 dieList.append(diceResult->getResultList()); 0063 } 0064 0065 for(auto die : dieList) 0066 { 0067 QStringList resultList; 0068 for(auto const& info : qAsConst(m_branchList)) 0069 { 0070 if(info->validatorList) 0071 { 0072 auto res= info->validatorList->hasValid(die, false); 0073 if(!res) 0074 continue; 0075 } 0076 else if(!resultList.isEmpty()) 0077 break; 0078 0079 auto replaceValresult= info->node->getResult(); 0080 if(replaceValresult) 0081 die->replaceLastValue(replaceValresult->getResult(Dice::RESULT_TYPE::SCALAR).toInt()); 0082 break; 0083 } 0084 m_diceResult->insertResult(die); 0085 } 0086 0087 if(nullptr != m_nextNode) 0088 { 0089 m_nextNode->run(this); 0090 } 0091 } 0092 0093 QString ReplaceValueNode::toString(bool withLabel) const 0094 { 0095 if(withLabel) 0096 { 0097 return QString("%1 [label=\"ReplaceValueNode\"]").arg(m_id); 0098 } 0099 else 0100 { 0101 return m_id; 0102 } 0103 } 0104 0105 qint64 ReplaceValueNode::getPriority() const 0106 { 0107 qint64 priority= 0; 0108 if(nullptr != m_previousNode) 0109 { 0110 priority= m_previousNode->getPriority(); 0111 } 0112 return priority; 0113 } 0114 0115 ExecutionNode* ReplaceValueNode::getCopy() const 0116 { 0117 ReplaceValueNode* node= new ReplaceValueNode(); 0118 for(auto const& info : qAsConst(m_branchList)) 0119 { 0120 node->insertCase(info->node, info->validatorList); 0121 } 0122 0123 if(nullptr != m_nextNode) 0124 { 0125 node->setNextNode(m_nextNode->getCopy()); 0126 } 0127 return node; 0128 } 0129 0130 void ReplaceValueNode::insertCase(ExecutionNode* node, ValidatorList* validator) 0131 { 0132 std::unique_ptr<Dice::CaseInfo> info(new Dice::CaseInfo{validator, node}); 0133 m_branchList.push_back(std::move(info)); 0134 }