[FilterScript] Sistema de Registro [SIMPLES] [MySQL]
#1

Um simples sistema de registro que salva senha, dinheiro e score, hб mais chances de alguem utilizar para estudar MySQL do que usar em seu gm ou oq for, mais ai vai.

pawn Код:
// Sistema de Registro [MySQL]

#include <a_samp>
#include <a_mysql>

#define FOREACH

#if defined FOREACH
    #include <foreach>
#endif

const D_REGISTRO = 1345;
const D_LOGIN = 1354;

new
    iQuery[ 128 ],
    bool: gPlayerLogged[ MAX_PLAYERS ],
    gPlayerName[ MAX_PLAYERS ][ MAX_PLAYER_NAME ]
;

public OnFilterScriptInit( ) {
    mysql_connect( "localhost" , "root" , "sa-mp" , "");
    if( mysql_ping( ) >= 1 ) print( "=> Conectado ao Banco de Dados" );
    else print( "=> Incapaz de conectar ao banco de dados\nFS descarregado" ) , SendRconCommand( "unloadfs pregister" );
   
    mysql_query( "CREATE TABLE IF NOT EXISTS `playerContas` (`playerNome` VARCHAR(24),`playerSenha` VARCHAR(128),`playerDinheiro` INT(11),`playerScore` INT(11))");
    return 0x01;
}

public OnFilterScriptExit( )
{
    #if defined FOREACH
        foreach( Player, i ) {
            if( gPlayerLogged[ i ] ) {
                salvarPlayer( i );
            }
        }
    #else
        for( new i = 0 ; i < MAX_PLAYERS ; ++i ) {
            if( IsPlayerConnected( i ) && gPlayerLogged[ i ] ) {
                salvarPlayer( i );
            }
        }
    }
    #endif
    mysql_close( );
    return 0x01;
}

public OnPlayerConnect( playerid ) {
    GetPlayerName( playerid , gPlayerName[ playerid ] , MAX_PLAYER_NAME );
    format( iQuery , sizeof( iQuery ) , "SELECT * FROM `playerContas` WHERE `playerNome` = '%s'" , gPlayerName[ playerid ] );
    mysql_query( iQuery );
    mysql_store_result( );
    if( mysql_num_rows( ) ) {
        ShowPlayerDialog( playerid , D_LOGIN , 3 , "{FFFFFF}Login" , "{FFFFFF}Digite sua senha para logar!" , "Logar" , "Sair" );
    }
    else ShowPlayerDialog( playerid , D_REGISTRO , 3 , "{FFFFFF}Registro" , "{FFFFFF}Digite uma senha para registrar!" , "Registrar" , "Sair" );
    mysql_free_result( );
    return 0x01;
}

public OnPlayerDisconnect( playerid , reason ) {
    #pragma unused reason
    if( gPlayerLogged[ playerid ] ) {
        salvarPlayer( playerid );
    }
    return 1;
}

public OnDialogResponse( playerid , dialogid , response , listitem , inputtext[ ] ) {
    switch( dialogid ) {
        case D_REGISTRO: {
            if( !response ) Kick( playerid );
            else registrarPlayer( playerid , inputtext );
        }
        case D_LOGIN: {
            if( !response ) Kick( playerid );
            else {
                format( iQuery , sizeof( iQuery ) , "SELECT * FROM `playerContas` WHERE `playerNome` = '%s' AND `playerSenha` = '%s'" , gPlayerName[ playerid ] , inputtext );
                mysql_query( iQuery );
                mysql_store_result( );
                if( mysql_num_rows( ) ) {
                    new
                        loadStr[ 90 ]
                    ;
                    gPlayerLogged[ playerid ] = true;
                    SendClientMessage( playerid , 0x00CD00AA , "Logado!" );
                    while( mysql_retrieve_row( ) ) {
                        mysql_get_field( "playerDinheiro" , loadStr );
                        GivePlayerMoney( playerid , strval( loadStr ) );
                       
                        mysql_get_field( "playerScore" , loadStr );
                        SetPlayerScore( playerid , strval( loadStr ) );
                    }
                }
                else SendClientMessage( playerid , 0xE31919AA , "Senha errada!" ) , Kick( playerid );
                mysql_free_result( );
            }
        }
    }
    return 1;
}

stock registrarPlayer( playerid , senha[ ] ) {
    format( iQuery , sizeof( iQuery ) , "INSERT INTO `playerContas` (`playerNome`,`playerSenha`,`playerDinheiro`,`playerScore`) VALUES ('%s','%s',0,0)" , gPlayerName[ playerid ] , senha );
    mysql_query( iQuery );
    return 1;
}

stock salvarPlayer( playerid ) {
    format( iQuery , sizeof( iQuery ) , "UPDATE `playerContas` SET `playerDinheiro` = '%i', `playerScore = '%i' WHERE `playerNome` = '%s'" , GetPlayerMoney( playerid ) , GetPlayerScore( playerid ) , gPlayerName[ playerid ] );
    mysql_query( iQuery );
    return 1;
}
Pastebin
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)