loading account data after login (SQLite)
#1

Hello guys,

So i am messing around with sqlite again with konstatinos tutorial and i already had a problem before saving the money and score. now my question is: how do i load it after i logged in? i tried it like this:
pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == 0)
    {
        if(response)
        {
            new buf[129];
            WP_Hash(buf, 129, inputtext);
            if(!strcmp(buf, User[playerid][USER_PASSWORD], false))
            {
                new Query[75], DBResult: Result;
                format(Query, sizeof(Query), "SELECT * FROM users WHERE username = '%s' LIMIT 0, 1", DB_Escape(User[playerid][USER_NAME]));
                Result = db_query(Database, Query);
                if(db_num_rows(Result))
                {
                    db_get_field_assoc(Result, "userid", Query, 7);
                    User[playerid][USER_ID] = strval(Query);

                    db_get_field_assoc(Result, "admin", Query, 3);
                    User[playerid][USER_ADMIN] = strval(Query);

                    db_get_field_assoc(Result, "money", Query, 3); //this
                    User[playerid][USER_MONEY] = strval(Query);

                    db_get_field_assoc(Result, "score", Query, 3); //this
                    User[playerid][USER_SCORE] = strval(Query);

                    User[playerid][USER_LOGGED_IN] = true;
                    SendClientMessage(playerid, 0x00FF00FF, ""chat" "COL_LIGHTBLUE"You have successfully logged in to your account!");
                }
                db_free_result( Result );
but konstatinos said this is not neccesary or something.
i'm lost, i have this on OnPlayerConnect:
pawn Code:
if(User[playerid][USER_LOGGED_IN] == true)
    {
        new Query[72];
        format(Query, sizeof(Query), "UPDATE users SET admin = %d, money = %d, score = %d WHERE username = '%s'", User[playerid][USER_ADMIN], DB_Escape(User[playerid][USER_NAME]));
        format(Query, sizeof(Query), "UPDATE users SET money = %d, score = %d WHERE username = '%s'", GetPlayerMoney(playerid), GetPlayerScore(playerid), DB_Escape(User[playerid][USER_NAME]));
        db_query(Database, Query);
    }

    for(new i; i < _: USER_DATA; ++i) User[playerid][USER_DATA: i] = 0;
    return 1;
}
Reply
#2

Anyone?
Reply
#3

UPDATE clause is to update the data, so saving after a player disconnects.
pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    if(User[playerid][USER_LOGGED_IN] == true)
    {
        new Query[128];
        format(Query, sizeof(Query), "UPDATE users SET admin = %d, money = %d, score = %d WHERE username = '%s'", User[playerid][USER_ADMIN], GetPlayerMoney(playerid), GetPlayerScore(playerid), DB_Escape(User[playerid][USER_NAME]));
    }

    for(new i; i < _: USER_DATA; ++i) User[playerid][USER_DATA: i] = 0;
    return 1;
}
About the login, that's pretty much depends on you.

If you followed the previous correctly and the dialogid 0 is called, then it's fine. Although, this is incorrect:
pawn Code:
db_get_field_assoc(Result, "money", Query, 3); //this
User[playerid][USER_MONEY] = strval(Query);

db_get_field_assoc(Result, "score", Query, 3); //this
User[playerid][USER_SCORE] = strval(Query);
because it will store 2 characters + NULL. Money can be 9 digits; therebefore, the max is 10. The score can be 7.

pawn Code:
db_get_field_assoc(Result, "money", Query, 10); //this
User[playerid][USER_MONEY] = strval(Query);

db_get_field_assoc(Result, "score", Query, 7); //this
User[playerid][USER_SCORE] = strval(Query);
And then don't forget to set the score and money for the player.

pawn Code:
GivePlayerMoney(playerid, User[playerid][USER_MONEY]);
SetPlayerScore(playerid, User[playerid][USER_SCORE]);
PS: If you don't get something, it doesn't matter to read the tutorial again and again until you get it. This is what I also do in tutorials!
Reply
#4

Wait set the score and money, how? sorry im derp atm... i just wanna do this correctly.

like this?
pawn Code:
public OnPlayerSpawn(playerid)
{
    GivePlayerMoney(playerid, 0);
    SetPlayerScore(playerid, 0);
    return 1;
}
Reply
#5

On the login part, after loading from the database and storing the data to the variables - set them.
pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == 0)
    {
        if(response)
        {
            new buf[129];
            WP_Hash(buf, 129, inputtext);
            if(!strcmp(buf, User[playerid][USER_PASSWORD], false))
            {
                new Query[75], DBResult: Result;
                format(Query, sizeof(Query), "SELECT * FROM users WHERE username = '%s' LIMIT 0, 1", DB_Escape(User[playerid][USER_NAME]));
                Result = db_query(Database, Query);
                if(db_num_rows(Result))
                {
                    db_get_field_assoc(Result, "userid", Query, 7);
                    User[playerid][USER_ID] = strval(Query);
                   
                    db_get_field_assoc(Result, "admin", Query, 3);
                    User[playerid][USER_ADMIN] = strval(Query);
                   
                    db_get_field_assoc(Result, "money", Query, 10); //this
                    User[playerid][USER_MONEY] = strval(Query);
                   
                    db_get_field_assoc(Result, "score", Query, 7); //this
                    User[playerid][USER_SCORE] = strval(Query);
                   
                    User[playerid][USER_LOGGED_IN] = true;
                   
                    GivePlayerMoney(playerid, User[playerid][USER_MONEY]);
                    SetPlayerScore(playerid, User[playerid][USER_SCORE]);
                   
                    SendClientMessage(playerid, 0x00FF00FF, ""chat" "COL_LIGHTBLUE"You have successfully logged in to your account!");
                }
Reply
#6

It doesnt work, i think i might have this wrong:
pawn Code:
db_query(Database, "CREATE TABLE IF NOT EXISTS users (userid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(24) COLLATE NOCASE, password VARCHAR(129), admin INTEGER DEFAULT 0 NOT NULL, money INTEGER, score INTEGER)");
are score and money suposed to be INTEGER? or does there need to be added something?

when i look in the database it just shows money and score and on the field it doesnt have a value...
Reply
#7

Just wanted to point out you can get query error checking by using sqlitei.

https://sampforum.blast.hk/showthread.php?tid=303682

"You can see error messages from db_query (for example, no such column name: test)."

@Edit - It only works on Windows
Reply
#8

When a player registers and it inserts a new row, if you use default values then it set them. If not, you'll need insert the values yourself (not recommended).

If it didn't update that means that there's something wrong with it.

Post the whole code of register/login.

About what Pottus said, use either a SQLite Browser or https://sampforum.blast.hk/showthread.php?tid=468260
Reply
#9

Allright...
pawn Code:
enum USER_DATA
{
    USER_ID,
    USER_NAME[MAX_PLAYER_NAME],
    USER_PASSWORD[129],
    USER_ADMIN,
    USER_MONEY,
    USER_SCORE,

    bool: USER_LOGGED_IN
};

new User[MAX_PLAYERS][USER_DATA];
new DB: Database;

public OnGameModeInit()
{
    Database = db_open("server.db");
    db_query(Database, "CREATE TABLE IF NOT EXISTS users (userid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(24) COLLATE NOCASE, password VARCHAR(129), admin INTEGER DEFAULT 0 NOT NULL, money INTEGER, score INTEGER)");
    return 1;
}

public OnGameModeExit()
{
    db_close( Database );
    return 1;
}

public OnPlayerConnect(playerid)
{
    for(new i; i < _: USER_DATA; ++i) User[playerid][USER_DATA: i] = 0;
   
    GetPlayerName(playerid, User[playerid][USER_NAME], MAX_PLAYER_NAME);

    new Query[71], DBResult: Result;
    format(Query, sizeof(Query), "SELECT password FROM users WHERE username = '%s' LIMIT 0, 1", DB_Escape(User[playerid][USER_NAME]));
    Result = db_query(Database, Query);
    if(db_num_rows(Result))
    {
        db_get_field_assoc(Result, "password", User[playerid][USER_PASSWORD], 129);
        ShowPlayerDialog(playerid, 0, DIALOG_STYLE_PASSWORD, ""COL_LIGHTBLUE"CSZM"COL_WHITE" Login pannel", ""chat" Please login to play on "COL_LIGHTBLUE"Counter Strike Zombie Mode\n "COL_WHITE"Enter your "COL_LIGHTBLUE"password"COL_WHITE":", "Login", "Exit");
    }
    else ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, ""COL_LIGHTBLUE"CSZM"COL_WHITE" Register pannel", ""chat" Please register to play on "COL_LIGHTBLUE"Counter Strike Zombie Mode\n "COL_WHITE"Enter a "COL_LIGHTBLUE"password"COL_WHITE":", "Register", "Exit");
    db_free_result(Result);
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    if(User[playerid][USER_LOGGED_IN] == true)
    {
        new Query[128];
        format(Query, sizeof(Query), "UPDATE users SET admin = %d, money = %d, score = %d WHERE username = '%s'", User[playerid][USER_ADMIN], GetPlayerMoney(playerid), GetPlayerScore(playerid), DB_Escape(User[playerid][USER_NAME]));
    }

    for(new i; i < _: USER_DATA; ++i) User[playerid][USER_DATA: i] = 0;
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == 0)
    {
        if(response)
        {
            new buf[129];
            WP_Hash(buf, 129, inputtext);
            if(!strcmp(buf, User[playerid][USER_PASSWORD], false))
            {
                new Query[75], DBResult: Result;
                format(Query, sizeof(Query), "SELECT * FROM users WHERE username = '%s' LIMIT 0, 1", DB_Escape(User[playerid][USER_NAME]));
                Result = db_query(Database, Query);
                if(db_num_rows(Result))
                {
                    db_get_field_assoc(Result, "userid", Query, 7);
                    User[playerid][USER_ID] = strval(Query);

                    db_get_field_assoc(Result, "admin", Query, 3);
                    User[playerid][USER_ADMIN] = strval(Query);

                    db_get_field_assoc(Result, "money", Query, 10); //this
                    User[playerid][USER_MONEY] = strval(Query);

                    db_get_field_assoc(Result, "score", Query, 7); //this
                    User[playerid][USER_SCORE] = strval(Query);

                    User[playerid][USER_LOGGED_IN] = true;

                    GivePlayerMoney(playerid, User[playerid][USER_MONEY]);
                    SetPlayerScore(playerid, User[playerid][USER_SCORE]);

                    SendClientMessage(playerid, 0x00FF00FF, ""chat" "COL_LIGHTBLUE"You have successfully logged in to your account!");
                }
                db_free_result(Result);
            }
            else
            {
                SendClientMessage(playerid, -1, ""chat" "COL_RED"Incorrect password!");
                ShowPlayerDialog(playerid, 0, DIALOG_STYLE_PASSWORD, ""COL_LIGHTBLUE"CSZM"COL_WHITE" Login pannel", ""chat" Please login to play on "COL_LIGHTBLUE"Counter Strike Zombie Mode\n "COL_WHITE"Enter your "COL_LIGHTBLUE"password"COL_WHITE":", "Login", "Exit");

            }
        }
        else Kick(playerid);
        return 1;
    }
    if(dialogid == 1)
    {
        if(response)
        {
            if(!IsValidPassword(inputtext))
            {
                SendClientMessage(playerid, -1, ""chat" "COL_RED"The password is invalid, Valid characters are: A-Z, a-z, 0-9");
                ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, ""COL_LIGHTBLUE"CSZM"COL_WHITE" Register pannel", ""chat" Please register to play on "COL_LIGHTBLUE"Counter Strike Zombie Mode\n "COL_WHITE"Enter a "COL_LIGHTBLUE"password"COL_WHITE":", "Register", "Exit");
                return 1;
            }
            if(strlen(inputtext) < 3 || strlen(inputtext) > 24)
            {
                SendClientMessage(playerid, -1, ""chat" "COL_RED"The password is invalid, Its lenght should be 3-24 characters");
                ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, ""COL_LIGHTBLUE"CSZM"COL_WHITE" Register pannel", ""chat" Please register to play on "COL_LIGHTBLUE"Counter Strike Zombie Mode\n "COL_WHITE"Enter a "COL_LIGHTBLUE"password"COL_WHITE":", "Register", "Exit");
                return 1;
            }
            new Query[208];
            WP_Hash(User[playerid][USER_PASSWORD], 129, inputtext);
            format(Query, sizeof(Query), "INSERT INTO users (username, password) VALUES ('%s', '%s')", DB_Escape(User[playerid][USER_NAME]), DB_Escape(User[playerid][USER_PASSWORD]));
            db_query(Database, Query);

            User[playerid][USER_LOGGED_IN] = true;
            SendClientMessage(playerid, 0x00FF00FF, ""chat" "COL_LIGHTBLUE"You have just registered to our server! You have been automatically logged in!");
        }
        else Kick(playerid);
        return 1;
    }
    return 0;
}

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;
}
Reply
#10

It seems when I posted the OnPlayerDisconnect before I accidentally removed the line about db_query, sorry.

Change to:
pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    if(User[playerid][USER_LOGGED_IN] == true)
    {
        new Query[128];
        format(Query, sizeof(Query), "UPDATE users SET admin = %d, money = %d, score = %d WHERE username = '%s'", User[playerid][USER_ADMIN], GetPlayerMoney(playerid), GetPlayerScore(playerid), DB_Escape(User[playerid][USER_NAME]));
        db_query(Database, Query);
    }

    for(new i; i < _: USER_DATA; ++i) User[playerid][USER_DATA: i] = 0;
    return 1;
}
Add default values:
pawn Code:
db_query(Database, "CREATE TABLE IF NOT EXISTS users (userid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(24) COLLATE NOCASE, password VARCHAR(129), admin INTEGER DEFAULT 0 NOT NULL, money INTEGER DEFAULT 0 NOT NULL, score INTEGER DEFAULT 0 NOT NULL)");
And also check if the inputtext was not NULL.
pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == 0)
    {
        if(response)
        {
            if(!inputtext[0]) return ShowPlayerDialog(playerid, 0, DIALOG_STYLE_PASSWORD, ""COL_LIGHTBLUE"CSZM"COL_WHITE" Login pannel", ""chat" Please login to play on "COL_LIGHTBLUE"Counter Strike Zombie Mode\n "COL_WHITE"Enter your "COL_LIGHTBLUE"password"COL_WHITE":", "Login", "Exit");
            new buf[129];
            WP_Hash(buf, 129, inputtext);
            if(!strcmp(buf, User[playerid][USER_PASSWORD], false))
            {
                new Query[75], DBResult: Result;
                format(Query, sizeof(Query), "SELECT * FROM users WHERE username = '%s' LIMIT 0, 1", DB_Escape(User[playerid][USER_NAME]));
                Result = db_query(Database, Query);
                if(db_num_rows(Result))
                {
                    db_get_field_assoc(Result, "userid", Query, 7);
                    User[playerid][USER_ID] = strval(Query);
                   
                    db_get_field_assoc(Result, "admin", Query, 3);
                    User[playerid][USER_ADMIN] = strval(Query);
                   
                    db_get_field_assoc(Result, "money", Query, 10); //this
                    User[playerid][USER_MONEY] = strval(Query);
                   
                    db_get_field_assoc(Result, "score", Query, 7); //this
                    User[playerid][USER_SCORE] = strval(Query);
                   
                    User[playerid][USER_LOGGED_IN] = true;
                   
                    GivePlayerMoney(playerid, User[playerid][USER_MONEY]);
                    SetPlayerScore(playerid, User[playerid][USER_SCORE]);
                   
                    SendClientMessage(playerid, 0x00FF00FF, ""chat" "COL_LIGHTBLUE"You have successfully logged in to your account!");
                }
                db_free_result(Result);
            }
            else
            {
                SendClientMessage(playerid, -1, ""chat" "COL_RED"Incorrect password!");
                ShowPlayerDialog(playerid, 0, DIALOG_STYLE_PASSWORD, ""COL_LIGHTBLUE"CSZM"COL_WHITE" Login pannel", ""chat" Please login to play on "COL_LIGHTBLUE"Counter Strike Zombie Mode\n "COL_WHITE"Enter your "COL_LIGHTBLUE"password"COL_WHITE":", "Login", "Exit");
               
            }
        }
        else Kick(playerid);
        return 1;
    }
    // rest..
Add those, remove the database and try again. It should work.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)