예제 #1
0
        private LinkCondition[] ParseCondition(string text)
        {
            const string haveLexem = "have";

            const string notLexem = "not";

            const string divider = ",";

            string[] conditionTextArray = text.Split(divider.ToCharArray());

            LinkCondition[] linkConditions = new LinkCondition[conditionTextArray.Length];

            for (int i = 0; i < linkConditions.Length; ++i)
            {
                if (string.IsNullOrEmpty(conditionTextArray[i]))
                {
                    throw new SyntaxError("missed condition between ','"); //
                }

                string[] tokens = conditionTextArray[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                linkConditions[i] = new LinkCondition();

                if (tokens.Length == 0)
                {
                    throw new SyntaxError($"Empty condition");
                }

                if (!tokens[0].Equals(haveLexem))
                {
                    throw new SyntaxError($"Expected: '{haveLexem}', found: '{tokens[0]}'");
                }

                if (tokens.Length == 1)
                {
                    throw new SyntaxError($"Incorrect condition definition: expected name of the key or 'not' after 'have'");
                }

                if (tokens.Length == 3)
                {
                    if (tokens[1].Equals(notLexem))
                    {
                        linkConditions[i].Type = ConditionType.HaveNot;
                    }
                    else
                    {
                        throw new SyntaxError($"Expected: '{notLexem}', found: '{tokens[0]}'");
                    }
                }
                else if (tokens.Length == 2)
                {
                    linkConditions[i].Type = ConditionType.Have;
                }
                else
                {
                    throw new SyntaxError($"Incorrect condition: {conditionTextArray[i]}");
                }

                int keyPosition = tokens.Length - 1;

                if (!Regex.IsMatch(tokens[keyPosition], keyRegExp))
                {
                    throw new SyntaxError($"Unacceptable key definition: {tokens[0]}");
                }

                MatchCollection collection = Regex.Matches(tokens[keyPosition], "[\\d]+");

                foreach (Match num in collection)
                {
                    linkConditions[i].KeyNumber = int.Parse(num.Value);
                }
            }

            return(linkConditions);
        }
 public DialogLink()
 {
     Actions    = new GameAction[0];
     Conditions = new LinkCondition[0];
 }