diff options
| author | 2020-08-22 17:09:57 +0200 | |
|---|---|---|
| committer | 2020-08-22 17:09:57 +0200 | |
| commit | 8209ac895fac8f993c13187195287f4021457da5 (patch) | |
| tree | 98a318b93febe66426f2ab01d88ceabb8bdc9c1b | |
| parent | 8ee9c4372efce878e817c49b375a8fd904ea5a43 (diff) | |
| download | OneRoll-8209ac895fac8f993c13187195287f4021457da5.tar.gz OneRoll-8209ac895fac8f993c13187195287f4021457da5.zip | |
Fix repeat node.
| -rw-r--r-- | cli/displaytoolbox.cpp | 90 | ||||
| -rw-r--r-- | cli/displaytoolbox.h | 2 | ||||
| -rw-r--r-- | cli/main.cpp | 332 | ||||
| -rw-r--r-- | diceparser.cpp | 15 | ||||
| -rw-r--r-- | include/diceparser.h | 1 | ||||
| -rw-r--r-- | include/parsingtoolbox.h | 20 | ||||
| -rw-r--r-- | node/repeaternode.cpp | 62 | ||||
| -rw-r--r-- | parsingtoolbox.cpp | 7 | ||||
| -rw-r--r-- | tests/dice/tst_dice.cpp | 20 |
9 files changed, 207 insertions, 342 deletions
diff --git a/cli/displaytoolbox.cpp b/cli/displaytoolbox.cpp index 9dc8fb6..0c527e0 100644 --- a/cli/displaytoolbox.cpp +++ b/cli/displaytoolbox.cpp @@ -122,54 +122,7 @@ QString DisplayToolBox::diceToSvg(QJsonArray array, bool withColor, bool allSame } } #include <QVariantList> -QJsonArray DisplayToolBox::diceToJson(QList<ExportedDiceResult>& diceList, bool& allSameFaceCount, bool& allSameColor) -{ - allSameFaceCount= true; - QJsonArray array; - QStringList colorList; - for(auto dice : diceList) - { - if(dice.size() > 1) - { - allSameFaceCount= false; - } - for(quint64 face : dice.keys()) - { - ListDiceResult diceResults= dice.value(face); - QJsonObject object; - QVariantList listVariant; - object["face"]= static_cast<int>(face); - listVariant.reserve(diceResults.size()); - for(auto const& dice : diceResults) - { - QJsonObject diceObj; - auto listValues= dice.getResult(); - if(!listValues.isEmpty()) - { - diceObj["total"]= static_cast<qint64>(listValues.takeFirst()); - diceObj["face"]= static_cast<int>(face); - auto color= dice.getColor(); - diceObj["color"]= color; - if(!colorList.contains(color)) - colorList.append(color); - QJsonArray subValues; - for(auto result : listValues) - { - subValues.push_back(static_cast<qint64>(result)); - } - diceObj["subvalues"]= subValues; - } - listVariant.append(QVariant::fromValue(diceObj)); - } - object["values"]= QJsonArray::fromVariantList(listVariant); - array.push_back(object); - } - } - if(colorList.size() > 1) - allSameColor= false; - return array; -} QString DisplayToolBox::diceResultToString(QJsonObject val, Output type, bool hasColor) { auto total= QString::number(val["total"].toDouble()); @@ -204,46 +157,3 @@ QString DisplayToolBox::diceResultToString(QJsonObject val, Output type, bool ha } return total; } -QString DisplayToolBox::diceToText(QJsonArray array, bool withColor, bool allSameFaceCount, bool allSameColor) -{ - Q_UNUSED(allSameColor) - QStringList result; - for(auto item : array) - { - QString subResult(""); - auto obj= item.toObject(); - auto values= obj["values"].toArray(); - - QStringList diceResult; - for(auto valRef : values) - { - diceResult+= diceResultToString(valRef.toObject(), Output::Terminal, withColor); - } - if(!diceResult.isEmpty()) - { - if(!allSameFaceCount) - { - subResult+= QStringLiteral("d%1:(").arg(obj["face"].toInt()); - } - if(withColor) - { - subResult+= DisplayToolBox::colorToTermCode(obj["color"].toString()); - } - subResult+= diceResult.join(" "); - if(withColor) - { - subResult+= DisplayToolBox::colorToTermCode(QStringLiteral("reset")); - } - if(!allSameFaceCount) - { - subResult+= QStringLiteral(")"); - } - } - - if(!subResult.isEmpty()) - { - result+= subResult; - } - } - return result.join(" - "); -} diff --git a/cli/displaytoolbox.h b/cli/displaytoolbox.h index 30b7ba3..11f92c3 100644 --- a/cli/displaytoolbox.h +++ b/cli/displaytoolbox.h @@ -21,8 +21,6 @@ public: static QString makeImage(QByteArray svgCode); #endif static QString colorToTermCode(QString str); - static QString diceToText(QJsonArray array, bool withColor, bool allSameFaceCount, bool allSameColor); - static QJsonArray diceToJson(QList<ExportedDiceResult>& diceList, bool& allSameFaceCount, bool& allSameColor); static QString diceToSvg(QJsonArray array, bool withColor, bool allSameColor, bool allSameFaceCount); static QString diceResultToString(QJsonObject val, Output type, bool hasColor); }; diff --git a/cli/main.cpp b/cli/main.cpp index 8362cdb..392d555 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -118,28 +118,35 @@ QString diceToMarkdown(QJsonArray array, bool withColor, bool allSameColor, bool } } -void displayJSon(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, - QString warning, QString comment, bool allSameFaceCount, bool allSameColor) +void displayJSon(QString json) { - Q_UNUSED(withColor); - QJsonDocument doc; - QJsonObject obj; - obj["values"]= array; - obj["comment"]= comment; - obj["error"]= error; - obj["scalar"]= scalarText; - obj["string"]= resultStr; - obj["allSameFace"]= allSameFaceCount; - obj["allSameColor"]= allSameColor; - obj["warning"]= warning; - obj["command"]= cmd; - doc.setObject(obj); - out << doc.toJson() << "\n"; + out << json << "\n"; } -void displayMarkdown(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, - QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor) +void displayMarkdown(QString json) { - Q_UNUSED(withColor); + QJsonDocument doc= QJsonDocument::fromJson(json.toUtf8()); + auto obj= doc.object(); + auto error= obj["error"].toString(); + auto warning= obj["warning"].toString(); + auto comment= obj["warning"].toString(); + auto arrayInst= obj["instructions"].toArray(); + QStringList diceResults; + for(auto inst : arrayInst) + { + auto obj= inst.toObject(); + auto diceVals= obj["diceval"].toArray(); + for(auto diceval : diceVals) + { + auto objval= diceval.toObject(); + auto resultStr= QString::number(objval["value"].toDouble()); + diceResults << resultStr; + } + } + auto diceList= diceResults.join(","); + auto resultStr= obj["string"].toString(); + auto scalarText= obj["scalar"].toString(); + auto cmd= obj["command"].toString(); + QString str("```Markdown\n"); if(!error.isEmpty()) { @@ -154,23 +161,44 @@ void displayMarkdown(QString scalarText, QString resultStr, QJsonArray array, bo { str.prepend(QStringLiteral("%1\n").arg(comment)); } - auto diceList= DisplayToolBox::diceToText(array, false, allSameFaceCount, allSameColor); - if(resultStr.isEmpty()) - { - str.append(QStringLiteral("# %1\nDetails:[%3 (%2)]\n").arg(scalarText).arg(diceList).arg(cmd)); - } - else if(!resultStr.isEmpty()) + if(!resultStr.isEmpty() && resultStr != scalarText) { // resultStr.replace("%2", diceList.trimmed()); str.append(QStringLiteral("%1\n").arg(resultStr)); } + else + { + str.append(QStringLiteral("# %1\nDetails:[%3 (%2)]\n").arg(scalarText).arg(diceList).arg(cmd)); + } } str.append(QStringLiteral("```")); out << str; } -QString displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, - QString warning, QString comment, bool allSameFaceCount, bool allSameColor) +QString displaySVG(QString json, QJsonArray array, bool withColor, bool allSameColor) { + QJsonDocument doc= QJsonDocument::fromJson(json.toUtf8()); + auto obj= doc.object(); + auto error= obj["error"].toString(); + auto warning= obj["warning"].toString(); + auto comment= obj["warning"].toString(); + auto arrayInst= obj["instructions"].toArray(); + QStringList diceResults; + for(auto inst : arrayInst) + { + auto obj= inst.toObject(); + auto diceVals= obj["diceval"].toArray(); + for(auto diceval : diceVals) + { + auto objval= diceval.toObject(); + auto resultStr= objval["string"].toString(); + diceResults << resultStr; + } + } + auto diceList= diceResults.join(","); + auto resultStr= obj["string"].toString(); + auto scalarText= obj["scalar"].toString(); + auto cmd= obj["command"].toString(); + QString str("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<svg version=\"1.1\" " "xmlns=\"http://www.w3.org/2000/svg\" " "xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n"); @@ -196,8 +224,13 @@ QString displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool .arg(y)); y+= 20; } - auto diceList= DisplayToolBox::diceToSvg(array, withColor, allSameColor, allSameFaceCount); - if(resultStr.isEmpty()) + // auto diceList= DisplayToolBox::diceToSvg(array, withColor, allSameColor, allSameFaceCount); + if(!resultStr.isEmpty() && resultStr != scalarText) + { + resultStr.replace("%2", diceList.trimmed()); + str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%2\">%1</text>").arg(resultStr).arg(y)); + } + else { if(withColor) str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%4\"><tspan " @@ -217,30 +250,44 @@ QString displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool .arg(y) .arg(y * 2)); } - else if(!resultStr.isEmpty()) - { - resultStr.replace("%2", diceList.trimmed()); - str.append(QStringLiteral("<text font-size=\"16\" x=\"0\" y=\"%2\">%1</text>").arg(resultStr).arg(y)); - } } str.append(QStringLiteral("</svg>\n")); return str; } #ifdef PAINTER_OP -void displayImage(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, - QString warning, QString comment, bool allSameFaceCount, bool allSameColor) +void displayImage(QString json, QJsonArray array, bool withColor, bool allSameColor) { - auto svg= displaySVG(scalarText, resultStr, array, withColor, cmd, error, warning, comment, allSameFaceCount, - allSameColor); + auto svg= displaySVG(json, array, withColor, allSameColor); out << DisplayToolBox::makeImage(svg.toUtf8()); } #endif -void displayCommandResult(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, - QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor) +void displayCommandResult(QString json, QJsonArray array, bool withColor, bool allSameColor) { - // TODO display warning + QJsonDocument doc= QJsonDocument::fromJson(json.toUtf8()); + auto obj= doc.object(); + auto error= obj["error"].toString(); + auto warning= obj["warning"].toString(); + auto comment= obj["comment"].toString(); + auto arrayInst= obj["instructions"].toArray(); + QStringList diceResults; + for(auto inst : arrayInst) + { + auto obj= inst.toObject(); + auto diceVals= obj["diceval"].toArray(); + for(auto diceval : diceVals) + { + auto objval= diceval.toObject(); + auto resultStr= objval["string"].toString(); + diceResults << resultStr; + } + } + auto diceList= diceResults.join(","); + auto scalarText= obj["scalar"].toString(); + auto cmd= obj["command"].toString(); + auto resultStr= obj["string"].toString(); + if(!error.isEmpty()) { err << "Error" << error << "\n"; @@ -252,14 +299,12 @@ void displayCommandResult(QString scalarText, QString resultStr, QJsonArray arra QString str; - auto diceList= DisplayToolBox::diceToText(array, withColor, allSameFaceCount, allSameColor); - if(withColor) str= QString("Result: \e[0;31m%1\e[0m - details:[%3 (%2)]").arg(scalarText).arg(diceList).arg(cmd); else str= QString("Result: %1 - details:[%3 (%2)]").arg(scalarText).arg(diceList).arg(cmd); - if(!resultStr.isEmpty()) + if(!resultStr.isEmpty() && resultStr != scalarText) { resultStr.replace("%2", diceList.trimmed()); str= resultStr; @@ -271,7 +316,7 @@ void displayCommandResult(QString scalarText, QString resultStr, QJsonArray arra } out << str << "\n"; } - +#include <QDebug> int startDiceParsing(QStringList& cmds, QString& treeFile, bool withColor, EXPORTFORMAT format, QJsonArray array) { DiceParser parser; @@ -301,118 +346,62 @@ int startDiceParsing(QStringList& cmds, QString& treeFile, bool withColor, EXPOR if(parser.parseLine(cmd)) { parser.start(); - QList<ExportedDiceResult> list; - QList<ExportedDiceResult> listFull; - bool homogeneous= true; - parser.getLastDiceResult(list, homogeneous); - parser.getDiceResultFromAllInstruction(listFull); - bool allSameFaceCount= true; - bool allSameColor= true; - auto array= DisplayToolBox::diceToJson(list, allSameFaceCount, allSameColor); - QString resultStr; - QString scalarText; - QString lastScalarText; - QString comment= parser.getComment(); - QString error= parser.humanReadableError(); - QString warnings= parser.humanReadableWarning(); - QStringList strLst; - QStringList listOfDiceResult; - QString cmdRework= parser.getDiceCommand(); - - if(parser.hasIntegerResultNotInFirst()) - { - auto values= parser.getLastIntegerResults(); - for(auto val : values) - { - strLst << QString::number(val); - } - scalarText= QString("%1").arg(strLst.join(',')); - lastScalarText= strLst.last(); - } - else if(!list.isEmpty()) - { - auto values= parser.getSumOfDiceResult(); - for(auto val : values) - { - strLst << QString::number(val); - } - scalarText= QString("%1").arg(strLst.join(',')); - } - for(auto map : list) + std::set<EXPORTFORMAT> svgFormat({SVG, IMAGE, BOT}); + QString json; + bool allSameColor= true; + if(svgFormat.find(format) != svgFormat.end()) { - for(auto key : map.keys()) - { - auto dice= map[key]; - QString stringVal; - for(auto val : dice) - { - qint64 total= 0; - QStringList dicelist; - for(auto score : val.getResult()) - { - total+= score; - dicelist << QString::number(score); - } - if(val.getResult().size() > 1) + allSameColor= true; + QString colorP; + json= parser.resultAsJSon( + [&colorP, &allSameColor](const QString& value, const QString& color, bool highlight) { + if(colorP.isNull()) + colorP= color; + else if(colorP != color) + allSameColor= false; + + QString result= value; + bool hasColor= !color.isEmpty(); + QString style; + if(hasColor) { - stringVal= QString("%1 [%2]").arg(total).arg(dicelist.join(',')); - listOfDiceResult << stringVal; + style+= QStringLiteral("fill=\"%1\" ").arg(color); } - else + if(highlight) { - listOfDiceResult << QString::number(total); + if(style.isEmpty()) + style+= QStringLiteral("fill=\"%1\" ") + .arg("red"); // default color must get the value from the setting object + style+= QStringLiteral("font-weight=\"bold\" "); } - } - } + if(!style.isEmpty()) + result= QString("<tspan %2>%1</tspan>").arg(value).arg(style); + return result; + }); } - - if(parser.hasStringResult()) + else if(TERMINAL == format) { - bool ok; - QStringList allStringlist= parser.allFirstResultAsString(ok); - - 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); - }); - - if(resultWithPlaceHolder.isEmpty()) - allStringlist.erase(std::remove_if(allStringlist.begin(), allStringlist.end(), - [](const QString& result) { - bool ok; - result.toInt(&ok); - return ok; - }), - allStringlist.end()); - auto stringResult - = resultWithPlaceHolder.isEmpty() ? allStringlist.join(" ; ") : resultWithPlaceHolder.join(" ; "); - - stringResult.replace("%1", scalarText); - stringResult.replace("%2", listOfDiceResult.join(",").trimmed()); - stringResult.replace("%3", lastScalarText); - stringResult.replace("\\n", "\n"); - - QMap<Dice::ERROR_CODE, QString> errorMap; - stringResult= ParsingToolBox::replaceVariableToValue(stringResult, allStringlist, errorMap); - stringResult= ParsingToolBox::replacePlaceHolderToValue(stringResult, listFull); - - error= errorMap.values().join(", "); - - int i= strLst.size(); - for(auto it= strLst.rbegin(); it != strLst.rend(); ++it) - { - stringResult.replace(QStringLiteral("µ%1").arg(i), (*it)); - --i; - } - - bool isInt= true; - stringResult.toInt(&isInt); - if(!isInt || !resultWithPlaceHolder.isEmpty()) - resultStr= stringResult; + allSameColor= true; + QString colorP; + json= parser.resultAsJSon( + [&colorP, &allSameColor](const QString& result, const QString& color, bool highlight) { + if(colorP.isNull()) + colorP= color; + else if(colorP != color) + allSameColor= false; + + auto front= DisplayToolBox::colorToTermCode(color); + auto end= front.isEmpty() ? "" : DisplayToolBox::colorToTermCode("reset"); + return result; + }); + } + else + { + allSameColor= true; + json= parser.resultAsJSon([](const QString& result, const QString&, bool) { return result; }); } + if(format == BOT) { if(allSameColor) @@ -427,58 +416,40 @@ int startDiceParsing(QStringList& cmds, QString& treeFile, bool withColor, EXPOR format= MARKDOWN; #endif } - if(!error.isEmpty()) + if(!parser.humanReadableError().isEmpty()) { format= MARKDOWN; } } + if(!withColor && format == TERMINAL) + format= TEXT; + + // qDebug().noquote() << json << format; switch(format) { case TERMINAL: - displayCommandResult(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment, - allSameFaceCount, allSameColor); + displayCommandResult(json, array, true, allSameColor); break; case SVG: - out << displaySVG(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment, - allSameFaceCount, allSameColor) - << "\n"; + out << displaySVG(json, array, withColor, allSameColor) << "\n"; break; case BOT: case MARKDOWN: - displayMarkdown(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment, - allSameFaceCount, allSameColor); + displayMarkdown(json); break; case TEXT: - displayCommandResult(scalarText, resultStr, array, false, cmdRework, error, warnings, comment, - allSameFaceCount, allSameColor); + displayCommandResult(json, array, false, allSameColor); break; case JSON: - displayJSon(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment, - allSameFaceCount, allSameColor); + displayJSon(json); break; #ifdef PAINTER_OP case IMAGE: - displayImage(scalarText, resultStr, array, withColor, cmdRework, error, warnings, comment, - allSameFaceCount, allSameColor); + displayImage(json, array, withColor, allSameColor); break; #endif } - if(!treeFile.isEmpty()) - { - parser.writeDownDotTree(treeFile); - } - - if(!error.isEmpty()) - { - rt= 1; - } - } - else - { - QString error= parser.humanReadableError(); - err << "Error: " << error << "\n"; - rt= 1; } } return rt; @@ -604,9 +575,6 @@ int main(int argc, char* argv[]) } QStringList cmdList= optionParser.positionalArguments(); - cmdList.append("repeat(2d6c[<4];1d6;2d6c[<4];\"échecs: $1 [@1], succès $3 [@3], le dé " - "de balance: $2\",5)"); - if(!cmdList.isEmpty()) { if(cmdList[0].startsWith('&')) @@ -614,9 +582,7 @@ int main(int argc, char* argv[]) colorb= false; } } - // cmdList << "8d10;\$1c[>6];\$1c[=1];\$2-\$3i:[>0]{\"%3 - // Success[%2]\"}{i:[<0]{\"Critical fail %3 [%2]\"}{\"Fail %3 - // [%2]\"}}"; + QJsonArray aliases; if(optionParser.isSet(alias)) { diff --git a/diceparser.cpp b/diceparser.cpp index e32d9e8..d177d78 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -266,6 +266,16 @@ int DiceParser::startNodeCount() const return static_cast<int>(m_parsingToolbox->getStartNodes().size()); } +QList<qreal> DiceParser::scalarResultsFromEachInstruction() const +{ + return m_parsingToolbox->scalarResultsFromEachInstruction(); +} + +QStringList DiceParser::stringResultFromEachInstruction(bool& hasAlias) const +{ + return m_parsingToolbox->allFirstResultAsString(hasAlias); +} + QString DiceParser::comment() const { return m_parsingToolbox->getComment(); @@ -323,6 +333,11 @@ QString DiceParser::humanReadableWarning() const return str; } +QString DiceParser::finalStringResult() const +{ + return m_parsingToolbox->finalStringResult(); +} + QString DiceParser::resultAsJSon(std::function<QString(const QString&, const QString&, bool)> colorize) const { QJsonObject obj; diff --git a/include/diceparser.h b/include/diceparser.h index 56dcb7d..8951a99 100644 --- a/include/diceparser.h +++ b/include/diceparser.h @@ -99,6 +99,7 @@ public: QList<qreal> scalarResultsFromEachInstruction() const; QStringList stringResultFromEachInstruction(bool& hasAlias) const; void diceResultFromEachInstruction(QList<ExportedDiceResult>& resultList) const; + QString finalStringResult() const; QString diceCommand() const; QMap<Dice::ERROR_CODE, QString> errorMap() const; diff --git a/include/parsingtoolbox.h b/include/parsingtoolbox.h index f9c7484..ae70c2c 100644 --- a/include/parsingtoolbox.h +++ b/include/parsingtoolbox.h @@ -86,9 +86,6 @@ public: { REPEAT }; - /** - * @brief The OptionOperator enum gathering all options availables for result. - */ enum OptionOperator { KeepAndExplode, @@ -110,33 +107,17 @@ public: Bind, AllSameExplode }; - /** - * @brief The DiceOperator enum gathering all dice operators - */ enum DiceOperator { D, L }; - /** - * @brief The DiceSymbol enum - */ enum NodeAction { JumpBackward }; - /** - * @brief ParsingToolBox - */ ParsingToolBox(); - /** - * @brief ParsingToolBox - * @param data - */ ParsingToolBox(const ParsingToolBox& data); - /** - * @brief ~ParsingToolBox - */ virtual ~ParsingToolBox(); void clearUp(); @@ -241,6 +222,7 @@ public: void setHelpPath(const QString& path); static QHash<QString, QString> getVariableHash(); static void setVariableHash(const QHash<QString, QString>& variableHash); + void setStartNodes(std::vector<ExecutionNode*> nodes); // Aliases QString convertAlias(QString str); diff --git a/node/repeaternode.cpp b/node/repeaternode.cpp index 93d946b..14ed956 100644 --- a/node/repeaternode.cpp +++ b/node/repeaternode.cpp @@ -25,24 +25,27 @@ #include "executionnode.h" #include "parsingtoolbox.h" #include "result/stringresult.h" +#include <QDebug> using InstructionSet= std::vector<ExecutionNode*>; -QStringList allFirstResultAsString(std::vector<InstructionSet> startingNodes, bool& hasAlias) +QStringList allFirstResultAsString(const InstructionSet& startingNodes, bool& hasAlias) { + ParsingToolBox parsingBox; // QStringList allResult; QStringList stringListResult; for(auto node : startingNodes) { - QVariant var; - if(ParsingToolBox::hasResultOfType(Dice::RESULT_TYPE::STRING, node, var)) + auto pair= parsingBox.hasResultOfType(Dice::RESULT_TYPE::STRING, node); + auto pairStr= parsingBox.hasResultOfType(Dice::RESULT_TYPE::SCALAR, node, true); + if(pair.first) { - stringListResult << var.toString(); + stringListResult << pair.second.toString(); hasAlias= true; } - else if(hasResultOfType(Dice::RESULT_TYPE::SCALAR, node, var, true)) + else if(pairStr.first) { - stringListResult << QString::number(var.toReal()); + stringListResult << QString::number(pairStr.second.toReal()); hasAlias= true; } } @@ -102,37 +105,22 @@ void RepeaterNode::run(ExecutionNode* previousNode) } else { - auto list= allFirstResultAsString(m_startingNodes, true); - // auto string= new StringResult(); - // QStringList list; - /*std::for_each(resultVec.begin(), resultVec.end(), [&list](Result* result) { - auto value= result->getResult(Dice::RESULT_TYPE::SCALAR).toDouble(); - auto diceList= result->getResult(Dice::RESULT_TYPE::DICE_LIST).value<QList<Die*>>(); - auto string= result->getResult(Dice::RESULT_TYPE::STRING).toString(); - - if(!string.isEmpty()) - list.append(string); - else - { - QStringList diceStr; - std::transform(diceList.begin(), diceList.end(), std::back_inserter(diceStr), [](Die* die) { - auto values= die->getListValue(); - - QStringList valuesStr; - std::transform(values.begin(), values.end(), std::back_inserter(valuesStr), - [](qint64 val) { return QString::number(val); }); - - if(valuesStr.size() == 1) - return QStringLiteral("%1").arg(die->getValue()); - else - return QStringLiteral("%1 [%2]").arg(die->getValue()).arg(valuesStr.join(",")); - }); - list.append(QStringLiteral("%1 - Details [%2]").arg(value).arg(diceStr.join(","))); - } - }); - string->addText(list.join('\n')); - string->finished(); - m_result= string;*/ + auto string= new StringResult(); + + QStringList listOfStrResult; + for(auto instructions : m_startingNodes) + { + ParsingToolBox parsingBox; + parsingBox.setStartNodes(instructions); + auto finalString= parsingBox.finalStringResult(); + listOfStrResult << finalString; + } + if(!listOfStrResult.isEmpty()) + string->addText(listOfStrResult.join('\n')); + + m_result= string; + + qDebug().noquote() << listOfStrResult.join('\n'); } if(nullptr != m_nextNode) diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index 8261d8a..6224d13 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -712,7 +712,7 @@ QString ParsingToolBox::finalStringResult() const if(sub.contains(ex)) resultWithPlaceHolder.append(sub); }); - auto stringResult= resultWithPlaceHolder.isEmpty() ? allStringlist.join(" ; ") : resultWithPlaceHolder.join(" ; "); + auto stringResult= resultWithPlaceHolder.isEmpty() ? allStringlist.join(",") : resultWithPlaceHolder.join(","); auto pairScalar= finalScalarResult(); @@ -1000,6 +1000,11 @@ void ParsingToolBox::setVariableHash(const QHash<QString, QString>& variableHash m_variableHash= variableHash; } +void ParsingToolBox::setStartNodes(std::vector<ExecutionNode*> nodes) +{ + m_startNodes= nodes; +} + void ParsingToolBox::readProbability(QStringList& str, QList<Range>& ranges) { quint64 totalDistance= 0; diff --git a/tests/dice/tst_dice.cpp b/tests/dice/tst_dice.cpp index 3c31f12..8cff99b 100644 --- a/tests/dice/tst_dice.cpp +++ b/tests/dice/tst_dice.cpp @@ -240,7 +240,7 @@ void TestDice::validatorListTest() QVERIFY2(m_diceParser->humanReadableError().isEmpty(), "no error"); QVERIFY2(m_diceParser->humanReadableWarning().isEmpty(), "no warning"); - auto resultCmd= m_diceParser->getLastIntegerResults(); + auto resultCmd= m_diceParser->scalarResultsFromEachInstruction(); QCOMPARE(resultCmd.size(), 1); @@ -393,7 +393,7 @@ void TestDice::rangedCommandsTest() QVERIFY2(a, "parsing"); m_diceParser->start(); - auto results= m_diceParser->getLastIntegerResults(); + auto results= m_diceParser->scalarResultsFromEachInstruction(); QVERIFY(results.size() == 1); @@ -479,7 +479,7 @@ void TestDice::scopeDF() bool test= m_diceParser->parseLine(cmd); QVERIFY2(test == valid, cmd.toStdString().c_str()); m_diceParser->start(); - auto results= m_diceParser->getLastIntegerResults(); + auto results= m_diceParser->scalarResultsFromEachInstruction(); for(auto result : results) QVERIFY(result >= min && result <= max); @@ -549,7 +549,7 @@ void TestDice::severalInstruction() { auto test= m_diceParser->parseLine(cmd); QVERIFY2(test, cmd.toStdString().c_str()); - QVERIFY2(m_diceParser->getStartNodeCount() == results[i], "Wrong number of instruction"); + QVERIFY2(m_diceParser->startNodeCount() == results[i], "Wrong number of instruction"); } } void TestDice::mathPriority() @@ -560,7 +560,7 @@ void TestDice::mathPriority() bool test= m_diceParser->parseLine(cmd); QVERIFY(test); m_diceParser->start(); - auto resultList= m_diceParser->getLastIntegerResults(); + auto resultList= m_diceParser->scalarResultsFromEachInstruction(); QCOMPARE(resultList.size(), 1); auto value= resultList.first(); @@ -1006,7 +1006,7 @@ void TestDice::filterTest() QVERIFY2(m_diceParser->humanReadableError().isEmpty(), "no error"); QVERIFY2(m_diceParser->humanReadableWarning().isEmpty(), "no warning"); - auto resultCmd= m_diceParser->getLastIntegerResults(); + auto resultCmd= m_diceParser->scalarResultsFromEachInstruction(); QCOMPARE(resultCmd.size(), 1); @@ -1135,11 +1135,11 @@ void TestDice::ifCommandTest() bool test= m_diceParser->parseLine(cmd); QVERIFY2(test, cmd.toStdString().c_str()); m_diceParser->start(); - auto results= m_diceParser->getLastIntegerResults(); - auto strResult= m_diceParser->getStringResult(); + auto results= m_diceParser->scalarResultsFromEachInstruction(); + auto strResult= m_diceParser->finalStringResult(); + qDebug() << strResult; QCOMPARE(results.size(), 1); - QCOMPARE(strResult.size(), 1); auto result= results.first(); auto it= std::find_if(level.begin(), level.end(), [compare, result](int level) { @@ -1160,7 +1160,7 @@ void TestDice::ifCommandTest() auto index= std::distance(level.begin(), it); auto strResultExpected= startExperted[index]; - auto resultText= strResult.first(); + auto resultText= strResult; QVERIFY2(resultText.startsWith(strResultExpected), "string result does not fit the expectation"); } |