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

0001 /***************************************************************************
0002  * Copyright (C) 2019 by Renaud Guezennec                                   *
0003  * http://www.rolisteam.org/contact                      *
0004  *                                                                          *
0005  *  This file is part of DiceParser                                         *
0006  *                                                                          *
0007  * DiceParser is free software; you can redistribute it and/or modify       *
0008  * it under the terms of the GNU General Public License as published by     *
0009  * the Free Software Foundation; either version 2 of the License, or        *
0010  * (at your option) any later version.                                      *
0011  *                                                                          *
0012  * This program is distributed in the hope that it will be useful,          *
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of           *
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
0015  * GNU General Public License for more details.                             *
0016  *                                                                          *
0017  * You should have received a copy of the GNU General Public License        *
0018  * along with this program; if not, write to the                            *
0019  * Free Software Foundation, Inc.,                                          *
0020  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.                 *
0021  ***************************************************************************/
0022 #include "node/repeaternode.h"
0023 
0024 #include "executionnode.h"
0025 #include "result/scalarresult.h"
0026 #include "result/stringresult.h"
0027 #include <QDebug>
0028 #include <diceparser/diceparserhelper.h>
0029 #include <diceparser/parsingtoolbox.h>
0030 
0031 using InstructionSet= std::vector<ExecutionNode*>;
0032 
0033 QStringList allFirstResultAsString(const InstructionSet& startingNodes, bool& hasAlias)
0034 {
0035     ParsingToolBox parsingBox;
0036     // QStringList allResult;
0037     QStringList stringListResult;
0038     for(auto node : startingNodes)
0039     {
0040         auto pair= parsingBox.hasResultOfType(Dice::RESULT_TYPE::STRING, node);
0041         auto pairStr= parsingBox.hasResultOfType(Dice::RESULT_TYPE::SCALAR, node, true);
0042         if(pair.first)
0043         {
0044             stringListResult << pair.second.toString();
0045             hasAlias= true;
0046         }
0047         else if(pairStr.first)
0048         {
0049             stringListResult << QString::number(pairStr.second.toReal());
0050             hasAlias= true;
0051         }
0052     }
0053     return stringListResult;
0054 }
0055 
0056 std::vector<ExecutionNode*> makeCopy(std::vector<ExecutionNode*> cmds)
0057 {
0058     std::vector<ExecutionNode*> copy;
0059     std::transform(cmds.begin(), cmds.end(), std::back_inserter(copy),
0060                    [](ExecutionNode* node) { return node->getCopy(); });
0061     return copy;
0062 }
0063 
0064 RepeaterNode::RepeaterNode() {}
0065 
0066 void RepeaterNode::run(ExecutionNode* previousNode)
0067 {
0068     m_previousNode= previousNode;
0069 
0070     if(nullptr == m_times || m_cmd.empty())
0071         return;
0072 
0073     m_times->run(this);
0074     m_times= ParsingToolBox::getLeafNode(m_times);
0075     auto times= m_times->getResult();
0076     if(!times)
0077         return;
0078 
0079     std::vector<InstructionSet> m_startingNodes;
0080     auto timeCount= times->getResult(Dice::RESULT_TYPE::SCALAR).toInt();
0081     auto cmd= makeCopy(m_cmd);
0082     std::vector<Result*> resultVec;
0083     for(int i= 0; i < timeCount; ++i)
0084     {
0085         m_startingNodes.push_back(cmd);
0086         std::for_each(cmd.begin(), cmd.end(),
0087                       [this, &resultVec](ExecutionNode* node)
0088                       {
0089                           node->run(this);
0090                           auto end= ParsingToolBox::getLeafNode(node);
0091                           auto leafResult= end->getResult();
0092 
0093                           if(nullptr == leafResult)
0094                               return;
0095 
0096                           resultVec.push_back(leafResult);
0097                       });
0098         cmd= makeCopy(m_cmd);
0099     }
0100     if(m_sumAll)
0101     {
0102         auto scalar= new ScalarResult();
0103         qreal value= 0.0;
0104         std::for_each(resultVec.begin(), resultVec.end(),
0105                       [&value](Result* result) { value+= result->getResult(Dice::RESULT_TYPE::SCALAR).toDouble(); });
0106         scalar->setValue(value);
0107         m_result= scalar;
0108     }
0109     else
0110     {
0111         auto string= new StringResult();
0112 
0113         QStringList listOfStrResult;
0114         for(auto instructions : m_startingNodes)
0115         {
0116             ParsingToolBox parsingBox;
0117             parsingBox.setStartNodes(instructions);
0118             auto finalString
0119                 = parsingBox.finalStringResult([](const QString& result, const QString&, bool) { return result; });
0120             listOfStrResult << finalString;
0121         }
0122         if(!listOfStrResult.isEmpty())
0123             string->addText(listOfStrResult.join('\n'));
0124 
0125         m_result= string;
0126 
0127         // qDebug().noquote() << listOfStrResult.join('\n');
0128     }
0129 
0130     if(nullptr != m_nextNode)
0131         m_nextNode->run(this);
0132 }
0133 
0134 QString RepeaterNode::toString(bool withLabel) const
0135 {
0136     return withLabel ? QStringLiteral("") : QStringLiteral("");
0137 }
0138 
0139 qint64 RepeaterNode::getPriority() const
0140 {
0141     return 4;
0142 }
0143 
0144 ExecutionNode* RepeaterNode::getCopy() const
0145 {
0146     return nullptr;
0147 }
0148 
0149 void RepeaterNode::setCommand(const std::vector<ExecutionNode*>& cmd)
0150 {
0151     m_cmd= cmd;
0152 }
0153 
0154 void RepeaterNode::setTimeNode(ExecutionNode* time)
0155 {
0156     m_times= time;
0157 }
0158 
0159 void RepeaterNode::setSumAll(bool b)
0160 {
0161     m_sumAll= b;
0162 }