diff options
| -rw-r--r-- | diceparser.cpp | 41 | ||||
| -rw-r--r-- | node/listsetrollnode.cpp | 60 | ||||
| -rw-r--r-- | node/listsetrollnode.h | 19 | ||||
| -rw-r--r-- | parsingtoolbox.cpp | 15 | ||||
| -rw-r--r-- | parsingtoolbox.h | 8 |
5 files changed, 131 insertions, 12 deletions
diff --git a/diceparser.cpp b/diceparser.cpp index 3f1c21c..ea6dcc9 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -36,6 +36,7 @@ #include "node/parenthesesnode.h" #include "node/helpnode.h" #include "node/jumpbackwardnode.h" +#include "node/listsetrollnode.h" #define DEFAULT_FACES_NUMBER 10 @@ -316,26 +317,42 @@ QString DiceParser::displayResult() bool DiceParser::readDice(QString& str,ExecutionNode* & node) { - DiceOperator myOperator; + DiceOperator currentOperator; - if(readDiceOperator(str,myOperator)) + if(readDiceOperator(str,currentOperator)) { int num; - if(m_parsingToolbox->readNumber(str,num)) + if(currentOperator==D) { - DiceRollerNode* drNode = new DiceRollerNode(num); - // dice.m_diceOp = myOperator; - // dice.m_faces = num; - node = drNode; - ExecutionNode* current = drNode; - while(readOption(str,current)) + if(m_parsingToolbox->readNumber(str,num)) { - current = getLatestNode(current); - } + DiceRollerNode* drNode = new DiceRollerNode(num); + // dice.m_diceOp = myOperator; + // dice.m_faces = num; + node = drNode; + ExecutionNode* current = drNode; + while(readOption(str,current)) + { + current = getLatestNode(current); + } - return true; + return true; + } } + else if(currentOperator ==L) + { + QStringList list; + if(m_parsingToolbox->readList(str,list)) + { + qDebug() << list; + ListSetRollNode* lsrNode = new ListSetRollNode(); + lsrNode->setListValue(list); + node = lsrNode; + return true; + } + } + } return false; diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp index e89e139..3ab9a69 100644 --- a/node/listsetrollnode.cpp +++ b/node/listsetrollnode.cpp @@ -1,5 +1,65 @@ #include "listsetrollnode.h" +#include "die.h" ListSetRollNode::ListSetRollNode() + :m_diceResult(new DiceResult()),m_stringResult(new StringResult()) { + m_result = m_stringResult; +} + +QStringList ListSetRollNode::getList() +{ + return m_values; +} +QString ListSetRollNode::toString() const +{ + return QString("ListSetRollNode_").arg(m_values.join(',')); +} +qint64 ListSetRollNode::getPriority() const +{ + qint64 priority=4; +// if(NULL!=m_nextNode) +// { +// priority = m_nextNode->getPriority(); +// } + + + return priority; +} +void ListSetRollNode::run(ExecutionNode* previous) +{ + m_previousNode = previous; + if(NULL!=previous) + { + Result* result=previous->getResult(); + if(NULL!=result) + { + quint64 diceCount = result->getResult(Result::SCALAR).toReal(); + m_result->setPrevious(result); + QStringList rollResult; + for(quint64 i=0; i < diceCount ; ++i) + { + Die* die = new Die(); + die->setFaces(m_values.size()); + die->roll(); + m_diceResult->insertResult(die); + if(die->getValue()-1<m_values.size()) + { + rollResult << m_values[die->getValue()-1]; + } + } + m_stringResult->setText(rollResult.join(",")); + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } + } + } + + + +} +void ListSetRollNode::setListValue(QStringList lirs) +{ + m_values = lirs; } diff --git a/node/listsetrollnode.h b/node/listsetrollnode.h index 006557b..26fb378 100644 --- a/node/listsetrollnode.h +++ b/node/listsetrollnode.h @@ -1,10 +1,29 @@ #ifndef LISTSETROLLNODE_H #define LISTSETROLLNODE_H + +#include <QStringList> + +#include "executionnode.h" +#include "result/diceresult.h" +#include "result/stringresult.h" + class ListSetRollNode : public ExecutionNode { public: ListSetRollNode(); + virtual void run(ExecutionNode* previous = NULL); + virtual QString toString()const; + virtual qint64 getPriority() const; + QStringList getList(); + + void setListValue(QStringList); + +private: + QStringList m_values; + DiceResult* m_diceResult; + StringResult* m_stringResult; + }; #endif // LISTSETROLLNODE_H diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index f23bc3d..0d749cc 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -171,3 +171,18 @@ bool ParsingToolBox::readCloseParentheses(QString& str) else return false; } +bool ParsingToolBox::readList(QString& str,QStringList& list) +{ + if(str.startsWith("[")) + { + str=str.remove(0,1); + int pos = str.indexOf("]"); + if(-1!=pos) + { + QString liststr = str.left(pos); + list = liststr.split(","); + return true; + } + } + return false; +} diff --git a/parsingtoolbox.h b/parsingtoolbox.h index 6090b44..4c5ce8d 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -79,6 +79,14 @@ public: */ bool readCloseParentheses(QString& str); + /** + * @brief readList + * @param str + * @param list + * @return + */ + bool readList(QString& str,QStringList& list); + private: QMap<QString,BooleanCondition::LogicOperator>* m_logicOp; }; |