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

0001 /***************************************************************************
0002  *   Copyright (C) 2018 by Renaud Guezennec                                *
0003  *   https://rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   rolisteam 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 "occurencecountnode.h"
0021 #include "result/diceresult.h"
0022 #include "result/stringresult.h"
0023 #include "validatorlist.h"
0024 #include <QVector>
0025 
0026 OccurenceCountNode::OccurenceCountNode() : ExecutionNode() {}
0027 
0028 void OccurenceCountNode::run(ExecutionNode* previous)
0029 {
0030     m_previousNode= previous;
0031     std::map<qint64, qint64> mapOccurence;
0032     if(nullptr == m_previousNode)
0033         return;
0034 
0035     DiceResult* previousDiceResult= dynamic_cast<DiceResult*>(m_previousNode->getResult());
0036     if(nullptr == previousDiceResult)
0037         return;
0038 
0039     auto const& diceList= previousDiceResult->getResultList();
0040     QVector<qint64> vec;
0041 
0042     for(auto dice : diceList)
0043     {
0044         auto val= dice->getValue();
0045 
0046         vec << val;
0047         auto it= mapOccurence.find(val);
0048         if(it == mapOccurence.end())
0049             mapOccurence[val]= 1;
0050         else
0051             mapOccurence[val]+= 1;
0052     }
0053 
0054     std::sort(vec.begin(), vec.end());
0055     if(nullptr == m_nextNode)
0056     {
0057         runForStringResult(mapOccurence, vec);
0058     }
0059     else
0060     {
0061         runForDiceResult(mapOccurence);
0062     }
0063 }
0064 QString OccurenceCountNode::toString(bool label) const
0065 {
0066     if(label)
0067     {
0068         return QString("%1 [label=\"OccurenceCountNode %2\"]").arg(m_id);
0069     }
0070     else
0071     {
0072         return m_id;
0073     }
0074 }
0075 ExecutionNode* OccurenceCountNode::getCopy() const
0076 {
0077     return nullptr;
0078 }
0079 qint64 OccurenceCountNode::getPriority() const
0080 {
0081     qint64 priority= 0;
0082 
0083     if(nullptr != m_previousNode)
0084     {
0085         priority= m_previousNode->getPriority();
0086     }
0087     return priority;
0088 }
0089 
0090 qint64 OccurenceCountNode::getWidth() const
0091 {
0092     return m_width;
0093 }
0094 
0095 void OccurenceCountNode::setWidth(const qint64& width)
0096 {
0097     m_width= width;
0098 }
0099 
0100 qint64 OccurenceCountNode::getHeight() const
0101 {
0102     return m_height;
0103 }
0104 
0105 void OccurenceCountNode::setHeight(const qint64& height)
0106 {
0107     m_height= height;
0108 }
0109 
0110 ValidatorList* OccurenceCountNode::getValidatorList() const
0111 {
0112     return m_validatorList;
0113 }
0114 
0115 void OccurenceCountNode::setValidatorList(ValidatorList* validatorlist)
0116 {
0117     m_validatorList= validatorlist;
0118 }
0119 void OccurenceCountNode::runForStringResult(const std::map<qint64, qint64>& mapOccurence, QVector<qint64>& vec)
0120 {
0121     m_stringResult= new StringResult();
0122     m_result= m_stringResult;
0123     QStringList list;
0124     for(auto key : mapOccurence)
0125     {
0126         if(nullptr != m_validatorList)
0127         {
0128             Die die;
0129             die.insertRollValue(key.first);
0130             if(!m_validatorList->hasValid(&die, true))
0131                 continue;
0132         }
0133 
0134         if(key.second < m_width)
0135             continue;
0136 
0137         if(key.first >= m_height)
0138             list << QStringLiteral("%1x%2").arg(key.second).arg(key.first);
0139     }
0140 
0141     QStringList resultList;
0142     std::for_each(vec.begin(), vec.end(), [&resultList](qint64 val) { resultList << QString::number(val); });
0143 
0144     QString result;
0145 
0146     if(!list.isEmpty())
0147         result= list.join(',');
0148     else
0149         result= QObject::tr("No matching result");
0150 
0151     m_stringResult->addText(QStringLiteral("%1 - [%2]").arg(result).arg(resultList.join(',')));
0152     m_stringResult->finished();
0153 }
0154 void OccurenceCountNode::runForDiceResult(const std::map<qint64, qint64>& mapOccurence)
0155 {
0156     m_diceResult= new DiceResult();
0157     m_result= m_diceResult;
0158     QStringList list;
0159     for(auto key : mapOccurence)
0160     {
0161         if(nullptr != m_validatorList)
0162         {
0163             Die die;
0164             die.insertRollValue(key.first);
0165             if(!m_validatorList->hasValid(&die, true))
0166                 continue;
0167         }
0168 
0169         if(key.second < m_width)
0170             continue;
0171 
0172         if(key.first >= m_height)
0173         {
0174             // list << QStringLiteral("%1x%2").arg(key.second).arg(key.first);
0175             Die* die= new Die();
0176             die->insertRollValue(key.second * key.first);
0177             m_diceResult->insertResult(die);
0178         }
0179     }
0180 
0181     if(nullptr != m_nextNode)
0182     {
0183         m_nextNode->run(this);
0184     }
0185 }