Ejemplo n.º 1
0
 /// <summary>
 /// Called when somebody uses this command.
 /// Type can be ignored if this is not a default handler.
 /// </summary>
 public abstract void Handle(CommandTarget target, string type, string[] parameters);
Ejemplo n.º 2
0
        public static bool Handle(CommandTarget target, string message, string commandHeader)
        {
            if (string.IsNullOrWhiteSpace(message) || !message.StartsWith(commandHeader))
                return false;

            try
            {
                var commandStr = message.Substring(commandHeader.Length);

                if (string.IsNullOrWhiteSpace(commandStr))
                    return false;

                var reader = new StringReader(commandStr);
                var type = (ReadWord(reader) ?? "").ToLower();
                Command command = null;
                string format = null;

                Func<string, bool> tryResolve = s =>
                {
                    if (!Commands.TryGetValue(s, out command))
                        return false;

                    format = command.Format(target, type);
                    if (format == null)
                        return false;

                    return true;
                };

                // TODO: is there a better way to do this?
                if (!target.IsRoom || !tryResolve(target.Room.CommandPrefix + type))
                    if (!target.IsRoom || !tryResolve(target.Room.CommandPrefix))
                        if (!tryResolve(type))
                            if (!tryResolve(""))
                                return true;

                var parameters = new List<string>();

                foreach (var p in format)
                {
                    var param = "";

                    switch (p)
                    {
                        case '-':
                            param = ReadWord(reader);
                            break;
                        case ']':
                            param = ReadRemaining(reader);
                            break;
                    }

                    if (param == null)
                        break;

                    parameters.Add(param);
                }

                command.Handle(target, type, parameters.ToArray());
                return true;
            }
            catch (Exception e)
            {
                if (target != null)
                    target.Send("Command failed.");

                Program.Logger.Error("Command failed: ", e);
                return true;
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Return a parameter format string. '-' is a short parameter and ']' uses remaining space.
 /// Returns null if it can not handle type.
 /// Target and type can be ignored if this is not a default handler.
 /// Examples:
 ///   ""        = No parameters
 ///   "-"       = One short parameter (word or text enclosed in double quotes)
 ///   "]"       = One parameter containing all text after Type
 ///   "--]"     = Two short parameters and one parameter containing the leftovers
 /// </summary>
 public abstract string Format(CommandTarget target, string type);