Example #1
0
        public static void parseCommand(scPlayer sPly, string Text, scCommand command, bool CanBypassPermissions)
        {
            List<string> Parameters = parseParameters(Text, true);
            Parameters.RemoveAt(0); //remove the cmd alias (/cmd)

            List<Command> validCommands = new List<Command>();
            List<string> validText = new List<string>();
            List<List<string>> validArgs = new List<List<string>>();

            bool IsError = false;
            int maxParameters = 0;
            List<string> invalidCommands = new List<string>();
            List<string> cantRun = new List<string>();
            List<string> formatException = new List<string>();
            foreach (var executeCmd in command.commands)
            {
                //Commands.HandleCommand(sPly.tsPly, string.Format(cmd, Parameters));

                //Custom Handle:
                #region Custom Handle
                string cmdText = executeCmd.Remove(0, 1);

                int maxParams = maxFormat(cmdText) + 1;
                if (Parameters.Count >= maxParams)
                {
                    try
                    {
                        cmdText = string.Format(cmdText, Parameters.ToArray());
                    }
                    catch (FormatException)
                    {
                        formatException.Add(string.Concat("/", (cmdText.Contains(' ') ? cmdText.Split(' ')[0] : cmdText)));
                        continue;
                    }
                }
                else
                {
                    IsError = true;
                    if (maxParams > maxParameters)
                        maxParameters = maxParams;
                    continue;
                }

                if (cmdText.Contains("%account") && !sPly.tsPly.IsLoggedIn)
                {
                    sPly.tsPly.SendWarningMessage("You must be logged in to execute that command");
                    return;
                }
                cmdText = cmdText.Replace("%name", string.Format("\"{0}\"", sPly.tsPly.Name));
                cmdText = cmdText.Replace("%account", string.Format("\"{0}\"", sPly.tsPly.UserAccountName));
                cmdText = cmdText.Replace("%group", string.Format("\"{0}\"", sPly.tsPly.Group.Name));

                var args = parseParameters(cmdText);
                if (args.Count < 1 || string.IsNullOrWhiteSpace(cmdText))
                    continue;

                string cmdName = args[0].ToLower();
                args.RemoveAt(0);

                IEnumerable<Command> cmds = Commands.ChatCommands.Where(c => c.HasAlias(cmdName));

                if (cmds.Count() == 0)
                {
                    if (sPly.tsPly.AwaitingResponse.ContainsKey(cmdName))
                    {
                        Action<CommandArgs> call = sPly.tsPly.AwaitingResponse[cmdName];
                        sPly.tsPly.AwaitingResponse.Remove(cmdName);
                        call(new CommandArgs(cmdText, sPly.tsPly, args));
                        continue;
                    }
                    IsError = true;
                    invalidCommands.Add(string.Concat("/", (cmdText.Contains(' ') ? cmdText.Split(' ')[0] : cmdText)));
                    continue;
                }
                foreach (Command cmd in cmds)
                {
                    if (!CanBypassPermissions && !cmd.CanRun(sPly.tsPly))
                    {
                        IsError = true;
                        cantRun.Add(string.Concat("/", cmdText));
                        break;

                    }
                    else if (!cmd.AllowServer && !sPly.tsPly.RealPlayer)
                        break;
                    else
                    {
                        validCommands.Add(cmd);
                        validText.Add(cmdText);
                        validArgs.Add(args);
                    }
                }
                #endregion
            }
            if (validCommands.Count > 0)
            {
                handleCommad(sPly, Text, validCommands, validText, validArgs, CanBypassPermissions);
                if (sPly.Cooldowns.ContainsKey(command.alias))
                    sPly.Cooldowns[command.alias] = DateTime.UtcNow;
                else
                    sPly.Cooldowns.Add(command.alias, DateTime.UtcNow);
            }
            if (IsError)
            {
                if (maxParameters > 0)
                    sPly.tsPly.SendErrorMessage("{0} parameter{1} required.".SFormat(maxParameters, (maxParameters > 1 ? "s are" : " is")));
                if (invalidCommands.Count > 0)
                    sPly.tsPly.SendErrorMessage("Invalid command{0} {1}.".SFormat((invalidCommands.Count > 1 ? "s" : string.Empty), string.Join(", ", invalidCommands)));
                if (cantRun.Count > 0)
                    sPly.tsPly.SendErrorMessage("You do not have permission to execute {0}.".SFormat(string.Join(", ", cantRun)));
                if (formatException.Count > 0)
                    sPly.tsPly.SendErrorMessage("Format Exception in command{0} {1}.".SFormat((formatException.Count > 1 ? "s" : string.Empty), string.Join(", ", formatException)));
            }
        }
Example #2
0
 public static bool hasCooldown(scPlayer sPly, scCommand command)
 {
     if (sPly.tsPly.Group.HasPermission("shortcmd.nocooldown") || command.cooldown < 1) return false;
     if (sPly.Cooldowns.ContainsKey(command.alias))
     {
         var seconds = (DateTime.UtcNow - sPly.Cooldowns[command.alias]).TotalSeconds;
         if (seconds < command.cooldown)
         {
             sPly.tsPly.SendMessage("You must wait another {0} seconds before using that command!".SFormat((int)(command.cooldown - seconds)), Color.Red);
             return true;
         }
     }
     return false;
 }