static Rule CreateRuleFromContext(DeltinScriptParser.Ow_ruleContext ruleContext)
        {
            string ruleName = ruleContext.STRINGLITERAL().GetText();

            ruleName = ruleName.Substring(1, ruleName.Length - 2);

            RuleEvent      ruleEvent = RuleEvent.Ongoing_Global;
            TeamSelector   team      = TeamSelector.All;
            PlayerSelector player    = PlayerSelector.All;

            {
                var additionalArgs = ruleContext.expr();

                foreach (var arg in additionalArgs)
                {
                    string type = arg.GetText().Split('.').ElementAtOrDefault(0);
                    string name = arg.GetText().Split('.').ElementAtOrDefault(1);

                    if (type == "Event")
                    {
                        if (Enum.TryParse(name, out RuleEvent setEvent))
                        {
                            ruleEvent = setEvent;
                        }
                        else
                        {
                            throw new SyntaxErrorException($"Unknown event type \"{arg.GetText()}\".", arg.start);
                        }
                    }
                    else if (type == "Team")
                    {
                        if (Enum.TryParse(name, out TeamSelector setTeam))
                        {
                            team = setTeam;
                        }
                        else
                        {
                            throw new SyntaxErrorException($"Unknown team type \"{arg.GetText()}\".", arg.start);
                        }
                    }
                    else if (type == "Player")
                    {
                        if (Enum.TryParse(name, out PlayerSelector setPlayer))
                        {
                            player = setPlayer;
                        }
                        else
                        {
                            throw new SyntaxErrorException($"Unknown player type \"{arg.GetText()}\".", arg.start);
                        }
                    }
                    else
                    {
                        throw new SyntaxErrorException($"Unknown rule argument \"{arg.GetText()}\".", arg.start);
                    }
                }
            }

            return(new Rule(ruleName, ruleEvent, team, player));
        }
        public Rule(string name, RuleEvent ruleEvent = RuleEvent.OngoingGlobal, Team team = Team.All, PlayerSelector player = PlayerSelector.All) // Creates a rule.
        {
            if (ruleEvent == RuleEvent.OngoingGlobal && (team != Team.All || player != PlayerSelector.All))
            {
                ruleEvent = RuleEvent.OngoingPlayer;
            }

            Name      = name;
            RuleEvent = ruleEvent;
            Team      = team;
            Player    = player;

            if (RuleEvent == RuleEvent.OngoingGlobal)
            {
                RuleType = RuleType.Global;
            }
            else if (RuleEvent == RuleEvent.Subroutine)
            {
                RuleType = RuleType.Subroutine;
            }
            else
            {
                RuleType = RuleType.PlayerBased;
            }
        }
Example #3
0
 public Rule(string name, RuleEvent ruleEvent = RuleEvent.OngoingGlobal, Team team = Team.All, PlayerSelector player = PlayerSelector.All) // Creates a rule.
 {
     Name      = name;
     RuleEvent = ruleEvent;
     Team      = team;
     Player    = player;
     IsGlobal  = ruleEvent == RuleEvent.OngoingGlobal;
 }
Example #4
0
 public TranslateRule(DeltinScript deltinScript, string name, RuleEvent eventType, Team team, PlayerSelector player, bool disabled = false)
 {
     DeltinScript = deltinScript;
     IsGlobal     = eventType == RuleEvent.OngoingGlobal;
     Name         = name;
     EventType    = eventType;
     Team         = team;
     Player       = player;
     Disabled     = disabled;
     ActionSet    = new ActionSet(this, null, Actions);
 }
Example #5
0
        public RuleNode(string name, RuleEvent eventType, TeamSelector team, PlayerSelector player, IExpressionNode[] conditions, BlockNode block,
                        Range eventRange, Range teamRange, Range playerRange, Range range) : base(range, eventRange, teamRange, playerRange)
        {
            Name = name;

            Event  = eventType;
            Team   = team;
            Player = player;

            Conditions = conditions;
            Block      = block;
        }
Example #6
0
        public Rule(string name, RuleEvent ruleEvent, TeamSelector team, PlayerSelector player) // Creates a rule.
        {
            if (name.Length > Constants.RULE_NAME_MAX_LENGTH)
            {
                throw new ArgumentOutOfRangeException(nameof(name), name, $"Rule names cannot be longer than {Constants.RULE_NAME_MAX_LENGTH} characters.");
            }

            Name      = name;
            RuleEvent = ruleEvent;
            Team      = team;
            Player    = player;
            IsGlobal  = ruleEvent == RuleEvent.OngoingGlobal;
        }
Example #7
0
        public override Node VisitOw_rule(DeltinScriptParser.Ow_ruleContext context)
        {
            string    name  = context.STRINGLITERAL().GetText().Trim('"');
            BlockNode block = (BlockNode)VisitBlock(context.block());

            IExpressionNode[] conditions      = new IExpressionNode[context.rule_if()?.expr().Length ?? 0];
            Range[]           conditionRanges = new Range[context.rule_if()?.expr().Length ?? 0];
            for (int i = 0; i < conditions.Length; i++)
            {
                conditions[i]      = (IExpressionNode)VisitExpr(context.rule_if().expr()[i]);
                conditionRanges[i] = Range.GetRange(context.rule_if().expr()[i]);
            }

            RuleEvent      eventType = RuleEvent.OngoingGlobal;
            TeamSelector   team      = TeamSelector.All;
            PlayerSelector player    = PlayerSelector.All;

            Range eventRange  = null;
            Range teamRange   = null;
            Range playerRange = null;

            foreach (var ruleOption in context.rule_option())
            {
                string option      = ruleOption.PART(0).GetText();
                Range  optionRange = Range.GetRange(ruleOption.PART(0).Symbol);

                string value      = null;
                Range  valueRange = null;
                if (ruleOption.PART().Length == 2)
                {
                    value      = ruleOption.PART(1).GetText();
                    valueRange = Range.GetRange(ruleOption.PART(1).Symbol);
                }

                switch (option)
                {
                case "Event":
                    if (!Enum.TryParse <RuleEvent>(value, out eventType))
                    {
                        _diagnostics.Add(new Diagnostic($"{value} is not a valid Event type.", valueRange));
                    }
                    eventRange = Range.GetRange(ruleOption);
                    break;

                case "Team":
                    if (!Enum.TryParse <TeamSelector>(value, out team))
                    {
                        _diagnostics.Add(new Diagnostic($"{value} is not a valid Team type.", valueRange));
                    }
                    teamRange = Range.GetRange(ruleOption);
                    break;

                case "Player":
                    if (!Enum.TryParse <PlayerSelector>(value, out player))
                    {
                        _diagnostics.Add(new Diagnostic($"{value} is not a valid Player type.", valueRange));
                    }
                    playerRange = Range.GetRange(ruleOption);
                    break;

                default:
                    _diagnostics.Add(new Diagnostic($"{value} is not a valid rule option.", optionRange));
                    break;
                }
            }

            var node = new RuleNode(name, eventType, team, player, conditions, block, eventRange, teamRange, playerRange, Range.GetRange(context));

            CheckRange(node);
            return(node);
        }
Example #8
0
 public TranslateRule(DeltinScript deltinScript, string name, RuleEvent eventType) : this(deltinScript, name, eventType, Team.All, PlayerSelector.All)
 {
 }
Example #9
0
 public UnconditionalRule(RuleEvent _event)
 {
     this._event = _event;
 }
Example #10
0
        private void UpdateRule(RuleEvent @event, EnvelopeHeaders headers, Action <JsonRuleEntity> updater = null)
        {
            var id = @event.RuleId;

            Rules = Rules.SetItem(id, x => x.Clone().Update(@event, headers, updater));
        }
 public ConditionalRule(string conditionalStamp, RuleEvent onConditionMet, RuleEvent onConditionNotMet)
 {
     this.conditionalStamp = conditionalStamp;
     event1 = onConditionMet;
     event2 = onConditionNotMet;
 }
Example #12
0
 public Department(string id, string conditionalStamp, RuleEvent onConditionMet, RuleEvent onConditionNotMet)
 {
     Id        = id;
     this.Rule = new ConditionalRule(conditionalStamp, onConditionMet, onConditionNotMet);
 }
Example #13
0
 public Department(string id, RuleEvent ruleConfiguration)
 {
     Id        = id;
     this.Rule = new UnconditionalRule(ruleConfiguration);
 }
        public RuleNode(DeltinScriptParser.Ow_ruleContext context, BuildAstVisitor visitor) : base(new Location(visitor.file, Range.GetRange(context)))
        {
            Name  = context.STRINGLITERAL().GetText().Trim('"');
            Block = (BlockNode)visitor.VisitBlock(context.block());

            Conditions = new Node[context.rule_if().Length];
            Range[] conditionRanges = new Range          [context.rule_if().Length];

            for (int i = 0; i < context.rule_if().Length; i++)
            {
                if (context.rule_if(i).expr() != null)
                {
                    Conditions[i] = visitor.VisitExpr(context.rule_if(i).expr());
                }

                // Get the range between the ().
                conditionRanges[i] = Range.GetRange(
                    context.rule_if(i).LEFT_PAREN().Symbol,
                    context.rule_if(i).RIGHT_PAREN().Symbol
                    );
            }

            RuleEvent      eventType = RuleEvent.OngoingGlobal;
            Team           team      = Team.All;
            PlayerSelector player    = PlayerSelector.All;

            Range eventRange  = null;
            Range teamRange   = null;
            Range playerRange = null;

            foreach (var ruleOption in context.@enum())
            {
                string option      = ruleOption.PART(0).GetText();
                Range  optionRange = Range.GetRange(ruleOption.PART(0).Symbol);

                string value      = ruleOption.PART(1)?.GetText();
                Range  valueRange = null;
                if (value != null)
                {
                    valueRange = Range.GetRange(ruleOption.PART(1).Symbol);
                }

                Range totalRange;
                if (ruleOption.PART(1) != null)
                {
                    totalRange = Range.GetRange(ruleOption.PART(0).Symbol, ruleOption.PART(1).Symbol);
                }
                else
                {
                    totalRange = Range.GetRange(ruleOption.PART(0));
                }

                switch (option)
                {
                case "Event":
                    if (eventRange != null)
                    {
                        visitor._diagnostics.Error("Event already set.", new Location(visitor.file, totalRange));
                    }

                    if (!Enum.TryParse <RuleEvent>(value, out eventType))
                    {
                        visitor._diagnostics.Error($"{value} is not a valid Event type.", new Location(visitor.file, valueRange));
                    }

                    eventRange = Range.GetRange(ruleOption);
                    break;

                case "Team":
                    if (teamRange != null)
                    {
                        visitor._diagnostics.Error("Team already set.", new Location(visitor.file, totalRange));
                    }

                    if (!Enum.TryParse <Team>(value, out team))
                    {
                        visitor._diagnostics.Error($"{value} is not a valid Team type.", new Location(visitor.file, valueRange));
                    }

                    teamRange = Range.GetRange(ruleOption);
                    break;

                case "Player":
                    if (playerRange != null)
                    {
                        visitor._diagnostics.Error("Player already set.", new Location(visitor.file, totalRange));
                    }

                    if (!Enum.TryParse <PlayerSelector>(value, out player))
                    {
                        visitor._diagnostics.Error($"{value} is not a valid Player type.", new Location(visitor.file, valueRange));
                    }

                    playerRange = Range.GetRange(ruleOption);
                    break;

                default:
                    visitor._diagnostics.Error($"{option} is not a valid rule option.", new Location(visitor.file, optionRange));
                    break;
                }
            }
            Event  = eventType;
            Team   = team;
            Player = player;

            SubRanges = ArrayBuilder <Range> .Build(eventRange, teamRange, playerRange, conditionRanges);
        }
Example #15
0
        public void ToWorkshop(WorkshopBuilder builder)
        {
            if (Disabled)
            {
                builder.AppendKeyword("disabled")
                .Append(" ");
            }
            builder.AppendKeyword("rule")
            .AppendLine("(\"" + Name + "\")")
            .AppendLine("{")
            .AppendLine()
            .Indent()
            .AppendKeywordLine("event")
            .AppendLine("{")
            .Indent();

            ElementRoot.Instance.GetEnumValue("Event", RuleEvent.ToString()).ToWorkshop(builder, ToWorkshopContext.Other);
            builder.Append(";").AppendLine();

            // Add attributes.
            switch (RuleType)
            {
            case RuleType.PlayerBased:
                // Player based attributes
                ElementEnumMember.Team(Team).ToWorkshop(builder, ToWorkshopContext.Other);                                   // Team attribute
                builder.Append(";").AppendLine();
                ElementRoot.Instance.GetEnumValue("Player", Player.ToString()).ToWorkshop(builder, ToWorkshopContext.Other); // Player attribute
                builder.Append(";").AppendLine();
                break;

            case RuleType.Subroutine:
                Subroutine.ToWorkshop(builder, ToWorkshopContext.Other);     // Attribute name
                builder.Append(";").AppendLine();
                break;
            }
            builder.Outdent()
            .AppendLine("}");

            if (Conditions?.Length > 0)
            {
                builder.AppendLine()
                .AppendKeywordLine("conditions")
                .AppendLine("{")
                .Indent();

                foreach (var condition in Conditions)
                {
                    condition.ToWorkshop(builder);
                }

                builder.Outdent().AppendLine("}");
            }

            // Add actions.
            if (Actions?.Length > 0)
            {
                builder.AppendLine()
                .AppendLine("// Action count: " + Actions.Length)     // Action count comment.
                .AppendKeywordLine("actions")
                .AppendLine("{")
                .Indent();

                foreach (var action in Actions)
                {
                    action.ToWorkshop(builder, ToWorkshopContext.Action);
                }

                builder.Outdent().AppendLine("}");
            }
            builder.Outdent().AppendLine("}");
        }
Example #16
0
        private Envelope <RuleEvent> AppEvent(RuleEvent @event)
        {
            @event.AppId = appId;

            return(Envelope.Create(@event).SetAggregateId(DomainId.Combine(appId.Id, @event.RuleId)));
        }