Monday, May 2, 2011

How Does Strcmp work !


1. What is Strcmp?
- Strcmp stands for String Compare, and all it does is compare 2 strings.

2. How does it work
- strcmp works by taking each characters of a string, and substract it's ASCII-Code with the ASCII-Code of the character in the other string. So lets say you want to compare the string "samp" with the string "samp". This is what strcmp will do:
- Take the first character ('s') and substract it with the character from the second string ('s')
- If the characters are the same, the result of the substraction will be 0 (null)
- Do this for all characters of the strings

If the 2 strings are the same, the total of all the characters substracted from eachother will be 0.
This is why to compare 2 strings you need to check if the result is 0, not 1.

Example:
if(strcmp("Hello", "Hello") == 0)
{
    // Do something if they the same
}
else
{
    // Do something if they are different from another
}

If the 2 strings are not equal to eachother, strcmp will return the result from the substractions.

Thursday, April 28, 2011

Making a simple Register/Login dialog.


First we will need the "Dini" include, so the information can be saved on ".ini " file!
Download and put the "Dini" include in /pawno/includes folder.


Now... Let's begin scripting.
Open Pawno and load your GameMode.
On top of it we must include the "Dini" file so add :
#include <Dini>

Then, we will identify every dialog with a number like this :
#define Register 0
#define Logged 1

Also, we must add a new condition:
new Login[MAX_PLAYERS];

Then, we must put this under OnPlayerConnect :
Login[playerid] = 0;
new nombre[MAX_PLAYER_NAME], archivo[256];
GetPlayerName(playerid, nombre, sizeof(nombre));
format(archivo, sizeof(archivo), "/Users/%s.ini", nombre);
if (!dini_Exists(archivo))
{
    ShowPlayerDialog(playerid, Register, DIALOG_STYLE_INPUT, "Register", "He writes your password:", "Acept", "Cancel");
}
else
{
    ShowPlayerDialog(playerid, Logged, DIALOG_STYLE_INPUT, "Login", "He writes your password", "Acept", "Cancel");
}

This will check if the player is registered and will show the dialog box for registration.


Now, under the callback OnDialogResponse we will put the following thing:
if (dialogid == Register)
{
    new nombrejugador[MAX_PLAYER_NAME], archivo[256];
    if (!strlen(inputtext)) return ShowPlayerDialog(playerid, Register, DIALOG_STYLE_INPUT, "Register", "He writes your password", "Acept", "Cancel");
    if (!response) return ShowPlayerDialog(playerid, Register, DIALOG_STYLE_INPUT, "Register", "He writes your password:", "Acept", "Cancel");
    GetPlayerName(playerid, nombrejugador, sizeof(nombrejugador));
    format(archivo, sizeof(archivo), "/Users/%s.ini", nombrejugador);
    dini_Create(archivo);
    dini_Set(archivo, "User", nombrejugador);
    dini_Set(archivo, "Password", inputtext);
    ShowPlayerDialog(playerid, Logged, DIALOG_STYLE_INPUT, "Login", "He writes your password", "Acept", "Cancel");
}

This will make sure that if a player is not registered, he will get the proper dialog.
If the dialog is closed it will be shown again, so that the player cannot spawn.


The last thing we need is the dialog for the login function.
So we add this:
if (dialogid == Logged)
{
    new nombrejugador[MAX_PLAYER_NAME], archivo[256], comprobante[256];
    if (!strlen(inputtext)) return ShowPlayerDialog(playerid, Logged, DIALOG_STYLE_INPUT, "Login", "He writes your password", "Acept", "Cancel");
    if (!response) return ShowPlayerDialog(playerid, Logged, DIALOG_STYLE_INPUT, "Login", "He writes your password", "Acept", "Cancel");
    GetPlayerName(playerid, nombrejugador, sizeof(nombrejugador));
    format(archivo, sizeof(archivo), "/Users/%s.ini", nombrejugador);
    format(comprobante, sizeof(comprobante), "%s", dini_Get(archivo, "Password"));
    if (!strcmp (inputtext, comprobante))
    {
        Login[playerid] = 1;
    }
    else
    {
        ShowPlayerDialog(playerid, Logged, DIALOG_STYLE_INPUT, "Login", "He writes your password", "Acept", "Cancel");
    }
}

Now compile the script and create a new folder called "users" in your Scriptfiles folder!

If you do everything properly the script will work 100%