aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorRenaud G <renaud@rolisteam.org>2020-01-29 01:40:46 +0100
committerRenaud G <renaud@rolisteam.org>2020-01-29 01:40:46 +0100
commitec4e06259947921b6958e3cdf27786904a79af2f (patch)
treeea3fd9964199a43217419e1317ebb4519264d8d1
parent825bcda767d2bab9f470d81cd76b9e5985fbea9c (diff)
downloadOneRoll-ec4e06259947921b6958e3cdf27786904a79af2f.tar.gz
OneRoll-ec4e06259947921b6958e3cdf27786904a79af2f.zip
Output tags for dice list.
-rw-r--r--HelpMe.md99
-rw-r--r--cli/main.cpp5
-rw-r--r--highlightdice.cpp8
-rw-r--r--include/diceparser.h6
-rw-r--r--include/highlightdice.h6
-rw-r--r--include/parsingtoolbox.h5
-rw-r--r--parsingtoolbox.cpp88
7 files changed, 175 insertions, 42 deletions
diff --git a/HelpMe.md b/HelpMe.md
index 7350271..406291b 100644
--- a/HelpMe.md
+++ b/HelpMe.md
@@ -407,25 +407,73 @@ Same as above, but the final result is displayed beside Success or Fail.
Same as above, but the result of each die is displayed inside square brackets.
+### Group
+
+Group dices, then count the number of group (7th sea system).
+
+#### Example
+
+> 3d20g10
+
+This will roll 3 dices and then try to group them to make groups of 10. If you get `9 9 2`, you can only create one group whose value is more or equal to ten (`{9,2}`, the second `9` being "wasted").
+
+The `g` operator is allowed to re-order dices to create groups. When rolling `4d20g10`, if you get `7 4 3 6`, the result will be `2` (`{7,3}` and `{6,4}`).
+
+
+### Comment (\#)
+
+> 2D6 # Sword attack
+
+Display "Sword attack" and the result of the two dice.
+DiceParser ignore everything after the \#. The whole part is treated as one comment.
+So DiceParser can answer question:
+
+> 1L[yes,no] # Am I evil ?
+
+```
+Am I evil ?
+yes
+```
+
+## The output
+
+DiceParser provides features to let you control the command output.
+The final instruction must be a string instruction.
+String instruction starts with `"` and ends with `"`.
+
+Rolling:
+> `"result"`
+
+Output:
+`result`
+You can set string instruction inside if operator:
-#### String Result
+> 1d6i:[>3]{"Success"}{"Fail"}
-To improve readability, it is possible to set the text that should be displayed after the roll.
+Output:
+`Success` or `Fail`
-Several data can be displayed:
-* %1: last scalar result from each instruction.
-* %2: all dice results
-* %3: last scalar result from the last instruction.
+It offers a quick answer but sometimes you need to see the rolled values.
+DiceParser can replace some special tags in order to see values, computation result and whatever.
+
+### Shortcuts
+
+There are 3 shortcut tags.
+
+* `%1`: last scalar result from each instruction.
+* `%2`: all dice results.
+* `%3`: last scalar result from the last instruction.
The default output is `%1 details[%2]`.
So, it shows the last scalar result of each instruction and dice result.
-%1 and %3 are equivalent when there is only one instruction (no \;).
+`%1` and `%3` are equivalent when there is only one instruction (no \;).
+They are really useful but if you have many instructions that can become a bit messy.
-#### Specific value from instrustion
+### Final result
It is also possible to set reference to the scalar result of specific instruction.
- To reference the first instruction: `$1`
@@ -434,8 +482,7 @@ It is also possible to set reference to the scalar result of specific instructio
etc…
The number of instruction is not limited.
-
-#### Examples:
+#### Let see some examples:
> 8d10;$1c[>6];$1c1;$2-$3
@@ -451,40 +498,20 @@ or
In this example, the critical fail happens when there are more fails than success.
-
-
In the next example, the critical fail happens when there was no success and a least one fail.
> 8d10;$1c[>6];$1c1;$2-$3;$4i:[=0]{"Fail $4 [%2]"}{$4i:[>0]{"$2 Success[%2]"}{$2i:[=0]{"Critical Fail $4 [%2]"}{"Fail $4 [%2]"}}}
+### Dice Result
-### Group
-
-Group dices, then count the number of group (7th sea system).
-
-#### Example
-
-> 3d20g10
+DiceParser provides tags to display dice result (and each rolled values from a specific instruction).
-This will roll 3 dices and then try to group them to make groups of 10. If you get `9 9 2`, you can only create one group whose value is more or equal to ten (`{9,2}`, the second `9` being "wasted").
+To show dice values from a specific instrustion, just add `@` followed by the instruction's number (e.g: `@1`)
-The `g` operator is allowed to re-order dices to create groups. When rolling `4d20g10`, if you get `7 4 3 6`, the result will be `2` (`{7,3}` and `{6,4}`).
+> `2d6;3d8;"Result $2 - d8:[@2] - d6:[@1]"`
-
-### Comment (\#)
-
-> 2D6 # Sword attack
-
-Display "Sword attack" and the result of the two dice.
-DiceParser ignore everything after the \#. The whole part is treated as one comment.
-So DiceParser can answer question:
-
-> 1L[yes,no] # Am I evil ?
-
-```
-Am I evil ?
-yes
-```
+The output:
+`Result 15 - d8:[7,4,4] - d6:[3,6]`
## Arithmetic
diff --git a/cli/main.cpp b/cli/main.cpp
index ef3a642..e09a1de 100644
--- a/cli/main.cpp
+++ b/cli/main.cpp
@@ -365,7 +365,10 @@ int startDiceParsing(QStringList& cmds, QString& treeFile, bool withColor, EXPOR
stringResult.replace("%3", lastScalarText);
stringResult.replace("\\n", "\n");
+ // qDebug() << "before replace variable: " << lastScalarText << scalarText << listOfDiceResult
+ // << listFull.size();
stringResult= ParsingToolBox::replaceVariableToValue(stringResult, strLst);
+ stringResult= ParsingToolBox::replacePlaceHolderToValue(stringResult, listFull);
int i= strLst.size();
for(auto it= strLst.rbegin(); it != strLst.rend(); ++it)
@@ -561,7 +564,7 @@ int main(int argc, char* argv[])
{
cmd= "help";
}
- QStringList cmdList = optionParser.positionalArguments();
+ QStringList cmdList= optionParser.positionalArguments();
// cmdList << "8d10;\$1c[>6];\$1c[=1];\$2-\$3i:[>0]{\"%3 Success[%2]\"}{i:[<0]{\"Critical fail %3 [%2]\"}{\"Fail %3
// [%2]\"}}";
QJsonArray aliases;
diff --git a/highlightdice.cpp b/highlightdice.cpp
index c2abdb6..58d9eec 100644
--- a/highlightdice.cpp
+++ b/highlightdice.cpp
@@ -75,3 +75,11 @@ void HighLightDice::setFaces(const quint64& faces)
{
m_faces= faces;
}
+
+QString HighLightDice::getResultString() const
+{
+ QStringList list;
+ std::transform(std::begin(m_result), std::end(m_result), std::back_inserter(list),
+ [](qint64 value) { return QString::number(value); });
+ return list.join(",");
+}
diff --git a/include/diceparser.h b/include/diceparser.h
index 1926e5f..31f9760 100644
--- a/include/diceparser.h
+++ b/include/diceparser.h
@@ -26,13 +26,10 @@
#include <QString>
#include <vector>
-#include "highlightdice.h"
#include "diceparserhelper.h"
+#include "highlightdice.h"
//#include "node/executionnode.h"
-typedef QList<HighLightDice> ListDiceResult;
-typedef QMap<quint64, ListDiceResult> ExportedDiceResult;
-
class ExplodeDiceNode;
class ParsingToolBox;
class DiceRollerNode;
@@ -232,6 +229,7 @@ public:
bool readValuesList(QString& str, ExecutionNode*& node);
void cleanAll();
+
private:
/**
* @brief readIfInstruction reads the current command to build if node with proper parameters.
diff --git a/include/highlightdice.h b/include/highlightdice.h
index 2e8f08a..c70a371 100644
--- a/include/highlightdice.h
+++ b/include/highlightdice.h
@@ -21,6 +21,7 @@
#define HighLightDice_H
#include <QList>
+#include <QMap>
#include <QString>
class HighLightDice
@@ -44,6 +45,8 @@ public:
quint64 getFaces() const;
void setFaces(const quint64& faces);
+ QString getResultString() const;
+
private:
QList<qint64> m_result;
bool m_hasHighlight= true;
@@ -52,4 +55,7 @@ private:
quint64 m_faces;
};
+typedef QList<HighLightDice> ListDiceResult;
+typedef QMap<quint64, ListDiceResult> ExportedDiceResult;
+
#endif // HighLightDice_H
diff --git a/include/parsingtoolbox.h b/include/parsingtoolbox.h
index 8d5f62b..038427f 100644
--- a/include/parsingtoolbox.h
+++ b/include/parsingtoolbox.h
@@ -26,6 +26,7 @@
#include "booleancondition.h"
#include "compositevalidator.h"
+#include "highlightdice.h"
#include "node/dicerollernode.h"
#include "node/executionnode.h"
#include "node/ifnode.h"
@@ -111,7 +112,7 @@ public:
* @param str
* @return
*/
- Validator* readValidator(QString& str, bool hasSquare=false);
+ Validator* readValidator(QString& str, bool hasSquare= false);
/**
* @brief readCompositeValidator
* @param str
@@ -227,8 +228,10 @@ public:
static int findClosingCharacterIndexOf(QChar open, QChar closing, const QString& str, int offset);
static QString replaceVariableToValue(const QString& source, QStringList values);
+ 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);
diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp
index a28ad30..c4cbf6f 100644
--- a/parsingtoolbox.cpp
+++ b/parsingtoolbox.cpp
@@ -838,6 +838,68 @@ QString ParsingToolBox::replaceVariableToValue(const QString& source, QStringLis
return result;
}
+QString ParsingToolBox::replacePlaceHolderToValue(const QString& source, const QList<ExportedDiceResult>& list)
+{
+ QStringList resultList;
+ std::transform(
+ std::begin(list), std::end(list), std::back_inserter(resultList), [](const ExportedDiceResult& dice) {
+ QStringList valuesStr;
+ if(dice.size() == 1)
+ {
+ auto values= dice.values();
+ std::transform(std::begin(values), std::end(values), std::back_inserter(valuesStr),
+ [](const ListDiceResult& dice) {
+ QStringList textList;
+ std::transform(std::begin(dice), std::end(dice), std::back_inserter(textList),
+ [](const HighLightDice& dice) { return dice.getResultString(); });
+ return textList.join(",");
+ });
+ }
+ else if(dice.size() > 1)
+ {
+ 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(",")));
+ }
+ }
+ return valuesStr.join(",");
+ });
+
+ QString result= source;
+ int start= source.size() - 1;
+ bool valid= true;
+ do
+ {
+ auto ref= readPlaceHolderFromString(source, start);
+ if(ref.isValid())
+ {
+ 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
+ {
+ valid= false;
+ }
+ } while(valid);
+
+ return result;
+
+ // return source;
+}
+
void ParsingToolBox::readSubtitutionParameters(SubtituteInfo& info, QString& rest)
{
auto sizeS= rest.size();
@@ -883,6 +945,32 @@ SubtituteInfo ParsingToolBox::readVariableFromString(const QString& source, int&
return info;
}
+SubtituteInfo ParsingToolBox::readPlaceHolderFromString(const QString& source, int& start)
+{
+ bool found= false;
+ SubtituteInfo info;
+ int i= start;
+ for(; i >= 0 && !found; --i)
+ {
+ if(source.at(i) == '@')
+ {
+ auto rest= source.mid(i + 1, 1 + start - i);
+ qint64 number;
+ if(readNumber(rest, number))
+ {
+ auto len= QString::number(number).size() - 1;
+ readSubtitutionParameters(info, rest);
+ info.setLength(info.length() + len);
+ info.setResultIndex(static_cast<int>(number));
+ info.setPosition(i);
+ found= true;
+ }
+ }
+ }
+ start= i;
+ return info;
+}
+
SubtituteInfo::SubtituteInfo() {}
bool SubtituteInfo::isValid() const