Пример #1
0
        private async void WaitForMessage(NetworkStream stream)
        {
            while (true)
            {
                var message = await NetworkHelper.ReceiveMessage(stream).ConfigureAwait(false);

                message.LastPlayBy = LastPlayerPlayed;
                if (message.MessageType == NetworkHelper.MessageType.Pass)
                {
                    if ((message.Player !.PlayerNumber + 1) % 4 == LastPlayerPlayed)
                    {
                        RuleHandler.FreeTurn();
                    }
                    foreach (var clientStream in ClientStreams)
                    {
                        NetworkHelper.SendMessage(clientStream, message);
                    }
                }
                else
                {
                    var validPlay = RuleHandler.IsValidPlay(message.Hand !, Pile);
                    if (validPlay)
                    {
                        Pile = message.Hand;
                        foreach (var clientStream in ClientStreams)
                        {
                            NetworkHelper.SendMessage(clientStream, message);
                        }
                        LastPlayerPlayed = message.Player !.PlayerNumber;
                    }
                }
            }
        }
Пример #2
0
 public RuleMethod(object target, RuleHandler handler, string propertyName)
 {
     _target   = target;
     _handler  = handler;
     _args     = new RuleArgs(propertyName);
     _ruleName = _handler.Method.Name + "!" + _args.ToString();
 }
Пример #3
0
        /// <summary>
        /// Adds a lambda property level rule to business rules.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="businessRules">Business rules instance.</param>
        /// <param name="ruleHandler">Rule method.</param>
        /// <param name="primaryProperty">Primary property.</param>
        /// <param name="ruleArgs">Rule args object.</param>
        /// <param name="priority">Priority.</param>
        //[Obsolete("For migration of older apps to Csla 4.x only")]
        public static void AddRule <T>(this BusinessRules businessRules, RuleHandler <T, RuleArgs> ruleHandler, Csla.Core.IPropertyInfo primaryProperty, RuleArgs ruleArgs, int priority) where T : BusinessBase <T>
        {
            var rule = new Csla.Rules.CommonRules.Lambda(primaryProperty, (o) =>
            {
                var target = (T)o.Target;
                using (new ObjectAccessor().BypassPropertyChecks(target))
                {
                    if (!ruleHandler(target, ruleArgs))
                    {
                        o.Results.Add(new RuleResult(o.Rule.RuleName, o.Rule.PrimaryProperty, ruleArgs.Description)
                        {
                            Severity = ruleArgs.Severity, StopProcessing = ruleArgs.StopProcessing
                        });
                    }
                    else if (ruleArgs.StopProcessing)
                    {
                        o.AddSuccessResult(true);
                    }
                    else
                    {
                        o.AddSuccessResult(false);
                    }
                    ruleArgs.StopProcessing = false;
                }
            });

#if NETFX_CORE
            var methodName = Guid.NewGuid().ToString();
#else
            var methodName = ruleHandler.Method.ToString();
#endif
            rule.AddQueryParameter("r", Convert.ToBase64String(Encoding.Unicode.GetBytes(methodName)));
            rule.Priority = priority;
            businessRules.AddRule(rule);
        }
Пример #4
0
        /// <summary>
        /// Creates a shared or instance validator
        /// </summary>
        public Validator(RuleHandler ruleHandler, RuleDescriptorBase e, RuleType ruleType)
        {
            _ruleHandler  = ruleHandler;
            this.RuleBase = e;
            this.RuleType = ruleType;

            switch (ruleType)
            {
            case RuleType.Instance:
                this.RuleName = _INSTANCE + this.RuleName;

                break;

            case RuleType.Shared:
                break;

            case RuleType.Attribute:
                this.RuleName = String.Format(_RULE_FORMATSTRING, ruleHandler.Method.Name, e.PropertyName, (Guid.NewGuid()));

                break;

            default:
                throw new ArgumentException(Resources.Validator_Validator_RuleType_not_programmed_yet, "RuleType");
            }
        }
Пример #5
0
 public RuleMethod(object target, RuleHandler handler, RuleArgs args)
 {
     _target   = target;
     _handler  = handler;
     _args     = args;
     _ruleName = _handler.Method.Name + "!" + _args.ToString();
 }
Пример #6
0
        /// <summary>
        /// Creates a shared or instance validator
        /// </summary>
        /// <param name="ruleHandler">The rule handler.</param>
        /// <param name="ruleDescriptor">The rule descriptor.</param>
        /// <param name="ruleType"><see cref="RuleType" /> of the rule.</param>
        /// <exception cref="ArgumentNullException">
        /// Either ruleHandler or ruleDescriptor is null
        /// </exception>
        /// <exception cref="InvalidEnumArgumentException">ruleType is not a member of RuleType enum.</exception>
        public Validator(RuleHandler ruleHandler, RuleDescriptorBase ruleDescriptor, RuleType ruleType)
        {
            if (ruleHandler == null)
            {
                throw new ArgumentNullException(nameof(ruleHandler));
            }
            if (ruleDescriptor == null)
            {
                throw new ArgumentNullException(nameof(ruleDescriptor));
            }
            if (!Enum.IsDefined(typeof(RuleType), ruleType))
            {
                throw new InvalidEnumArgumentException(nameof(ruleType), (Int32)ruleType, typeof(RuleType));
            }
            _ruleHandler  = ruleHandler;
            this.RuleBase = ruleDescriptor;
            this.RuleType = ruleType;
            switch (ruleType)
            {
            case RuleType.Instance:
                this.RuleName = $"rule://Instance-{ruleHandler.GetMethodInfo().Name}/{ruleDescriptor.PropertyName}-{(Guid.NewGuid())}";
                break;

            case RuleType.Shared:
            case RuleType.Attribute:
                this.RuleName = $"rule://{ruleHandler.GetMethodInfo().Name}/{ruleDescriptor.PropertyName}-{Guid.NewGuid()}";
                break;
            }
        }
        public void AddRule <T, R>(RuleHandler <T, R> handler, R args, int priority) where R : RuleArgs
        {
            // get the list of rules for the property
            List <IRuleMethod> list = GetRulesForProperty(args.PropertyName, true).GetList(false);

            // we have the list, add out new rule
            list.Add(new RuleMethod <T, R>(handler, args, priority));
        }
Пример #8
0
        //public void AddRule(AsyncRuleHandler handler, RuleSeverity severity, AsyncRuleArgs args)
        //{
        //  // get the list of rules for the primary property
        //  List<IRuleMethod> list = GetRulesForProperty(args.Properties[0].Name, true).GetList(false);

        //  // we have the list, add our new rule
        //  list.Add(new AsyncRuleMethod(handler, args, severity));
        //}

        public void AddRule(RuleHandler handler, RuleArgs args, int priority)
        {
            // get the list of rules for the property
            List <IRuleMethod> list = GetRulesForProperty(args.PropertyName, true).GetList(false);

            // we have the list, add our new rule
            list.Add(new RuleMethod(handler, args, priority));
        }
Пример #9
0
        /// <summary>
        /// Adds a rule to the list of rules to be enforced.
        /// </summary>
        /// <remarks>
        /// <para>
        /// A rule is implemented by a method which conforms to the
        /// method signature defined by the RuleHandler delegate.
        /// </para><para>
        /// The propertyName may be used by the method that implements the rule
        /// in order to retrieve the value to be validated. If the rule
        /// implementation is inside the target object then it probably has
        /// direct access to all data. However, if the rule implementation
        /// is outside the target object then it will need to use reflection
        /// or CallByName to dynamically invoke this property to retrieve
        /// the value to be validated.
        /// </para>
        /// </remarks>
        /// <param name="handler">The method that implements the rule.</param>
        /// <param name="propertyName">
        /// The property name on the target object where the rule implementation can retrieve
        /// the value to be validated.
        /// </param>
        public void AddRule(RuleHandler handler, string propertyName)
        {
            // get the list of rules for the property
            List <RuleMethod> list = GetRulesForProperty(propertyName);

            // we have the list, add our new rule
            list.Add(new RuleMethod(_target, handler, propertyName));
        }
Пример #10
0
 /// <summary>Creates and initializes the rule.</summary>
 /// <param name="pHandler">The address of the method implementing the rule.</param>
 /// <param name="pArgs">A RuleArgs object.</param>
 /// <param name="pPropertyNames">The fields, properties or columns to which the rule applies.</param>
 public MultiPropertyRuleMethod(RuleHandler pHandler, RuleArgs pArgs, params string[] pPropertyNames)
     : base(pHandler, pArgs)
 {
     if (pPropertyNames == null || pPropertyNames.Length < 2)
     {
         throw new ArgumentException("MultiPropertyRules require at least two properties.");
     }
     mPropertyNames = pPropertyNames;
 }
Пример #11
0
 /// <summary>
 /// Creates and initializes the rule.
 /// </summary>
 /// <param name="handler">The address of the method implementing the rule.</param>
 /// <param name="args">A RuleArgs object.</param>
 public RuleMethod(RuleHandler handler, RuleArgs args)
 {
     _handler  = handler;
     _args     = args;
     _ruleName = string.Format(@"rule://{0}/{1}/{2}",
                               Uri.EscapeDataString(_handler.Method.DeclaringType.FullName),
                               _handler.Method.Name,
                               _args.ToString());
 }
Пример #12
0
 /// <summary>
 /// Creates and initializes the rule.
 /// </summary>
 /// <param name="handler">The address of the method implementing the rule.</param>
 /// <param name="args">A RuleArgs object.</param>
 public RuleMethod(RuleHandler handler, RuleArgs args)
 {
     _handler  = handler;
     _args     = args;
     _ruleName = string.Format(@"rule://{0}/{1}/{2}",
                               Uri.EscapeDataString(_handler.Method.DeclaringType.GetAssemplyQualifiedName(true)),
                               _handler.Method.Name,
                               _args.ToString());
 }
Пример #13
0
        /// <summary>
        /// Adds a lambda property level rule to business rules.
        /// </summary>
        /// <param name="businessRules">Business rules</param>
        /// <param name="ruleHandler">Rule method</param>
        /// <param name="primaryPropertyName">Primary Property name</param>
        /// <param name="priority">Priority</param>
        //[Obsolete("For migration of older apps to Csla 4.x only")]
        public static void AddRule(this BusinessRules businessRules, RuleHandler ruleHandler, string primaryPropertyName, int priority)
        {
            if (string.IsNullOrEmpty(primaryPropertyName))
            {
                throw new ArgumentException("primaryPropertyName");
            }
            var primaryProperty = PropertyHelper.GetRegisteredProperty(businessRules, primaryPropertyName);

            AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), priority);
        }
Пример #14
0
        /// <summary>
        /// Adds a lambda property level rule to business rules.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="businessRules">Business rules</param>
        /// <param name="ruleHandler">Rule method</param>
        /// <param name="primaryPropertyName">Name of the primary property.</param>
        ///// <param name="primaryPropertyName">Primary Property name</param>
        //[Obsolete("For migration of older apps to Csla 4.x only")]
        public static void AddRule <T>(this BusinessRules businessRules, RuleHandler <T, RuleArgs> ruleHandler, string primaryPropertyName) where T : BusinessBase <T>
        {
            if (string.IsNullOrEmpty(primaryPropertyName))
            {
                throw new ArgumentException("primaryPropertyName");
            }

            var primaryProperty = PropertyHelper.GetRegisteredProperty(businessRules, primaryPropertyName);

            AddRule(businessRules, ruleHandler, primaryProperty, new RuleArgs(primaryProperty), 0);
        }
Пример #15
0
        /// <summary>Remove a specific rule from the rule list.</summary>
        public void Remove(RuleHandler pHandler, string pPropertyName)
        {
            // get the list of rules for the property
            List <RuleMethod> list = this[pPropertyName];

            if (list == null)
            {
                return;         // no such list
            }
            Remove(pHandler, list);
        }
Пример #16
0
 private static void Remove(RuleHandler pHandler, List <RuleMethod> pList)
 {
     // we have the list, remove the rule by handler
     for (int index = 0; index < pList.Count; index++)
     {
         if (pList[index].RuleHandler == pHandler)
         {
             pList.RemoveAt(index);
             break;
         }
     }
 }
Пример #17
0
        public void Has3OfSpades_UsingInvalidPlay_ReturnsFalse()
        {
            var         sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("3", 0, "Diamonds", 2)
            };

            bool result = sut.Has3OfSpades(cards);

            Assert.IsFalse(result);
        }
Пример #18
0
        public void IsValidPlay_UsingInvalidValidSingleOnFirstPlay_Fail()
        {
            RuleHandler sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("4", 1, "Spades", 0)
            };

            bool result = sut.IsValidPlay(cards, null);

            Assert.IsFalse(result);
        }
Пример #19
0
        public void Has3OfSpades_Using3OfSpades_ReturnsTrue()
        {
            var         sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("3", 0, "Spades", 0)
            };

            bool result = sut.Has3OfSpades(cards);

            Assert.IsTrue(result);
        }
Пример #20
0
        public void FindCategory_UsingSingle_ReturnsSingle()
        {
            RuleHandler sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("3", 0, "Spades", 0)
            };

            RuleHandler.Category result = sut.FindCategory(cards);

            Assert.AreEqual(RuleHandler.Category.Single, result);
        }
Пример #21
0
        public void IsValidPlay_UsingValidSingleOnFirstPlay_Success()
        {
            RuleHandler sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("3", 0, "Spades", 0)
            };

            bool result = sut.IsValidPlay(cards, null);

            Assert.IsTrue(result);
            Assert.AreEqual(RuleHandler.Category.Single, sut.CurrentCategory);
        }
Пример #22
0
 /// <summary>Creates and initializes the rule.</summary>
 /// <param name="pHandler">The address of the method implementing the rule.</param>
 /// <param name="pArgs">Rule arguments.</param>
 /// <param name="pBusinessPropertys">The entity columns to which the rule applies.</param>
 public MultiPropertyRuleMethod(RuleHandler pHandler, RuleArgs pArgs, params BusinessProperty[] pBusinessPropertys)
     : base(pHandler, pArgs)
 {
     if (pBusinessPropertys == null || pBusinessPropertys.Length < 2)
     {
         throw new ArgumentException("MultiPropertyRules require at least two properties.");
     }
     mPropertyNames = new string[pBusinessPropertys.Length];
     for (int i = 0; i < pBusinessPropertys.Length; i++)
     {
         mPropertyNames[i] = pBusinessPropertys[i].Name;
     }
 }
Пример #23
0
        public void FindCategory_UsingInvalidDouble_ReturnsInvalid()
        {
            RuleHandler sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("3", 0, "Spades", 0),
                new Card("4", 1, "Clubs", 1)
            };

            RuleHandler.Category result = sut.FindCategory(cards);

            Assert.AreEqual(RuleHandler.Category.Invalid, result);
        }
Пример #24
0
        public void FindCategory_UsingTriple_ReturnsTriple()
        {
            RuleHandler sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("3", 0, "Spades", 0),
                new Card("3", 0, "Clubs", 1),
                new Card("3", 0, "Diamonds", 2)
            };

            RuleHandler.Category result = sut.FindCategory(cards);

            Assert.AreEqual(RuleHandler.Category.Triple, result);
        }
Пример #25
0
        public void FindCategory_UsingStraightFlush_ReturnsStraightFlush()
        {
            RuleHandler sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("3", 0, "Spades", 0),
                new Card("4", 1, "Spades", 0),
                new Card("5", 2, "Spades", 0)
            };

            RuleHandler.Category result = sut.FindCategory(cards);

            Assert.AreEqual(RuleHandler.Category.StraightFlush, result);
        }
Пример #26
0
        public void FindCategory_Using4OfAKind_ReturnsBomb()
        {
            RuleHandler sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("3", 0, "Spades", 0),
                new Card("3", 0, "Clubs", 1),
                new Card("3", 0, "Diamonds", 2),
                new Card("3", 0, "Hearts", 3)
            };

            RuleHandler.Category result = sut.FindCategory(cards);

            Assert.AreEqual(RuleHandler.Category.Bomb, result);
        }
Пример #27
0
        public void IsValidPlay_Using4OfAKindBomb_Success()
        {
            RuleHandler sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("3", 0, "Spades", 0),
                new Card("3", 0, "Clubs", 1),
                new Card("3", 0, "Diamonds", 2),
                new Card("3", 0, "Hearts", 3)
            };

            bool result = sut.IsValidPlay(cards, null);

            Assert.IsTrue(result);
            Assert.AreEqual(RuleHandler.Category.Bomb, sut.CurrentCategory);
        }
Пример #28
0
        public void IsValidPlay_UsingEqualSingleLowerSuit_Fail()
        {
            RuleHandler sut   = new RuleHandler();
            List <Card> cards = new List <Card>();

            sut.FreeTurn();
            cards.Add(new Card("J", 8, "Clubs", 1));
            sut.IsValidPlay(cards, null);
            var cards1 = new List <Card>
            {
                new Card("J", 8, "Spades", 0)
            };

            bool result = sut.IsValidPlay(cards1, cards);

            Assert.IsFalse(result);
        }
Пример #29
0
        public void IsValidPlay_UsingHigherSingle_Success()
        {
            RuleHandler sut   = new RuleHandler();
            List <Card> cards = new List <Card>
            {
                new Card("3", 0, "Spades", 0)
            };

            sut.IsValidPlay(cards, null);
            var cards1 = new List <Card>
            {
                new Card("4", 1, "Spades", 0)
            };

            bool result = sut.IsValidPlay(cards1, cards);

            Assert.IsTrue(result);
        }
        /// <summary>
        /// Adds a rule to the list of rules to be enforced.
        /// </summary>
        /// <param name="handler">The handler.</param>
        /// <param name="e">The e.</param>
        /// <param name="ruleType">Type of the rule.</param>
        /// <exception cref="ArgumentNullException">
        /// Either handler or e is null
        /// </exception>
        /// <exception cref="InvalidEnumArgumentException">ruleType is not a member of RuleType enum.</exception>
        public void AddRule(RuleHandler handler, RuleDescriptorBase e, RuleType ruleType)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }
            if (!Enum.IsDefined(typeof(RuleType), ruleType))
            {
                throw new InvalidEnumArgumentException(nameof(ruleType), (Int32)ruleType, typeof(RuleType));
            }
            List <IValidationRuleMethod> list = GetRulesForProperty(e.PropertyName).List;

            list.Add(new Validator(handler, e, ruleType));
        }
Пример #31
0
 public void AddRules(RuleHandler oHandler, string strPropertyName)
 {
     List<RuleMethod> oList = GetRulesForProperty(strPropertyName);
     oList.Add(new RuleMethod(m_oTarget, oHandler, strPropertyName));
 }
Пример #32
0
            public RuleMethod(object oTarget, RuleHandler oHandler, string strPropertyName)
            {
                m_oTarget = oTarget;
                m_oHandler = oHandler;
                m_oArgs = new RuleArgs(strPropertyName);

                m_strRuleName = oHandler.Method.Name + "!" + m_oArgs.ToString();
            }
Пример #33
0
 public void Start()
 {
     ruleHandler = GameObject.FindGameObjectWithTag("DiceRule").GetComponent<RuleHandler>();
 }