aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/validator.cpp
diff options
context:
space:
mode:
authorRenaud Guezennec <renaud@rolisteam.org>2020-03-28 01:08:11 +0000
committerRenaud Guezennec <renaud@rolisteam.org>2020-03-28 01:08:11 +0000
commit944072519b4e7bc31e93024ece61b1079f7427a5 (patch)
tree522475f7c4d5dade1c0b3482ab35d3f625b99ebe /validator.cpp
parent932d863c2a2c9b08d8ce1f4e1041e5795daedc6d (diff)
parent088b2fa45dc2c763187109ec7d77f9eb096e498e (diff)
downloadOneRoll-944072519b4e7bc31e93024ece61b1079f7427a5.tar.gz
OneRoll-944072519b4e7bc31e93024ece61b1079f7427a5.zip
Merge branch 'repeat2' into 'master'
All new features See merge request kde/rolisteam-diceparser!4
Diffstat (limited to 'validator.cpp')
-rw-r--r--validator.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/validator.cpp b/validator.cpp
index 9f717f8..3d0b7af 100644
--- a/validator.cpp
+++ b/validator.cpp
@@ -24,7 +24,91 @@
Validator::Validator() {}
Validator::~Validator() {}
+template <typename Functor>
+qint64 Validator::onEach(const std::vector<Die*>& 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 <typename Functor>
+qint64 Validator::oneOfThem(const std::vector<Die*>& b, bool recursive, bool unlight, Functor functor) const
+{
+ auto oneOfThem= std::any_of(b.begin(), b.end(),
+ [this, recursive, unlight](Die* die) { return hasValid(die, recursive, unlight); });
+ if(oneOfThem)
+ functor(recursive, unlight);
+ return oneOfThem ? 1 : 0;
+}
+
+template <typename Functor>
+qint64 Validator::allOfThem(const std::vector<Die*>& b, bool recursive, bool unlight, Functor functor) const
+{
+ auto all= std::all_of(b.begin(), b.end(),
+ [this, recursive, unlight](Die* die) { return hasValid(die, recursive, unlight); });
+ if(all)
+ functor(recursive, unlight);
+ return all ? 1 : 0;
+}
+
+template <typename Functor>
+qint64 Validator::onScalar(const std::vector<Die*>& b, bool recursive, bool unlight, Functor functor) const
+{
+ qint64 result= 0;
+ for(const auto& die : b)
+ {
+ result+= die->getValue();
+ }
+ Die die;
+ die.setValue(result);
+ if(hasValid(&die, recursive, unlight))
+ {
+ functor(recursive, unlight);
+ return 1;
+ }
+ return 0;
+}
+
const std::set<qint64>& Validator::getPossibleValues(const std::pair<qint64, qint64>&)
{
return m_values;
}
+
+template <typename Functor>
+qint64 Validator::validResult(const std::vector<Die*>& b, bool recursive, bool unlight, Functor functor) const
+{
+ qint64 result;
+ switch(m_conditionType)
+ {
+ case Dice::OnEach:
+ result= onEach(b, recursive, unlight, functor);
+ break;
+ case Dice::OneOfThem:
+ result= oneOfThem(b, recursive, unlight, functor);
+ break;
+ case Dice::AllOfThem:
+ result= allOfThem(b, recursive, unlight, functor);
+ break;
+ case Dice::OnScalar:
+ result= onScalar(b, recursive, unlight, functor);
+ break;
+ }
+ return result;
+}
+
+Dice::ConditionType Validator::getConditionType() const
+{
+ return m_conditionType;
+}
+
+void Validator::setConditionType(const Dice::ConditionType& conditionType)
+{
+ m_conditionType= conditionType;
+}