diff options
| -rw-r--r-- | diceparser.cpp | 67 | ||||
| -rw-r--r-- | diceresult.cpp | 33 | ||||
| -rw-r--r-- | diceresult.h | 25 | ||||
| -rw-r--r-- | die.cpp | 12 | ||||
| -rw-r--r-- | die.h | 5 | ||||
| -rw-r--r-- | irc/mainwindow.cpp | 4 | ||||
| -rw-r--r-- | node/dicerollernode.cpp | 2 | ||||
| -rw-r--r-- | node/dicerollernode.h | 2 | ||||
| -rw-r--r-- | node/helpnode.cpp | 20 | ||||
| -rw-r--r-- | node/helpnode.h | 1 | ||||
| -rw-r--r-- | node/scalaroperatornode.cpp | 12 | ||||
| -rw-r--r-- | node/scalaroperatornode.h | 2 | ||||
| -rw-r--r-- | result.cpp | 8 | ||||
| -rw-r--r-- | result.h | 18 | ||||
| -rw-r--r-- | scalarresult.cpp | 7 | ||||
| -rw-r--r-- | scalarresult.h | 3 | ||||
| -rw-r--r-- | stringresult.cpp | 24 | ||||
| -rw-r--r-- | stringresult.h | 25 |
18 files changed, 195 insertions, 75 deletions
diff --git a/diceparser.cpp b/diceparser.cpp index 5d4a610..f4370f6 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -34,6 +34,7 @@ #include "node/rerolldicenode.h" #include "node/explosedicenode.h" #include "node/parenthesesnode.h" +#include "node/helpnode.h" #define DEFAULT_FACES_NUMBER 10 @@ -64,6 +65,7 @@ DiceParser::DiceParser() m_commandList = new QList<QString>(); m_commandList->append("help"); + } ExecutionNode* DiceParser::getLatestNode(ExecutionNode* node) @@ -148,6 +150,7 @@ bool DiceParser::readExpression(QString& str,ExecutionNode* & node) } else if(readCommand(str,operandNode)) { + node = operandNode; return true; } else @@ -202,48 +205,55 @@ QString DiceParser::displayResult() while(NULL!=myResult) { ++resulCount; - if((myResult->isScalar())&&(!scalarDone)) + if((myResult->hasResultOfType(Result::SCALAR))&&(!scalarDone)) { - stream << totalValue.arg(myResult->getScalar()) << endl; //.arg(m_command) + stream << totalValue.arg(myResult->getResult(Result::SCALAR).toReal()) << endl; //.arg(m_command) scalarDone=true; } - - DiceResult* myDiceResult = dynamic_cast<DiceResult*>(myResult); - if(NULL!=myDiceResult) + else if(myResult->hasResultOfType(Result::DICE_LIST)) { - QString resulStr; - quint64 face=0; - foreach(Die* die, myDiceResult->getResultList()) + DiceResult* myDiceResult = dynamic_cast<DiceResult*>(myResult); + if(NULL!=myDiceResult) { - if(!die->hasBeenDisplayed()) + + QString resulStr; + quint64 face=0; + foreach(Die* die, myDiceResult->getResultList()) { - resulStr+=QString("%1").arg(die->getValue()); - die->displayed(); - face = die->getFaces(); + if(!die->hasBeenDisplayed()) + { + resulStr+=QString("%1").arg(die->getValue()); + die->displayed(); + face = die->getFaces(); - if(die->hasChildrenValue()) - { - resulStr+=" ["; - foreach(qint64 i, die->getListValue()) + if(die->hasChildrenValue()) { - - resulStr+=QString("%1 ").arg(i); + resulStr+=" ["; + foreach(qint64 i, die->getListValue()) + { + + resulStr+=QString("%1 ").arg(i); + } + resulStr.remove(resulStr.size()-1,1); + resulStr+="]"; } - resulStr.remove(resulStr.size()-1,1); - resulStr+="]"; + resulStr+=", "; } - resulStr+=", "; } - } - resulStr.remove(resulStr.size()-2,2); + resulStr.remove(resulStr.size()-2,2); - if(!resulStr.isEmpty()) - { - stream << dieValue.arg(face).arg(resulStr); - } + if(!resulStr.isEmpty()) + { + stream << dieValue.arg(face).arg(resulStr); + } + } + } + else if(myResult->hasResultOfType(Result::STRING)) + { + stream << myResult->getResult(Result::STRING).toString(); } myResult = myResult->getPrevious(); @@ -306,7 +316,8 @@ bool DiceParser::readCommand(QString& str,ExecutionNode* & node) { if(m_commandList->contains(str)) { - // node = new HelpNode(); + node = new HelpNode(); + return true; } } diff --git a/diceresult.cpp b/diceresult.cpp index f111ad4..0cabb11 100644 --- a/diceresult.cpp +++ b/diceresult.cpp @@ -23,7 +23,7 @@ DiceResult::DiceResult() { - + m_resultTypes= (DICE_LIST); } void DiceResult::insertResult(Die* die) { @@ -38,17 +38,36 @@ void DiceResult::setResultList(QList<Die*> list) m_diceValues.clear(); m_diceValues << list; } -bool DiceResult::isScalar() const +//bool DiceResult::isScalar() const +//{ +// if(m_diceValues.size()==1) +// { +// return true; +// } +// return false; +//} +QVariant DiceResult::getResult(RESULT_TYPE type) { - if(m_diceValues.size()==1) + + switch (type) + { + case SCALAR: + return getScalarResult(); + break; + case DICE_LIST: { - return true; + + return QVariant(); + break; + } + default: + break; } - return false; + return QVariant(); + } -qreal DiceResult::getScalar() +qreal DiceResult::getScalarResult() { - if(m_diceValues.size()==1) { return m_diceValues[0]->getValue(); diff --git a/diceresult.h b/diceresult.h index ff3edc3..838a83d 100644 --- a/diceresult.h +++ b/diceresult.h @@ -31,16 +31,35 @@ class DiceResult : public Result { public: + /** + * @brief DiceResult + */ DiceResult(); - qint64 getSum(); + /** + * @brief getResultList + * @return + */ QList<Die*>& getResultList(); + /** + * @brief insertResult + */ void insertResult(Die*); + /** + * @brief setResultList + * @param list + */ void setResultList(QList<Die*> list); - bool isScalar() const; - virtual qreal getScalar(); + /** + * @brief getScalar + * @return + */ + virtual QVariant getResult(RESULT_TYPE); + +private: + qreal getScalarResult(); private: QList<Die*> m_diceValues; }; @@ -30,9 +30,19 @@ Die::Die() { uint seed = quintptr(this) + QDateTime::currentDateTime().toMSecsSinceEpoch(); qsrand(seed); -} +} +Die::Die(const Die& die) +{ + m_value = die.m_value; + m_rollResult = die.m_rollResult; + m_selected = die.m_selected; + m_hasValue = die.m_hasValue; + m_displayStatus = die.m_displayStatus; + m_faces = die.m_faces; +} + void Die::setValue(qint64 r) { m_value = r; @@ -34,7 +34,10 @@ public: * @brief Die */ Die(); - + /** + * @brief Die + */ + Die(const Die& ); /** * @brief setValue * @param r diff --git a/irc/mainwindow.cpp b/irc/mainwindow.cpp index dede122..cc72b0e 100644 --- a/irc/mainwindow.cpp +++ b/irc/mainwindow.cpp @@ -45,7 +45,7 @@ void MainWindow::readData() { // qDebug()<< "in /dice"; - QString dice=".*PRIVMSG.*\!(.*)"; + QString dice=".*PRIVMSG.*!(.*)"; QRegExp exp(dice); exp.indexIn(readLine); @@ -56,7 +56,7 @@ void MainWindow::readData() if(list.size()==2) { QString cmd = list[1]; - if(m_parser->parseLine(cmd)) + if(m_parser->parseLine(cmd.simplified())) { m_parser->Start(); QString result = m_parser->displayResult(); diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index 27750df..8e86cce 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -47,7 +47,7 @@ void DiceRollerNode::run(ExecutionNode* previous) Result* result=previous->getResult(); if(NULL!=result) { - m_diceCount = result->getScalar(); + m_diceCount = result->getResult(Result::SCALAR).toReal(); m_result->setPrevious(result); for(quint64 i=0; i < m_diceCount ; ++i) diff --git a/node/dicerollernode.h b/node/dicerollernode.h index d46f8ba..67d4c2d 100644 --- a/node/dicerollernode.h +++ b/node/dicerollernode.h @@ -16,7 +16,7 @@ private: QMutex* m_mutex; DiceResult* m_sharedDiceResult; int m_faces; - int m_diceCount; + quint64 m_diceCount; }; /** diff --git a/node/helpnode.cpp b/node/helpnode.cpp index a11c2b3..a46d29a 100644 --- a/node/helpnode.cpp +++ b/node/helpnode.cpp @@ -2,21 +2,35 @@ HelpNode::HelpNode() { + m_result = new StringResult(); } void HelpNode::run(ExecutionNode* previous) { + StringResult* txtResult = dynamic_cast<StringResult*>(m_result); + + qDebug() << m_result->hasResultOfType(Result::SCALAR) << m_result->hasResultOfType(Result::STRING); if(NULL != previous) { + if(previous->getResult() == NULL) + { + txtResult->setText(toString()); + } + else + { + txtResult->setText(previous->getHelp()); + } + m_result->setPrevious(previous->getResult()); } - else - { + if(NULL!=m_nextNode) + { + m_nextNode->run(this); } } QString HelpNode::toString()const { - return QObject::tr("see full documentation at: <a href=\"https://github.com/obiwankennedy/DiceParser/blob/master/HelpMe.md\">https://github.com/obiwankennedy/DiceParser/blob/master/HelpMe.md</a>"); + return QObject::tr("Rolisteam Dice Parser: Full documentation at: https://github.com/obiwankennedy/DiceParser/blob/master/HelpMe.md"); } qint64 HelpNode::getPriority() const diff --git a/node/helpnode.h b/node/helpnode.h index f8b3f78..27f38b3 100644 --- a/node/helpnode.h +++ b/node/helpnode.h @@ -32,6 +32,7 @@ public: * @return */ virtual qint64 getPriority() const; + }; #endif // HELPNODE_H diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp index 97d17d9..fb7a110 100644 --- a/node/scalaroperatornode.cpp +++ b/node/scalaroperatornode.cpp @@ -5,7 +5,7 @@ ScalarOperatorNode::ScalarOperatorNode() - : m_internalNode(NULL),m_myScalarResult(new ScalarResult()) + : m_internalNode(NULL),m_scalarResult(new ScalarResult()) { m_scalarOperationList.insert('+',PLUS); m_scalarOperationList.insert('-',MINUS); @@ -13,7 +13,7 @@ ScalarOperatorNode::ScalarOperatorNode() m_scalarOperationList.insert('*',MULTIPLICATION); m_scalarOperationList.insert('/',DIVIDE); - m_result = m_myScalarResult; + m_result = m_scalarResult; } void ScalarOperatorNode::run(ExecutionNode* previous) @@ -42,16 +42,16 @@ void ScalarOperatorNode::run(ExecutionNode* previous) switch(m_myOperator) { case PLUS: - m_myScalarResult->setValue(add(previousResult->getScalar(),internalResult->getScalar())); + m_scalarResult->setValue(add(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal())); break; case MINUS: - m_myScalarResult->setValue(substract(previousResult->getScalar(),internalResult->getScalar())); + m_scalarResult->setValue(substract(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal())); break; case MULTIPLICATION: - m_myScalarResult->setValue(multiple(previousResult->getScalar(),internalResult->getScalar())); + m_scalarResult->setValue(multiple(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal())); break; case DIVIDE: - m_myScalarResult->setValue(divide(previousResult->getScalar(),internalResult->getScalar())); + m_scalarResult->setValue(divide(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal())); break; default: break; diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h index 2cd8405..14f759b 100644 --- a/node/scalaroperatornode.h +++ b/node/scalaroperatornode.h @@ -30,7 +30,7 @@ private: ScalarOperator m_myOperator; ExecutionNode* m_internalNode; QMap<QChar,ScalarOperator> m_scalarOperationList; - ScalarResult* m_myScalarResult; + ScalarResult* m_scalarResult; }; #endif // SCALAROPERATORNODE_H @@ -36,12 +36,8 @@ void Result::setPrevious(Result* p) m_previous = p; } -bool Result::isStringResult() -{ - return false; -} -QString Result::getStringResult() +bool Result::hasResultOfType(RESULT_TYPE type) const { - return QString(); + return (m_resultTypes & type); } @@ -22,9 +22,9 @@ #ifndef RESULT_H #define RESULT_H -#include <Qt> +//#include <Qt> #include <QString> - +#include <QVariant> /** * @brief The Result class */ @@ -32,6 +32,10 @@ class Result { public: /** + * @brief The RESULT_TYPE enum or combinaison + */ + enum RESULT_TYPE {SCALAR=1,STRING=2,DICE_LIST=4}; + /** * @brief Result */ Result(); @@ -39,12 +43,12 @@ public: * @brief isScalar * @return */ - virtual bool isScalar() const = 0; + virtual bool hasResultOfType(RESULT_TYPE) const; /** * @brief getScalar * @return */ - virtual qreal getScalar() = 0; + virtual QVariant getResult(RESULT_TYPE) = 0; /** * @brief getPrevious * @return @@ -55,11 +59,11 @@ public: */ virtual void setPrevious(Result*); - virtual bool isStringResult(); - virtual QString getStringResult(); - +protected: + int m_resultTypes;/// @brief private: Result* m_previous;/// @brief + }; #endif // RESULT_H diff --git a/scalarresult.cpp b/scalarresult.cpp index 3500675..8c924fe 100644 --- a/scalarresult.cpp +++ b/scalarresult.cpp @@ -23,6 +23,7 @@ ScalarResult::ScalarResult() { + m_resultTypes = Result::SCALAR; } @@ -30,12 +31,8 @@ void ScalarResult::setValue(qreal i) { m_value=i; } -qreal ScalarResult::getScalar() +QVariant ScalarResult::getResult(Result::RESULT_TYPE type) { return m_value; } -bool ScalarResult::isScalar() const -{ - return true; -} diff --git a/scalarresult.h b/scalarresult.h index cbcbf66..35982be 100644 --- a/scalarresult.h +++ b/scalarresult.h @@ -31,8 +31,7 @@ class ScalarResult : public Result public: ScalarResult(); - virtual bool isScalar() const; - virtual qreal getScalar(); + virtual QVariant getResult(Result::RESULT_TYPE); void setValue(qreal i); diff --git a/stringresult.cpp b/stringresult.cpp index 7f7b822..4831a76 100644 --- a/stringresult.cpp +++ b/stringresult.cpp @@ -2,4 +2,28 @@ StringResult::StringResult() { + m_resultTypes = Result::STRING; +} +void StringResult::setText(QString text) +{ + m_value=text; +} + +QString StringResult::getText() const +{ + return m_value; +} +QVariant StringResult::getResult(RESULT_TYPE type) +{ + + switch(type) + { + case STRING: + return getText(); + break; + + } + + + return QVariant(); } diff --git a/stringresult.h b/stringresult.h index 7db55a1..caa7e06 100644 --- a/stringresult.h +++ b/stringresult.h @@ -1,13 +1,36 @@ #ifndef STRINGRESULT_H #define STRINGRESULT_H +#include <QString> +#include "result.h" /** * @brief The StringResult class */ -class StringResult +class StringResult : public Result { public: + /** + * @brief StringResult + */ StringResult(); + /** + * @brief setText + * @param text + */ + void setText(QString text); + /** + * @brief getText + * @return + */ + QString getText() const; + /** + * @brief getScalar + * @return + */ + virtual QVariant getResult(RESULT_TYPE); + +private: + QString m_value; }; #endif // STRINGRESULT_H |