diff options
| author | 2020-07-11 00:45:44 +0200 | |
|---|---|---|
| committer | 2020-08-21 22:51:43 +0200 | |
| commit | 8087429c108c4f6bef5f792ce256b8d5b8b69101 (patch) | |
| tree | d847b1acadb3d4ac7166bda415794f61816de682 | |
| parent | 35d019ec86f272d4189852a3e69ede403c415a8c (diff) | |
| download | OneRoll-8087429c108c4f6bef5f792ce256b8d5b8b69101.tar.gz OneRoll-8087429c108c4f6bef5f792ce256b8d5b8b69101.zip | |
Rework exporting
| -rw-r--r-- | diceparser.cpp | 193 | ||||
| -rw-r--r-- | diceroller.cpp | 51 | ||||
| -rw-r--r-- | highlightdice.cpp | 28 | ||||
| -rw-r--r-- | include/diceparser.h | 64 | ||||
| -rw-r--r-- | include/highlightdice.h | 17 | ||||
| -rw-r--r-- | include/parsingtoolbox.h | 224 | ||||
| -rw-r--r-- | parsingtoolbox.cpp | 400 |
7 files changed, 523 insertions, 454 deletions
diff --git a/diceparser.cpp b/diceparser.cpp index 87f052a..1113013 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -23,9 +23,12 @@ #include <QDebug> #include <QFile> #include <QJsonArray> +#include <QJsonDocument> +#include <QJsonObject> #include <QObject> #include <QStringList> #include <functional> +#include <numeric> #include "booleancondition.h" #include "dicealias.h" @@ -70,12 +73,12 @@ bool DiceParser::parseLine(QString str, bool allowAlias) bool value= !instructions.empty(); if(!value) { - m_parsingToolbox->addError( - Dice::ERROR_CODE::NOTHING_UNDERSTOOD, - QObject::tr("Nothing was understood. To roll dice: !1d6 - full documation: " - "<a " - "href=\"https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md\">https://github.com/" - "Rolisteam/DiceParser/blob/master/HelpMe.md</a>")); + m_parsingToolbox->addError(Dice::ERROR_CODE::NOTHING_UNDERSTOOD, + QObject::tr("Nothing was understood. To roll dice: !1d6 - full documation: " + "<a " + "href=\"https://github.com/Rolisteam/DiceParser/blob/master/" + "HelpMe.md\">https://github.com/" + "Rolisteam/DiceParser/blob/master/HelpMe.md</a>")); } else if(value && !str.isEmpty()) { @@ -104,31 +107,7 @@ void DiceParser::start() } } -/*QList<qreal> DiceParser::lastIntegerResults() const -{ - QList<qreal> resultValues; - QStringList alreadyVisitedNode; - for(auto node : m_parsingToolbox->getStartNodes()) - { - ExecutionNode* next= ParsingToolBox::getLeafNode(node); - Result* result= next->getResult(); - bool scalarDone= false; - while((result != nullptr) && (!scalarDone)) - { - if(result->hasResultOfType(Dice::RESULT_TYPE::SCALAR)) - { - if(!alreadyVisitedNode.contains(result->getId())) - { - resultValues << result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); - alreadyVisitedNode << result->getId(); - } - scalarDone= true; - } - result= result->getPrevious(); - } - } - return resultValues; -} +/* QStringList DiceParser::stringResult() const { QStringList stringListResult; @@ -152,26 +131,7 @@ QStringList DiceParser::stringResult() const } return stringListResult; } -QStringList DiceParser::allFirstResultAsString(bool& hasAlias) -{ - // QStringList allResult; - QStringList stringListResult; - for(auto node : m_parsingToolbox->getStartNodes()) - { - QVariant var; - if(hasResultOfType(Dice::RESULT_TYPE::STRING, node, var)) - { - stringListResult << var.toString(); - hasAlias= true; - } - else if(hasResultOfType(Dice::RESULT_TYPE::SCALAR, node, var, true)) - { - stringListResult << QString::number(var.toReal()); - hasAlias= true; - } - } - return stringListResult; -} + QStringList DiceParser::allDiceResult(bool& hasAlias) const { QStringList stringListResult; @@ -216,54 +176,8 @@ QStringList DiceParser::allDiceResult(bool& hasAlias) const return stringListResult; }*/ -void DiceParser::diceResultFromEachInstruction(QList<ExportedDiceResult>& resultList) const -{ - for(auto start : m_parsingToolbox->getStartNodes()) - { - ExecutionNode* next= ParsingToolBox::getLeafNode(start); - Result* result= next->getResult(); - ExportedDiceResult nodeResult; - QSet<QString> alreadyAdded; - // qDebug() << "start"; - while(nullptr != result) - { - if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) - { - DiceResult* diceResult= dynamic_cast<DiceResult*>(result); - QList<HighLightDice> list; - quint64 faces= 0; - // qDebug() << "Begin of loop"; - for(auto& die : diceResult->getResultList()) - { - faces= die->getFaces(); - // qDebug() << "face" << faces << die->getValue() << - // die->getListValue() - // << next->toString(true); - HighLightDice hlDice(die->getListValue(), die->isHighlighted(), die->getColor(), - die->hasBeenDisplayed(), die->getFaces()); - // qDebug() << die->getListValue() << die->getFaces() << die->getUuid() << list.size(); - if(!alreadyAdded.contains(die->getUuid())) - { - list.append(hlDice); - alreadyAdded.insert(die->getUuid()); - } - } - // qDebug() << "End of loop" << list.size(); - if(!list.isEmpty()) - nodeResult.insert(faces, list); - } - if(nodeResult.isEmpty()) - result= result->getPrevious(); - else - result= nullptr; - } - // qDebug() << "end"; - resultList.append(nodeResult); - } - // qDebug() << resultList.size(); -} - -/*void DiceParser::lastDiceResult(QList<ExportedDiceResult>& diceValuesList, bool& homogeneous) const +/*void DiceParser::lastDiceResult(QList<ExportedDiceResult>& diceValuesList, +bool& homogeneous) const { QSet<QString> alreadySeenDice; for(auto start : m_parsingToolbox->getStartNodes()) @@ -286,8 +200,8 @@ void DiceParser::diceResultFromEachInstruction(QList<ExportedDiceResult>& result ListDiceResult listpair; for(auto& die : diceResult->getResultList()) { - if(die->hasBeenDisplayed() || alreadySeenDice.contains(die->getUuid())) - continue; + if(die->hasBeenDisplayed() || +alreadySeenDice.contains(die->getUuid())) continue; QList<qint64> valuesResult; valuesResult.append(die->getValue()); @@ -300,9 +214,8 @@ void DiceParser::diceResultFromEachInstruction(QList<ExportedDiceResult>& result valuesResult.append(i); } } - HighLightDice hlDice(valuesResult, die->isHighlighted(), die->getColor(), - die->hasBeenDisplayed(), 0); - listpair.append(hlDice); + HighLightDice hlDice(valuesResult, die->isHighlighted(), +die->getColor(), die->hasBeenDisplayed(), 0); listpair.append(hlDice); alreadySeenDice << die->getUuid(); } if(!listpair.isEmpty()) @@ -336,24 +249,12 @@ QString DiceParser::diceCommand() const bool DiceParser::hasIntegerResultNotInFirst() const { - bool result= false; - for(auto node : m_parsingToolbox->getStartNodes()) - { - QVariant var; - result|= hasResultOfType(Dice::RESULT_TYPE::SCALAR, node, var, true); - } - return result; + return m_parsingToolbox->hasIntegerResultNotInFirst(); } bool DiceParser::hasDiceResult() const { - bool result= false; - for(auto node : m_parsingToolbox->getStartNodes()) - { - QVariant var; - result|= hasResultOfType(Dice::RESULT_TYPE::DICE_LIST, node, var); - } - return result; + return m_parsingToolbox->hasDiceResult(); } bool DiceParser::hasStringResult() const { @@ -370,12 +271,11 @@ bool DiceParser::hasResultOfType(Dice::RESULT_TYPE type, ExecutionNode* node, QV bool scalarDone= false; ExecutionNode* next= ParsingToolBox::getLeafNode(node); Result* result= next->getResult(); - int i= 0; while((result != nullptr) && (!scalarDone)) { bool lastResult= false; if(notthelast) - lastResult= (nullptr == result->getPrevious() && i != 0); + lastResult= (nullptr == result->getPrevious()); if(result->hasResultOfType(type) && !lastResult) { @@ -383,41 +283,13 @@ bool DiceParser::hasResultOfType(Dice::RESULT_TYPE type, ExecutionNode* node, QV value= result->getResult(type); } result= result->getPrevious(); - ++i; } return scalarDone; +======= + return m_parsingToolbox->hasStringResult(); +>>>>>>> Rework exporting } -/*QList<qreal> DiceParser::sumOfDiceResult() const -{ - QList<qreal> resultValues; - for(auto node : m_parsingToolbox->getStartNodes()) - { - qreal resultValue= 0; - ExecutionNode* next= ParsingToolBox::getLeafNode(node); - Result* result= next->getResult(); - bool found= false; - while((nullptr != result) && (!found)) - { - if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) - { - DiceResult* myDiceResult= dynamic_cast<DiceResult*>(result); - if(nullptr != myDiceResult) - { - for(auto& die : myDiceResult->getResultList()) - { - resultValue+= die->getValue(); - } - found= true; - } - } - result= result->getPrevious(); - } - resultValues << resultValue; - } - return resultValues; -}*/ - int DiceParser::startNodeCount() const { return static_cast<int>(m_parsingToolbox->getStartNodes().size()); @@ -480,7 +352,7 @@ QString DiceParser::humanReadableWarning() const return str; } -QJsonObject DiceParser::exportResult() const +QString DiceParser::resultAsJSon() const { QJsonObject obj; QJsonArray instructions; @@ -488,20 +360,23 @@ QJsonObject DiceParser::exportResult() const { QJsonObject inst; - inst["str"]= ; - inst["diceval"]= ; - inst["scalar"]= ; + m_parsingToolbox->addResultInJson(inst, Dice::RESULT_TYPE::SCALAR, "scalar", start, true); + m_parsingToolbox->addResultInJson(inst, Dice::RESULT_TYPE::STRING, "string", start, false); + m_parsingToolbox->addDiceResultInJson(inst, start); instructions.append(inst); } - obj["instruction"]= instructions; + obj["instructions"]= instructions; obj["comment"]= m_parsingToolbox->getComment(); obj["error"]= humanReadableError(); - obj["scalar"]= scalarText; - obj["string"]= resultStr; + obj["scalar"]= m_parsingToolbox->finalScalarResult().first; + obj["string"]= m_parsingToolbox->finalStringResult(); obj["warning"]= humanReadableWarning(); obj["command"]= m_command; - return obj; + + QJsonDocument doc; + doc.setObject(obj); + return doc.toJson(); } void DiceParser::writeDownDotTree(QString filepath) diff --git a/diceroller.cpp b/diceroller.cpp index cba24df..e970a7c 100644 --- a/diceroller.cpp +++ b/diceroller.cpp @@ -60,39 +60,42 @@ QString DiceRoller::diceToText(QList<ExportedDiceResult>& diceList) for(auto& face : keys) { QStringList result; - ListDiceResult diceResult= dice.value(face); - for(const HighLightDice& tmp : diceResult) + auto list= dice.value(face); + for(auto diceResult : list) { - QStringList diceListStr; - QStringList diceListChildren; - int i= 0; - for(qint64& dievalue : tmp.getResult()) + for(const HighLightDice& tmp : diceResult) { - QString prefix("%1"); - if(i == 0) + QStringList diceListStr; + QStringList diceListChildren; + int i= 0; + for(qint64& dievalue : tmp.result()) { - diceListStr << prefix.arg(QString::number(dievalue)); + QString prefix("%1"); + if(i == 0) + { + diceListStr << prefix.arg(QString::number(dievalue)); + } + else + { + diceListChildren << prefix.arg(QString::number(dievalue)); + } + ++i; } - else + if(!diceListChildren.isEmpty()) { - diceListChildren << prefix.arg(QString::number(dievalue)); + diceListStr << QString("[%1]").arg(diceListChildren.join(' ')); } - ++i; + result << diceListStr.join(' '); } - if(!diceListChildren.isEmpty()) + + if(keys.size() > 1) { - diceListStr << QString("[%1]").arg(diceListChildren.join(' ')); + resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face); + } + else + { + resultGlobal << result; } - result << diceListStr.join(' '); - } - - if(keys.size() > 1) - { - resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face); - } - else - { - resultGlobal << result; } } global << resultGlobal.join(' '); diff --git a/highlightdice.cpp b/highlightdice.cpp index d180a8d..d0c9faa 100644 --- a/highlightdice.cpp +++ b/highlightdice.cpp @@ -19,14 +19,20 @@ ***************************************************************************/ #include "include/highlightdice.h" -HighLightDice::HighLightDice(QList<qint64> result, bool isHighlighted, QString color, bool displayed, quint64 faces) - : m_result(result), m_hasHighlight(isHighlighted), m_color(color), m_displayed(displayed), m_faces(faces) +HighLightDice::HighLightDice(QList<qint64> result, bool isHighlighted, QString color, bool displayed, quint64 faces, + const QString& uuid) + : m_result(result) + , m_hasHighlight(isHighlighted) + , m_color(color) + , m_displayed(displayed) + , m_faces(faces) + , m_uuid(uuid) { } HighLightDice::~HighLightDice() {} -QList<qint64> HighLightDice::getResult() const +QList<qint64> HighLightDice::result() const { return m_result; } @@ -46,7 +52,7 @@ void HighLightDice::setHighlight(bool hasHighlight) m_hasHighlight= hasHighlight; } -QString HighLightDice::getColor() const +QString HighLightDice::color() const { return m_color; } @@ -56,7 +62,7 @@ void HighLightDice::setColor(const QString& color) m_color= color; } -bool HighLightDice::getDisplayed() const +bool HighLightDice::displayed() const { return m_displayed; } @@ -66,7 +72,7 @@ void HighLightDice::setDisplayed(bool displayed) m_displayed= displayed; } -quint64 HighLightDice::getFaces() const +quint64 HighLightDice::faces() const { return m_faces; } @@ -92,3 +98,13 @@ QString HighLightDice::getResultString() const return QStringLiteral("%2 [%1]").arg(list.join(",")).arg(totalScore); } } + +QString HighLightDice::uuid() const +{ + return m_uuid; +} + +void HighLightDice::setUuid(const QString& uuid) +{ + m_uuid= uuid; +} diff --git a/include/diceparser.h b/include/diceparser.h index eb4dee4..4d5555e 100644 --- a/include/diceparser.h +++ b/include/diceparser.h @@ -89,61 +89,10 @@ public: QList<DiceAlias*>* aliases() const; void cleanAliases(); void insertAlias(DiceAlias*, int); - /** - * @brief getErrorList - * @return - */ - QMap<Dice::ERROR_CODE, QString> errorMap() const; - /** - * @brief setPathToHelp set the path to the documentation, this path must be adatped to the lang of application etc… - * @param l the path. - */ - void setPathToHelp(QString l); - /** - * @brief allFirstResultAsString - * @return - */ + QString convertAlias(const QString& cmd) const; + QStringList allFirstResultAsString(bool& hasAlias); - /** - * @brief getAllDiceResult - * @param hasAlias - * @return - */ QStringList getAllDiceResult(bool& hasAlias); - /** - * @brief hasSeparator allows to know if the current command has separator. - * @return true when the command has separator, false otherwise. - */ - bool hasSeparator() const; -// beginning of strange code ||||||| parent of af8b69b... change the way diceparser is giving its result. - /** - * @brief getErrorList - * @return - */ - QMap<Dice::ERROR_CODE, QString> getErrorMap(); - /** - * @brief setPathToHelp set the path to the documentation, this path must be adatped to the lang of application etc… - * @param l the path. - */ - void setPathToHelp(QString l); - /** - * @brief getAllStringResult - * @return - */ - QStringList getAllStringResult(bool& hasAlias); - /** - * @brief getAllDiceResult - * @param hasAlias - * @return - */ - QStringList getAllDiceResult(bool& hasAlias); - /** - * @brief hasSeparator allows to know if the current command has separator. - * @return true when the command has separator, false otherwise. - */ - bool hasSeparator() const; -// END of strange code - QString convertAlias(const QString& cmd) const; // Accessors int startNodeCount() const; @@ -156,12 +105,11 @@ public: QString comment() const; QString humanReadableWarning() const; QString humanReadableError() const; - QJsonObject exportResult() const; + QString resultAsJSon() const; // QStringList stringResult() const; // QStringList allDiceResult(bool& hasAlias) const; // void lastDiceResult(QList<ExportedDiceResult>& diceValues, bool& homogeneous) const; - // QList<qreal> sumOfDiceResult() const; // setters void setPathToHelp(QString l); @@ -169,12 +117,6 @@ public: void setComment(const QString& comment); private: - /** - * @brief hasResultOfType - * @param notthelast - * @return - */ - bool hasResultOfType(Dice::RESULT_TYPE, ExecutionNode* node, QVariant& value, bool notthelast= false); bool readBlocInstruction(QString& str, ExecutionNode*& resultnode); private: diff --git a/include/highlightdice.h b/include/highlightdice.h index c70a371..66bd445 100644 --- a/include/highlightdice.h +++ b/include/highlightdice.h @@ -27,35 +27,40 @@ class HighLightDice { public: - HighLightDice(QList<qint64> result, bool isHighlighted, QString color, bool displayed, quint64 faces); + HighLightDice(QList<qint64> result, bool isHighlighted, QString color, bool displayed, quint64 faces, + const QString& uuid); virtual ~HighLightDice(); - QList<qint64> getResult() const; + QList<qint64> result() const; void setResult(const QList<qint64>& result); bool isHighlighted() const; void setHighlight(bool hasHighlight); - QString getColor() const; + QString color() const; void setColor(const QString& color); - bool getDisplayed() const; + bool displayed() const; void setDisplayed(bool displayed); - quint64 getFaces() const; + quint64 faces() const; void setFaces(const quint64& faces); QString getResultString() const; + QString uuid() const; + void setUuid(const QString& uuid); + private: QList<qint64> m_result; bool m_hasHighlight= true; QString m_color; bool m_displayed= false; quint64 m_faces; + QString m_uuid; }; typedef QList<HighLightDice> ListDiceResult; -typedef QMap<quint64, ListDiceResult> ExportedDiceResult; +typedef QMap<quint64, QList<ListDiceResult>> ExportedDiceResult; #endif // HighLightDice_H diff --git a/include/parsingtoolbox.h b/include/parsingtoolbox.h index 40d2b33..d0a757f 100644 --- a/include/parsingtoolbox.h +++ b/include/parsingtoolbox.h @@ -138,174 +138,45 @@ public: * @brief ~ParsingToolBox */ virtual ~ParsingToolBox(); - void clearUp(); - /** - * @brief addSort - * @param e - * @param b - * @return - */ + + // Build execution tree + DiceRollerNode* getDiceRollerNode(ExecutionNode* previous); + DiceRollerNode* addRollDiceNode(qint64 faces, ExecutionNode*); + ExplodeDiceNode* addExplodeDiceNode(qint64 faces, ExecutionNode* previous); + Dice::CONDITION_STATE isValidValidator(ExecutionNode* previous, ValidatorList* val); ExecutionNode* addSort(ExecutionNode* e, bool b); - /** - * @brief readAscending - * @param str - * @return - */ + + // parsing tools static bool readAscending(QString& str); - /** - * @brief readLogicOperator - * @param str - * @param op - * @return - */ bool readLogicOperator(QString& str, BooleanCondition::LogicOperator& op); - /** - * @brief readValidator - * @param str - * @return - */ Validator* readValidator(QString& str, bool hasSquare= false); - /** - * @brief readCompositeValidator - * @param str - * @return - */ ValidatorList* readValidatorList(QString& str); - - /** - * @brief readNumber read number in the given str and remove from the string the read character. - * @param str the command line - * @param myNumber reference to the found number - * @return true, succeed to read number, false otherwise. - */ static bool readNumber(QString& str, qint64& myNumber); - - /** - * @brief readString - * @param str - * @param strResult - * @return - */ static bool readString(QString& str, QString& strresult); - /** - * @brief readVariable - * @param str - * @param myNumber - * @return - */ static bool readVariable(QString& str, qint64& myNumber, QString& reasonFail); - /** - * @brief readOpenParentheses - * @param str - * @return - */ static bool readOpenParentheses(QString& str); - /** - * @brief readCloseParentheses - * @param str - * @return - */ static bool readCloseParentheses(QString& str); - /** - * @brief readNumber read number in the given str and remove from the string the read character. - * @param str the command line - * @param myNumber reference to the found number - * @return true, succeed to read number, false otherwise. - */ static bool readDynamicVariable(QString& str, qint64& index); - - /** - * @brief readList - * @param str - * @param list - * @return - */ bool readList(QString& str, QStringList& list, QList<Range>& ranges); - /** - * @brief isValidValidator - * @param previous - * @param val - * @return - */ - Dice::CONDITION_STATE isValidValidator(ExecutionNode* previous, ValidatorList* val); - /** - * @brief getDiceRollerNode - * @param previous - * @return - */ - DiceRollerNode* getDiceRollerNode(ExecutionNode* previous); - - /** - * @brief readDiceRange - * @param str - * @param start - * @param end - * @return - */ bool readDiceRange(QString& str, qint64& start, qint64& end); - /** - * @brief readListOperator - * @param str - * @return - */ static LIST_OPERATOR readListOperator(QString& str); - void readProbability(QStringList& str, QList<Range>& ranges); - bool readLogicOperation(QString& str, ValidatorList::LogicOperation& op); - bool readDiceLogicOperator(QString& str, OperationCondition::ConditionOperator& op); - bool readArithmeticOperator(QString& str, Die::ArithmeticOperator& op); std::vector<ExecutionNode*> readInstructionList(QString& str, bool startNode); - - static bool readPainterParameter(PainterNode* painter, QString& str); - - static QHash<QString, QString> getVariableHash(); - static void setVariableHash(const QHash<QString, QString>& variableHash); - /** - * @brief readConditionType - * @param str - * @return - */ static Dice::ConditionType readConditionType(QString& str); - bool readComment(QString& str, QString&, QString&); - static ExecutionNode* getLatestNode(ExecutionNode* node); - - const std::vector<ExecutionNode*>& getStartNodes(); - static void setStartNodes(std::vector<ExecutionNode*>* startNodes); - bool readOperand(QString& str, ExecutionNode*& node); static int findClosingCharacterIndexOf(QChar open, QChar closing, const QString& str, int offset); - - static QString replaceVariableToValue(const QString& source, QStringList values, - QMap<Dice::ERROR_CODE, QString>& errorMap); - static QString replacePlaceHolderToValue(const QString& source, const QList<ExportedDiceResult>& list); - - static SubtituteInfo readVariableFromString(const QString& source, int& start); - static SubtituteInfo readPlaceHolderFromString(const QString& source, int& start); - static void readSubtitutionParameters(SubtituteInfo& info, QString& rest); - + static bool readPainterParameter(PainterNode* painter, QString& str); static bool readComma(QString& str); - bool readReaperArguments(RepeaterNode* node, QString& source); - - DiceRollerNode* addRollDiceNode(qint64 faces, ExecutionNode*); - - ExplodeDiceNode* addExplodeDiceNode(qint64 faces, ExecutionNode* previous); - bool readExpression(QString& str, ExecutionNode*& node); - - static ExecutionNode* getLeafNode(ExecutionNode* start); - - bool hasError() const; - bool readInstructionOperator(QChar c); - bool readNode(QString& str, ExecutionNode*& node); /** * @brief readIfInstruction reads the current command to build if node with proper parameters. @@ -316,67 +187,62 @@ public: * @return true, ifNode has been found, false otherwise */ bool readIfInstruction(QString& str, ExecutionNode*& trueNode, ExecutionNode*& falseNode); - bool readOptionFromNull(QString& str, ExecutionNode*& node); bool readOperatorFromNull(QString& str, ExecutionNode*& node); bool readParameterNode(QString& str, ExecutionNode*& node); - - /** - * - */ bool readFunction(QString& str, ExecutionNode*& node); - /** - * @brief readDice - * @param str - * @return - */ bool readDice(QString& str, ExecutionNode*& node); - /** - * @brief readDiceOperator - * @return - */ bool readDiceOperator(QString&, DiceOperator&); - /** - * @brief readDiceExpression - * @param node - * @return - */ bool readDiceExpression(QString&, ExecutionNode*& node); - /** - * @brief readOperator - * @return - */ bool readOperator(QString&, ExecutionNode* previous); - /** - * @brief DiceParser::readCommand - * @param str - * @param node - * @return - */ bool readCommand(QString& str, ExecutionNode*& node); - - /** - * @brief readOption - */ + bool readBlocInstruction(QString& str, ExecutionNode*& resultnode); bool readOption(QString&, ExecutionNode* node); // OptionOperator& option, - bool readValuesList(QString& str, ExecutionNode*& node); + // Error + bool hasError() const; void addError(Dice::ERROR_CODE code, const QString& msg); void addWarning(Dice::ERROR_CODE code, const QString& msg); - - void setComment(const QString& comment); - QString getComment() const; - const QMap<Dice::ERROR_CODE, QString>& getErrorList() const; const QMap<Dice::ERROR_CODE, QString>& getWarningList() const; - QString convertAlias(QString str); - - bool readBlocInstruction(QString& str, ExecutionNode*& resultnode); + // Traversal functions + static ExecutionNode* getLatestNode(ExecutionNode* node); + static ExecutionNode* getLeafNode(ExecutionNode* start); + const std::vector<ExecutionNode*>& getStartNodes(); + static void setStartNodes(std::vector<ExecutionNode*>* startNodes); + std::pair<bool, QVariant> hasResultOfType(Dice::RESULT_TYPE, ExecutionNode* node, bool notthelast= false) const; + QList<qreal> scalarResultsFromEachInstruction() const; + std::pair<QString, QString> finalScalarResult() const; + QString finalStringResult() const; + QStringList allFirstResultAsString(bool& hasAlias) const; + QList<qreal> sumOfDiceResult() const; + QList<ExportedDiceResult> diceResultFromEachInstruction() const; + bool hasIntegerResultNotInFirst() const; + bool hasDiceResult() const; + bool hasStringResult() const; + + // result + static QString replaceVariableToValue(const QString& source, QStringList values, + QMap<Dice::ERROR_CODE, QString>& errorMap); + static QString replacePlaceHolderToValue(const QString& source, const QList<ExportedDiceResult>& list); + static SubtituteInfo readVariableFromString(const QString& source, int& start); + static SubtituteInfo readPlaceHolderFromString(const QString& source, int& start); + static ExportedDiceResult finalDiceResultFromInstruction(ExecutionNode* start); + static ExportedDiceResult allDiceResultFromInstruction(ExecutionNode* start); + void addResultInJson(QJsonObject obj, Dice::RESULT_TYPE type, const QString& key, ExecutionNode* start, bool b); + void addDiceResultInJson(QJsonObject obj, ExecutionNode* start); + // accessors + void setComment(const QString& comment); + QString getComment() const; void setHelpPath(const QString& path); + static QHash<QString, QString> getVariableHash(); + static void setVariableHash(const QHash<QString, QString>& variableHash); + // Aliases + QString convertAlias(QString str); void insertAlias(DiceAlias* dice, int i); const QList<DiceAlias*>& getAliases() const; QList<DiceAlias*>* aliases(); diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index de7fe05..b357716 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -21,6 +21,12 @@ ***************************************************************************/ #include "parsingtoolbox.h" +#include <QJsonArray> +#include <QJsonObject> +#include <QRegularExpression> +#include <QString> +#include <set> + #include "node/allsamenode.h" #include "node/bind.h" #include "node/countexecutenode.h" @@ -51,8 +57,6 @@ #include "node/valueslistnode.h" #include "node/variablenode.h" -#include <QString> - QHash<QString, QString> ParsingToolBox::m_variableHash; ParsingToolBox::ParsingToolBox() @@ -490,6 +494,245 @@ const std::vector<ExecutionNode*>& ParsingToolBox::getStartNodes() return m_startNodes; } +QStringList ParsingToolBox::allFirstResultAsString(bool& hasAlias) const +{ + // QStringList allResult; + QStringList stringListResult; + for(auto node : m_startNodes) + { + QVariant var; + auto stringPair= hasResultOfType(Dice::RESULT_TYPE::STRING, node); + auto scalarPair= hasResultOfType(Dice::RESULT_TYPE::SCALAR, node, true); + if(stringPair.first) + { + stringListResult << stringPair.second.toString(); + hasAlias= true; + } + else if(scalarPair.first) + { + stringListResult << QString::number(scalarPair.second.toReal()); + hasAlias= true; + } + } + return stringListResult; +} +std::pair<bool, QVariant> ParsingToolBox::hasResultOfType(Dice::RESULT_TYPE type, ExecutionNode* node, + bool notthelast) const +{ + bool hasValidResult= false; + QVariant var; + ExecutionNode* next= ParsingToolBox::getLeafNode(node); + Result* result= next->getResult(); + while((result != nullptr) && (!hasValidResult)) + { + bool lastResult= false; + if(notthelast) + lastResult= (nullptr == result->getPrevious()); + + if(result->hasResultOfType(type) && !lastResult) + { + hasValidResult= true; + var= result->getResult(type); + } + result= result->getPrevious(); + } + return {hasValidResult, var}; +} + +QList<qreal> ParsingToolBox::scalarResultsFromEachInstruction() const +{ + QList<qreal> resultValues; + std::set<QString> alreadyVisitedNode; + for(auto node : m_startNodes) + { + ExecutionNode* next= ParsingToolBox::getLeafNode(node); + Result* result= next->getResult(); + bool scalarDone= false; + while((result != nullptr) && (!scalarDone)) + { + if(result->hasResultOfType(Dice::RESULT_TYPE::SCALAR)) + { + if(alreadyVisitedNode.find(result->getId()) == alreadyVisitedNode.end()) + { + resultValues << result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); + alreadyVisitedNode.insert(result->getId()); + } + scalarDone= true; + } + result= result->getPrevious(); + } + } + return resultValues; +} + +QList<qreal> ParsingToolBox::sumOfDiceResult() const +{ + QList<qreal> resultValues; + for(auto node : m_startNodes) + { + qreal resultValue= 0; + ExecutionNode* next= ParsingToolBox::getLeafNode(node); + Result* result= next->getResult(); + bool found= false; + while((nullptr != result) && (!found)) + { + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) + { + DiceResult* myDiceResult= dynamic_cast<DiceResult*>(result); + if(nullptr != myDiceResult) + { + for(auto& die : myDiceResult->getResultList()) + { + resultValue+= die->getValue(); + } + found= true; + } + } + result= result->getPrevious(); + } + resultValues << resultValue; + } + return resultValues; +} + +std::pair<QString, QString> ParsingToolBox::finalScalarResult() const +{ + QString scalarText; + QString lastScalarText; + auto listDie= diceResultFromEachInstruction(); + if(hasIntegerResultNotInFirst()) + { + QStringList strLst; + auto listScalar= scalarResultsFromEachInstruction(); + for(auto val : listScalar) + { + strLst << QString::number(val); + } + scalarText= QString("%1").arg(strLst.join(',')); + lastScalarText= strLst.last(); + } + else if(!listDie.isEmpty()) + { + auto values= sumOfDiceResult(); + QStringList strLst; + for(auto val : values) + { + strLst << QString::number(val); + } + scalarText= QString("%1").arg(strLst.join(',')); + } + return {scalarText, lastScalarText}; +} + +bool ParsingToolBox::hasIntegerResultNotInFirst() const +{ + bool result= false; + for(auto node : m_startNodes) + { + result|= hasResultOfType(Dice::RESULT_TYPE::SCALAR, node, true).first; + } + return result; +} + +bool ParsingToolBox::hasDiceResult() const +{ + bool result= false; + for(auto node : m_startNodes) + { + result|= hasResultOfType(Dice::RESULT_TYPE::DICE_LIST, node).first; + } + return result; +} +bool ParsingToolBox::hasStringResult() const +{ + bool result= false; + for(auto node : m_startNodes) + { + result|= hasResultOfType(Dice::RESULT_TYPE::STRING, node).first; + } + return result; +} + +QList<ExportedDiceResult> ParsingToolBox::diceResultFromEachInstruction() const +{ + QList<ExportedDiceResult> resultList; + for(auto start : m_startNodes) + { + auto result= ParsingToolBox::finalDiceResultFromInstruction(start); + resultList.append(result); + } + return resultList; +} + +QStringList listOfDiceResult(const QList<ExportedDiceResult>& list) +{ + QStringList listOfDiceResult; + for(auto map : list) + { + for(auto key : map.keys()) + { + auto listOfList= map.value(key); + for(auto dice : listOfList) + { + QString stringVal; + for(auto val : dice) + { + qint64 total= 0; + QStringList dicelist; + for(auto score : val.result()) + { + total+= score; + dicelist << QString::number(score); + } + if(val.result().size() > 1) + { + stringVal= QString("%1 [%2]").arg(total).arg(dicelist.join(',')); + listOfDiceResult << stringVal; + } + else + { + listOfDiceResult << QString::number(total); + } + } + } + } + } + return listOfDiceResult; +} + +QString ParsingToolBox::finalStringResult() const +{ + bool ok; + QStringList allStringlist= allFirstResultAsString(ok); + auto listFull= diceResultFromEachInstruction(); + + QStringList resultWithPlaceHolder; + std::for_each(allStringlist.begin(), allStringlist.end(), [&resultWithPlaceHolder](const QString& sub) { + QRegularExpression ex("%[1-3]?|\\$[1-9]+|@[1-9]+"); + if(sub.contains(ex)) + resultWithPlaceHolder.append(sub); + }); + auto stringResult= resultWithPlaceHolder.isEmpty() ? allStringlist.join(" ; ") : resultWithPlaceHolder.join(" ; "); + + auto pairScalar= finalScalarResult(); + + stringResult.replace("%1", pairScalar.first); + stringResult.replace("%2", listOfDiceResult(diceResultFromEachInstruction()).join(",").trimmed()); + stringResult.replace("%3", pairScalar.second); + stringResult.replace("\\n", "\n"); + + QMap<Dice::ERROR_CODE, QString> errorMap; + stringResult= ParsingToolBox::replaceVariableToValue(stringResult, allStringlist, errorMap); + stringResult= ParsingToolBox::replacePlaceHolderToValue(stringResult, listFull); + + /*bool isInt= true; + stringResult.toInt(&isInt); + if(!isInt) + resultStr= stringResult;*/ + + return stringResult; +} + bool ParsingToolBox::readString(QString& str, QString& strResult) { if(str.isEmpty()) @@ -925,10 +1168,16 @@ QString ParsingToolBox::replacePlaceHolderToValue(const QString& source, const Q { auto values= dice.values(); std::transform(std::begin(values), std::end(values), std::back_inserter(valuesStr), - [](const ListDiceResult& dice) { + [](const QList<ListDiceResult>& dice) { QStringList textList; std::transform(std::begin(dice), std::end(dice), std::back_inserter(textList), - [](const HighLightDice& dice) { return dice.getResultString(); }); + [](const ListDiceResult& dice) { + QStringList list; + std::transform( + std::begin(dice), std::end(dice), std::back_inserter(list), + [](const HighLightDice& hl) { return hl.getResultString(); }); + return list.join(","); + }); return textList.join(","); }); } @@ -936,11 +1185,14 @@ QString ParsingToolBox::replacePlaceHolderToValue(const QString& source, const Q { for(auto key : dice.keys()) { - auto values= dice[key]; - QStringList textVals; - std::transform(std::begin(values), std::end(values), std::back_inserter(textVals), - [](const HighLightDice& dice) { return dice.getResultString(); }); - valuesStr.append(QString("d%1 [%2]").arg(key).arg(textVals.join(","))); + auto list= dice.value(key); + for(auto values : list) + { + QStringList textVals; + std::transform(std::begin(values), std::end(values), std::back_inserter(textVals), + [](const HighLightDice& dice) { return dice.getResultString(); }); + valuesStr.append(QString("d%1 [%2]").arg(key).arg(textVals.join(","))); + } } } return valuesStr.join(","); @@ -957,14 +1209,6 @@ QString ParsingToolBox::replacePlaceHolderToValue(const QString& source, const Q result.remove(ref.position(), ref.length()); auto val= resultList[ref.resultIndex() - 1]; result.insert(ref.position(), val); - /* if(ref.digitNumber() != 0) - { - auto realVal= QString("%1").arg(val, ref.digitNumber(), QChar('0')); - result.insert(ref.position(), realVal); - } - else - { - }*/ } else { @@ -973,8 +1217,6 @@ QString ParsingToolBox::replacePlaceHolderToValue(const QString& source, const Q } while(valid); return result; - - // return source; } void ParsingToolBox::readSubtitutionParameters(SubtituteInfo& info, QString& rest) { @@ -2020,6 +2262,126 @@ SubtituteInfo ParsingToolBox::readPlaceHolderFromString(const QString& source, i return info; } +ExportedDiceResult ParsingToolBox::finalDiceResultFromInstruction(ExecutionNode* start) +{ + ExecutionNode* next= ParsingToolBox::getLeafNode(start); + Result* result= next->getResult(); + ExportedDiceResult nodeResult; + std::set<QString> alreadyAdded; + while(nullptr != result) + { + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) + { + DiceResult* diceResult= dynamic_cast<DiceResult*>(result); + QList<HighLightDice> list; + quint64 faces= 0; + for(auto& die : diceResult->getResultList()) + { + faces= die->getFaces(); + HighLightDice hlDice(die->getListValue(), die->isHighlighted(), die->getColor(), + die->hasBeenDisplayed(), die->getFaces(), die->getUuid()); + if(alreadyAdded.find(die->getUuid()) == alreadyAdded.end()) + { + list.append(hlDice); + alreadyAdded.insert(die->getUuid()); + } + } + if(!list.isEmpty()) + { + auto vals= nodeResult.value(faces); + vals.append(list); + nodeResult.insert(faces, vals); + } + } + if(nodeResult.isEmpty()) + result= result->getPrevious(); + else + result= nullptr; + } + return nodeResult; +} + +ExportedDiceResult ParsingToolBox::allDiceResultFromInstruction(ExecutionNode* start) +{ + ExecutionNode* next= ParsingToolBox::getLeafNode(start); + Result* result= next->getResult(); + ExportedDiceResult nodeResult; + std::set<QString> alreadyAdded; + while(nullptr != result) + { + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) + { + DiceResult* diceResult= dynamic_cast<DiceResult*>(result); + QList<HighLightDice> list; + quint64 faces= 0; + for(auto& die : diceResult->getResultList()) + { + faces= die->getFaces(); + HighLightDice hlDice(die->getListValue(), die->isHighlighted(), die->getColor(), + die->hasBeenDisplayed(), die->getFaces(), die->getUuid()); + if(alreadyAdded.find(die->getUuid()) == alreadyAdded.end()) + { + list.append(hlDice); + alreadyAdded.insert(die->getUuid()); + } + } + if(!list.isEmpty()) + { + auto vals= nodeResult.value(faces); + vals.append(list); + nodeResult.insert(faces, vals); + } + } + result= result->getPrevious(); + } + return nodeResult; +} + +void ParsingToolBox::addResultInJson(QJsonObject obj, Dice::RESULT_TYPE type, const QString& key, ExecutionNode* start, + bool b) +{ + auto pair= hasResultOfType(type, start, b); + if(pair.first) + obj[key]= pair.second.toReal(); +} + +void ParsingToolBox::addDiceResultInJson(QJsonObject obj, ExecutionNode* start) +{ + QJsonArray diceValues; + auto result= ParsingToolBox::allDiceResultFromInstruction(start); + for(auto listOfList : result.values()) + { + for(auto listDiceResult : listOfList) + { + for(auto hlDice : listDiceResult) + { + QJsonObject diceObj; + diceObj["face"]= static_cast<qreal>(hlDice.faces()); + diceObj["color"]= hlDice.color(); + diceObj["displayed"]= hlDice.displayed(); + diceObj["string"]= hlDice.getResultString(); + diceObj["highlight"]= hlDice.isHighlighted(); + diceObj["uuid"]= hlDice.uuid(); + auto val= hlDice.result(); + if(!val.isEmpty()) + { + diceObj["value"]= std::accumulate(val.begin(), val.end(), 0); + if(val.size() > 1) + { + QJsonArray intValues; + std::transform(val.begin(), val.end(), std::back_inserter(intValues), + [](qint64 val) { return static_cast<int>(val); }); + diceObj["subvalues"]= intValues; + } + } + diceValues.append(diceObj); + } + } + } + if(!diceValues.isEmpty()) + obj["diceval"]= diceValues; +} + SubtituteInfo::SubtituteInfo() {} bool SubtituteInfo::isValid() const |