[FSOD] Rank Command

Posts 1–15 of 15 · Page 1 of 1
[FSOD] Rank Command
Hey guys, this'll be my second release for MPGH. In this one, I'll be releasing a /rank command I made. This command is 100% bug-free, and is easily customizable to fit your server. For the sake of the release, I have edited the code to the standard ranks on a FSOD source.

How to use it?: /rank username rankId

How does it work?: I made a comment above the command if you read it'll explain. I'll get a little more in-depth as to why it should be bug free.

So pretty much I have created a basic boolean that checks if a string has the chars 0-9 in it. If they do not, it'll send an message to the user executing the command. Now we have an extra check in place to ensure the rank we are giving is actually a rank. So in FSOD the max rank would be 3 because the hierarchy for ranks goes: Player (0), Donator (1), Admin (2), Owner (3). So we added checks to ensure that the argument being given was a valid rank and if not, would also throw a message to the player.

Now, the player has been given a rank but we need to tell them they have been given a rank and what rank they were given, as well as tell the user who executed the command in case they gave the wrong rank, etc.

Then, I created an Array called ranks to hold all the rank names as well as Parsed the rank argument from a string to an int so it can be used to access the name of the rank in the ranks Array.

Let's get started:

in wServer/realm/commands/AdminCommands.cs, Add the following command:

Code:
    /**
     *    @Author Wilson (AKA: NigrKiller @ MPGH)
     * 
     * Sets any player's rank. 
     * 
     * Iterates through chars 0-9 for our {@code rank} argument to see if it is
     * a valid rank being set. 
     * 
     * Parses our {@code rank} argument into an int and displays the newly 
     * earned rank to the players through our {@code ranks} Array.
     */
    internal class Rank : Command {
        public Rank() : base("rank", 1) { }

        private const int MAX_RANK = 3;

        bool validRank(string rankId) {
            return rankId.All(c => c >= '0' && c <= '9');
        }

        String[] ranks = { "Player", "Donator", "Administrator",
            "Owner" };

        protected override bool Process(Player player, RealmTime time, string[] args) {
            
            var playerToRank = player.Manager.FindPlayer(args[0]);
            string rank = args[1];

            if (playerToRank == null) {
                player.SendError("Player not found! They may be offline.");
                return false;
            }

            if (!validRank(rank) && int.Parse(rank) < 0 && int.Parse(rank)  > MAX_RANK) {
                player.SendError("Invalid Rank! Use: /rank username rankId");
                return false;
            }
        
            player.Manager.Database.DoActionAsync(db => {
                var execute = db.CreateQuery();
                execute.CommandText = "UPDATE accounts SET rank="+ rank +" WHERE id=@accId;";
                execute.Parameters.AddWithValue("@accId", playerToRank.AccountId);
                execute.ExecuteNonQuery();
            });

            playerToRank.SendInfo("You have been ranked "+ ranks[int.Parse(rank)] +" by "+ player.Name +".");
            player.SendInfo("You have given "+ playerToRank.Name +" the rank, "+ ranks[int.Parse(rank)] +".");
            return true;
        }
    }
To edit the command, simply do the following:

This is the default permission on FSOD. It pretty much means if you're rank 3 you can use it. Change it to suit your permissions.

For the MAX_RANK constant, change the value to your max rank on your server.

For the ranks Array, simply add all of the names of your ranks (in order) from 0-?

Note: You MUST add the names of the ranks to the Array if you change the max rank otherwise you will get an ArrayIndexOutOfBounds Exception (Is that what it's called in C#? Again, sorry I come from Java lol)

Conclusion: All in all, I could not find a single bug while testing this, but if you find one I'll be happy to fix it. This works flawlessly on a regular FSOD source so if it doesn't work you're either typing the command wrong or couldn't follow instructions to add this command...
Why not use the regular one that comes with the source?
Quote Originally Posted by Orbit View Post
Why not use the regular one that comes with the source?
For a few reasons...

1) It is literally awful.
2) It is not configured with the current commands and takes a bit of touching up to even make it minimally functional (Which may be too hard for some people)
3) You can rank yourself anything, such as "j", "9999999", "fuckmylife", "thisCommandIsShit", etc..
4) Whoever made that command does not know how to program or is simply lazy and made a shit command

Why to use mine?
1) Handles ALL valid ranks and does not allow you (or others) to give them a rank outside of the ones available.
2) Clean and easy to read code
3) Customizable
4) Easy to set up
5) Literally 100% Bug-free so there's no way you can fuck up anyones ranks using this command.
6) Sends a neat message to the user of the command and the player receiving the rank which tells what rank they were given.

All in all, anyone is pretty much a fool to opt for the vanilla FSOD /rank command over mine. There was a reason it was commented out lol
Seeing that you're aiming for a perfected command, it can be improved by changing
Code:
         player.Manager.Database.DoActionAsync(db => {
                var execute = db.CreateQuery();
                execute.CommandText = "UPDATE accounts SET rank="+ rank +" WHERE id=@accId;";
                execute.Parameters.AddWithValue("@accId", playerToRank.AccountId);
                execute.ExecuteNonQuery();
            });
to,
Code:
         player.Manager.Database.DoActionAsync(db => {
            using (var ex = db.CreateQuery())
               {
                ex.CommandText = "UPDATE accounts SET rank="+ rank +" WHERE id=@accId;";
                ex.Parameters.AddWithValue("@accId", playerToRank.AccountId);
                ex.ExecuteNonQuery();
               }
            });
I didn't test it but the command should still work.
Quote Originally Posted by NigrKiller View Post
Hey guys, this'll be my second release for MPGH. In this one, I'll be releasing a /rank command I made. This command is 100% bug-free, and is easily customizable to fit your server. For the sake of the release, I have edited the code to the standard ranks on a FSOD source.

How to use it?: /rank username rankId

How does it work?: I made a comment above the command if you read it'll explain. I'll get a little more in-depth as to why it should be bug free.

So pretty much I have created a basic boolean that checks if a string has the chars 0-9 in it. If they do not, it'll send an message to the user executing the command. Now we have an extra check in place to ensure the rank we are giving is actually a rank. So in FSOD the max rank would be 3 because the hierarchy for ranks goes: Player (0), Donator (1), Admin (2), Owner (3). So we added checks to ensure that the argument being given was a valid rank and if not, would also throw a message to the player.

Now, the player has been given a rank but we need to tell them they have been given a rank and what rank they were given, as well as tell the user who executed the command in case they gave the wrong rank, etc.

Then, I created an Array called ranks to hold all the rank names as well as Parsed the rank argument from a string to an int so it can be used to access the name of the rank in the ranks Array.

Let's get started:

in wServer/realm/commands/AdminCommands.cs, Add the following command:

Code:
    /**
     *    @Author Wilson (AKA: NigrKiller @ MPGH)
     * 
     * Sets any player's rank. 
     * 
     * Iterates through chars 0-9 for our {@code rank} argument to see if it is
     * a valid rank being set. 
     * 
     * Parses our {@code rank} argument into an int and displays the newly 
     * earned rank to the players through our {@code ranks} Array.
     */
    internal class Rank : Command {
        public Rank() : base("rank", 1) { }

        private const int MAX_RANK = 3;

        bool validRank(string rankId) {
            return rankId.All(c => c >= '0' && c <= '9');
        }

        String[] ranks = { "Player", "Donator", "Administrator",
            "Owner" };

        protected override bool Process(Player player, RealmTime time, string[] args) {
            
            var playerToRank = player.Manager.FindPlayer(args[0]);
            string rank = args[1];

            if (playerToRank == null) {
                player.SendError("Player not found! They may be offline.");
                return false;
            }

            if (!validRank(rank) && int.Parse(rank) < 0 && int.Parse(rank)  > MAX_RANK) {
                player.SendError("Invalid Rank! Use: /rank username rankId");
                return false;
            }
        
            player.Manager.Database.DoActionAsync(db => {
                var execute = db.CreateQuery();
                execute.CommandText = "UPDATE accounts SET rank="+ rank +" WHERE id=@accId;";
                execute.Parameters.AddWithValue("@accId", playerToRank.AccountId);
                execute.ExecuteNonQuery();
            });

            playerToRank.SendInfo("You have been ranked "+ ranks[int.Parse(rank)] +" by "+ player.Name +".");
            player.SendInfo("You have given "+ playerToRank.Name +" the rank, "+ ranks[int.Parse(rank)] +".");
            return true;
        }
    }
To edit the command, simply do the following:

This is the default permission on FSOD. It pretty much means if you're rank 3 you can use it. Change it to suit your permissions.

For the MAX_RANK constant, change the value to your max rank on your server.

For the ranks Array, simply add all of the names of your ranks (in order) from 0-?

Note: You MUST add the names of the ranks to the Array if you change the max rank otherwise you will get an ArrayIndexOutOfBounds Exception (Is that what it's called in C#? Again, sorry I come from Java lol)

Conclusion: All in all, I could not find a single bug while testing this, but if you find one I'll be happy to fix it. This works flawlessly on a regular FSOD source so if it doesn't work you're either typing the command wrong or couldn't follow instructions to add this command...

The only problem with your code is that when it's used, it doesn't check if you're an admin before running the command.

Code:
internal class Rank : Command {
        public Rank() : base("rank", 1) {}
Should be changed to,

Code:
Code:
internal class Rank : Command {
	public Rank() : base("rank",  4) {}
	            //This way admins cannot give owner rank lol
Other than that, I like the code, because it allows you to rank people without opening heidiSQL or another SQL program and making them relog to apply the effect.

In the future, you should proof-the code so that donators/admins aren't allowed to give higher ranks than themselves.
Quote Originally Posted by reveng3d View Post
The only problem with your code is that when it's used, it doesn't check if you're an admin before running the command.

Code:
internal class Rank : Command {
        public Rank() : base("rank", 1) {}
Should be changed to,

Code:
Code:
internal class Rank : Command {
	public Rank() : base("rank",  4) {}
	            //This way admins cannot give owner rank lol
Other than that, I like the code, because it allows you to rank people without opening heidiSQL or another SQL program and making them relog to apply the effect.

In the future, you should proof-the code so that donators/admins aren't allowed to give higher ranks than themselves.
obviously, thats not something wrong with the code lol, everyone usually just leaves the perm rank as 1 when releasing a command because all servers have different ranking numbers, its common sense to change that to whatever rank you want to be able to use it.
Quote Originally Posted by NigrKiller View Post
Hey guys, this'll be my second release for MPGH. In this one, I'll be releasing a /rank command I made. This command is 100% bug-free, and is easily customizable to fit your server. For the sake of the release, I have edited the code to the standard ranks on a FSOD source.

How to use it?: /rank username rankId

How does it work?: I made a comment above the command if you read it'll explain. I'll get a little more in-depth as to why it should be bug free.

So pretty much I have created a basic boolean that checks if a string has the chars 0-9 in it. If they do not, it'll send an message to the user executing the command. Now we have an extra check in place to ensure the rank we are giving is actually a rank. So in FSOD the max rank would be 3 because the hierarchy for ranks goes: Player (0), Donator (1), Admin (2), Owner (3). So we added checks to ensure that the argument being given was a valid rank and if not, would also throw a message to the player.

Now, the player has been given a rank but we need to tell them they have been given a rank and what rank they were given, as well as tell the user who executed the command in case they gave the wrong rank, etc.

Then, I created an Array called ranks to hold all the rank names as well as Parsed the rank argument from a string to an int so it can be used to access the name of the rank in the ranks Array.

Let's get started:

in wServer/realm/commands/AdminCommands.cs, Add the following command:

Code:
    /**
     *    @Author Wilson (AKA: NigrKiller @ MPGH)
     * 
     * Sets any player's rank. 
     * 
     * Iterates through chars 0-9 for our {@code rank} argument to see if it is
     * a valid rank being set. 
     * 
     * Parses our {@code rank} argument into an int and displays the newly 
     * earned rank to the players through our {@code ranks} Array.
     */
    internal class Rank : Command {
        public Rank() : base("rank", 1) { }

        private const int MAX_RANK = 3;

        bool validRank(string rankId) {
            return rankId.All(c => c >= '0' && c <= '9');
        }

        String[] ranks = { "Player", "Donator", "Administrator",
            "Owner" };

        protected override bool Process(Player player, RealmTime time, string[] args) {
            
            var playerToRank = player.Manager.FindPlayer(args[0]);
            string rank = args[1];

            if (playerToRank == null) {
                player.SendError("Player not found! They may be offline.");
                return false;
            }

            if (!validRank(rank) && int.Parse(rank) < 0 && int.Parse(rank)  > MAX_RANK) {
                player.SendError("Invalid Rank! Use: /rank username rankId");
                return false;
            }
        
            player.Manager.Database.DoActionAsync(db => {
                var execute = db.CreateQuery();
                execute.CommandText = "UPDATE accounts SET rank="+ rank +" WHERE id=@accId;";
                execute.Parameters.AddWithValue("@accId", playerToRank.AccountId);
                execute.ExecuteNonQuery();
            });

            playerToRank.SendInfo("You have been ranked "+ ranks[int.Parse(rank)] +" by "+ player.Name +".");
            player.SendInfo("You have given "+ playerToRank.Name +" the rank, "+ ranks[int.Parse(rank)] +".");
            return true;
        }
    }
To edit the command, simply do the following:

This is the default permission on FSOD. It pretty much means if you're rank 3 you can use it. Change it to suit your permissions.

For the MAX_RANK constant, change the value to your max rank on your server.

For the ranks Array, simply add all of the names of your ranks (in order) from 0-?

Note: You MUST add the names of the ranks to the Array if you change the max rank otherwise you will get an ArrayIndexOutOfBounds Exception (Is that what it's called in C#? Again, sorry I come from Java lol)

Conclusion: All in all, I could not find a single bug while testing this, but if you find one I'll be happy to fix it. This works flawlessly on a regular FSOD source so if it doesn't work you're either typing the command wrong or couldn't follow instructions to add this command...
Why so much lines? You could take GRank command(Made by Ace's Sheep http://www.mpgh.net/forum/showthread.php?t=1160762) and not make a whole new command, I'm not saying that your command is shit, unlike, good work, I just say that you didn't have to waste time making it :/
Quote Originally Posted by reveng3d View Post
The only problem with your code is that when it's used, it doesn't check if you're an admin before running the command.

Code:
internal class Rank : Command {
        public Rank() : base("rank", 1) {}
Should be changed to,

Code:
Code:
internal class Rank : Command {
	public Rank() : base("rank",  4) {}
	            //This way admins cannot give owner rank lol
Other than that, I like the code, because it allows you to rank people without opening heidiSQL or another SQL program and making them relog to apply the effect.

In the future, you should proof-the code so that donators/admins aren't allowed to give higher ranks than themselves.
In FSOD there is only the value 1 to be set in AdminCommands. If the command has the 1 like mine does, it allows anyone with rank 3 to use, which is an owner rank. My server has 9 ranks however, and I changed the code to suit the default fsod server.

This code is not meant for donators/admins? Lol
This is mostly for if your server is hosted on a VPS and you need to make changes to the database on the fly with 0 chance at a fuck up


Quote Originally Posted by Zolmex View Post
Why so much lines? You could take GRank command(Made by Ace's Sheep http://www.mpgh.net/forum/showthread.php?t=1160762) and not make a whole new command, I'm not saying that your command is shit, unlike, good work, I just say that you didn't have to waste time making it :/
His code is pretty much the vanilla fsod command uncommented and barely configured to the same as the other commands. I'll show you what I mean
Ace Sheep's:
Code:
internal class GRankCommand : Command
    {
        public GRankCommand() : base("guildrank", 85) { }
        protected override bool Process(Player player, RealmTime time, string[] args)
        {
            if (string.IsNullOrEmpty(args[0]))
            {
                player.SendHelp("Usage: /guildrank <name> <guildRank>");
                return false;
            }
            player.Manager.Database.DoActionAsync(db =>
            {
                var cmd = db.CreateQuery();
                cmd.CommandText = "UPDATE accounts SET guildRank=@guildRank WHERE name=@name";
                cmd.Parameters.AddWithValue("@guildRank", args[1]);
                cmd.Parameters.AddWithValue("@name", args[0]);
                if (cmd.ExecuteNonQuery() == 0)
                {
                    player.SendInfo("Could not change guild rank. Use 0 (Initiate), 10 (Member), 20 (Officer), 30 (Leader), or 40 (Founder)");
                }
                else
                    player.SendInfo("Guild rank successfully changed");
                log.InfoFormat(args[1] + "'s guild rank has been changed");
            });
            return true;
        }
    }
FSOD's commented out:
Code:
//class GuildRank : ICommand
    //{
    //    public string Command { get { return "grank"; } }
    //    public int RequiredRank { get { return 4; } }

    //    protected override bool Process(Player player, RealmTime time, string[] args)
    //    {
    //        if (args.Length < 2)
    //        {
    //            player.SendHelp("Usage: /grank <username> <number>");
    //        }
    //        else
    //        {
    //            try
    //            {
    //                using (Database dbx = new Database())
    //                {
    //                    var cmd = dbx.CreateQuery();
    //                    cmd.CommandText = "UPDATE accounts SET guildRank=@guildRank WHERE name=@name";
    //                    cmd.Parameters.AddWithValue("@guildRank", args[1]);
    //                    cmd.Parameters.AddWithValue("@name", args[0]);
    //                    if (cmd.ExecuteNonQuery() == 0)
    //                    {
    //                        player.SendInfo("Could not change guild rank. Use 10, 20, 30, 40, or 50 (invisible)");
    //                    }
    //                    else
    //                        player.SendInfo("Guild rank successfully changed");
    //                    log.InfoFormat(args[1] + "'s guild rank has been changed");
    //                }
    //            }
    //            catch
    //            {
    //                player.SendInfo("Could not change rank, please change rank in database");
    //            }
    //        }
    //    }
    //}
Not bashing the guy, but anyone can look at the ban command and see how to fix the guild ranks lol

Also, I only spent like 15 minutes on this, and 15 minutes for a command that cannot be misused or require you to go into the database is well worth it in my book.
Quote Originally Posted by TemplateYTR View Post
Skype name?
I'll PM you it.
Quote Originally Posted by NigrKiller View Post
I'll PM you it.
whats ur skype?
Nice 2 month bump
Quote Originally Posted by Orbit View Post
Nice 2 month bump

//2 short
Quote Originally Posted by SeXZeFPS View Post

//2 short
Pm'd. //10chars
okay, i might just be stupid, but no matter what rank i give my account in rotmgprod.cs\accounts, i am not able to do this rank command, even when my rank is exactly what i set the minimum rank to be? help please!
Posts 1–15 of 15 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?