Пример #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);
            }
        }