diff options
Diffstat (limited to 'src/libparser/result')
| -rw-r--r-- | src/libparser/result/diceresult.cpp | 193 | ||||
| -rw-r--r-- | src/libparser/result/diceresult.h | 97 | ||||
| -rw-r--r-- | src/libparser/result/result.cpp | 86 | ||||
| -rw-r--r-- | src/libparser/result/result.h | 96 | ||||
| -rw-r--r-- | src/libparser/result/scalarresult.cpp | 58 | ||||
| -rw-r--r-- | src/libparser/result/scalarresult.h | 59 | ||||
| -rw-r--r-- | src/libparser/result/stringresult.cpp | 117 | ||||
| -rw-r--r-- | src/libparser/result/stringresult.h | 45 |
8 files changed, 751 insertions, 0 deletions
diff --git a/src/libparser/result/diceresult.cpp b/src/libparser/result/diceresult.cpp new file mode 100644 index 0000000..31e316b --- /dev/null +++ b/src/libparser/result/diceresult.cpp @@ -0,0 +1,193 @@ +/*************************************************************************** + * Copyright (C) 2014 by Renaud Guezennec * + * https://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 "diceresult.h" +#include <QDebug> + +DiceResult::DiceResult() +{ + m_resultTypes= (static_cast<int>(Dice::RESULT_TYPE::DICE_LIST) | static_cast<int>(Dice::RESULT_TYPE::SCALAR)); + m_homogeneous= true; +} +void DiceResult::insertResult(Die* die) +{ + m_diceValues.append(die); +} +QList<Die*>& DiceResult::getResultList() +{ + return m_diceValues; +} +bool DiceResult::isHomogeneous() const +{ + return m_homogeneous; +} +void DiceResult::setHomogeneous(bool b) +{ + m_homogeneous= b; +} + +void DiceResult::setResultList(QList<Die*> list) +{ + m_diceValues.erase( + std::remove_if(m_diceValues.begin(), m_diceValues.end(), [list](Die* die) { return list.contains(die); }), + m_diceValues.end()); + + qDeleteAll(m_diceValues.begin(), m_diceValues.end()); + m_diceValues.clear(); + m_diceValues << list; +} +DiceResult::~DiceResult() +{ + if(!m_diceValues.isEmpty()) + { + qDeleteAll(m_diceValues.begin(), m_diceValues.end()); + m_diceValues.clear(); + } +} +QVariant DiceResult::getResult(Dice::RESULT_TYPE type) +{ + switch(type) + { + case Dice::RESULT_TYPE::SCALAR: + { + return getScalarResult(); + } + case Dice::RESULT_TYPE::DICE_LIST: + { + return QVariant::fromValue(m_diceValues); + } + default: + break; + } + return QVariant(); +} +bool DiceResult::contains(Die* die, const std::function<bool(const Die*, const Die*)> equal) +{ + for(auto& value : m_diceValues) + { + if(equal(value, die)) + { + return true; + } + } + return false; +} +qreal DiceResult::getScalarResult() +{ + if(m_diceValues.size() == 1) + { + return m_diceValues[0]->getValue(); + } + else + { + qint64 scalar= 0; + int i= 0; + for(auto& tmp : m_diceValues) + { + if(i > 0) + { + switch(m_operator) + { + case Dice::ArithmeticOperator::PLUS: + scalar+= tmp->getValue(); + break; + case Dice::ArithmeticOperator::MULTIPLICATION: + scalar*= tmp->getValue(); + break; + case Dice::ArithmeticOperator::MINUS: + scalar-= tmp->getValue(); + break; + case Dice::ArithmeticOperator::POW: + scalar= static_cast<int>(pow(static_cast<double>(scalar), static_cast<double>(tmp->getValue()))); + break; + case Dice::ArithmeticOperator::DIVIDE: + case Dice::ArithmeticOperator::INTEGER_DIVIDE: + if(tmp->getValue() != 0) + { + scalar/= tmp->getValue(); + } + else + { + /// @todo Error cant divide by 0. Must be displayed. + } + break; + } + } + else + { + scalar= tmp->getValue(); + } + ++i; + } + return scalar; + } +} + +Dice::ArithmeticOperator DiceResult::getOperator() const +{ + return m_operator; +} + +void DiceResult::clear() +{ + m_diceValues.clear(); +} + +void DiceResult::setOperator(const Dice::ArithmeticOperator& dieOperator) +{ + m_operator= dieOperator; +} +QString DiceResult::toString(bool wl) +{ + QStringList scalarSum; + for(auto& die : m_diceValues) + { + scalarSum << QString::number(die->getValue()); + } + if(wl) + { + return QStringLiteral("%3 [label=\"DiceResult Value %1 dice %2\"]") + .arg(QString::number(getScalarResult()), scalarSum.join('_'), m_id); + } + else + { + return m_id; + } +} +Result* DiceResult::getCopy() const +{ + auto copy= new DiceResult(); + copy->setHomogeneous(m_homogeneous); + copy->setOperator(m_operator); + copy->m_id= m_id; + QList<Die*> list; + for(auto die : m_diceValues) + { + auto newdie= new Die(*die); + newdie->setDisplayed(false); + // die->displayed(); + list.append(newdie); + } + copy->setResultList(list); + copy->setPrevious(getPrevious()); + return copy; +} diff --git a/src/libparser/result/diceresult.h b/src/libparser/result/diceresult.h new file mode 100644 index 0000000..1b35b7e --- /dev/null +++ b/src/libparser/result/diceresult.h @@ -0,0 +1,97 @@ +/*************************************************************************** + * Copyright (C) 2014 by Renaud Guezennec * + * https://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 DICERESULT_H +#define DICERESULT_H +#include <QList> +#include <functional> + +#include "diceparser/diceparserhelper.h" +#include "die.h" +#include "result.h" +/** + * @brief The DiceResult class + */ +class DiceResult : public Result +{ +public: + /** + * @brief DiceResult + */ + DiceResult(); + /** + * @brief ~DiceResult + */ + virtual ~DiceResult() override; + + /** + * @brief getResultList + * @return + */ + virtual QList<Die*>& getResultList(); + /** + * @brief insertResult + */ + virtual void insertResult(Die*); + + /** + * @brief setResultList + * @param list + */ + virtual void setResultList(QList<Die*> list); + + /** + * @brief getScalar + * @return + */ + virtual QVariant getResult(Dice::RESULT_TYPE) override; + /** + * @brief toString + * @return + */ + virtual QString toString(bool wl) override; + /** + * @brief isHomogeneous + */ + virtual bool isHomogeneous() const; + /** + * @brief setHomogeneous + */ + virtual void setHomogeneous(bool); + + Dice::ArithmeticOperator getOperator() const; + void setOperator(const Dice::ArithmeticOperator& dieOperator); + bool contains(Die* die, const std::function<bool(const Die*, const Die*)> equal); + + void clear() override; + + virtual Result* getCopy() const override; + +protected: + qreal getScalarResult(); + +protected: + QList<Die*> m_diceValues; + bool m_homogeneous; + Dice::ArithmeticOperator m_operator{Dice::ArithmeticOperator::PLUS}; +}; +Q_DECLARE_METATYPE(QList<Die*>) +#endif // DICERESULT_H diff --git a/src/libparser/result/result.cpp b/src/libparser/result/result.cpp new file mode 100644 index 0000000..ca410b3 --- /dev/null +++ b/src/libparser/result/result.cpp @@ -0,0 +1,86 @@ +/*************************************************************************** + * Copyright (C) 2014 by Renaud Guezennec * + * https://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 "result.h" +#include <QUuid> + +Result::Result() + : m_resultTypes(static_cast<int>(Dice::RESULT_TYPE::NONE)) + , m_id(QString("\"%1\"").arg(QUuid::createUuid().toString())) + , m_previous(nullptr) +{ +} +Result::~Result() {} + +Result* Result::getPrevious() const +{ + return m_previous; +} + +void Result::setPrevious(Result* p) +{ + Q_ASSERT(p != this); + m_previous= p; +} + +bool Result::isStringResult() const +{ + return false; +} +void Result::clear() {} +bool Result::hasResultOfType(Dice::RESULT_TYPE type) const +{ + return (m_resultTypes & static_cast<int>(type)); +} +void Result::generateDotTree(QString& s) +{ + auto str= toString(true); + if(s.contains(str)) + return; + s.append(str); + s.append(";\n"); + + if(nullptr != m_previous) + { + s.append(toString(false)); + s.append(" -> "); + s.append(m_previous->toString(false)); + s.append("[label=\"previousResult\"]\n"); + m_previous->generateDotTree(s); + } + else + { + s.append(toString(false)); + s.append(" -> "); + s.append("nullptr"); + s.append(" [label=\"previousResult\", shape=\"box\"];\n"); + } +} + +QString Result::getId() const +{ + return m_id; +} + +QString Result::getStringResult() const +{ + return {}; +} diff --git a/src/libparser/result/result.h b/src/libparser/result/result.h new file mode 100644 index 0000000..4432682 --- /dev/null +++ b/src/libparser/result/result.h @@ -0,0 +1,96 @@ +/*************************************************************************** + * Copyright (C) 2014 by Renaud Guezennec * + * https://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 RESULT_H +#define RESULT_H + +#include <QString> +#include <QVariant> +#include <diceparser/diceparserhelper.h> +/** + * @brief The Result class + */ +class Result +{ +public: + /** + * @brief Result + */ + Result(); + /** + * @brief ~Result + */ + virtual ~Result(); + + /** + * @brief isScalar + * @return + */ + virtual bool hasResultOfType(Dice::RESULT_TYPE) const; + /** + * @brief getScalar + * @return + */ + virtual QVariant getResult(Dice::RESULT_TYPE)= 0; + /** + * @brief getPrevious + * @return + */ + virtual Result* getPrevious() const; + /** + * @brief setPrevious + */ + virtual void setPrevious(Result*); + /** + * @brief isStringResult + * @return + */ + virtual bool isStringResult() const; + + virtual void clear(); + + /** + * @brief getStringResult + * @return + */ + virtual QString getStringResult() const; + /** + * @brief generateDotTree + */ + void generateDotTree(QString&); + /** + * @brief toString + * @return + */ + virtual QString toString(bool wl)= 0; + virtual Result* getCopy() const= 0; + + QString getId() const; + +protected: + int m_resultTypes; /// @brief + QString m_id; + +private: + Result* m_previous= nullptr; /// @brief +}; + +#endif // RESULT_H diff --git a/src/libparser/result/scalarresult.cpp b/src/libparser/result/scalarresult.cpp new file mode 100644 index 0000000..dcbaa61 --- /dev/null +++ b/src/libparser/result/scalarresult.cpp @@ -0,0 +1,58 @@ +/*************************************************************************** + * Copyright (C) 2014 by Renaud Guezennec * + * https://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 "scalarresult.h" + +ScalarResult::ScalarResult() +{ + m_resultTypes= static_cast<int>(Dice::RESULT_TYPE::SCALAR); +} + +void ScalarResult::setValue(qreal i) +{ + m_value= i; +} +QVariant ScalarResult::getResult(Dice::RESULT_TYPE type) +{ + if(Dice::RESULT_TYPE::SCALAR == type) + { + return m_value; + } + else + return {}; +} +Result* ScalarResult::getCopy() const +{ + auto copy= new ScalarResult(); + copy->setValue(m_value); + return copy; +} +QString ScalarResult::toString(bool wl) +{ + if(wl) + { + return QString("%2 [label=\"ScalarResult %1\"]").arg(m_value).arg(m_id); + } + else + { + return m_id; + } +} diff --git a/src/libparser/result/scalarresult.h b/src/libparser/result/scalarresult.h new file mode 100644 index 0000000..73fe73b --- /dev/null +++ b/src/libparser/result/scalarresult.h @@ -0,0 +1,59 @@ +/*************************************************************************** + * Copyright (C) 2014 by Renaud Guezennec * + * https://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 SCALARRESULT_H +#define SCALARRESULT_H + +#include "result.h" +#include <Qt> + +/** + * @brief The ScalarResult class is used to store scalar result by many ExecutionNode. + */ +class ScalarResult : public Result +{ +public: + /** + * @brief ScalarResult + */ + ScalarResult(); + /** + * @brief getResult + * @return + */ + virtual QVariant getResult(Dice::RESULT_TYPE); + /** + * @brief setValue + * @param i + */ + void setValue(qreal i); + /** + * @brief toString + * @return + */ + virtual QString toString(bool); + virtual Result* getCopy() const; + +private: + qreal m_value= 0; +}; + +#endif // SCALARRESULT_H diff --git a/src/libparser/result/stringresult.cpp b/src/libparser/result/stringresult.cpp new file mode 100644 index 0000000..113c22c --- /dev/null +++ b/src/libparser/result/stringresult.cpp @@ -0,0 +1,117 @@ +#include "stringresult.h" +#include <QDebug> + +StringResult::StringResult() +{ + m_highlight= true; + m_resultTypes= static_cast<int>(Dice::RESULT_TYPE::STRING); +} +void StringResult::addText(QString text) +{ + m_value.append(text); +} +StringResult::~StringResult() {} +bool StringResult::hasResultOfType(Dice::RESULT_TYPE resultType) const +{ + bool val= false; + + switch(resultType) + { + case Dice::RESULT_TYPE::STRING: + val= !isDigitOnly(); + break; + case Dice::RESULT_TYPE::SCALAR: + val= isDigitOnly(); + break; + case Dice::RESULT_TYPE::DICE_LIST: + val= (isDigitOnly() && m_value.size() > 1); + break; + default: + break; + } + return val; +} + +void StringResult::setNoComma(bool b) +{ + m_commaSeparator= !b; +} + +QString StringResult::getText() const +{ + return m_commaSeparator ? m_value.join(",") : m_value.join(QString()); +} + +QVariant StringResult::getResult(Dice::RESULT_TYPE type) +{ + switch(type) + { + case Dice::RESULT_TYPE::STRING: + return getText(); + case Dice::RESULT_TYPE::SCALAR: + return getScalarResult(); + default: + return QVariant(); + } +} +QString StringResult::toString(bool wl) +{ + if(wl) + { + return QString("%2 [label=\"StringResult_value_%1\"]").arg(getText().replace("%", "_"), m_id); + } + else + { + return m_id; + } +} +void StringResult::setHighLight(bool b) +{ + m_highlight= b; +} + +bool StringResult::hasHighLight() const +{ + return m_highlight; +} + +void StringResult::finished() +{ + if(isDigitOnly()) + { + std::for_each(m_value.begin(), m_value.end(), [this](const QString& str) { + auto die= new Die(); + die->setMaxValue(m_stringCount); + die->setValue(str.toInt()); + insertResult(die); + }); + } +} + +void StringResult::setStringCount(int count) +{ + m_stringCount= count; +} + +bool StringResult::isDigitOnly() const +{ + return std::all_of(m_value.begin(), m_value.end(), [](const QString& str) { + bool ok= false; + str.toInt(&ok); + return ok; + }); +} + +Result* StringResult::getCopy() const +{ + auto copy= new StringResult(); + copy->setPrevious(getPrevious()); + copy->setHighLight(m_highlight); + std::for_each(m_value.begin(), m_value.end(), [copy](const QString& str) { copy->addText(str); }); + return copy; +} + +QString StringResult::getStringResult() const +{ + return getText(); +} diff --git a/src/libparser/result/stringresult.h b/src/libparser/result/stringresult.h new file mode 100644 index 0000000..b0ca539 --- /dev/null +++ b/src/libparser/result/stringresult.h @@ -0,0 +1,45 @@ +#ifndef STRINGRESULT_H +#define STRINGRESULT_H + +#include "diceresult.h" +#include <QString> +/** + * @brief The StringResult class stores command result for String. + */ + +class StringResult : public DiceResult +{ +public: + /** + * @brief StringResult + */ + StringResult(); + /** + * @brief StringResult + */ + virtual ~StringResult() override; + void addText(QString text); + void finished(); + QString getText() const; + virtual QVariant getResult(Dice::RESULT_TYPE) override; + virtual QString toString(bool) override; + + virtual void setHighLight(bool); + virtual bool hasHighLight() const; + virtual bool hasResultOfType(Dice::RESULT_TYPE resultType) const override; + virtual Result* getCopy() const override; + + bool isDigitOnly() const; + + void setStringCount(int count); + QString getStringResult() const override; + void setNoComma(bool b); + +private: + QStringList m_value; + bool m_highlight= true; + int m_stringCount= 0; + bool m_commaSeparator= true; +}; + +#endif // STRINGRESULT_H |