diff options
| -rw-r--r-- | diceparser.cpp | 27 | ||||
| -rw-r--r-- | diceparser.h | 3 | ||||
| -rw-r--r-- | node/occurencecountnode.cpp | 112 | ||||
| -rw-r--r-- | node/occurencecountnode.h | 11 | ||||
| -rw-r--r-- | parsingtoolbox.cpp | 11 | ||||
| -rw-r--r-- | parsingtoolbox.h | 2 |
6 files changed, 164 insertions, 2 deletions
diff --git a/diceparser.cpp b/diceparser.cpp index 5ec7122..9f018b4 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -39,6 +39,7 @@ #include "node/listsetrollnode.h" #include "node/mergenode.h" #include "node/numbernode.h" +#include "node/occurencecountnode.h" #include "node/paintnode.h" #include "node/parenthesesnode.h" #include "node/rerolldicenode.h" @@ -78,6 +79,7 @@ DiceParser::DiceParser() m_OptionOp->insert(QStringLiteral("u"), Split); m_OptionOp->insert(QStringLiteral("g"), Group); m_OptionOp->insert(QStringLiteral("b"), Bind); + m_OptionOp->insert(QStringLiteral("o"), Occurences); m_aliasList= new QList<DiceAlias*>(); @@ -1113,6 +1115,31 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, found= true; } break; + case Occurences: + { + qint64 number= 0; + auto occNode= new OccurenceCountNode(); + if(m_parsingToolbox->readNumber(str, number)) + { + occNode->setWidth(number); + /*Validator* validator= m_parsingToolbox->readCompositeValidator(str); + if(validator) + { + occNode->setValidator(validator); + }*/ + if(m_parsingToolbox->readComma(str)) + { + if(m_parsingToolbox->readNumber(str, number)) + { + occNode->setHeight(number); + previous->setNextNode(occNode); + node= occNode; + found= true; + } + } + } + } + break; case Painter: { PainterNode* painter= new PainterNode(); diff --git a/diceparser.h b/diceparser.h index bb3f5f3..ca50d10 100644 --- a/diceparser.h +++ b/diceparser.h @@ -90,7 +90,8 @@ public: Filter, Split, Group, - Bind + Bind, + Occurences }; /** * @brief The CommandOperator enum diff --git a/node/occurencecountnode.cpp b/node/occurencecountnode.cpp index 6343e8e..8826188 100644 --- a/node/occurencecountnode.cpp +++ b/node/occurencecountnode.cpp @@ -1,6 +1,116 @@ #include "occurencecountnode.h" +#include "result/diceresult.h" +#include "result/stringresult.h" -OccurenceCountNode::OccurenceCountNode() +OccurenceCountNode::OccurenceCountNode() : ExecutionNode() { + m_stringResult= new StringResult(); + m_result= m_stringResult; +} + +void OccurenceCountNode::run(ExecutionNode* previous) +{ + m_previousNode= previous; + std::map<qint64, qint64> mapOccurence; + if(nullptr == m_previousNode) + return; + + DiceResult* previousDiceResult= dynamic_cast<DiceResult*>(m_previousNode->getResult()); + // m_diceResult->setPrevious(previousDiceResult); + if(nullptr == previousDiceResult) + return; + + auto const& diceList= previousDiceResult->getResultList(); + QVector<qint64> vec; + + for(auto dice : diceList) + { + auto val= dice->getValue(); + vec << val; + auto it= mapOccurence.find(val); + if(it == mapOccurence.end()) + mapOccurence[val]= 1; + else + mapOccurence[val]+= 1; + } + + std::sort(vec.begin(), vec.end()); + + QStringList list; + for(auto key : mapOccurence) + { + if(key.first >= m_height && key.second >= m_width) + list << QStringLiteral("%1x%2").arg(key.first).arg(key.second); + } + + QStringList resultList; + std::for_each(vec.begin(), vec.end(), [&resultList](qint64 val) { resultList << QString::number(val); }); + + QString result; + + if(!list.isEmpty()) + result= list.join(','); + else + result= QObject::tr("No matching result"); + + m_stringResult->setText(QStringLiteral("%1 - [%2]").arg(result).arg(resultList.join(','))); + + if(nullptr != m_nextNode) + { + m_nextNode->run(this); + } +} +QString OccurenceCountNode::toString(bool label) const +{ + if(label) + { + return QString("%1 [label=\"OccurenceCountNode %2\"]").arg(m_id); + } + else + { + return m_id; + } +} +ExecutionNode* OccurenceCountNode::getCopy() const +{ + return nullptr; +} +qint64 OccurenceCountNode::getPriority() const +{ + qint64 priority= 0; + if(nullptr != m_previousNode) + { + priority= m_previousNode->getPriority(); + } + return priority; +} + +qint64 OccurenceCountNode::getWidth() const +{ + return m_width; +} + +void OccurenceCountNode::setWidth(const qint64& width) +{ + m_width= width; +} + +qint64 OccurenceCountNode::getHeight() const +{ + return m_height; +} + +void OccurenceCountNode::setHeight(const qint64& height) +{ + m_height= height; +} +Validator* OccurenceCountNode::getValidator() const +{ + return m_validator; +} + +void OccurenceCountNode::setValidator(Validator* validator) +{ + m_validator= validator; } diff --git a/node/occurencecountnode.h b/node/occurencecountnode.h index 9c9aa13..ed7f8b4 100644 --- a/node/occurencecountnode.h +++ b/node/occurencecountnode.h @@ -4,6 +4,7 @@ #include "executionnode.h" #include "validator.h" +class StringResult; class OccurenceCountNode : public ExecutionNode { public: @@ -15,10 +16,20 @@ public: ExecutionNode* getCopy() const; qint64 getPriority() const; + qint64 getWidth() const; + void setWidth(const qint64 &width); + + qint64 getHeight() const; + void setHeight(const qint64 &height); + + Validator *getValidator() const; + void setValidator(Validator *validator); + private: qint64 m_width=0; qint64 m_height=0; Validator* m_validator; + StringResult* m_stringResult; }; #endif // OCCURENCECOUNTNODE_H diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index 2b77fe2..ffc4a60 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -517,6 +517,16 @@ bool ParsingToolBox::readVariable(QString& str, qint64& myNumber, QString& reaso return false; } +bool ParsingToolBox::readComma(QString& str) +{ + if(str.startsWith(",")) + { + str= str.remove(0, 1); + return true; + } + else + return false; +} bool ParsingToolBox::readOpenParentheses(QString& str) { if(str.startsWith("(")) @@ -527,6 +537,7 @@ bool ParsingToolBox::readOpenParentheses(QString& str) else return false; } + bool ParsingToolBox::readCloseParentheses(QString& str) { if(str.startsWith(")")) diff --git a/parsingtoolbox.h b/parsingtoolbox.h index 4b50b6d..7bb6059 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -232,6 +232,8 @@ public: static void readSubtitutionParameters(SubtituteInfo& info, QString& rest); + static bool readComma(QString& str); + private: QMap<QString, BooleanCondition::LogicOperator>* m_logicOp; QMap<QString, CompositeValidator::LogicOperation>* m_logicOperation; |