Ejemplo n.º 1
0
        /// <summary>
        /// Replaces the values and verifies if a condition is true or false.
        /// </summary>
        /// <param name="kcCond">The keycheck condition struct</param>
        /// <param name="data">The BotData used for variable replacement</param>
        /// <returns>Whether the comparison is verified or not.</returns>
        public static bool ReplaceAndVerify(KeycheckCondition kcCond, BotData data)
        {
            var style    = NumberStyles.Number | NumberStyles.AllowCurrencySymbol; // Needed when comparing values with a currency symbol
            var provider = new CultureInfo("en-US");
            var L        = BlockBase.ReplaceValuesRecursive(kcCond.Left, data);    // The left-hand term can accept recursive values like <LIST[*]>
            var r        = BlockBase.ReplaceValues(kcCond.Right, data);            // The right-hand term cannot

            switch (kcCond.Comparer)
            {
            case Comparer.EqualTo:
                return(L.Any(l => l == r));

            case Comparer.NotEqualTo:
                return(L.Any(l => l != r));

            case Comparer.GreaterThan:
                return(L.Any(l => decimal.Parse(l.Replace(',', '.'), style, provider) > decimal.Parse(r.Replace(',', '.'), style, provider)));

            case Comparer.GreaterOrEqual:
                return(L.Any(l => decimal.Parse(l.Replace(',', '.'), style, provider) >= decimal.Parse(r.Replace(',', '.'), style, provider)));

            case Comparer.LessThan:
                return(L.Any(l => decimal.Parse(l.Replace(',', '.'), style, provider) < decimal.Parse(r.Replace(',', '.'), style, provider)));

            case Comparer.LessThanOrEqual:
                return(L.Any(l => decimal.Parse(l.Replace(',', '.'), style, provider) <= decimal.Parse(r.Replace(',', '.'), style, provider)));

            case Comparer.Contains:
                return(L.Any(l => l.Contains(r)));

            case Comparer.DoesNotContain:
                return(L.Any(l => !l.Contains(r)));

            case Comparer.Exists:
                return(L.Any(l => l != kcCond.Left));    // Returns true if any replacement took place

            case Comparer.DoesNotExist:
                return(L.All(l => l == kcCond.Left));    // Returns true if no replacement took place

            case Comparer.MatchesRegex:
                return(L.Any(l => Regex.Match(l, r).Success));

            case Comparer.DoesNotMatchRegex:
                return(L.Any(l => !Regex.Match(l, r).Success));

            default:
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Verifies if a condition is true or false (without replacing the values).
        /// </summary>
        /// <param name="kcCond">The keycheck condition struct</param>
        /// <returns>Whether the comparison is verified or not.</returns>
        public static bool Verify(KeycheckCondition kcCond)
        {
            var style    = NumberStyles.Number | NumberStyles.AllowCurrencySymbol; // Needed when comparing values with a currency symbol
            var provider = new CultureInfo("en-US");

            switch (kcCond.Comparer)
            {
            case Comparer.EqualTo:
                return(kcCond.Left == kcCond.Right);

            case Comparer.NotEqualTo:
                return(kcCond.Left != kcCond.Right);

            case Comparer.GreaterThan:
                return(decimal.Parse(kcCond.Left.Replace(',', '.'), style, provider) > decimal.Parse(kcCond.Right.Replace(',', '.'), style, provider));

            case Comparer.LessThan:
                return(decimal.Parse(kcCond.Left.Replace(',', '.'), style, provider) < decimal.Parse(kcCond.Right.Replace(',', '.'), style, provider));

            case Comparer.Contains:
                return(kcCond.Left.Contains(kcCond.Right));

            case Comparer.DoesNotContain:
                return(!kcCond.Left.Contains(kcCond.Right));

            case Comparer.Exists:
            case Comparer.DoesNotExist:
                throw new NotSupportedException("Exists and DoesNotExist operators are only supported in the ReplaceAndVerify method.");

            case Comparer.MatchesRegex:
                return(Regex.Match(kcCond.Left, kcCond.Right).Success);

            case Comparer.DoesNotMatchRegex:
                return(!Regex.Match(kcCond.Left, kcCond.Right).Success);

            default:
                return(false);
            }
        }