SA-MP Forums Archive
[FilterScript] [SQLite] Simples registro - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: Non-English (https://sampforum.blast.hk/forumdisplay.php?fid=9)
+--- Forum: Languages (https://sampforum.blast.hk/forumdisplay.php?fid=33)
+---- Forum: Português/Portuguese (https://sampforum.blast.hk/forumdisplay.php?fid=34)
+----- Forum: Lançamentos/Releases (https://sampforum.blast.hk/forumdisplay.php?fid=56)
+----- Thread: [FilterScript] [SQLite] Simples registro (/showthread.php?tid=439283)



[SQLite] Simples registro - Jason` - 24.05.2013

Estava vendo como se usa SQLite e desenvolvi este pequeno filterscript.
Como nгo foi trabalhoso, resolvi compartilhar.


Cуdigo:
pawn Код:
#include <a_samp>


#define DIALOG_REGISTER (1000)
#define DIALOG_LOGIN    (2000)


#define DEFAULT_MONEY   (5000)
#define DEFAULT_SKIN    (136)


#define DEFAULT_X       (2247.6187)
#define DEFAULT_Y       (-1262.2136)
#define DEFAULT_Z       (23.9550)
#define DEFAULT_A       (266.1028)


new DB:db_handle;


public OnFilterScriptInit() {

    db_handle = db_open("sqlite.db");

    db_query(db_handle, "CREATE TABLE IF NOT EXISTS `player_info` (`name`, `password`, `money`, `skin`)");
    return true;
}

public OnFilterScriptExit() {

    db_close(db_handle);
    return true;
}

public OnPlayerConnect(playerid) {
    new
        szName[25],
        szString[150],
        DBResult:szResult;

    GetPlayerName(playerid, szName, sizeof szName);

    format(szString, sizeof szString, "SELECT * FROM `player_info` WHERE `name` = '%s'", DB_Escape(szName));
    szResult = db_query(db_handle, szString);

    if(db_num_rows(szResult)) {
        format(szString, sizeof szString, "{ffffff}Bem-vindo, {f6f600}%s{ffffff}!\n\nDigite sua senha para logar.", szName);
        ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{ffffff}Login", szString, "Login", "Sair");
    }
    else {
        format(szString, sizeof szString, "{ffffff}Bem-vindo, {f6f600}%s{ffffff}!\nVocк ainda nгo й registrado no servidor.\n\nDigite uma senha abaixo para se registrar.", szName);
        ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{ffffff}Registro", szString, "Registrar", "Sair");
    }
    db_free_result(szResult);
    return true;
}

public OnPlayerDisconnect(playerid, reason) {

    if(GetPVarInt(playerid, "pLogged")) {
        new
            szName[25],
            szString[150];

        GetPlayerName(playerid, szName, sizeof szName);

        format(szString, sizeof szString, "UPDATE `player_info` SET `money` = %d, `skin` = %d WHERE `name` = '%s'", GetPlayerMoney(playerid), GetPlayerSkin(playerid), DB_Escape(szName));
        db_query(db_handle, szString);
    }
    return true;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {

    new
        szName[25],
        szString[150];

    GetPlayerName(playerid, szName, sizeof szName);
    switch(dialogid) {

        case DIALOG_REGISTER: {
            if(!response)
                Kick(playerid);
            else {
                if(!(4 < strlen(inputtext) < 33))
                    ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{ffffff}Registro", "{ffffff}Sua senha deve conter no mнnimo 05 e no mбximo 32 caracteres.\nPor favor, tente novamente.", "Registrar", "Sair");
                else {
                    format(szString, sizeof szString, "INSERT INTO `player_info` (`name`, `password`, `money`, `skin`) VALUES ('%s', '%s', %d, %d)", szName, inputtext, DEFAULT_MONEY, DEFAULT_SKIN);
                    db_query(db_handle, szString);

                    format(szString, sizeof szString, "{ffffff}Muito bem, {f6f600}%s{ffffff}.\nVocк se registrou com sucesso!\n\nDigite sua senha abaixo para logar.", szName);
                    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{ffffff}Login", szString, "Login", "Sair");
                }
            }
        }

        case DIALOG_LOGIN: {
            if(!response)
                Kick(playerid);
            else {
                new
                    DBResult:szResult;

                format(szString, sizeof szString, "SELECT * FROM `player_info` WHERE `name` = '%s' AND `password` = '%s'", DB_Escape(szName), DB_Escape(inputtext));
                szResult = db_query(db_handle, szString);

                if(db_num_rows(szResult)) {
                    new
                        szField[32];

                    SetPVarInt(playerid, "pLogged", 1);

                    db_get_field_assoc(szResult, "money", szField, 32);
                    ResetPlayerMoney(playerid);
                    GivePlayerMoney(playerid, strval(szField));

                    db_get_field_assoc(szResult, "skin", szField, 32);
                    SetSpawnInfo(playerid, 0, strval(szField), DEFAULT_X, DEFAULT_Y, DEFAULT_Z, DEFAULT_A, 0, 0, 0, 0, 0, 0);
                    SpawnPlayer(playerid);

                    SendClientMessage(playerid, 0x32cd32ff, ">> Logado com sucesso!");
                }
                else {
                    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{ffffff}Login", "{ffffff}Senha incorreta.\nPor favor, tente novamente.", "Login", "Sair");
                }
            }
        }

        //
    }
    return true;
}



stock DB_Escape(text[])
{
    new
        ret[80 * 2],
        ch,
        i,
        j;
    while ((ch = text[i++]) && j < sizeof (ret))
    {
        if (ch == '\'')
        {
            if (j < sizeof (ret) - 2)
            {
                ret[j++] = '\'';
                ret[j++] = '\'';
            }
        }
        else if (j < sizeof (ret))
        {
            ret[j++] = ch;
        }
        else
        {
            j++;
        }
    }
    ret[sizeof (ret) - 1] = '\0';
    return ret;
}


Download:
http://pastebin.com/mML15SJx


Re: [SQLite] Simples registro - Nice-.- - 24.05.2013

Legal, agora sу falta fazer um tutorial simples ensinando a mecher com SQLite '-'


Re: [SQLite] Simples registro - iFox - 24.05.2013

parece ser dificil mecher com isso :^


Re: [SQLite] Simples registro - feliphemort - 24.05.2013

bacana...


Re: [SQLite] Simples registro - Don_Speed - 24.05.2013

Muito bom esse sistema!


Respuesta: [SQLite] Simples registro - Pedro Pawno - 25.05.2013

Bem legal amigo parabйns


Re: [SQLite] Simples registro - steki. - 25.05.2013

Adoro SQLite. Sу nгo uso por ser um cabaзo.


Re: [SQLite] Simples registro - Jason` - 25.05.2013

Quote:
Originally Posted by steki.
Посмотреть сообщение
Adoro SQLite. Sу nгo uso por ser um cabaзo.
Ele trava o servidor durante a consulta?


Re: [SQLite] Simples registro - steki. - 25.05.2013

Trava, mas й bem leve com quantidades de dados mediana.


Re: [SQLite] Simples registro - Jason` - 25.05.2013

Eu fiquei em dъvida de como usar auto_increment pra criar um campo 'id'. Como seria?