Example #1
0
 public override void Init(GameObject go)
 {
     _PhysicInstance              = go.GetComponent <PhysicalAttribute>();
     _ConditionAttribute          = new ConditionAttribute();
     CardCondition.OriginCard     = this;
     PhysicalCondition.OriginCard = this;
 }
Example #2
0
        private bool ResolveCondition(ConditionAttribute condition, ValueStorage storage)
        {
            // If no condition is given, resolve it to true so the field is read
            if (condition == null)
            {
                return(true);
            }

            var value = storage.Get(condition.FieldName);

            switch (condition.Comparer)
            {
            case ConditionComparer.Equal:
                return(Convert.ToUInt64(value) == condition.Value);

            case ConditionComparer.Greater:
                return(Convert.ToUInt64(value) > condition.Value);

            case ConditionComparer.Smaller:
                return(Convert.ToUInt64(value) < condition.Value);

            case ConditionComparer.GEqual:
                return(Convert.ToUInt64(value) >= condition.Value);

            case ConditionComparer.SEqual:
                return(Convert.ToUInt64(value) <= condition.Value);

            default:
                throw new InvalidOperationException($"Unknown comparer {condition.Comparer}.");
            }
        }
Example #3
0
        public static bool Passes(this ConditionAttribute condition, SyntaxNode node)
        {
            if (condition == null)
            {
                return(false);
            }

            return(condition.Expect);
        }
Example #4
0
        public static bool IPasses(this ConditionAttribute condition, SyntaxNode node)
        {
            if (condition == null || node == null)
            {
                return(false);
            }

            return(Passes(condition as dynamic, node as dynamic));
        }
Example #5
0
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        if (target == null)
        {
            target = attribute as ConditionAttribute;
        }

        return(!result && target.visibilityType == ConditionAttribute.VisibilityType.Hidden ? 0f : EditorGUI.GetPropertyHeight(property));
    }
Example #6
0
        /// <summary> Применяет описаные условия для коллекции элементов типа DBObject </summary>
        /// <param name="propertyName">Свойство, к которому приписаны условия</param>
        public static List <DBObject> ApplyConditions(this IEnumerable <DBObject> dbObjects, PropertyInfo property)
        {
            List <Func <DBObject, bool> > conditions = ConditionAttribute.GetConditions(property);

            foreach (Func <DBObject, bool> condition in conditions)
            {
                dbObjects = dbObjects.Where(condition);
            }
            return(dbObjects.ToList());
        }
        /// <summary>
        /// Check If 'c' can attack now
        /// 1. c is  on  field.
        /// 2. c is able to attack.
        /// 3. c is not attacking.
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public bool CheckCanAttack(CreatureCard c)
        {
            bool result = false;
            ConditionAttribute condition = c.CardCondition;

            if (!c.PhysicalCondition.IsOnField() ||
                !condition.CanUse ||
                !condition.IsAttacking)
            {
                return(result);
            }
            else
            {
                result = true;
            }
            return(result);
        }
Example #8
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (target == null)
        {
            target = attribute as ConditionAttribute;
        }

        bool result = CheckCondition(property);

        if (target.visibilityType == ConditionAttribute.VisibilityType.NotEditable)
        {
            GUI.enabled = result;
            EditorGUI.PropertyField(position, property, true);
            GUI.enabled = true;
        }
        else
        {
            if (result)
            {
                EditorGUI.PropertyField(position, property, true);
            }
        }
    }
Example #9
0
    protected override void Initialize()
    {
        // setup drop down
        Assembly ass = Assembly.GetAssembly(typeof(Condition));

        foreach (Type t in ass.GetTypes().Where(t => t.IsSubclassOf(typeof(Condition))))
        {
            ConditionAttribute attribute = t.GetCustomAttribute <ConditionAttribute>();

            if (attribute == null || attribute.Type != ConditionType)
            {
                continue;
            }

            string s = t.ToString();

            s = string.Concat(s.Select(x => char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');

            typeMapping.Add(s, t);

            dropDown.AddOption(s);
        }
    }
        // default validation
        private void DefaultValidateAction(JProperty jprop, ConditionAttribute attr)
        {
            string propertyName = attr.CheckName;

            // 檢查設定的名稱與Json的是否一致
            if (!propertyName.Equals(jprop.Name))
            {
                ErrorList.Add(String.Format("property:({0}) [Name] expected: [{0}], actual is [{1}]", propertyName, jprop.Name));
            }
            // 檢查(非預設值且必要)設定的資料型別與json的值型別是否一致
            if (attr.JsonType != JTokenType.None && attr.Required && jprop.Value.Type != attr.JsonType)
            {
                ErrorList.Add(String.Format("property:({0}) [Type] expected: {1}, actual is {2}", propertyName, attr.JsonType.ToString(), jprop.Value.Type.ToString()));
            }
            // (若為字串型別)檢查內容字串的長度與設定是否一致
            if (attr.JsonType == JTokenType.String && attr.StringMaxLength != 0)
            {
                var val = jprop.Value.Value <string>();
                if (val != null && val.Length > attr.StringMaxLength)
                {
                    ErrorList.Add(String.Format("property:({0}) [StringMaxLength] expected: {1}, actual is {2}", propertyName, attr.StringMaxLength, val.Length));
                }
            }
        }
        // Recursive All JsonNode and Models properties
        private void WalkNode(JToken token, Type obejctTyoe, Action <JProperty, ConditionAttribute> action)
        {
            if (token.Type == JTokenType.Object)
            {
                List <PropertyInfo> props = null;
                // 1.取得此層要映射的物件Prperties,若為泛型List<T>,則取得T的Prperties
                if (obejctTyoe.IsGenericType && obejctTyoe.GetGenericTypeDefinition() == typeof(List <>))
                {
                    props = obejctTyoe.GetGenericArguments()[0].GetProperties().ToList();
                }
                else
                {
                    props = obejctTyoe.GetProperties().ToList();
                }

                // 2.遍歷此層的json node並搜尋propertInfo內的自定屬性 MyAttribute 並確認條件
                foreach (JProperty child in token.Children <JProperty>())
                {
                    ConditionAttribute myAttr            = null;
                    Type                     genericType = null;
                    PropertyInfo             hitProp     = null;
                    Predicate <PropertyInfo> task        = (prop) =>
                    {
                        var attr = prop.GetCustomAttribute <ConditionAttribute>(false);
                        if (attr != null && attr.CheckName.Equals(child.Name, StringComparison.CurrentCultureIgnoreCase))
                        {
                            hitProp     = prop;
                            genericType = prop.PropertyType;
                            myAttr      = attr;
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    };

                    if (props.Exists(task))
                    {
                        props.Remove(hitProp);
                        action(child, myAttr);//委派執行錯誤檢驗
                    }
                    // T have MyAttribute(未設置自定Attribute就不再繼續往其內層node檢驗)
                    if (myAttr != null && (child.Value.Type == JTokenType.Object || child.Value.Type == JTokenType.Array))
                    {
                        WalkNode(child.Value, genericType, action);
                    }
                }


                // 3.取出剩餘properties且為Required的並設定錯誤訊息
                props.ForEach(prop =>
                {
                    var attr = prop.GetCustomAttribute <ConditionAttribute>(false);
                    if (attr != null && attr.Required)
                    {
                        ErrorList.Add(String.Format("property:({0}) is required,but not find", attr.CheckName));
                    }
                });
            }
            else if (token.Type == JTokenType.Array)
            {
                foreach (JToken child in token.Children())
                {
                    WalkNode(child, obejctTyoe, action);
                }
            }
        }