From f17d771c68dbb80708880e186d14c7e707c1bf4f Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 24 Dec 2015 11:19:55 +0100 Subject: -Adding mergenode to the project. --- node/mergenode.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ node/mergenode.h | 43 ++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 node/mergenode.cpp create mode 100644 node/mergenode.h diff --git a/node/mergenode.cpp b/node/mergenode.cpp new file mode 100644 index 0000000..7068cf5 --- /dev/null +++ b/node/mergenode.cpp @@ -0,0 +1,78 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://renaudguezennec.homelinux.org/accueil,3.html * +* * +* This file is part of DiceParser * +* * +* DiceParser is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the * +* Free Software Foundation, Inc., * +* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * +***************************************************************************/ +#include "mergenode.h" + +MergeNode::MergeNode() + : m_diceResult(new DiceResult()) +{ + m_result = m_diceResult; +} +void MergeNode::run(ExecutionNode* previous) +{ + m_previousNode = previous; + if(NULL!=previous) + { + m_result->setPrevious(previous->getResult()); + } + Result* tmpResult = previous->getResult(); + while(NULL!=tmpResult) + { + DiceResult* dice = dynamic_cast(tmpResult); + if(NULL!=dice) + { + foreach(Die* die, dice->getResultList()) + { + if(!m_diceResult->getResultList().contains(die)) + { + m_diceResult->getResultList().append(die); + } + } + } + tmpResult = tmpResult->getPrevious(); + } + + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } +} + +QString MergeNode::toString(bool withLabel) const +{ + if(withLabel) + { + return QString("%1 [label=\"Merge Node %2\"]").arg(m_id).arg(m_number); + } + else + { + return m_id; + } +} +qint64 MergeNode::getPriority() const +{ + qint64 priority=0; + if(NULL!=m_nextNode) + { + priority = m_nextNode->getPriority(); + } + return priority; +} diff --git a/node/mergenode.h b/node/mergenode.h new file mode 100644 index 0000000..347975f --- /dev/null +++ b/node/mergenode.h @@ -0,0 +1,43 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://renaudguezennec.homelinux.org/accueil,3.html * +* * +* This file is part of DiceParser * +* * +* DiceParser is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the * +* Free Software Foundation, Inc., * +* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * +***************************************************************************/ +#ifndef MERGENODE_H +#define MERGENODE_H + +#include "node/executionnode.h" +#include "result/diceresult.h" + +/** + * @brief The MergeNode class is an ExecutionNode. It is dedicated to merge result of several commands. + */ +class MergeNode : public ExecutionNode +{ +public: + MergeNode(); + void run(ExecutionNode* previous); + virtual QString toString(bool withLabel)const; + virtual qint64 getPriority() const; +private: + qint64 m_number; + DiceResult* m_diceResult; +}; + +#endif // NUMBERNODE_H -- cgit v1.2.3-70-g09d2 From 536ba83d1ee1daf44f8c7d5f67fb55dcce696743 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 24 Dec 2015 11:20:13 +0100 Subject: -Adding rules to compile new file. --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9aae367..977d1f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ add_executable( node/executionnode.cpp node/explosedicenode.cpp node/helpnode.cpp + node/mergenode.cpp node/jumpbackwardnode.cpp node/keepdiceexecnode.cpp node/listaliasnode.cpp -- cgit v1.2.3-70-g09d2 From 0332e66a2ff0fed8a307d2ec1deddaf4dc00c75e Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 24 Dec 2015 11:20:57 +0100 Subject: Start modification for the display function when displaying result. --- cli/main.cpp | 128 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 88 insertions(+), 40 deletions(-) diff --git a/cli/main.cpp b/cli/main.cpp index 9a966ba..80c19a9 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -43,56 +43,103 @@ QTextStream out(stdout, QIODevice::WriteOnly); -QString diceToText(ExportedDiceResult& dice,bool highlight) +QString diceToText(ExportedDiceResult& dice,bool highlight,bool homogeneous) { QStringList resultGlobal; - foreach(int face, dice.keys()) + if(homogeneous) { - QStringList result; - ListDiceResult diceResult = dice.value(face); - //patternColor = patternColorarg(); - foreach (DiceAndHighlight tmp, diceResult) - { - QStringList diceListStr; - QStringList diceListChildren; - + foreach(int face, dice.keys()) + { + QStringList result; + ListDiceResult diceResult = dice.value(face); + //patternColor = patternColorarg(); + foreach (DiceAndHighlight tmp, diceResult) + { + QStringList diceListStr; + QStringList diceListChildren; - for(int i =0; i < tmp.first.size(); ++i) - { - qint64 dievalue = tmp.first[i]; - QString prefix("%1"); - if((tmp.second)&&(highlight)) + for(int i =0; i < tmp.first.size(); ++i) + { + qint64 dievalue = tmp.first[i]; + QString prefix("%1"); + + if((tmp.second)&&(highlight)) + { + prefix = "\e[0;31m%1\e[0m"; + } + + if(i==0) + { + diceListStr << prefix.arg(QString::number(dievalue)); + } + else + { + diceListChildren << prefix.arg(QString::number(dievalue)); + } + } + if(!diceListChildren.isEmpty()) { - prefix = "\e[0;31m%1\e[0m"; + diceListStr << QString("[%1]").arg(diceListChildren.join(' ')); } - if(i==0) + result << diceListStr.join(' '); + // qDebug() << result << tmp.first << tmp.second; + } + + if(dice.keys().size()>1) + { + resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face); + } + else + { + resultGlobal << result; + } + } + } + else + { + foreach(int face, dice.keys()) + { + QStringList result; + ListDiceResult diceResult = dice.value(face); + foreach (DiceAndHighlight tmp, diceResult) + { + QStringList diceListStr; + QStringList diceListChildren; + + + for(int i =0; i < tmp.first.size(); ++i) { - diceListStr << prefix.arg(QString::number(dievalue)); + qint64 dievalue = tmp.first[i]; + QString prefix("%1"); + + if((tmp.second)&&(highlight)) + { + prefix = "\e[0;31m%1\e[0m"; + } + + if(i==0) + { + diceListStr << prefix.arg(QString::number(dievalue)); + } + else + { + diceListChildren << prefix.arg(QString::number(dievalue)); + } } - else + if(!diceListChildren.isEmpty()) { - diceListChildren << prefix.arg(QString::number(dievalue)); + diceListStr << QString("[%1]").arg(diceListChildren.join(' ')); } - } - if(!diceListChildren.isEmpty()) - { - diceListStr << QString("[%1]").arg(diceListChildren.join(' ')); - } - - result << diceListStr.join(' '); - // qDebug() << result << tmp.first << tmp.second; - } - - if(dice.keys().size()>1) - { - resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face); - } - else - { - resultGlobal << result; - } + + result << diceListStr.join(' '); + // qDebug() << result << tmp.first << tmp.second; + } + + resultGlobal << QString(" (%1) ").arg(result.join(',')); + + } } return resultGlobal.join(' '); } @@ -114,8 +161,9 @@ void startDiceParsing(QString& cmd,QString& treeFile,bool highlight) } ExportedDiceResult list; - parser->getLastDiceResult(list); - QString diceText = diceToText(list,highlight); + bool homogeneous; + parser->getLastDiceResult(list,homogeneous); + QString diceText = diceToText(list,highlight,homogeneous); QString scalarText; QString str; -- cgit v1.2.3-70-g09d2 From 8572e4c63bf4c3b1decfb95e09960f899f198dd2 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 24 Dec 2015 11:21:35 +0100 Subject: -Add MergeOp and homogeneous status for result. --- diceparser.cpp | 17 ++++++++++++++++- diceparser.h | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/diceparser.cpp b/diceparser.cpp index d07e555..0937733 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -38,6 +38,7 @@ #include "node/jumpbackwardnode.h" #include "node/listsetrollnode.h" #include "node/listaliasnode.h" +#include "node/mergenode.h" #define DEFAULT_FACES_NUMBER 10 @@ -59,6 +60,7 @@ DiceParser::DiceParser() m_OptionOp->insert(QObject::tr("r"),Reroll); m_OptionOp->insert(QObject::tr("e"),Explosing); m_OptionOp->insert(QObject::tr("a"),RerollAndAdd); + m_OptionOp->insert(QObject::tr("m"),Merge); //m_OptionOp->insert(QObject::tr("@"),JumpBackward); @@ -443,7 +445,7 @@ QStringList DiceParser::getAllDiceResult(bool& hasAlias) return stringListResult; } -void DiceParser::getLastDiceResult(ExportedDiceResult& diceValues) +void DiceParser::getLastDiceResult(ExportedDiceResult& diceValues,bool& homogeneous) { ExecutionNode* next = getLeafNode(); Result* result=next->getResult(); @@ -455,6 +457,10 @@ void DiceParser::getLastDiceResult(ExportedDiceResult& diceValues) DiceResult* diceResult = dynamic_cast(result); if(NULL!=diceResult) { + if(homogeneous) + { + homogeneous = diceResult->isHomogeneous(); + } quint64 face=0; ListDiceResult listpair; foreach(Die* die, diceResult->getResultList()) @@ -935,7 +941,16 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous, bool hasDice)/ m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the e operator. Please, change it")); } } + break; + case Merge: + { + MergeNode* mergeNode = new MergeNode(); + previous->setNextNode(mergeNode); + node = mergeNode; + isFine = true; + } + break; } } diff --git a/diceparser.h b/diceparser.h index 6b9056f..045ec12 100644 --- a/diceparser.h +++ b/diceparser.h @@ -77,7 +77,7 @@ public: /** * @brief The OptionOperator enum gathering all options availables for result. */ - enum OptionOperator {KeepAndExplose,Keep,Reroll,Explosing,Sort,Count,RerollAndAdd}; + enum OptionOperator {KeepAndExplose,Keep,Reroll,Explosing,Sort,Count,RerollAndAdd,Merge}; /** * @brief The CommandOperator enum */ @@ -135,7 +135,7 @@ public: * @brief getLastDiceResult * @return */ - void getLastDiceResult(ExportedDiceResult& diceValues); + void getLastDiceResult(ExportedDiceResult& diceValues,bool& homogeneous); /** * @brief hasIntegerResultNotInFirst * @return -- cgit v1.2.3-70-g09d2 From 8f388a9ec72c7af6832a65ec3cfdd80fa7413492 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 24 Dec 2015 11:25:16 +0100 Subject: Adding new status "isHomogeneous" for diceresult. --- result/diceresult.cpp | 10 ++++++++++ result/diceresult.h | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/result/diceresult.cpp b/result/diceresult.cpp index ceb77b8..57e9da4 100644 --- a/result/diceresult.cpp +++ b/result/diceresult.cpp @@ -24,6 +24,7 @@ DiceResult::DiceResult() { m_resultTypes= (DICE_LIST); + m_homogeneous = true; } void DiceResult::insertResult(Die* die) { @@ -33,6 +34,15 @@ QList& DiceResult::getResultList() { return m_diceValues; } +bool DiceResult::isHomogeneous() const +{ + return m_homogeneous; +} +void DiceResult::setHomogeneous(bool b) +{ + m_homogeneous = b; +} + void DiceResult::setResultList(QList list) { qDeleteAll(m_diceValues.begin(), m_diceValues.end()); diff --git a/result/diceresult.h b/result/diceresult.h index 84a4621..fe24277 100644 --- a/result/diceresult.h +++ b/result/diceresult.h @@ -66,11 +66,20 @@ public: * @return */ virtual QString toString(bool wl); + /** + * @brief isHomogeneous + */ + bool isHomogeneous() const; + /** + * @brief setHomogeneous + */ + void setHomogeneous(bool); private: qreal getScalarResult(); private: QList m_diceValues; + bool m_homogeneous; }; #endif // DICERESULT_H -- cgit v1.2.3-70-g09d2 From a7f70afca994c1dbd37230459032c2eca9a99dc9 Mon Sep 17 00:00:00 2001 From: Renaud Date: Mon, 28 Dec 2015 20:58:47 +0100 Subject: manage homogeneous --- cli/main.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/cli/main.cpp b/cli/main.cpp index 80c19a9..dd21ea7 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -46,8 +46,6 @@ QTextStream out(stdout, QIODevice::WriteOnly); QString diceToText(ExportedDiceResult& dice,bool highlight,bool homogeneous) { QStringList resultGlobal; - if(homogeneous) - { foreach(int face, dice.keys()) { QStringList result; @@ -96,7 +94,7 @@ QString diceToText(ExportedDiceResult& dice,bool highlight,bool homogeneous) resultGlobal << result; } } - } + /* } else { foreach(int face, dice.keys()) @@ -140,7 +138,7 @@ QString diceToText(ExportedDiceResult& dice,bool highlight,bool homogeneous) resultGlobal << QString(" (%1) ").arg(result.join(',')); } - } + }*/ return resultGlobal.join(' '); } @@ -161,7 +159,7 @@ void startDiceParsing(QString& cmd,QString& treeFile,bool highlight) } ExportedDiceResult list; - bool homogeneous; + bool homogeneous = true; parser->getLastDiceResult(list,homogeneous); QString diceText = diceToText(list,highlight,homogeneous); QString scalarText; -- cgit v1.2.3-70-g09d2 From 6657f88c22fb5422541b80a15a2dc98f76ec03a0 Mon Sep 17 00:00:00 2001 From: Renaud Date: Mon, 28 Dec 2015 20:59:13 +0100 Subject: manage homogeneous --- diceparser.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/diceparser.cpp b/diceparser.cpp index 0937733..9bb4309 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -459,6 +459,7 @@ void DiceParser::getLastDiceResult(ExportedDiceResult& diceValues,bool& homogene { if(homogeneous) { + homogeneous = diceResult->isHomogeneous(); } quint64 face=0; -- cgit v1.2.3-70-g09d2 From 597d3790ce9eb10ae1f7d69bee74f5e46f4e1eb4 Mon Sep 17 00:00:00 2001 From: Renaud Date: Mon, 28 Dec 2015 21:01:05 +0100 Subject: manage homogeneous --- node/mergenode.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/node/mergenode.cpp b/node/mergenode.cpp index 7068cf5..5145bc8 100644 --- a/node/mergenode.cpp +++ b/node/mergenode.cpp @@ -39,6 +39,8 @@ void MergeNode::run(ExecutionNode* previous) DiceResult* dice = dynamic_cast(tmpResult); if(NULL!=dice) { + ///@todo improve here to set homogeneous while is really + m_diceResult->setHomogeneous(false); foreach(Die* die, dice->getResultList()) { if(!m_diceResult->getResultList().contains(die)) -- cgit v1.2.3-70-g09d2 From a5405c2f61184c16cb0e432dd741e9a0b365faa1 Mon Sep 17 00:00:00 2001 From: Renaud Date: Mon, 28 Dec 2015 21:02:45 +0100 Subject: manage homogeneous --- result/diceresult.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/result/diceresult.cpp b/result/diceresult.cpp index 57e9da4..26b8ef8 100644 --- a/result/diceresult.cpp +++ b/result/diceresult.cpp @@ -23,7 +23,7 @@ DiceResult::DiceResult() { - m_resultTypes= (DICE_LIST); + m_resultTypes= (DICE_LIST | SCALAR); m_homogeneous = true; } void DiceResult::insertResult(Die* die) @@ -73,6 +73,10 @@ QVariant DiceResult::getResult(RESULT_TYPE type) return QVariant(); } +/*bool DiceResult::hasResultOfType(RESULT_TYPE type) const +{ + return (m_resultTypes & type); +}*/ qreal DiceResult::getScalarResult() { if(m_diceValues.size()==1) -- cgit v1.2.3-70-g09d2 From 3fa675ba81650cc66c22d875bda35a48f1727ff0 Mon Sep 17 00:00:00 2001 From: Renaud Date: Mon, 28 Dec 2015 21:02:58 +0100 Subject: add color management --- die.h | 1 + 1 file changed, 1 insertion(+) diff --git a/die.h b/die.h index 2c10894..a07b110 100644 --- a/die.h +++ b/die.h @@ -133,6 +133,7 @@ private: bool m_highlighted; quint64 m_faces; qint64 m_base; + QString m_color; }; -- cgit v1.2.3-70-g09d2 From 20812f196cfba490952d2e148b4e321c10cbad7e Mon Sep 17 00:00:00 2001 From: Renaud G Date: Mon, 28 Dec 2015 21:20:54 +0100 Subject: add mergenode to pri. --- diceparser.pri | 2 ++ 1 file changed, 2 insertions(+) diff --git a/diceparser.pri b/diceparser.pri index 94c44df..5b06b1a 100644 --- a/diceparser.pri +++ b/diceparser.pri @@ -46,6 +46,7 @@ HEADERS += \ $$PWD/node/parenthesesnode.h \ $$PWD/node/helpnode.h \ $$PWD/node/jumpbackwardnode.h \ + $$PWD/node/mergenode.h \ $$PWD/node/listaliasnode.h \ $$PWD/node/listsetrollnode.h @@ -63,6 +64,7 @@ SOURCES += \ $$PWD/node/parenthesesnode.cpp \ $$PWD/node/helpnode.cpp \ $$PWD/node/jumpbackwardnode.cpp \ + $$PWD/node/mergenode.cpp \ $$PWD/node/listaliasnode.cpp \ $$PWD/node/listsetrollnode.cpp -- cgit v1.2.3-70-g09d2