Пример #1
0
        internal Command ToGameObject(ChatRuntime rt)
        {
            ChatParser  parser = rt.Parser();
            LineContext lc     = new LineContext(parser, actor, command, text, label, meta);
            var         cmd    = parser.CreateCommand(lc);

            if (cmd is Ask)
            {
                Ask ask = (Dialogic.Ask)cmd;
                for (int i = 0; i < options.Length; i++)
                {
                    var parts = options[i].Split(OPT_DELIM);
                    if (parts.Length != 2)
                    {
                        throw new DialogicException
                                  ("Bad option:" + options[i]);
                    }
                    Opt opt = new Opt();
                    parts[1] = parts[1].TrimFirst(Ch.LABEL);
                    opt.Init(parts[0], Ch.LABEL + parts[1], null);
                    ask.AddOption(opt);
                }
            }
            return(cmd);
        }
Пример #2
0
 public LineContext(ChatParser parser, string line, int lineNo = -1, bool showMatch = false)
 {
     this.parser = parser;
     this.line   = line;
     this.lineNo = lineNo;
     ParseLineContext(showMatch);
 }
Пример #3
0
        public void FindAll2()
        {
            string[] lines =
            {
                "CHAT c0",
                "FIND {dev=1,day=fri}",
                "CHAT c1 {day=fri}",
                "CHAT c2 {dev=2,day=fri}",
                "CHAT c3 {}"
            };

            string contents = String.Join("\n", lines);


            List <Chat> chats = ChatParser.ParseText(contents, NO_VALIDATORS);
            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            Command finder = chats[0].commands[0]; finder.Resolve(null);

            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Find)));

            chats = new ChatRuntime(chats).DoFindAll((Find)finder, null);

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(2));

            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c1"));

            lines = new[] {
                "CHAT c0",
                "FIND {!dev=1,day=fri}",
                "CHAT c1 {day=fri}",
                "CHAT c2 {dev=2,day=fri}",
                "CHAT c3 {}"
            };

            contents = String.Join("\n", lines);

            chats = ChatParser.ParseText(contents, NO_VALIDATORS);
            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            finder = chats[0].commands[0]; finder.Resolve(null);
            var mdev = finder.GetMeta("dev");

            Assert.That(mdev is Constraint, Is.True);
            Constraint cons = (Constraint)mdev;

            Assert.That(cons.type, Is.EqualTo(ConstraintType.Hard));
            Assert.That(cons.IsStrict(), Is.True);
            Assert.That(finder.GetType(), Is.EqualTo(typeof(Find)));

            var rt = new ChatRuntime(chats);

            rt.Chats().ForEach(c => c.Resolve(null));
            chats = rt.DoFindAll((Find)finder, null);

            //chats.ForEach((obj) => Console.WriteLine(obj.text));

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(0));
        }
Пример #4
0
        public void FindChatByLabel()
        {
            string[] lines =
            {
                "CHAT c1 {day=fri}",
                "CHAT c2 {dev=2,day=fri}",
                "CHAT c3"
            };
            string      contents = String.Join("\n", lines);
            List <Chat> chats    = ChatParser.ParseText(contents, NO_VALIDATORS);
            Chat        result   = new ChatRuntime(chats).FindChatByLabel("c1");

            //chats.ForEach(c=>Console.WriteLine(c));
            Assert.That(result, Is.Not.Null);
            Assert.That(result.text, Is.EqualTo("c1"));

            Chat c;

            chats       = new List <Chat>();
            chats.Add(c = Chat.Create("c1"));
            chats.Add(c = Chat.Create("c2"));
            chats.Add(c = Chat.Create("c3"));
            ChatRuntime cr  = new ChatRuntime(chats);
            Chat        res = new ChatRuntime(chats).FindChatByLabel("c2");

            Assert.That(res.text, Is.EqualTo("c2"));
        }
Пример #5
0
        public void FindAllHard4()
        {
            var lines = new string[] {
                "CHAT c0",
                "FIND {dev=1,!!day=fri}",
                "CHAT c1 {day=fri}",
                "CHAT c2 {dev=2,day=fri}",
                "CHAT c3 {}"
            };

            var chats = ChatParser.ParseText(String.Join("\n", lines), NO_VALIDATORS);

            chats.ForEach(c => c.Resolve(null));

            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            var finder = chats[0].commands[0]; finder.Resolve(null);

            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Find)));

            var mday = finder.GetMeta("day");

            Assert.That(mday is Constraint, Is.True);
            var cons = (Constraint)mday;

            Assert.That(cons.type, Is.EqualTo(ConstraintType.Absolute));
            Assert.That(cons.IsStrict(), Is.True);

            chats = new ChatRuntime(chats).DoFindAll((Find)finder, null);
            //chats.ForEach((obj) => Console.WriteLine(obj.text));
            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));
            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c1"));
        }
Пример #6
0
 public LineContext(ChatParser parser, string actor, string command, string text, string label, string meta)
 {
     this.parser  = parser;
     this.actor   = actor;
     this.command = command;
     this.text    = text;
     this.label   = label;
     this.meta    = meta;
 }
Пример #7
0
        public void FindAllMultiHard2()
        {
            string[] lines =
            {
                "CHAT c0",
                "FIND {emotion*=(hot|cool), !day =fri}",
                "CHAT c1",
                "CHAT c2 {emotion=hot,day=fri}",
                "CHAT c3 {emotion=dry}",
                "CHAT c4 {emotion=dry,day=fri}",
            };

            string contents = String.Join("\n", lines);

            List <Chat> chats = ChatParser.ParseText(contents, NO_VALIDATORS);
            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            Command finder = chats[0].commands[0]; finder.Resolve(null);

            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Find)));

            chats = new ChatRuntime(chats).DoFindAll((Find)finder, null);
            //chats.ForEach((obj) => Console.WriteLine(obj.text));

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));

            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c2"));

            lines = new string[] {
                "CHAT c0 {day=sunday}",
                "FIND {emotion *= (hot|cool|blah), !day*=(fri|wed)}",
                "CHAT c1 {emotion=wet, day = wed }",
                "CHAT c2 {emotion=hot,day=fri}",
                "CHAT c3 {emotion=dry}",
                "CHAT c4 {emotion=cool,day=tues}",
            };

            contents = String.Join("\n", lines);

            chats = ChatParser.ParseText(contents, NO_VALIDATORS);
            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            finder = chats[0].commands[0]; finder.Resolve(null);
            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Find)));


            chats = new ChatRuntime(chats).DoFindAll((Find)finder, null);
            //chats.ForEach((obj) => Console.WriteLine(obj.text));

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));

            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c2"));
        }
Пример #8
0
        public void FindAllOpsHard()
        {
            string[] lines =
            {
                "CHAT c0",
                "FIND {!dev>1,day=fri}",
                "CHAT c1 {dev=2,day=fri}",
                "CHAT c2 {dev=1,day=fri}",
                "CHAT c3 {}"
            };

            string contents = String.Join("\n", lines);

            List <Chat> chats = ChatParser.ParseText(contents, NO_VALIDATORS);
            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            Command finder = chats[0].commands[0]; finder.Resolve(null);

            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Find)));
            ChatRuntime cr = new ChatRuntime(chats);

            chats = cr.DoFindAll((Find)finder, null);
            //chats.ForEach((obj) => Console.WriteLine(obj.text));

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));

            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c1"));

            lines = new[] {
                "CHAT c0",
                "FIND {dev<2,!day=fri}",
                "CHAT c1 {dev=2,day=fri}",
                "CHAT c2 {dev=1,day=fri}",
                "CHAT c3 {}"
            };

            contents = String.Join("\n", lines);

            chats = ChatParser.ParseText(contents, NO_VALIDATORS);
            //chats.ForEach((ch) => Console.WriteLine(ch.ToTree()));
            finder = chats[0].commands[0]; finder.Resolve(null);
            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Find)));
            cr = new ChatRuntime(chats);

            chats = cr.DoFindAll((Find)finder, null);
            //chats.ForEach((obj) => Console.WriteLine(obj.text));

            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));

            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c2"));
        }
Пример #9
0
        public void ParsedVar()
        {
            Chat chat = ChatParser.ParseText("SAY Thank $count")[0];

            Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Say)));
            Command say = chat.commands[0];

            Assert.That(say.GetType(), Is.EqualTo(typeof(Say)));
            say.Resolve(globals);
            Assert.That(say.Text(), Is.EqualTo("Thank 4"));
            Assert.That(say.Resolved(Meta.TYPE), Is.EqualTo("Say"));
        }
Пример #10
0
        public void TextGroup()
        {
            var  ok   = new string[] { "The boy was sad", "The boy was happy", "The boy was dead" };
            Chat chat = ChatParser.ParseText("SAY The boy was (sad | happy | dead)")[0];

            Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Say)));
            Command c = (Say)chat.commands[0];

            Assert.That(c.GetType(), Is.EqualTo(typeof(Say)));
            c.Resolve(globals);
            Assert.That(c.Resolved(Meta.TYPE), Is.EqualTo("Say"));
            CollectionAssert.Contains(ok, c.Text());
        }
Пример #11
0
        public static void Main(string[] args)
        {
            //List<Chat> chats = ChatParser.ParseText("PACE 13\nSAY Hello\n");
            List <Chat> chats = ChatParser.ParseFile("gscript.gs");
            ChatRuntime cm    = new ChatRuntime(chats);

            ConsoleClient cl = new ConsoleClient(); // Example client

            cl.Subscribe(cm);                       // Client subscribes to chat events
            cm.Subscribe(cl);                       // Dialogic subscribes to Unity events

            cm.Run();
        }
Пример #12
0
        public void FindAll()
        {
            string[] lines =
            {
                "CHAT c1 {day=fri}",
                "CHAT c2 {dev=2,day=fri}",
                "CHAT c3 {}"
            };
            string      contents = String.Join("\n", lines);
            List <Chat> chats    = ChatParser.ParseText(contents, NO_VALIDATORS);
            List <Chat> result   = new ChatRuntime(chats).DoFindAll(null,
                                                                    new Constraint("dev", "1"),
                                                                    new Constraint("day", "fri")
                                                                    );

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Count, Is.EqualTo(2));
            Assert.That(result[0].text, Is.EqualTo("c1"));

            Chat c;

            chats       = new List <Chat>();
            chats.Add(c = Chat.Create("c1"));
            chats.Add(c = Chat.Create("c2"));
            c.SetMeta("dev", "1");
            c.SetMeta("day", "hello");
            chats.Add(c = Chat.Create("c3"));
            ChatRuntime cr = new ChatRuntime(chats);

            chats = cr.DoFindAll(null, new Constraint("dev", "1"));
            //chats.ForEach((obj) => Console.WriteLine(obj.text));
            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(3));
            Assert.That(chats[0].text, Is.EqualTo("c2"));

            chats       = new List <Chat>();
            chats.Add(c = Chat.Create("c1"));
            c.SetMeta("dev", "2");
            chats.Add(c = Chat.Create("c2"));
            c.SetMeta("dev", "1");
            c.SetMeta("day", "hello");
            chats.Add(c = Chat.Create("c3"));

            cr    = new ChatRuntime(chats);
            chats = cr.DoFindAll(null, new Constraint("dev", "1"));
            //chats.ForEach((obj) => Console.WriteLine(obj.text));
            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(2));
            Assert.That(chats[0].text, Is.EqualTo("c2"));
            Assert.That(chats[1].text, Is.EqualTo("c3"));
        }
Пример #13
0
        public void ReplacePrompt()
        {
            List <Chat> chats = ChatParser.ParseText("ASK Want a $animal?\nOPT $group #Game\n\nOPT $count #End");

            Assert.That(chats.Count, Is.EqualTo(1));
            Assert.That(chats[0].Count, Is.EqualTo(1));
            Assert.That(chats[0].GetType(), Is.EqualTo(typeof(Chat)));
            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Ask)));

            Ask ask = (Ask)chats[0].commands[0];

            Assert.That(ask.text, Is.EqualTo("Want a $animal?"));
            ask.Resolve(globals);

            Assert.That(ask.text, Is.EqualTo("Want a $animal?"));
            Assert.That(ask.Text(), Is.EqualTo("Want a dog?"));
            Assert.That(ask.Options().Count, Is.EqualTo(2));

            var options = ask.Options();

            Assert.That(options[0].GetType(), Is.EqualTo(typeof(Opt)));
            //Assert.That(options[0].Text, Is.EqualTo("Y").Or.);
            CollectionAssert.Contains(new string[] { "a", "b" }, options[0].Text());
            Assert.That(options[0].action.GetType(), Is.EqualTo(typeof(Go)));
            Assert.That(options[1].GetType(), Is.EqualTo(typeof(Opt)));
            Assert.That(options[1].Text(), Is.EqualTo("4"));
            Assert.That(options[1].action.GetType(), Is.EqualTo(typeof(Go)));


            chats = ChatParser.ParseText("ASK Want a $obj-prop?\nOPT $group #Game\n\nOPT $count #End");
            Assert.That(chats.Count, Is.EqualTo(1));
            Assert.That(chats[0].Count, Is.EqualTo(1));
            Assert.That(chats[0].GetType(), Is.EqualTo(typeof(Chat)));
            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Ask)));

            ask = (Ask)chats[0].commands[0];
            ask.Resolve(globals);
            Assert.That(ask.text, Is.EqualTo("Want a $obj-prop?"));
            Assert.That(ask.Text(), Is.EqualTo("Want a dog?"));

            Assert.That(ask.Options().Count, Is.EqualTo(2));

            options = ask.Options();
            Assert.That(options[0].GetType(), Is.EqualTo(typeof(Opt)));
            //Assert.That(options[0].Text, Is.EqualTo("Y").Or.);
            CollectionAssert.Contains(new string[] { "a", "b" }, options[0].Text());
            Assert.That(options[0].action.GetType(), Is.EqualTo(typeof(Go)));
            Assert.That(options[1].GetType(), Is.EqualTo(typeof(Opt)));
            Assert.That(options[1].Text(), Is.EqualTo("4"));
            Assert.That(options[1].action.GetType(), Is.EqualTo(typeof(Go)));
        }
Пример #14
0
        public void Staleness()
        {
            Chat        c;
            List <Chat> chats = new List <Chat>();

            chats.Add(c = Chat.Create("c1"));
            c.SetMeta("dev", "hello");
            c.Staleness(2);
            chats.Add(c = Chat.Create("c2"));
            c.SetMeta("dev", "2");
            c.Staleness(3);
            chats.Add(c = Chat.Create("c3"));
            c.SetMeta("dev", "3");
            c.Staleness(4);
            var res = new ChatRuntime(chats).DoFindAll
                          (null, new Constraint(Operator.LT, "staleness", "3"));

            Assert.That(res.Count, Is.EqualTo(1));
            Assert.That(res[0].text, Is.EqualTo("c1"));

            string[] lines =
            {
                "CHAT c0",
                "FIND {dev=1,day=fri,staleness<5}",
                "CHAT c1 {day=fri}",
                "CHAT c2 {dev=2,day=fri}",
                "CHAT c3 {}"
            };

            string contents = String.Join("\n", lines);

            chats = ChatParser.ParseText(contents, NO_VALIDATORS);

            Command finder = chats[0].commands[0]; finder.Resolve(null);

            Assert.That(finder.GetType(), Is.EqualTo(typeof(Find)));

            var crt = new ChatRuntime(chats);

            c = crt.FindChatByLabel("#c1");
            c.Staleness(6);
            Assert.That(c, Is.Not.Null);
            Assert.That(c.text, Is.EqualTo("c1"));

            chats = crt.DoFindAll((Find)finder, null);
            Assert.That(chats, Is.Not.Null);
            Assert.That(chats.Count, Is.EqualTo(1));
            Assert.That(chats[0], Is.Not.Null);
            Assert.That(chats[0].text, Is.EqualTo("c3"));
        }
Пример #15
0
        public void TimingTest()
        {
            Say fast = (Say)ChatParser.ParseText("SAY Thank you { speed=fast}")[0].commands[0];
            Say defa = (Say)ChatParser.ParseText("SAY Thank you")[0].commands[0];
            Say slow = (Say)ChatParser.ParseText("SAY Thank you{speed=slow }")[0].commands[0];

            Assert.That(defa.ComputeDuration(), Is.EqualTo(fast.ComputeDuration() * 2).Within(1));
            Assert.That(slow.ComputeDuration(), Is.EqualTo(defa.ComputeDuration() * 2).Within(1));
            Say longer = (Say)ChatParser.ParseText("SAY Thank you very much")[0].commands[0];

            Assert.That(longer.ComputeDuration(), Is.GreaterThan(defa.ComputeDuration()));
            Assert.That(fast.text, Is.EqualTo(defa.text));
            Assert.That(slow.text, Is.EqualTo(defa.text));
        }
Пример #16
0
        protected static List <Chat> Parse(string[] lines)
        {
            HandleDefaultCommand(lines, "SAY");
            var            ais    = new AntlrInputStream(String.Join("\n", lines));
            DialogicParser parser = CreateParser(ais);

            parser.ErrorHandler = new BailErrorStrategy();
            ParserRuleContext prc = parser.script();
            ChatParser        cp  = new ChatParser();

            cp.Visit(prc);
            PrintLispTree(parser, prc);
            Console.WriteLine(cp);
            return(cp.chats);
        }
Пример #17
0
        public void TextVar()
        {
            Chat chat = ChatParser.ParseText("SAY Thank $count { pace=$animal}")[0];

            Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Say)));
            Command say = chat.commands[0];

            Assert.That(say.GetType(), Is.EqualTo(typeof(Say)));
            say.Resolve(globals);
            Assert.That(say.Text(), Is.EqualTo("Thank 4"));
            say.text = "Thank you";
            Assert.That(say.text, Is.EqualTo("Thank you"));
            Assert.That(say.Resolved(Meta.TYPE), Is.EqualTo("Say"));
            Assert.That(say.Resolved("pace"), Is.EqualTo("dog"));
        }
Пример #18
0
        public void GroupRecursionDepth()
        {
            var str = "CHAT c\n(I | (You | (doh | why | no) | ouY) | They)";

            str += "(want | hate | like | love)(coffee | bread | milk)";
            Chat chat = ChatParser.ParseText(str, NO_VALIDATORS)[0];

            Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Say)));
            Say say = (Dialogic.Say)chat.commands[0];

            say.Resolve(null);
            var s = say.Text();

            Assert.That(s, Is.Not.EqualTo(""));
        }
Пример #19
0
        /// <summary>
        /// Parse chat definitions from a file (or folder of files) ending with
        /// '*.gs', or the specified extension
        /// </summary>
        /// <param name="fileOrFolder">File (or folder of files) to parse</param>
        /// <param name="disableValidators">If set to <c>true</c> disable app-specific validators (default=false).</param>
        /// <param name="fileExt">File extension to load (empty-string for all files, default is ".gs")</param>
        public void ParseFile(FileInfo fileOrFolder,
                              bool disableValidators = false, string fileExt = null)
        {
            var file  = fileOrFolder.FullName;
            var files = Directory.Exists(file) ? Directory.GetFiles
                            (file, '*' + (fileExt ?? ".gs")) : new[] { file };

            this.validatorsDisabled = disableValidators;

            foreach (var f in files)
            {
                var text = File.ReadAllText(f);
                parser.Parse(ChatParser.StripComments(text));
            }
            //Chats().ForEach(c => Console.WriteLine(c.ToTree()));
        }
Пример #20
0
        /// <summary>
        /// Clear all chats and reset the runtime state
        /// </summary>
        public void Reset(List <Chat> theChats = null)
        {
            this.resolver    = new Resolver(this);
            this.parser      = new ChatParser(this);
            this.search      = new FuzzySearch(this);
            this.scheduler   = new ChatScheduler(this);
            this.appEvents   = new AppEventHandler(this);
            this.chatEvents  = new ChatEventHandler(this);
            this.chats       = new Dictionary <string, Chat>();
            this.choiceCache = new Dictionary <string, Choice>();

            if (!theChats.IsNullOrEmpty())
            {
                AppendChats(theChats);
            }
        }
Пример #21
0
        public void ComplexPlusTransform()
        {
            // "cmplx" -> "($group | $prep)"
            // "prep"  -> "then" },
            // "group" -> "(a|b)" },
            string[] ok   = { "letter an a", "letter a b", "letter a then" };
            Chat     chat = ChatParser.ParseText("SAY letter $cmplx.articlize()")[0];
            Command  c    = chat.commands[0];

            Assert.That(c.GetType(), Is.EqualTo(typeof(Say)));
            Resolver.DBUG = false;
            for (int i = 0; i < 1; i++)
            {
                c.Resolve(globals);
                var txt = c.Text();
                Assert.That(txt.IsOneOf(ok));
            }
        }
Пример #22
0
        public void ComplexReplacement()
        {
            // "cmplx" -> "($group | $prep)"
            // "prep"  -> "then" },
            // "group" -> "(a|b)" },
            string[] ok   = { "letter a", "letter b", "letter then" };
            Chat     chat = ChatParser.ParseText("SAY letter $cmplx")[0];
            Command  c    = chat.commands[0];

            Assert.That(c.GetType(), Is.EqualTo(typeof(Say)));
            for (int i = 0; i < 10; i++)
            {
                c.Resolve(globals);
                var txt = c.Text();
                //Console.WriteLine(i+") "+txt);
                CollectionAssert.Contains(ok, txt);
            }
        }
Пример #23
0
        //[Test]
        public void ResumeEventTest()
        {
            string[] lines =
            {
                "CHAT c1 {type=a}",
                "CHAT c2 {type=b,day=fri}",
                "CHAT c3 {type=c,day=thurs}",
            };

            string      contents = String.Join("\n", lines);
            List <Chat> chats    = ChatParser.ParseText(contents, NO_VALIDATORS);
            ChatRuntime rt       = new ChatRuntime(chats);

            for (int i = 0; i < 3; i++)
            {
                EventArgs icu = new ResumeEvent("{}");
                rt.Update(null, ref icu);
            }
        }
Пример #24
0
        public void SayNonRepeatingRecomb()
        {
            List <Chat> chats = ChatParser.ParseText("SAY (a|b|c)");

            Assert.That(chats.Count, Is.EqualTo(1));
            Assert.That(chats[0].Count, Is.EqualTo(1));
            Assert.That(chats[0].GetType(), Is.EqualTo(typeof(Chat)));
            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Say)));
            Say    say  = (Say)chats[0].commands[0];
            string last = "";

            for (int i = 0; i < 10; i++)
            {
                say.Resolve(globals);
                string said = say.Text();
                //System.Console.WriteLine(i+") "+said);
                Assert.That(said, Is.Not.EqualTo(last));
                last = said;
            }
        }
Пример #25
0
        public void FindAllWithMetaVars()
        {
            string[] lines =
            {
                "CHAT c1 {day=fri}", // parent not returned
                "CHAT c2 {dev=4,day=fri}",
                "CHAT c3 {dev=3}",
                "CHAT c4"
            };
            string      contents = String.Join("\n", lines);
            List <Chat> chats    = ChatParser.ParseText(contents, NO_VALIDATORS);

            Assert.That(globals["count"], Is.EqualTo(4));
            List <Chat> result = new ChatRuntime(chats).DoFindAll
                                     (chats[0], globals, new Constraint("dev", "$count"));

            //chats.ForEach((obj) => Console.WriteLine(obj.text));
            Assert.That(result, Is.Not.Null);
            Assert.That(result.Count, Is.EqualTo(2));
            Assert.That(result[0].text, Is.EqualTo("c2"));
        }
Пример #26
0
        public void SetGlobalsOnPath()
        {
            var  code = "CHAT c1\nSET $fish.name=Mary\nSAY Hi $fish.name";
            Chat chat = ChatParser.ParseText(code, null)[0];

            Assert.That(chat, Is.Not.Null);
            Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Set)));
            Assert.That(chat.commands[1].GetType(), Is.EqualTo(typeof(Say)));

            Set set = (Dialogic.Set)chat.commands[0];

            Assert.That(set.text, Is.EqualTo("fish.name"));
            Assert.That(set.value, Is.EqualTo("Mary"));

            Say say = (Dialogic.Say)chat.commands[1];

            Assert.That(say.text, Is.EqualTo("Hi $fish.name"));

            chat.Resolve(globals);
            Assert.That(say.Text(), Is.EqualTo("Hi Mary"));
        }
Пример #27
0
        internal Chat ToGameObject(ChatRuntime rt)
        {
            ChatParser  parser = rt.Parser();
            LineContext lc     = new LineContext(parser, null, "CHAT", text, null, meta);
            Chat        chat   = (Dialogic.Chat)parser.CreateCommand(lc);

            chat.Staleness(this.staleness);
            chat.Resumable(this.resumable);
            chat.Interruptable(this.interruptable);
            chat.ResumeAfterInterrupting(this.resumeAfterInt);
            chat.StalenessIncr(this.stalenessIncr);

            chat.cursor    = this.cursor;
            chat.lastRunAt = this.lastRunAt;
            chat.allowSmoothingOnResume = this.allowSmoothingOnResume;

            chat.commands = new List <Command>(this.commands.Count);
            this.commands.ForEach(cmd => cmd.ToGameObject(rt));

            return(chat);
        }
Пример #28
0
        public void MetaReplaceGroups()
        {
            List <Chat> chats = ChatParser.ParseText("DO emote {type=(A|B)}");

            Assert.That(chats.Count, Is.EqualTo(1));
            Assert.That(chats[0].Count, Is.EqualTo(1));
            Assert.That(chats[0].GetType(), Is.EqualTo(typeof(Chat)));
            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Do)));
            Do doo = (Do)chats[0].commands[0];

            Assert.That(doo.text, Is.EqualTo("emote"));
            Assert.That(doo.HasMeta("type"), Is.EqualTo(true));
            Assert.That(doo.GetMeta("type"), Is.EqualTo("(A|B)"));

            for (int i = 0; i < 10; i++)
            {
                doo.Resolve(globals);
                Assert.That(doo.Text(), Is.EqualTo("emote"));
                //Console.WriteLine(doo.GetResolved("type"));
                Assert.That(doo.Resolved("type"), Is.EqualTo("A").Or.EqualTo("B"));
            }
        }
Пример #29
0
        public void MetaReplaceValue()
        {
            Chat chat = ChatParser.ParseText("SAY Thank you { pace = fast}")[0];

            Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Say)));
            Command c = (Say)chat.commands[0];

            Assert.That(c.GetType(), Is.EqualTo(typeof(Say)));
            c.Resolve(globals);
            c.SetMeta("pace", "slow");

            Assert.That(c.Text(), Is.EqualTo("Thank you"));
            Assert.That(c.Resolved(Meta.TYPE), Is.EqualTo("Say"));
            Assert.That(c.Resolved("pace"), Is.EqualTo("fast"));
            Assert.That(c.GetMeta("pace"), Is.EqualTo("slow"));

            chat = ChatParser.ParseText("SAY Thank you { pace=$animal}")[0];
            Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Say)));
            c = (Say)chat.commands[0];
            Assert.That(c.GetType(), Is.EqualTo(typeof(Say)));
            c.Resolve(globals);
            c.SetMeta("pace", "slow");
            Assert.That(c.Resolved(Meta.TEXT), Is.EqualTo("Thank you"));
            Assert.That(c.Resolved(Meta.TYPE), Is.EqualTo("Say"));
            Assert.That(c.Resolved("pace"), Is.EqualTo("dog"));
            Assert.That(c.GetMeta("pace"), Is.EqualTo("slow"));

            chat = ChatParser.ParseText("SAY Thank you { pace=$obj-prop}")[0];
            Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Say)));
            c = (Say)chat.commands[0];
            Assert.That(c.GetType(), Is.EqualTo(typeof(Say)));
            c.Resolve(globals);
            c.SetMeta("pace", "slow");
            Assert.That(c.Resolved(Meta.TEXT), Is.EqualTo("Thank you"));
            Assert.That(c.Resolved(Meta.TYPE), Is.EqualTo("Say"));
            Assert.That(c.Resolved("pace"), Is.EqualTo("dog"));
            Assert.That(c.GetMeta("pace"), Is.EqualTo("slow"));
        }
Пример #30
0
        public void AskNonRepeatingRecomb()
        {
            List <Chat> chats = ChatParser.ParseText("ASK (a|b|c)?\nOPT (c|d|e) #f");

            Assert.That(chats.Count, Is.EqualTo(1));
            //Console.WriteLine(chats[0].ToTree());
            Assert.That(chats[0].GetType(), Is.EqualTo(typeof(Chat)));
            Assert.That(chats[0].commands[0].GetType(), Is.EqualTo(typeof(Ask)));
            Ask    ask = (Ask)chats[0].commands[0];
            string last = "", lastOpts = "";

            for (int i = 0; i < 10; i++)
            {
                ask.Resolve(globals);
                string asked = ask.Text();
                string opts  = ask.JoinOptions();
                //Console.WriteLine(i+") "+asked+" "+opts);
                Assert.That(asked, Is.Not.EqualTo(last));
                Assert.That(opts, Is.Not.EqualTo(lastOpts));
                lastOpts = opts;
                last     = asked;
            }
        }