From 5f68d8399b771da0f3500a5d037315607e25c78d Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 26 Apr 2020 16:31:31 +0200 Subject: Add support for OnEachValue compare method. --- HelpMe.md | 64 ++++++++++++++++++++++++++++++++++++++++++---- booleancondition.cpp | 6 ++++- include/diceparserhelper.h | 27 +++++++++---------- parsingtoolbox.cpp | 5 ++++ validator.cpp | 17 ++++++++++++ validator.h | 2 ++ validatorlist.cpp | 16 ++++++++++++ 7 files changed, 118 insertions(+), 19 deletions(-) diff --git a/HelpMe.md b/HelpMe.md index c3588b4..3f6c76f 100644 --- a/HelpMe.md +++ b/HelpMe.md @@ -397,11 +397,12 @@ There is also 2 optional parameters #### Compare method -There are 3 different methods. -* **On Each (default)**: the condition is tested on each die from the previous part of the command. \[Default method\] -* **All Of Them (`*`)**: All dice must fit the condition to trigger the true instruction. If all dice do not fit the condition the false instruction is run. -* **One Of Them (`.`)**: at least one die must fit the condition to trigger the true instruction. If no dices fit the condition the false instruction is run. -* **On Scalar (`:`)**: the condition is evaluated on the scalar result of the dice roll. +There are 4 different methods. +* **On Each**: the condition is tested on each die from the previous part of the command. \[Default method\] +* **On Each value** `?`: the condition is tested on each final value of die from the previous part of the command. +* **All Of Them** `*`: All dice must fit the condition to trigger the true instruction. If all dice do not fit the condition the false instruction is run. +* **One Of Them** `.`: at least one die must fit the condition to trigger the true instruction. If no dices fit the condition the false instruction is run. +* **On Scalar** `:`: the condition is evaluated on the scalar result of the dice roll. #### Examples: @@ -675,6 +676,59 @@ The Rolisteam Dice Parser allows you to use several logic operator: * Greater: `>` * Different: `!=` + +#### Compare methods + +As the `if` operator, you can specify the compare method. + +* **On Each**: the condition is tested on each die from the previous part of the command. \[Default method\] +* **On Each value** `?`: the condition is tested on each final value of die from the previous part of the command. +* **All Of Them** `*`: All dice must fit the condition to trigger the true instruction. If all dice do not fit the condition the false instruction is run. +* **One Of Them** `.`: at least one die must fit the condition to trigger the true instruction. If no dices fit the condition the false instruction is run. +* **On Scalar** `:`: the condition is evaluated on the scalar result of the dice roll. + +##### Examples: + +> 1L[7,8,9]c[>6] + +This command will return 0 because, no die has been rolled, so the result of `1L[7,8,9]` is a final value. + +> 1L[7,8,9]c[?>6] + +Output: 1 + +> 5d6e6sc[>=8] + +Output: +``` +0 +details: [8 [6,2] 2 1 1 1] +``` + +> 5d6e6f[?>=16] + +Output: +As the final sum is equal to 11. It's less than 16 so the filter is filtering everything. +``` +0 +details: [2 4 1 3 1] +``` + + +The final sum is higher than 16 so the whole result is accepted by the filter operator. +``` +23 +details: [3 6 3 5 6] +``` + +> 5d6e6sc[:>=8] + +Output: +``` +1 +details: [8 [6,2] 2 1 1 1] +``` + ### Operation Condition This validator offers modulo as operation and a Boolean Condition to validate the value: diff --git a/booleancondition.cpp b/booleancondition.cpp index af566b7..1c992c2 100644 --- a/booleancondition.cpp +++ b/booleancondition.cpp @@ -94,7 +94,11 @@ BooleanCondition::~BooleanCondition() qint64 BooleanCondition::hasValid(Die* b, bool recursive, bool unhighlight) const { QList listValues; - if(recursive) + if(m_conditionType == Dice::OnEachValue) + { + listValues.append(b->getValue()); + } + else if(recursive) { listValues= b->getListValue(); } diff --git a/include/diceparserhelper.h b/include/diceparserhelper.h index 461d127..c55b415 100644 --- a/include/diceparserhelper.h +++ b/include/diceparserhelper.h @@ -14,19 +14,19 @@ enum class CONDITION_STATE : int enum class ERROR_CODE : int { - NO_DICE_ERROR, - DIE_RESULT_EXPECTED, - BAD_SYNTAXE, - ENDLESS_LOOP_ERROR, - DIVIDE_BY_ZERO, - NOTHING_UNDERSTOOD, - NO_DICE_TO_ROLL, - TOO_MANY_DICE, - NO_VARIBALE, - INVALID_INDEX, - UNEXPECTED_CHARACTER, - NO_PREVIOUS_ERROR, - NO_VALID_RESULT + NO_DICE_ERROR, + DIE_RESULT_EXPECTED, + BAD_SYNTAXE, + ENDLESS_LOOP_ERROR, + DIVIDE_BY_ZERO, + NOTHING_UNDERSTOOD, + NO_DICE_TO_ROLL, + TOO_MANY_DICE, + NO_VARIBALE, + INVALID_INDEX, + UNEXPECTED_CHARACTER, + NO_PREVIOUS_ERROR, + NO_VALID_RESULT }; /** @@ -45,6 +45,7 @@ enum class RESULT_TYPE : int enum ConditionType { OnEach, + OnEachValue, OneOfThem, AllOfThem, OnScalar diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index e28c65d..e53cf96 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -330,6 +330,11 @@ Dice::ConditionType ParsingToolBox::readConditionType(QString& str) str= str.remove(0, 1); type= Dice::OneOfThem; } + else if(str.startsWith('?')) + { + str= str.remove(0, 1); + type= Dice::OnEachValue; + } else if(str.startsWith('*')) { str= str.remove(0, 1); diff --git a/validator.cpp b/validator.cpp index da6e3a1..201574b 100644 --- a/validator.cpp +++ b/validator.cpp @@ -38,6 +38,20 @@ qint64 Validator::onEach(const std::vector& b, bool recursive, bool unligh return result; } +template +qint64 Validator::onEachValue(const std::vector& b, bool recursive, bool unlight, Functor functor) const +{ + qint64 result= 0; + std::for_each(b.begin(), b.end(), [this, recursive, unlight, functor, &result](Die* die) { + if(hasValid(die, recursive, unlight)) + { + ++result; + functor(die, recursive, unlight); + } + }); + return result; +} + template qint64 Validator::oneOfThem(const std::vector& b, bool recursive, bool unlight, Functor functor) const { @@ -90,6 +104,9 @@ qint64 Validator::validResult(const std::vector& b, bool recursive, bool u case Dice::OnEach: result= onEach(b, recursive, unlight, functor); break; + case Dice::OnEachValue: + result= onEachValue(b, recursive, unlight, functor); + break; case Dice::OneOfThem: result= oneOfThem(b, recursive, unlight, functor); break; diff --git a/validator.h b/validator.h index fafe443..7180bac 100644 --- a/validator.h +++ b/validator.h @@ -90,6 +90,8 @@ protected: template qint64 onEach(const std::vector& b, bool recursive, bool unlight, Functor functor) const; template + qint64 onEachValue(const std::vector& b, bool recursive, bool unlight, Functor functor) const; + template qint64 oneOfThem(const std::vector& b, bool recursive, bool unlight, Functor functor) const; template qint64 allOfThem(const std::vector& b, bool recursive, bool unlight, Functor functor) const; diff --git a/validatorlist.cpp b/validatorlist.cpp index 50a989d..b665328 100644 --- a/validatorlist.cpp +++ b/validatorlist.cpp @@ -24,6 +24,7 @@ #include "result/diceresult.h" #include "result/result.h" #include "validator.h" +#include #include void mergeResultsAsAND(const ValidatorResult& diceList, ValidatorResult& result) @@ -318,6 +319,21 @@ void ValidatorList::validResult(Result* result, bool recursive, bool unlight, } } break; + case Dice::OnEachValue: + { + DiceResult* diceResult= getDiceResult(result); + if(nullptr == diceResult) + break; + for(auto die : diceResult->getResultList()) + { + auto score= validator->hasValid(die, recursive, unlight); + if(score) + { + validResult.appendValidDice(die, score); + } + } + } + break; case Dice::AllOfThem: { DiceResult* diceResult= getDiceResult(result); -- cgit v1.2.3-70-g09d2