diff options
| -rw-r--r-- | diceparser.pri | 6 | ||||
| -rw-r--r-- | operationcondition.cpp | 103 | ||||
| -rw-r--r-- | operationcondition.h | 52 | ||||
| -rw-r--r-- | parsingtoolbox.cpp | 48 | ||||
| -rw-r--r-- | parsingtoolbox.h | 3 |
5 files changed, 209 insertions, 3 deletions
diff --git a/diceparser.pri b/diceparser.pri index 94c44df..9fe29e3 100644 --- a/diceparser.pri +++ b/diceparser.pri @@ -14,7 +14,8 @@ SOURCES += $$PWD/diceparser.cpp \ $$PWD/parsingtoolbox.cpp \ $$PWD/result/stringresult.cpp \ $$PWD/compositevalidator.cpp \ - $$PWD/dicealias.cpp + $$PWD/dicealias.cpp \ + $$PWD/operationcondition.cpp HEADERS += \ @@ -29,7 +30,8 @@ HEADERS += \ $$PWD/parsingtoolbox.h \ $$PWD/result/stringresult.h \ $$PWD/compositevalidator.h \ - $$PWD/dicealias.h + $$PWD/dicealias.h \ + $$PWD/operationcondition.h HEADERS += \ diff --git a/operationcondition.cpp b/operationcondition.cpp new file mode 100644 index 0000000..121bd1e --- /dev/null +++ b/operationcondition.cpp @@ -0,0 +1,103 @@ +/*************************************************************************** + * Copyright (C) 2015 by Renaud Guezennec * + * http://renaudguezennec.homelinux.org/accueil,3.html * + * * + * rolisteam 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 "operationcondition.h" + +OperationCondition::OperationCondition() + : m_operator(Modulo) +{ + +} + +BooleanCondition *OperationCondition::getBoolean() const +{ + return m_boolean; +} + +void OperationCondition::setBoolean(BooleanCondition *boolean) +{ + m_boolean = boolean; +} + + +qint64 OperationCondition::hasValid(Die* b,bool recursive,bool unhighlight) const +{ + QList<qint64> listValues; + if(recursive) + { + listValues = b->getListValue(); + } + else + { + listValues.append(b->getLastRolledValue()); + } + + qint64 sum= 0; + foreach(qint64 value, listValues) + { + switch(m_operator) + { + case Modulo: + { + Die die; + die.setFaces(b->getFaces()); + die.insertRollValue(value%m_value); + sum+=m_boolean->hasValid(&die,recursive,false); + } + break; + + } + } + if((unhighlight)&&(sum==0)) + { + b->setHighlighted(false); + } + else + { + b->setHighlighted(true); + } + + return sum; +} + +void OperationCondition::setOperator(ConditionOperator m) +{ + m_operator = m; +} + +void OperationCondition::setValue(qint64 v) +{ + m_value=v; +} +QString OperationCondition::toString() +{ + QString str(QStringLiteral("")); + switch (m_operator) + { + case Modulo: + str.append(QStringLiteral("\%")); + break; + } + return QStringLiteral("[%1%2%3]").arg(str).arg(m_value).arg(m_boolean->toString()); +} +quint64 OperationCondition::getValidRangeSize(quint64 faces) const +{ + return faces/m_value; +} diff --git a/operationcondition.h b/operationcondition.h new file mode 100644 index 0000000..5d7510c --- /dev/null +++ b/operationcondition.h @@ -0,0 +1,52 @@ +/*************************************************************************** + * Copyright (C) 2015 by Renaud Guezennec * + * http://renaudguezennec.homelinux.org/accueil,3.html * + * * + * rolisteam 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 OPERATIONCONDITION_H +#define OPERATIONCONDITION_H + +#include <Qt> +#include "validator.h" +#include "booleancondition.h" + +class OperationCondition : public Validator +{ +public: + enum ConditionOperator { Modulo }; + OperationCondition(); + + virtual qint64 hasValid(Die* b,bool recursive, bool unhighlight = false) const; + + void setOperator(ConditionOperator m); + void setValue(qint64); + QString toString(); + + virtual quint64 getValidRangeSize(quint64 faces) const; + + BooleanCondition *getBoolean() const; + void setBoolean(BooleanCondition *boolean); + + +private: + ConditionOperator m_operator; + BooleanCondition* m_boolean; + qint64 m_value; +}; + +#endif // OPERATIONCONDITION_H diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index 495ae6e..7646970 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -39,6 +39,9 @@ ParsingToolBox::ParsingToolBox() m_logicOperation->insert("|",CompositeValidator::OR); m_logicOperation->insert("^",CompositeValidator::EXCLUSIVE_OR); m_logicOperation->insert("&",CompositeValidator::AND); + + m_conditionOperation = new QMap<QString,OperationCondition::ConditionOperator>(); + m_conditionOperation->insert("%",OperationCondition::Modulo); } ExecutionNode* ParsingToolBox::addSort(ExecutionNode* e,bool b) { @@ -47,6 +50,29 @@ ExecutionNode* ParsingToolBox::addSort(ExecutionNode* e,bool b) e->setNextNode(nodeSort); return nodeSort; } +bool ParsingToolBox::readDiceLogicOperator(QString& str,OperationCondition::ConditionOperator& op) +{ + QString longKey; + foreach(QString tmp, m_conditionOperation->keys()) + { + if(str.startsWith(tmp)) + { + if(longKey.size()<tmp.size()) + { + longKey = tmp; + } + } + } + if(longKey.size()>0) + { + str=str.remove(0,longKey.size()); + op = m_conditionOperation->value(longKey); + return true; + } + + return false; +} + bool ParsingToolBox::readLogicOperator(QString& str,BooleanCondition::LogicOperator& op) { QString longKey; @@ -82,9 +108,29 @@ Validator* ParsingToolBox::readValidator(QString& str) Validator* returnVal=NULL; BooleanCondition::LogicOperator myLogicOp = BooleanCondition::Equal; bool hasReadLogicOperator = readLogicOperator(str,myLogicOp); + + + OperationCondition::ConditionOperator condiOp = OperationCondition::Modulo; + bool hasDiceLogicOperator = readDiceLogicOperator(str,condiOp); qint64 value=0; - if(readNumber(str,value)) + if(hasDiceLogicOperator) + { + if(readNumber(str,value)) + { + OperationCondition* condition = new OperationCondition(); + condition->setValue(value); + Validator* valid = readValidator(str); + BooleanCondition* boolC = dynamic_cast<BooleanCondition*>(valid); + if(NULL!=boolC) + { + condition->setBoolean(boolC); + } + returnVal = condition; + } + + } + else if(readNumber(str,value)) { if(str.startsWith("-")) { diff --git a/parsingtoolbox.h b/parsingtoolbox.h index 3d37f7b..bf59c5c 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -27,6 +27,7 @@ #include "node/executionnode.h" #include "node/dicerollernode.h" #include "booleancondition.h" +#include "operationcondition.h" #include "compositevalidator.h" #include "range.h" @@ -141,9 +142,11 @@ public: bool readLogicOperation(QString& str,CompositeValidator::LogicOperation& op); + bool readDiceLogicOperator(QString &str, OperationCondition::ConditionOperator &op); private: QMap<QString,BooleanCondition::LogicOperator>* m_logicOp; QMap<QString,CompositeValidator::LogicOperation>* m_logicOperation; + QMap<QString,OperationCondition::ConditionOperator>* m_conditionOperation; }; #endif // PARSINGTOOLBOX_H |