From 76f1d072543957cd3231836a140fda5a58898ffe Mon Sep 17 00:00:00 2001 From: obiwankennedy Date: Thu, 11 Jan 2018 16:12:08 +0100 Subject: add json function --- cli/main.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'cli/main.cpp') diff --git a/cli/main.cpp b/cli/main.cpp index 0185aa6..d45a9b0 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -21,10 +21,13 @@ ***************************************************************************/ #include -#include "diceparser.h" #include #include #include +#include +#include + +#include "diceparser.h" #include "highlightdice.h" /** @@ -44,8 +47,66 @@ QTextStream out(stdout, QIODevice::WriteOnly); bool markdown = false; + + + +QJsonArray diceToJson(QList& diceList,bool& highlight,bool& homogeneous) +{ + QJsonArray array; + for(auto dice : diceList) + { + for(int face: dice.keys()) + { + QStringList result; + QJsonObject diceList; + ListDiceResult diceResults = dice.value(face); + std::vector> sameColorDice; + std::vector alreadyDoneColor; + for(auto & dice : diceResults) + { + auto it=std::find_if(alreadyDoneColor.begin(), alreadyDoneColor.end(),[dice](QString color){ + return color == dice.getColor(); + }); + + if( it == alreadyDoneColor.end()) + { + sameColorDice.push_back(std::vector()); + it = alreadyDoneColor.end(); + --it; + alreadyDoneColor.push_back(dice.getColor()); + } + + int i = std::distance(alreadyDoneColor.begin(), it); + sameColorDice[i].push_back(dice); + } + int i = 0; + for(auto it = alreadyDoneColor.begin() ; it != alreadyDoneColor.end(); ++it) + { + auto list = sameColorDice[i]; + QJsonObject object; + object["color"]=*it; + object["face"]=face; + QJsonArray values; + for(auto const dice : list) + { + for(auto result : dice.getResult()) + { + values.push_back((qint64)result); + } + } + object["values"]=values; + ++i; + array.push_back(object); + } + } + } + return array; +} + QString diceToMarkdown(QList& diceList,bool highlight,bool homogeneous) { + auto array = diceToJson(diceList,highlight,homogeneous); + qDebug() << array; QStringList global; for(auto dice : diceList) { @@ -188,6 +249,14 @@ QString diceToText(QList& diceList,bool highlight,bool homog { prefix = "\e[30m%1\e[0m"; } + if(tmp.getColor()=="green") + { + prefix = "\e[32m%1\e[0m"; + } + if(tmp.getColor()=="yellow") + { + prefix = "\e[33m%1\e[0m"; + } } if(i==0) -- cgit v1.2.3-70-g09d2