Exemplo n.º 1
0
        public async Task SaveRegion(SmartRegion region)
        {
            var command = new SQLiteCommand(
                "INSERT OR REPLACE INTO Regions (Name, Command, Cooldown) VALUES (@nm, @cmd, @cool)",
                Connection);

            command.Parameters.Add(new SQLiteParameter("@nm", region.name));
            command.Parameters.Add(new SQLiteParameter("@cmd", region.command));
            command.Parameters.Add(new SQLiteParameter("@cool", region.cooldown));
            await command.ExecuteNonQueryAsync();
        }
Exemplo n.º 2
0
 public void Reset()
 {
     cooldowns       = new Dictionary <SmartRegion, DateTime>();
     regionToReplace = null;
 }
Exemplo n.º 3
0
        public void regionCommand(CommandArgs args)
        {
            switch (args.Parameters.ElementAtOrDefault(0))
            {
            case "add":
            {
                if (args.Parameters.Count < 4)
                {
                    args.Player.SendErrorMessage("Invalid syntax! Correct syntax: /smartregion add <region name> <cooldown> <command or file>");
                }
                else
                {
                    double cooldown = 0;
                    if (!double.TryParse(args.Parameters[2], out cooldown))
                    {
                        args.Player.SendErrorMessage("Invalid syntax! Correct syntax: /smartregion add <region name> <cooldown> <command or file>");
                        return;
                    }
                    string command = string.Join(" ", args.Parameters.GetRange(3, args.Parameters.Count - 3));
                    if (!TShock.Regions.Regions.Exists(x => x.Name == args.Parameters[1]))
                    {
                        args.Player.SendErrorMessage("The region {0} doesn't exist!", args.Parameters[1]);
                        IEnumerable <string> regionNames = from region_ in TShock.Regions.Regions
                                                           where region_.WorldID == Main.worldID.ToString()
                                                           select region_.Name;
                        PaginationTools.SendPage(args.Player, 1, PaginationTools.BuildLinesFromTerms(regionNames),
                                                 new PaginationTools.Settings
                            {
                                HeaderFormat           = "Regions ({0}/{1}):",
                                FooterFormat           = "Type {0}region list {{0}} for more.".SFormat(Commands.Specifier),
                                NothingToDisplayString = "There are currently no regions defined."
                            });
                    }
                    else
                    {
                        string cmdName = "";
                        for (int i = 1; i < command.Length && command[i] != ' '; i++)
                        {
                            cmdName += command[i];
                        }
                        Command cmd = Commands.ChatCommands.FirstOrDefault(c => c.HasAlias(cmdName));
                        if (cmd != null && !cmd.CanRun(args.Player))
                        {
                            args.Player.SendErrorMessage("You cannot create a smart region with a command you don't have permission to use yourself!");
                            return;
                        }
                        if (cmd != null && !cmd.AllowServer)
                        {
                            args.Player.SendErrorMessage("Your command must be usable by the server!");
                            return;
                        }

                        var existingRegion = regions.FirstOrDefault(x => x.region == args.Parameters[1]);
                        var newRegion      = new SmartRegion {
                            region = args.Parameters[1], cooldown = cooldown, command = command
                        };
                        if (existingRegion != null)
                        {
                            players[args.Player.Index].regionToReplace = newRegion;
                            args.Player.SendErrorMessage("The smart region {0} already exists! Type /replace to replace it.", args.Parameters[1]);
                        }
                        else
                        {
                            regions.Add(newRegion);
                            args.Player.SendSuccessMessage("Smart region added!");
                            saveConfig();
                        }
                    }
                }
            }
            break;

            case "remove":
            {
                if (args.Parameters.Count != 2)
                {
                    args.Player.SendErrorMessage("Invalid syntax! Correct syntax: /smartregion remove <regionname>");
                }
                else
                {
                    var region = regions.FirstOrDefault(x => x.region == args.Parameters[1]);
                    if (region == null)
                    {
                        args.Player.SendErrorMessage("No such smart region exists!");
                    }
                    else
                    {
                        regions.Remove(region);
                        args.Player.SendSuccessMessage("The smart region {0} was removed!", args.Parameters[1]);
                        saveConfig();
                    }
                }
            }
            break;

            case "check":
            {
                if (args.Parameters.Count != 2)
                {
                    args.Player.SendErrorMessage("Invalid syntax! Correct syntax: /smartregion check <regionname>");
                }
                else
                {
                    var region = regions.FirstOrDefault(x => x.region == args.Parameters[1]);
                    if (region == null)
                    {
                        args.Player.SendInfoMessage("That region doesn't have a command associated with it.");
                    }
                    else
                    {
                        string file = Path.Combine(TShock.SavePath, "SmartRegions", region.command), commands;
                        if (File.Exists(file))
                        {
                            commands = "s:\n" + File.ReadAllText(file);
                        }
                        else
                        {
                            commands = ":\n" + region.command;
                        }
                        args.Player.SendInfoMessage("The region {0} has a cooldown of {1} second{2} and uses the command{3}", args.Parameters[1], region.cooldown, region.cooldown == 1.0 ? "" : "s", commands);
                    }
                }
            }
            break;

            case "list":
            {
                args.Player.SendInfoMessage("Smart regions ({0}):", regions.Count);
                int pageNumber = 1;
                if (args.Parameters.Count > 1)
                {
                    int.TryParse(args.Parameters[1], out pageNumber);
                }
                PaginationTools.SendPage(
                    args.Player, pageNumber, PaginationTools.BuildLinesFromTerms(regions.ConvertAll(x => x.region)),
                    new PaginationTools.Settings
                    {
                        IncludeHeader = false,
                        FooterFormat  = string.Format("Type {0}smartregion list {{0}} for more.", Commands.Specifier)
                    }
                    );
            }
            break;

            default:
            {
                args.Player.SendInfoMessage("/smartregion sub-commands:\nadd <region name> <cooldown> <command or file>\nremove <region name>\ncheck <region name>\nlist");
            }
            break;
            }
        }