[Request]Organized chat.cs?

03/04/2013 23:15 xScylez#1
ROFL Fixed it!..
Wasn't so hard after all xD
03/05/2013 12:15 abdoumatrix#2
u mean like this

03/05/2013 12:42 _DreadNought_#3
Don't do == use >= for that.

That way GMs can use player commands without making a copy of the command for them.
03/05/2013 13:04 nTL3fTy#4
You could always re-implement the way commands work, but that's probably asking too much...

Code:
[Command("test")]
public class TestCommand : CommandBase
{
    public TestCommand(GameUser user, string command)
        : base(user, command)
    {
    }

    protected override bool CanHandle { get { return !(_user.Permission < UserPermissionLevel.PM); } }

    protected override bool HandleImpl()
    {
        _user.SendSystemMsg("Congratulations! You have enough permissions to run this command.");
        return true;
    }
}
03/05/2013 15:09 _DreadNought_#5
That's very similar to how public mine craft emulators do it - It's a nice way.

Nice to add a description in if you use the command incorrectly.
03/05/2013 16:32 EgyptianMano#6
i guess if u divided them into voids would be much organized, something like

public bool GmCommand(byte State ,string Msg)
{
if (State<PlayerStates.Gm) return false;
//your commands here
}


public bool PmCommand(byte State ,string Msg)
{
if (State<PlayerStates.Pm) return false;
//your commands here
}


public bool PlayerCommand(byte State ,string Msg)
{
//your commands here
}
03/05/2013 23:06 abdoumatrix#7
ur old one

try to adjust this as u like
03/06/2013 01:46 xScylez#8
Quote:
Originally Posted by abdoumatrix View Post
ur old one

try to adjust this as u like
ohh, yea thanks! I'll try this :D
03/06/2013 02:16 Spirited#9
I would just like to point out how horrible that snippet of code is.
Code:
if (Cmd[0] == "/vp")
                            GC.LocalMessage(2000, "You have " + GC.MyChar.VP + " virtue points.");
                        if (Cmd[0] == "/map")
                            GC.LocalMessage(2000, "The ID of the map you are on is " + GC.MyChar.Loc.Map);
If you continue programming like that, you'll be executing a lot of code per command (increasing your cpu significantly more than it would be executing a command normally). Right now, you're checking if the command is "/vp", and if it is, it'll check for if the command is "/map" (and every single command you have under that) anyways. That's an issue. You need to have each command in a switch statement or at least have "else if" for every command after the first comparison.

I would also have the types of commands in different functions so if the command is found as a GM function, it will return true and not try to find the command in the PM region... Not that you two care what-so-fucking-ever. You both probably don't have a clue on how to program (especially since you learn conditionals in the first week of learning programming and what you have shown above is a MAJOR mistake).

PS: You are also repeating commands, and thus the commands are going to run twice (and so on). The code above is absolutely horrible, very bugged, and very costly. I tutor people in Computer Science every day as my job and I've never seen such major errors. This community continues to amaze me.
03/06/2013 05:30 EgyptianMano#10
i would do it like that

Code:
public class Chat
    {
        public static void Handle(Main.GameClient GC, byte[] Data)
        {
            MemoryStream MS = new MemoryStream(Data);
            BinaryReader BR = new BinaryReader(MS);
            BR.ReadBytes(8);
            ushort ChatType = (ushort)BR.ReadUInt32();
            BR.ReadBytes(13);
            string From = Encoding.ASCII.GetString(BR.ReadBytes(BR.ReadByte()));
            string To = Encoding.ASCII.GetString(BR.ReadBytes(BR.ReadByte()));
            BR.ReadByte();
            string Message = Encoding.ASCII.GetString(BR.ReadBytes(BR.ReadByte()));
            BR.Close();
            MS.Close();
            #region BadWords
            Message = Message.Replace("damn", "****");
            Message = Message.Replace("fuck", "****");
            Message = Message.Replace("shit", "****");
            Message = Message.Replace("stupid", "******");
            Message = Message.Replace("wtf", "***");
            Message = Message.Replace("idiot", "*****");
            Message = Message.Replace("fucker", "******");
            #endregion
            if (ChatType == 2104 && GC.MyChar.MyShop != null)
                GC.MyChar.MyShop.Hawk = Message;
            try
            {
                if (Message[0] == '/')
                {
                    string[] Cmd = Message.Split(' ');
                    if (!GMCommand(GC, Cmd)) if (!PMCommand(GC, Cmd)) if (!PlayerCommand(GC, Cmd))
                                Game.World.Chat(GC.MyChar, ChatType, From, To, Message);
//you can ofcourse use the && to merge that three conditions in one statement but i prefer it like that
                }
                else
                    Game.World.Chat(GC.MyChar, ChatType, From, To, Message);
            }
            catch { }
        }
        public static bool GMCommand(Main.GameClient GC, string[] Msg)
        {
            if (GC.AuthInfo.Status != "[GM]") return false;
            switch (Msg[0])
            {
                case "/vp":
                    {
                        //do ur codes here
                        return true;
                    }
                case "/map":
                    {
                        //do ur codes here
                        return true;
                    }
            }
            return false;
        }
        public static bool PMCommand(Main.GameClient GC, string[] Msg)
        {
            if (GC.AuthInfo.Status != "[GM]" && GC.AuthInfo.Status != "[PM]") return false;
            switch (Msg[0])
            {
                case "/vp":
                    {
                        //do ur codes here
                        return true;
                    }
                case "/map":
                    {
                        //do ur codes here
                        return true;
                    }
            }
            return false;
        }
        public static bool PlayerCommand(Main.GameClient GC, string[] Msg)
        {
            switch (Msg[0])
            {
                case "/vp":
                    {
                        //do ur codes here
                        return true;
                    }
                case "/map":
                    {
                        //do ur codes here
                        return true;
                    }
            }
            return false;
        }
    }
that way u can add any commands anytime without needs to check the Gm and Pm again , just put the commands at the target bool function
03/06/2013 05:56 Spirited#11
@EgyptianMano, yah - that's better. The only things wrong that I see with that are some very minor stylistic issues...

if (!GMCommand(GC, Cmd)) if (!PMCommand(GC, Cmd)) if (!PlayerCommand(GC, Cmd))
should be... (stylistically)
if (!PMCommand(GC, Cmd) && !GMCommand(GC, Cmd) && !PlayerCommand(GC, Cmd))

...and a few logic errors with duplicate commands. Much better. Btw, PM is better than GM. The PM is the project manager (like the administrator) and a GM is a game master (like a moderator).
03/06/2013 13:02 abdoumatrix#12
Quote:
Originally Posted by Fаng View Post
I would just like to point out how horrible that snippet of code is.
Code:
if (Cmd[0] == "/vp")
                            GC.LocalMessage(2000, "You have " + GC.MyChar.VP + " virtue points.");
                        if (Cmd[0] == "/map")
                            GC.LocalMessage(2000, "The ID of the map you are on is " + GC.MyChar.Loc.Map);
If you continue programming like that, you'll be executing a lot of code per command (increasing your cpu significantly more than it would be executing a command normally). Right now, you're checking if the command is "/vp", and if it is, it'll check for if the command is "/map" (and every single command you have under that) anyways. That's an issue. You need to have each command in a switch statement or at least have "else if" for every command after the first comparison.

I would also have the types of commands in different functions so if the command is found as a GM function, it will return true and not try to find the command in the PM region... Not that you two care what-so-fucking-ever. You both probably don't have a clue on how to program (especially since you learn conditionals in the first week of learning programming and what you have shown above is a MAJOR mistake).

PS: You are also repeating commands, and thus the commands are going to run twice (and so on). The code above is absolutely horrible, very bugged, and very costly. I tutor people in Computer Science every day as my job and I've never seen such major errors. This community continues to amaze me.
i just show him the (fastest)easiest way(may be isn't right)

ty for info's and advise

Quote:
Originally Posted by EgyptianMano View Post
i would do it like that

Code:
public class Chat
    {
        public static void Handle(Main.GameClient GC, byte[] Data)
        {
            MemoryStream MS = new MemoryStream(Data);
            BinaryReader BR = new BinaryReader(MS);
            BR.ReadBytes(8);
            ushort ChatType = (ushort)BR.ReadUInt32();
            BR.ReadBytes(13);
            string From = Encoding.ASCII.GetString(BR.ReadBytes(BR.ReadByte()));
            string To = Encoding.ASCII.GetString(BR.ReadBytes(BR.ReadByte()));
            BR.ReadByte();
            string Message = Encoding.ASCII.GetString(BR.ReadBytes(BR.ReadByte()));
            BR.Close();
            MS.Close();
            #region BadWords
            Message = Message.Replace("damn", "****");
            Message = Message.Replace("fuck", "****");
            Message = Message.Replace("shit", "****");
            Message = Message.Replace("stupid", "******");
            Message = Message.Replace("wtf", "***");
            Message = Message.Replace("idiot", "*****");
            Message = Message.Replace("fucker", "******");
            #endregion
            if (ChatType == 2104 && GC.MyChar.MyShop != null)
                GC.MyChar.MyShop.Hawk = Message;
            try
            {
                if (Message[0] == '/')
                {
                    string[] Cmd = Message.Split(' ');
                    if (!GMCommand(GC, Cmd)) if (!PMCommand(GC, Cmd)) if (!PlayerCommand(GC, Cmd))
                                Game.World.Chat(GC.MyChar, ChatType, From, To, Message);
//you can ofcourse use the && to merge that three conditions in one statement but i prefer it like that
                }
                else
                    Game.World.Chat(GC.MyChar, ChatType, From, To, Message);
            }
            catch { }
        }
        public static bool GMCommand(Main.GameClient GC, string[] Msg)
        {
            if (GC.AuthInfo.Status != "[GM]") return false;
            switch (Msg[0])
            {
                case "/vp":
                    {
                        //do ur codes here
                        return true;
                    }
                case "/map":
                    {
                        //do ur codes here
                        return true;
                    }
            }
            return false;
        }
        public static bool PMCommand(Main.GameClient GC, string[] Msg)
        {
            if (GC.AuthInfo.Status != "[GM]" && GC.AuthInfo.Status != "[PM]") return false;
            switch (Msg[0])
            {
                case "/vp":
                    {
                        //do ur codes here
                        return true;
                    }
                case "/map":
                    {
                        //do ur codes here
                        return true;
                    }
            }
            return false;
        }
        public static bool PlayerCommand(Main.GameClient GC, string[] Msg)
        {
            switch (Msg[0])
            {
                case "/vp":
                    {
                        //do ur codes here
                        return true;
                    }
                case "/map":
                    {
                        //do ur codes here
                        return true;
                    }
            }
            return false;
        }
    }
that way u can add any commands anytime without needs to check the Gm and Pm again , just put the commands at the target bool function
Nice job
03/06/2013 16:05 xScylez#13
Correct me if i'm wrong:

GM and PM:

ONLY PM:

ONLY NORMAL(Or all normal, gm and pm?):

Thank you all for taking your time helping me out :-)
03/06/2013 18:36 pro4never#14
Quote:
Originally Posted by xScylez View Post
Correct me if i'm wrong:

GM and PM:

ONLY PM:

ONLY NORMAL(Or all normal, gm and pm?):

Thank you all for taking your time helping me out :-)
This has always bothered me...

One cannot be 'both' a GM and a PM. PM outranked a GM and therefor already has all the permissions that a GM would have.

This is why you use a enumerated value for account permissions. Behind the scenes it's a number which you can then use simple operators on.

If you must be at least a Moderator to use a command then you do user.Permission >= PermissionType.Moderator and you can auto use the command if you're a moderator, GM or PM. It saves doing multiple checks like you're describing and has no legitimate drawbacks.

I cannot think of a single case where a higher permission account should not be able to use a lower permission command. This is why consistently naming your commands comes into play and should never pose a serious issue.
03/06/2013 18:45 xScylez#15
Quote:
Originally Posted by pro4never View Post
This has always bothered me...

One cannot be 'both' a GM and a PM. PM outranked a GM and therefor already has all the permissions that a GM would have.

This is why you use a enumerated value for account permissions. Behind the scenes it's a number which you can then use simple operators on.

If you must be at least a Moderator to use a command then you do user.Permission >= PermissionType.Moderator and you can auto use the command if you're a moderator, GM or PM. It saves doing multiple checks like you're describing and has no legitimate drawbacks.

I cannot think of a single case where a higher permission account should not be able to use a lower permission command. This is why consistently naming your commands comes into play and should never pose a serious issue.
ohh, ok! I understand :)

1 More question: I have 94errors:
Code:
The name "Cmd" does not exist in the current context

Here is an example where the error comes:
Code:
case "/robot":
                    {
                        string Account = "";
                        string Name = Cmd[1];

                        if (Game.World.CharacterFromName(Name) == null)
                        {
                            Game.Robot R = Database.LoadAsRobot(Name, ref Account);
                            if (R != null)
                                R.Init(Account);
                        }
                        return true;
                    }
On the
Code:
string Name = Cmd[1];
, i have the error on "Cmd"