katursis / Pawn.CMD

🚀 Plugin-powered command processor for SA:MP server
MIT License
119 stars 13 forks source link

Is it possible to make a Guess Command? #41

Closed noizza closed 2 years ago

noizza commented 2 years ago

I wanted to know if it is possible to do something like this with Pawn.CMD, and if it is possible how to do it, thank you very much.

What do I mean by Guess Command? An example would be:

When you type e.g. "/hel"

public OnPlayerCommandPerformed(playerid, cmd[], params[], result, flags)
{
    if(result == -1)
    {
        new guessCmd[32];
        Command_GuessFunc(guessCmd, cmd);
        SendClientMessageEx(playerid, -1, "{FF0000}ERROR:{FFFFFFFF} \"%s\" is not found, did you mean \"%s\"?", cmd, guessCmd);
    }
    return 1;
}
CMD:help(playerid) return 1;

Sorry for my bad English.

IstuntmanI commented 2 years ago

Technically, you could use these functions:

native CmdArray:PC_GetCommandArray(); // to get all commands in an array
native CmdArray:PC_GetAliasArray(const cmd[]); // to get all aliases of a command in an array
native PC_GetArraySize(CmdArray:arr); // to get the number of elements of an array (all commands or all aliases array)
native PC_GetCommandName(CmdArray:arr, index, dest[], size = sizeof dest); // get the name of a command from an array, the index from 0 to n (where n is the result of PC_GetArraySize)
native PC_FreeArray(&CmdArray:arr); // to clear the array from the memory, to prevent memory leak

Then, you should be able to create a function that makes use of something like the Levenshtein Distance, which may help you to return the closest matching command name. You can see an example recently done by Y_Less here.

This may get pretty slow, I guess, especially in Pawn.