diff options
| author | 2017-04-27 18:17:03 +0200 | |
|---|---|---|
| committer | 2017-04-27 18:17:03 +0200 | |
| commit | 9b2839b6f09177bda6b3654c56a750f84173123a (patch) | |
| tree | 259cbab92ad580b11ddd4a01bd8fa53b151a237c | |
| parent | 026ca5cb4829cd4c4ad6b945e27479303e96fe82 (diff) | |
| download | OneRoll-9b2839b6f09177bda6b3654c56a750f84173123a.tar.gz OneRoll-9b2839b6f09177bda6b3654c56a750f84173123a.zip | |
-management of arithmetic operator for scalar result of dice.
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | cli/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | diceparser.cpp | 26 | ||||
| -rw-r--r-- | diceparser.h | 3 | ||||
| -rw-r--r-- | die.cpp | 46 | ||||
| -rw-r--r-- | die.h | 10 | ||||
| -rw-r--r-- | node/dicerollernode.cpp | 11 | ||||
| -rw-r--r-- | node/dicerollernode.h | 6 | ||||
| -rw-r--r-- | node/scalaroperatornode.cpp | 24 | ||||
| -rw-r--r-- | node/scalaroperatornode.h | 9 | ||||
| -rw-r--r-- | node/splitnode.cpp | 91 | ||||
| -rw-r--r-- | node/splitnode.h | 43 | ||||
| -rw-r--r-- | parsingtoolbox.cpp | 16 | ||||
| -rw-r--r-- | parsingtoolbox.h | 4 | ||||
| -rw-r--r-- | result/diceresult.cpp | 56 | ||||
| -rw-r--r-- | result/diceresult.h | 4 |
16 files changed, 308 insertions, 44 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index da08188..731fa07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(diceparser) add_subdirectory(irc) add_subdirectory(cli) add_subdirectory(mobile) -add_subdirectory(webserver) +#add_subdirectory(webserver) #qt5_use_modules() diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 89ef835..150e90e 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -92,6 +92,7 @@ SET( dice_sources ../node/filternode.cpp ../node/stringnode.cpp ../node/ifnode.cpp + ../node/splitnode.cpp main.cpp ../highlightdice.cpp ) diff --git a/diceparser.cpp b/diceparser.cpp index b40d5e9..315c8d5 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -43,6 +43,7 @@ #include "node/ifnode.h" #include "node/paintnode.h" #include "node/stringnode.h" +#include "node/splitnode.h" #define DEFAULT_FACES_NUMBER 10 @@ -69,7 +70,7 @@ DiceParser::DiceParser() m_OptionOp->insert(QStringLiteral("i"),ifOperator); m_OptionOp->insert(QStringLiteral("p"),Painter); m_OptionOp->insert(QStringLiteral("f"),Filter); - + m_OptionOp->insert(QStringLiteral("u"),Split); m_aliasList = new QList<DiceAlias*>(); @@ -610,6 +611,13 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node) { qint64 max; qint64 min; + Die::ArithmeticOperator op; + + bool hasOp= m_parsingToolbox->readArithmeticOperator(str,op); + + + + if(m_parsingToolbox->readNumber(str,max)) { if(max<1) @@ -618,6 +626,10 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node) return false; } DiceRollerNode* drNode = new DiceRollerNode(max); + if(hasOp) + { + drNode->setOperator(op); + } node = drNode; ExecutionNode* current = drNode; while(readOption(str,current)) @@ -747,7 +759,7 @@ bool DiceParser::readOperator(QString& str,ExecutionNode* previous) return false; } - ScalarOperatorNode::ArithmeticOperator op; + Die::ArithmeticOperator op; if(m_parsingToolbox->readArithmeticOperator(str,op)) { ScalarOperatorNode* node = new ScalarOperatorNode(); @@ -1044,6 +1056,14 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous)//, delete nodeif; } } + case Split: + { + SplitNode* splitnode = new SplitNote(); + previous->setNextNode(splitnode); + node = splitnode; + found = true; + } + break; } } @@ -1068,7 +1088,7 @@ bool DiceParser::readBlocInstruction(QString& str,ExecutionNode*& resultnode) { str=str.remove(0,1); ExecutionNode* node; - ScalarOperatorNode::ArithmeticOperator op; + Die::ArithmeticOperator op; ScalarOperatorNode* scalarNode = NULL; if(m_parsingToolbox->readArithmeticOperator(str,op)) { diff --git a/diceparser.h b/diceparser.h index a1e58e1..2ff1e56 100644 --- a/diceparser.h +++ b/diceparser.h @@ -74,7 +74,6 @@ public: * @brief The DiceOperator enum gathering all dice operators */ enum DiceOperator {D,L}; - /** * @brief The DiceSymbol enum */ @@ -82,7 +81,7 @@ public: /** * @brief The OptionOperator enum gathering all options availables for result. */ - enum OptionOperator {KeepAndExplose,Keep,Reroll,Explosing,Sort,Count,RerollAndAdd,Merge,ifOperator,Painter,Filter}; + enum OptionOperator {KeepAndExplose,Keep,Reroll,Explosing,Sort,Count,RerollAndAdd,Merge,ifOperator,Painter,Filter,Split}; /** * @brief The CommandOperator enum */ @@ -27,7 +27,7 @@ #include <chrono> Die::Die() - : m_hasValue(false),m_displayStatus(false),m_highlighted(true),m_base(1),m_color("")//,m_mt(m_randomDevice) + : m_hasValue(false),m_displayStatus(false),m_highlighted(true),m_base(1),m_color(""),m_op(Die::PLUS)//,m_mt(m_randomDevice) { // uint seed = quintptr(this) + QDateTime::currentDateTime().toMSecsSinceEpoch(); @@ -48,6 +48,7 @@ Die::Die(const Die& die) m_highlighted = die.m_highlighted; m_base = die.m_base; m_color = die.getColor(); + m_op = die.getOp(); } void Die::setValue(qint64 r) @@ -80,9 +81,39 @@ qint64 Die::getValue() const else { qint64 value=0; - foreach(qint64 tmp,m_rollResult) + int i = 0; + for(qint64 tmp : m_rollResult) { - value+=tmp; + if(i>0) + { + switch(m_op) + { + case PLUS: + value+=tmp; + break; + case MULTIPLICATION: + value*=tmp; + break; + case MINUS: + value-=tmp; + break; + case DIVIDE: + if(tmp!=0) + { + value/=tmp; + } + else + { + //error(); + } + break; + } + } + else + { + value=tmp; + } + ++i; } return value; } @@ -179,3 +210,12 @@ void Die::setMaxValue(const qint64 &maxValue) m_maxValue = maxValue; } +Die::ArithmeticOperator Die::getOp() const +{ + return m_op; +} + +void Die::setOp(const Die::ArithmeticOperator &op) +{ + m_op = op; +} @@ -32,6 +32,10 @@ class Die { public: /** + * @brief The ArithmeticOperator enum + */ + enum ArithmeticOperator {PLUS,MINUS,DIVIDE,MULTIPLICATION}; + /** * @brief Die */ Die(); @@ -131,6 +135,9 @@ public: qint64 getMaxValue() const; void setMaxValue(const qint64 &maxValue); + Die::ArithmeticOperator getOp() const; + void setOp(const Die::ArithmeticOperator &op); + private: qint64 m_value; QList<qint64> m_rollResult; @@ -142,7 +149,10 @@ private: qint64 m_base; QString m_color; + Die::ArithmeticOperator m_op; + std::mt19937 m_rng; + }; diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index caff889..9ca0d35 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -33,6 +33,7 @@ void DiceRollerNode::run(ExecutionNode* previous) for(quint64 i=0; i < m_diceCount ; ++i) { Die* die = new Die(); + die->setOp(m_operator); die->setBase(m_min); die->setMaxValue(m_max); die->roll(); @@ -82,3 +83,13 @@ ExecutionNode* DiceRollerNode::getCopy() const } return node; } + +Die::ArithmeticOperator DiceRollerNode::getOperator() const +{ + return m_operator; +} + +void DiceRollerNode::setOperator(const Die::ArithmeticOperator &dieOperator) +{ + m_operator = dieOperator; +} diff --git a/node/dicerollernode.h b/node/dicerollernode.h index dcdbeb2..0857b30 100644 --- a/node/dicerollernode.h +++ b/node/dicerollernode.h @@ -44,11 +44,15 @@ public: virtual ExecutionNode* getCopy() const; //private members + Die::ArithmeticOperator getOperator() const; + void setOperator(const Die::ArithmeticOperator & dieOperator); + private: quint64 m_diceCount; qint64 m_max; /// faces - DiceResult* m_diceResult; + DiceResult* m_diceResult; qint64 m_min; + Die::ArithmeticOperator m_operator; }; #endif // DICEROLLERNODE_H diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp index e670dee..9c86917 100644 --- a/node/scalaroperatornode.cpp +++ b/node/scalaroperatornode.cpp @@ -26,7 +26,7 @@ ScalarOperatorNode::ScalarOperatorNode() - : m_internalNode(NULL),m_scalarResult(new ScalarResult()),m_arithmeticOperator(PLUS) + : m_internalNode(NULL),m_scalarResult(new ScalarResult()),m_arithmeticOperator(Die::PLUS) { /*m_scalarOperationList.insert('+',PLUS); m_scalarOperationList.insert('-',MINUS); @@ -76,16 +76,16 @@ void ScalarOperatorNode::run(ExecutionNode* previous) switch(m_arithmeticOperator) { - case PLUS: + case Die::PLUS: m_scalarResult->setValue(add(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal())); break; - case MINUS: + case Die::MINUS: m_scalarResult->setValue(substract(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal())); break; - case MULTIPLICATION: + case Die::MULTIPLICATION: m_scalarResult->setValue(multiple(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal())); break; - case DIVIDE: + case Die::DIVIDE: m_scalarResult->setValue(divide(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal())); break; default: @@ -137,12 +137,12 @@ qint64 ScalarOperatorNode::multiple(qint64 a,qint64 b) { return a*b; } -ScalarOperatorNode::ArithmeticOperator ScalarOperatorNode::getArithmeticOperator() const +Die::ArithmeticOperator ScalarOperatorNode::getArithmeticOperator() const { return m_arithmeticOperator; } -void ScalarOperatorNode::setArithmeticOperator(const ScalarOperatorNode::ArithmeticOperator &arithmeticOperator) +void ScalarOperatorNode::setArithmeticOperator(const Die::ArithmeticOperator &arithmeticOperator) { m_arithmeticOperator = arithmeticOperator; } @@ -152,16 +152,16 @@ QString ScalarOperatorNode::toString(bool wl) const QString op=""; switch(m_arithmeticOperator) { - case PLUS: + case Die::PLUS: op="+"; break; - case MINUS: + case Die::MINUS: op="-"; break; - case MULTIPLICATION: + case Die::MULTIPLICATION: op="*"; break; - case DIVIDE: + case Die::DIVIDE: op="/"; break; default: @@ -179,7 +179,7 @@ QString ScalarOperatorNode::toString(bool wl) const } qint64 ScalarOperatorNode::getPriority() const { - if((m_arithmeticOperator==PLUS)||(m_arithmeticOperator==MINUS)) + if((m_arithmeticOperator==Die::PLUS)||(m_arithmeticOperator==Die::MINUS)) { return 1; } diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h index 4b21b8c..0855a4c 100644 --- a/node/scalaroperatornode.h +++ b/node/scalaroperatornode.h @@ -27,6 +27,7 @@ #include "executionnode.h" #include "result/scalarresult.h" +#include "die.h" /** @@ -38,7 +39,7 @@ public: /** * @brief The ArithmeticOperator enum */ - enum ArithmeticOperator {PLUS,MINUS,DIVIDE,MULTIPLICATION}; + //enum ArithmeticOperator {PLUS,MINUS,DIVIDE,MULTIPLICATION}; /** * @brief ScalarOperatorNode */ @@ -81,12 +82,12 @@ public: * @brief getArithmeticOperator * @return */ - ScalarOperatorNode::ArithmeticOperator getArithmeticOperator() const; + Die::ArithmeticOperator getArithmeticOperator() const; /** * @brief setArithmeticOperator * @param arithmeticOperator */ - void setArithmeticOperator(const ScalarOperatorNode::ArithmeticOperator &arithmeticOperator); + void setArithmeticOperator(const Die::ArithmeticOperator &arithmeticOperator); /** * @brief getCopy @@ -118,7 +119,7 @@ private: private: ExecutionNode* m_internalNode; ScalarResult* m_scalarResult; - ArithmeticOperator m_arithmeticOperator; + Die::ArithmeticOperator m_arithmeticOperator; }; #endif // SCALAROPERATORNODE_H diff --git a/node/splitnode.cpp b/node/splitnode.cpp new file mode 100644 index 0000000..5ef082f --- /dev/null +++ b/node/splitnode.cpp @@ -0,0 +1,91 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://www.rolisteam.org/contact * +* * +* 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 "splitnode.h" + +SplitNode::SplitNode() + : m_diceResult(new DiceResult()) +{ + m_result = m_diceResult; +} +void SplitNode::run(ExecutionNode* previous) +{ + m_previousNode = previous; + if(NULL!=previous) + { + m_result->setPrevious(previous->getResult()); + + Result* tmpResult = previous->getResult(); + if(nullptr != tmpResult) + { + DiceResult* dice = dynamic_cast<DiceResult*>(tmpResult); + if(nullptr!=dice) + { + for(Die* oldDie : dice->getResultList()) + { + m_diceResult->setOperator(oldDie->getOp()); + for(qint64 value : oldDie->getListValue()) + { + Die* tmpdie = new Die(); + tmpdie->setValue(value); + tmpdie->setOp(oldDie->getOp()); + m_diceResult->insertResult(tmpDie); + } + } + } + } + } + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } +} + +QString SplitNode::toString(bool withLabel) const +{ + if(withLabel) + { + return QString("%1 [label=\"Merge Node\"]").arg(m_id); + } + else + { + return m_id; + } +} +qint64 SplitNode::getPriority() const +{ + qint64 priority=0; + if(NULL!=m_nextNode) + { + priority = m_nextNode->getPriority(); + } + return priority; +} +ExecutionNode* SplitNode::getCopy() const +{ + SplitNode* node = new SplitNode(); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/splitnode.h b/node/splitnode.h new file mode 100644 index 0000000..1bb092e --- /dev/null +++ b/node/splitnode.h @@ -0,0 +1,43 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://www.rolisteam.org/contact * +* * +* 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 SplitNode : public ExecutionNode +{ +public: + SplitNode(); + void run(ExecutionNode* previous); + virtual QString toString(bool withLabel)const; + virtual qint64 getPriority() const; + virtual ExecutionNode *getCopy() const; +private: + DiceResult* m_diceResult; +}; + +#endif // NUMBERNODE_H diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index e04dcbe..3ff7e21 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -31,7 +31,7 @@ ParsingToolBox::ParsingToolBox() : m_logicOp(new QMap<QString,BooleanCondition::LogicOperator>()), m_logicOperation(new QMap<QString,CompositeValidator::LogicOperation>()), m_conditionOperation(new QMap<QString,OperationCondition::ConditionOperator>()), - m_arithmeticOperation(new QHash<QString,ScalarOperatorNode::ArithmeticOperator>()) + m_arithmeticOperation(new QHash<QString,Die::ArithmeticOperator>()) { //m_logicOp = ; m_logicOp->insert(">=",BooleanCondition::GreaterOrEqual); @@ -52,12 +52,12 @@ ParsingToolBox::ParsingToolBox() //m_arithmeticOperation = new QHash<QString,ScalarOperatorNode::ArithmeticOperator>(); - m_arithmeticOperation->insert(QStringLiteral("+"),ScalarOperatorNode::PLUS); - m_arithmeticOperation->insert(QStringLiteral("-"),ScalarOperatorNode::MINUS); - m_arithmeticOperation->insert(QStringLiteral("*"),ScalarOperatorNode::MULTIPLICATION); - m_arithmeticOperation->insert(QStringLiteral("x"),ScalarOperatorNode::MULTIPLICATION); - m_arithmeticOperation->insert(QStringLiteral("/"),ScalarOperatorNode::DIVIDE); - m_arithmeticOperation->insert(QStringLiteral("÷"),ScalarOperatorNode::DIVIDE); + m_arithmeticOperation->insert(QStringLiteral("+"),Die::PLUS); + m_arithmeticOperation->insert(QStringLiteral("-"),Die::MINUS); + m_arithmeticOperation->insert(QStringLiteral("*"),Die::MULTIPLICATION); + m_arithmeticOperation->insert(QStringLiteral("x"),Die::MULTIPLICATION); + m_arithmeticOperation->insert(QStringLiteral("/"),Die::DIVIDE); + m_arithmeticOperation->insert(QStringLiteral("÷"),Die::DIVIDE); } @@ -103,7 +103,7 @@ bool ParsingToolBox::readDiceLogicOperator(QString& str,OperationCondition::Cond return false; } -bool ParsingToolBox::readArithmeticOperator(QString &str, ScalarOperatorNode::ArithmeticOperator &op) +bool ParsingToolBox::readArithmeticOperator(QString &str, Die::ArithmeticOperator &op) { bool found = false; //QHash<QString,ScalarOperatorNode::ArithmeticOperator>::Iterator diff --git a/parsingtoolbox.h b/parsingtoolbox.h index 6bfffd0..5a87573 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -166,7 +166,7 @@ public: bool readDiceLogicOperator(QString &str, OperationCondition::ConditionOperator &op); - bool readArithmeticOperator(QString& str, ScalarOperatorNode::ArithmeticOperator& op); + bool readArithmeticOperator(QString& str, Die::ArithmeticOperator& op); static void readPainterParameter(PainterNode *painter, QString &str); @@ -185,7 +185,7 @@ private: QMap<QString,BooleanCondition::LogicOperator>* m_logicOp; QMap<QString,CompositeValidator::LogicOperation>* m_logicOperation; QMap<QString,OperationCondition::ConditionOperator>* m_conditionOperation; - QHash<QString,ScalarOperatorNode::ArithmeticOperator>* m_arithmeticOperation; + QHash<QString,Die::ArithmeticOperator>* m_arithmeticOperation; static QHash<QString,QString>* m_variableHash; diff --git a/result/diceresult.cpp b/result/diceresult.cpp index aa39524..2825eec 100644 --- a/result/diceresult.cpp +++ b/result/diceresult.cpp @@ -87,16 +87,56 @@ qreal DiceResult::getScalarResult() } else { - qint64 scalarSum = 0; - foreach(Die* die,m_diceValues) - { - scalarSum+=die->getValue(); - } - return scalarSum; + qint64 scalar=0; + int i = 0; + for(auto tmp : m_diceValues) + { + if(i>0) + { + switch(m_operator) + { + case Die::PLUS: + scalar+=tmp->getValue(); + break; + case Die::MULTIPLICATION: + scalar*=tmp->getValue(); + break; + case Die::MINUS: + scalar-=tmp->getValue(); + break; + case Die::DIVIDE: + if(tmp!=0) + { + scalar/=tmp->getValue(); + } + else + { + //error(); + } + break; + } + } + else + { + scalar=tmp; + } + ++i; + } + return scalar; } return 0; } + +Die::ArithmeticOperator DiceResult::getOperator() const +{ + return m_operator; +} + +void DiceResult::setOperator(const Die::ArithmeticOperator& dieOperator) +{ + m_operator = dieOperator; +} QString DiceResult::toString(bool wl) { QStringList scalarSum; @@ -104,8 +144,8 @@ QString DiceResult::toString(bool wl) { scalarSum << QString::number(die->getValue()); } - if(wl) - { + if(wl) + { return QStringLiteral("%3 [label=\"DiceResult Value %1 dice %2\"]").arg(getScalarResult()).arg(scalarSum.join('_')).arg(m_id); } else diff --git a/result/diceresult.h b/result/diceresult.h index 3be47ed..7eded95 100644 --- a/result/diceresult.h +++ b/result/diceresult.h @@ -75,11 +75,15 @@ public: */ void setHomogeneous(bool); + Die::ArithmeticOperator getOperator() const; + void setOperator(const Die::ArithmeticOperator & dieOperator); + private: qreal getScalarResult(); private: QList<Die*> m_diceValues; bool m_homogeneous; + Die::ArithmeticOperator m_operator; }; #endif // DICERESULT_H |