diff options
| author | 2020-07-11 00:45:44 +0200 | |
|---|---|---|
| committer | 2020-08-21 22:51:43 +0200 | |
| commit | 8087429c108c4f6bef5f792ce256b8d5b8b69101 (patch) | |
| tree | d847b1acadb3d4ac7166bda415794f61816de682 /diceparser.cpp | |
| parent | 35d019ec86f272d4189852a3e69ede403c415a8c (diff) | |
| download | OneRoll-8087429c108c4f6bef5f792ce256b8d5b8b69101.tar.gz OneRoll-8087429c108c4f6bef5f792ce256b8d5b8b69101.zip | |
Rework exporting
Diffstat (limited to 'diceparser.cpp')
| -rw-r--r-- | diceparser.cpp | 193 |
1 files changed, 34 insertions, 159 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) |