public static bool PassesRule(this IPersistIfc Entity, MvdRule DataNode, MvdPropertyRuleValue requirement = null, PassMode PassMode = PassMode.Undefined)
        {
            // todo: add controls for schema cardinality
            //

            IfcType EntType = IfcMetaData.IfcType(Entity);

#if SYSDEBUG
            Debug.WriteLine(string.Format("testing {0} for {1}", Entity.ToString(), DataNode.Type));
#endif
            string RuleId        = "";
            bool   bEvaluateRule = false;
            if (DataNode.Properties.ContainsKey("RuleID") && requirement != null)
            {
                RuleId        = DataNode.Properties["RuleID"];
                bEvaluateRule = (requirement.Name == RuleId);
                // here the property is evaluated.
                if (bEvaluateRule)
                {
#if SYSDEBUG
                    Debug.Write("-- Evaluating");
#endif
                    if (Entity is IPersistIfcEntity)
                    {
                        Debug.Write(string.Format("Req: {0} Ent: {1}{2}\r\n", requirement.Name, Math.Abs(((IPersistIfcEntity)Entity).EntityLabel), EntType));
                    }
                    if (requirement.Prop == "Type")
                    {
                        string cmpVal = IfcMetaData.IfcType(Entity).Name;
                        return(requirement.Val == cmpVal);
                    }
                }
            }


            if (DataNode.Type == "AttributeRule")
            {
                string propName = DataNode.Properties["AttributeName"];
                var    prop     = EntType.IfcProperties.Where(x => x.Value.PropertyInfo.Name == propName).FirstOrDefault().Value;
                if (prop == null) // otherwise test inverses
                {
                    prop = EntType.IfcInverses.Where(x => x.PropertyInfo.Name == propName).FirstOrDefault();
                }
                if (prop == null)
                {
#if SYSDEBUG
                    Debug.WriteLine("AttributeRule failed on null value");
#endif
                    return(false);
                }

                object propVal = prop.PropertyInfo.GetValue(Entity, null);
                if (propVal == null)
                {
                    return(false);
                }
                if (propVal != null)
                {
                    if (bEvaluateRule)
                    {
                        if (requirement.Prop == "Value")
                        {
                            string cmpVal = propVal.ToString();
                            bool   retVal = (requirement.Val == cmpVal);
#if SYSDEBUG
                            if (requirement.Val.StartsWith(""))
                            {
                            }

                            if (!retVal)
                            {
                                Debug.WriteLine("AttributeRule failed on value");
                            }
#endif

                            return(retVal);
                        }
                    }
                    else if (requirement == null && propVal is IPersistIfc)
                    {
                        var  v            = (IPersistIfc)propVal;
                        bool AnyChildFail = false;
                        foreach (var nestedRule in DataNode.NestedRules)
                        {
                            Debug.Indent();
                            if (!v.PassesRule(nestedRule))
                            {
                                AnyChildFail = true;
                                Debug.Unindent();
                                break;
                            }
                            Debug.Unindent();
                        }
                        return(!AnyChildFail);
                    }

                    if (prop.IfcAttribute.IsEnumerable)
                    {
                        IEnumerable <object> propCollection = propVal as IEnumerable <object>;
                        if (propCollection == null)
                        {
                            return(false);
                        }
                        foreach (var child in propCollection)
                        {
                            if (child is IPersistIfc)
                            {
                                // todo: this might actually have to return fail if any nested rule fail, not if the first passes.
                                foreach (var nestedRule in DataNode.NestedRules)
                                {
                                    Debug.Indent();
                                    var loopret = ((IPersistIfc)child).PassesRule(nestedRule, requirement, PassMode);
                                    Debug.Unindent();
                                    if (loopret)
                                    {
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        IPersistIfc pe = propVal as IPersistIfc;
                        if (pe == null)
                        {
#if SYSDEBUG
                            Debug.WriteLine("AttributeRule failed: not found");
#endif
                            return(false);
                        }
                        foreach (var nestedRule in DataNode.NestedRules)
                        {
                            if (pe.PassesRule(nestedRule, requirement))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            else if (DataNode.Type == "EntityRule")
            {
                string  EName     = DataNode.Properties["EntityName"];
                IfcType ENameType = IfcMetaData.IfcType(EName.ToUpperInvariant());
                if (ENameType != null)
                {
                    // run type validation only if type matches
                    if (!(ENameType == EntType || ENameType.NonAbstractSubTypes.Contains(EntType.Type)))
                    {
#if SYSDEBUG
                        Debug.WriteLine("EntityRule failed: expected " + EName + " found: " + EntType.ToString());
#endif
                        return(false);
                    }
                    // if test is passed and no sub rules then return true
                    if (!DataNode.NestedRules.Any())
                    {
                        return(true);
                    }
                }
                else
                {
                    Debug.WriteLine("Probably IFC4 Type specified:" + EName);
                }
                foreach (var subRule in DataNode.NestedRules)
                {
                    var passed = Entity.PassesRule(subRule, requirement);
                    if (passed)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        if ( phase == Phase.Connecting ) {
            bool allConnected = true;
            for ( int i=0; i<players.Count; i++ ) {
                if ( players[i].controller == null ) {
                    UniMoveController controller = players[i].gameObject.AddComponent<UniMoveController>();
                    if ( controller.Init(i) ) {
                        controller.SetLED(Color.white);
                        controller.InitOrientation();
                        controller.ResetOrientation();
                        players[i].Init(controller, controllerObjs[i]);
                    } else {
                        Destroy(controller);
                        allConnected = false;
                    }
                }
            }
            if ( allConnected ) {
                phase = Phase.Waiting;

                int team = 0;
                foreach ( Player player in players ) {
                    player.SetTeam( team%2 + 1 );
                    team++;
                }
            }
        }

        else if ( phase == Phase.Waiting ) {
            // start game
            if ( Input.GetKeyDown(KeyCode.Space) ) {
                bool practice = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
                StartGame(practice);
            }

            // color switching
            foreach ( Player player in players ) {
                Color? setColor = null;
                string setName = null;
                if ( player.controller.GetButtonDown(PSMoveButton.Square) ) {
                    setColor = new Color(1, 0, 0.5f);
                    setName = "pink";
                }
                else if ( player.controller.GetButtonDown(PSMoveButton.Triangle) ) {
                    setColor = new Color(0, 1, 0);
                    setName = "green";
                }
                else if ( player.controller.GetButtonDown(PSMoveButton.Cross) ) {
                    setColor = new Color(0, 0, 1);
                    setName = "blue";
                }
                else if ( player.controller.GetButtonDown(PSMoveButton.Circle) ) {
                    setColor = new Color(1, 0.5f, 0);
                    setName = "orange";
                }

                if ( setColor != null ) {
                    if ( player.team == 1 && !TeamColor2.Equals(setColor) )  {
                        TeamColor1 = (Color)setColor;
                        TeamName1 = (string)setName;
                    }
                    if ( player.team == 2 && !TeamColor1.Equals(setColor) ) {
                        TeamColor2 = (Color)setColor;
                        TeamName2 = (string)setName;
                    }

                    foreach ( Player p in players )
                        p.UpdateTeamColor();

                    board.color1 = TeamColor1;
                    board.color2 = TeamColor2;
                }
            }
        }

        else if ( phase == Phase.Playing ) {
            // stop game
            if ( Input.GetKeyDown(KeyCode.Space) ) {
                StopGame();
            }

            if ( !isPractice ) {
                gameTime -= Time.deltaTime;
            }

            // end game?
            if ( gameTime <= 0 ) {
                EndGame();
            }

            //update board
            board.seconds = gameTime;
            board.score1 = scores[1];
            board.score2 = scores[2];

            // countdown
            if ( gameTime < 60 && !playedCountdownSounds.Contains(60) ) {
                PlayCountdown(sounds.OneMinute, 60);
                announcer.PlayOneMinute(scores[1] > scores[2] ? TeamName1 : TeamName2);
            }
            if ( gameTime < 30 && !playedCountdownSounds.Contains(30) ) {
                PlayCountdown(sounds.ThirtySeconds, 30);
                announcer.PlayThirtySeconds(scores[1] > scores[2] ? TeamName1 : TeamName2);
            }
            if ( gameTime < 10 && !playedCountdownSounds.Contains(10) ) PlayCountdown(sounds.TenSeconds, 10);
            if ( gameTime < 9 && !playedCountdownSounds.Contains(9) ) PlayCountdown(sounds.NineSeconds, 9);
            if ( gameTime < 8 && !playedCountdownSounds.Contains(8) ) PlayCountdown(sounds.EightSeconds, 8);
            if ( gameTime < 7 && !playedCountdownSounds.Contains(7) ) PlayCountdown(sounds.SevenSeconds, 7);
            if ( gameTime < 6 && !playedCountdownSounds.Contains(6) ) PlayCountdown(sounds.SixSeconds, 6);
            if ( gameTime < 5 && !playedCountdownSounds.Contains(5) ) PlayCountdown(sounds.FiveSeconds, 5);
            if ( gameTime < 4 && !playedCountdownSounds.Contains(4) ) PlayCountdown(sounds.FourSeconds, 4);
            if ( gameTime < 3 && !playedCountdownSounds.Contains(3) ) PlayCountdown(sounds.ThreeSeconds, 3);
            if ( gameTime < 2 && !playedCountdownSounds.Contains(2) ) PlayCountdown(sounds.TwoSeconds, 2);
            if ( gameTime < 1 && !playedCountdownSounds.Contains(1) ) PlayCountdown(sounds.OneSeconds, 1);
        }

        else if ( phase == Phase.Ended ) {
            // stop game
            if ( Input.GetKeyDown(KeyCode.Space) ) {
                StopGame();
            }
        }

        if ( Input.GetKeyDown(KeyCode.D) ) {
            debugContainer.SetActive(!debugContainer.activeSelf);
        }
        if ( Input.GetKeyDown(KeyCode.R) ) {
            foreach ( Player p in players ) {
                if ( p.controller != null ) {
                    p.controller.ResetOrientation();
                }
            }
        }
        if ( Input.GetKeyDown(KeyCode.P) ) {
            passMode = passMode==PassMode.Imaginary ? PassMode.Button : PassMode.Imaginary;
        }
        if ( Input.GetKeyDown(KeyCode.A) ) {
            enableAnnouncer = !enableAnnouncer;
            announcer.isEnabled = enableAnnouncer;
        }
    }