Exemplo n.º 1
0
        internal static Command Create(Type type, string txt, string lbl, string[] metas)
        {
            //Console.WriteLine("'"+type + "' '"+txt+ "' '"+ lbl+"' "+metas.Stringify());

            Command cmd = null;

            // ---------------- optimization ----------------
            if (type == typeof(Chat))
            {
                cmd = new Chat();
            }
            else if (type == typeof(Say))
            {
                cmd = new Say();
            }
            else if (type == typeof(Set))
            {
                cmd = new Set();
            }
            else if (type == typeof(Ask))
            {
                cmd = new Ask();
            }
            else if (type == typeof(Opt))
            {
                cmd = new Opt();
            }
            else if (type == typeof(Do))
            {
                cmd = new Do();
            }
            else if (type == typeof(Go))
            {
                cmd = new Go();
            }
            else if (type == typeof(Wait))
            {
                cmd = new Wait();
            }
            else if (type == typeof(Find))
            {
                cmd = new Find();
            }
            // ----------------- end opt -------------------

            else
            {
                cmd = (Command)Activator.CreateInstance(type);
            }

            cmd.Init(txt, lbl, metas);

            return(cmd);
        }
Exemplo n.º 2
0
        public override Chat VisitLine([NotNull] DialogicParser.LineContext context)
        {
            var cmd   = context.GetChild <DialogicParser.CommandContext>(0).GetText();
            var actx  = context.GetChild <DialogicParser.ArgsContext>(0);
            var xargs = actx.children.Where(arg => arg is DialogicParser.ArgContext).ToArray();
            var args  = Array.ConvertAll(xargs, arg => arg.GetText().Trim());

            //Console.WriteLine("cmd: " + cmd + " args: '" + String.Join(",",args)    + "'");

            Command c = Command.Create(cmd, args);

            if (c is Chat)
            {
                chats.Add((Chat)c);
            }
            else if (c is Opt)
            {
                Opt     o    = (Opt)c;
                Command last = LastOfType(parsed, typeof(Ask));
                if (!(last is Ask))
                {
                    throw new Exception("Opt must follow Ask");
                }
                ((Ask)last).AddOption(o);
            }
            else
            {
                if (chats.Count == 0)
                {
                    chats.Add(new Chat());
                }
                chats.Last().AddCommand(c);
            }

            parsed.Push(c);

            return(VisitChildren(context));
        }
Exemplo n.º 3
0
        private IUpdateEvent ChoiceHandler(ref EventArgs ea, IDictionary <string, object> globals)
        {
            IChoice ic  = (IChoice)ea;
            var     idx = ic.GetChoiceIndex();

            ea = null;

            if (idx < 0 || idx >= scheduler.prompt.Options().Count)
            {
                // bad index, so reprompt for now
                ChatRuntime.Info("Invalid index " + idx + ", reprompting\n");

                return(new UpdateEvent(scheduler.prompt.Resolve(globals)));
            }
            else
            {
                Opt opt = scheduler.prompt.Selected(idx);

                if (opt.action != Command.NOP)
                {
                    // We've gotten a response with a branch, so finish & take it
                    scheduler.Completed(false);
                    opt.action.Resolve(globals);
                    runtime.FindAsync((Find)opt.action); // find next

                    // Question: if the Chat is unfinished (and resumable)
                    // shouldn't we return to it later? See #154
                }
                else
                {
                    // No action chosen so continue the current chat
                    scheduler.Resume();
                }

                return(null);
            }
        }
Exemplo n.º 4
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();
                    opt.Init(parts[0], Ch.LABEL + parts[1], null);
                    ask.AddOption(opt);
                }
            }
            return(cmd);
        }
Exemplo n.º 5
0
 protected internal void AddOption(Opt o)
 {
     // set parent for Opt and Opt.action
     o.parent = o.action.parent = this.parent;
     options.Add(o);
 }
Exemplo n.º 6
0
 public void AddOption(Opt o)
 {
     options.Add(o);
 }