Position & Skin Saving
#1

Hello again. If you don't already know, I've made a GM release and I am having some trouble solving a semi-annoying bug in the script. When a player leaves the server, it will save their position and skin. However, when the server is restarted they will fall through the ground, loose their skin and end up in Blueberry. Is there something wrong with this code?

pawn Code:
public OnGameModeExit()
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnectedEx(i))
        {
            GetPlayerPos(i, PlayerStatistics[i][pPositionX], PlayerStatistics[i][pPositionY], PlayerStatistics[i][pPositionZ]);
            SavePlayerAccount(i);
        }
        else
        {
            return 1;
        }
    }
    mysql_close();
    return 1;
}
Perhaps it should go under "OnPlayerDisconnect"? I am lost on this deal, could you please help me?
Reply
#2

Firstly, we would need to see more parts of your code, like the SavePlayerAccount function to actually see your ways of saving.

Also, with the else check, you should replace the 'return 1;' with "continue;". This will go onto the next set, in this case, the next playerid instead of ending the function.
Reply
#3

Okay, sure. This is the "SavePlayerAccount(...);" and "LoadAccountVariables(...);" functions.

pawn Code:
stock SavePlayerAccount(playerid)
{
    if(PlayerStatistics[playerid][pAuth] == 1)
    {
        PlayerStatistics[playerid][pScore] = GetPlayerScore(playerid);
        PlayerStatistics[playerid][pMoney] = GetPlayerMoney(playerid);
        PlayerStatistics[playerid][pSkin] = GetPlayerSkin(playerid);
        format(Query, sizeof(Query), "UPDATE `Accounts` SET `Password` = '%s', `AdminLevel` = '%d', `AdminDuty` = '%d', `Money` = '%d', `Score` = '%d', `Skin` = '%d' WHERE `UserID` = '%d'", PlayerStatistics[playerid][pPassword], PlayerStatistics[playerid][pAdminLevel], PlayerStatistics[playerid][pAdminDuty], PlayerStatistics[playerid][pMoney], PlayerStatistics[playerid][pScore], PlayerStatistics[playerid][pSkin], PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(Query);

        GetPlayerPos(playerid, PlayerStatistics[playerid][pPositionX], PlayerStatistics[playerid][pPositionY], PlayerStatistics[playerid][pPositionZ]);
        format(Query, sizeof(Query), "UPDATE `Accounts` SET `PositionX` = '%f', `PositionY` = '%f', `PositionZ` = '%f' WHERE `UserID` = '%d'", PlayerStatistics[playerid][pPositionX], PlayerStatistics[playerid][pPositionY], PlayerStatistics[playerid][pPositionZ], PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(Query);
    }
    return 1;
}

stock LoadAccountVariables(playerid)
{
    if(IsPlayerConnected(playerid))
    {
        format(Query, sizeof(Query), "SELECT * FROM `Accounts` WHERE `UserID` = %d", PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(Query);
        mysql_store_result();
        mysql_fetch_row(Query, "|");
        sscanf(Query, "e<p<|>ds[23]s[129]dddddfff>", PlayerStatistics[playerid]);
        SpawnPlayer(playerid);
    }
    else
    {
        printf("[MySQL ERROR] LoadAccountVariables() was called, but to a non-connected ID.", playerid);
    }
    mysql_free_result();
}
Reply
#4

Quote:
Originally Posted by RealCop228
View Post
Okay, sure. This is the "SavePlayerAccount(...);" and "LoadAccountVariables(...);" functions.

pawn Code:
stock SavePlayerAccount(playerid)
{
    if(PlayerStatistics[playerid][pAuth] == 1)
    {
        PlayerStatistics[playerid][pScore] = GetPlayerScore(playerid);
        PlayerStatistics[playerid][pMoney] = GetPlayerMoney(playerid);
        PlayerStatistics[playerid][pSkin] = GetPlayerSkin(playerid);
        format(Query, sizeof(Query), "UPDATE `Accounts` SET `Password` = '%s', `AdminLevel` = '%d', `AdminDuty` = '%d', `Money` = '%d', `Score` = '%d', `Skin` = '%d' WHERE `UserID` = '%d'", PlayerStatistics[playerid][pPassword], PlayerStatistics[playerid][pAdminLevel], PlayerStatistics[playerid][pAdminDuty], PlayerStatistics[playerid][pMoney], PlayerStatistics[playerid][pScore], PlayerStatistics[playerid][pSkin], PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(Query);

        GetPlayerPos(playerid, PlayerStatistics[playerid][pPositionX], PlayerStatistics[playerid][pPositionY], PlayerStatistics[playerid][pPositionZ]);
        format(Query, sizeof(Query), "UPDATE `Accounts` SET `PositionX` = '%f', `PositionY` = '%f', `PositionZ` = '%f' WHERE `UserID` = '%d'", PlayerStatistics[playerid][pPositionX], PlayerStatistics[playerid][pPositionY], PlayerStatistics[playerid][pPositionZ], PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(Query);
    }
    return 1;
}

stock LoadAccountVariables(playerid)
{
    if(IsPlayerConnected(playerid))
    {
        format(Query, sizeof(Query), "SELECT * FROM `Accounts` WHERE `UserID` = %d", PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(Query);
        mysql_store_result();
        mysql_fetch_row(Query, "|");
        sscanf(Query, "e<p<|>ds[23]s[129]dddddfff>", PlayerStatistics[playerid]);
        SpawnPlayer(playerid);
    }
    else
    {
        printf("[MySQL ERROR] LoadAccountVariables() was called, but to a non-connected ID.", playerid);
    }
    mysql_free_result();
}
You never load the player's "PlayerStatistics[playerid][pDatabaseID]". Then you use it in a query. Unless there is more code behind this.
Reply
#5

I believe I do, actually...

OnGameModeInit - OnPlayerDisconnect

pawn Code:
public OnGameModeInit()
{
    mysql_init();
    mysql_connect(SQL_HOST, SQL_USER, SQL_PASS, SQL_DATA);
   
    AntiDeAMX();
    ShowNameTags(0);
   
    SetGameModeText(SVERSION);

    new sendcmd[50];
    format(sendcmd, sizeof(sendcmd), "hostname %s", ServerName);
    SendRconCommand(sendcmd);
   
    SetTimer("AAC", 21600000, true);
   
    LoadVehiclesFromDatabase();
    return 1;
}

public OnGameModeExit()
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnectedEx(i))
        {
            GetPlayerPos(i, PlayerStatistics[i][pPositionX], PlayerStatistics[i][pPositionY], PlayerStatistics[i][pPositionZ]);
            SavePlayerAccount(i);
        }
        else
        {
            return 1;
        }
    }
    mysql_close();
    return 1;
}

public OnPlayerConnect(playerid)
{
    format(string, sizeof(string), "%s has joined the server.", GetName(playerid));
    SendClientMessageToAll(COLOR_GREY, string);
    SetPlayerColor(playerid, PlayerColor);

    InitPlayerConnection(playerid);
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    format(string, sizeof(string), "%s has left the server.", GetName(playerid));
    SendClientMessageToAll(COLOR_GREY, string);

    SavePlayerAccount(playerid);
    return 1;
}
The rest...

pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case 1:
        {
            if(!response) // If the player hit "exit" we're going to kick them
            {
                SendClientMessage(playerid, COLOR_RED, "You selected exit, therefore you were kicked.");
                Kick(playerid);
            }
            else if(strlen(inputtext) == 0)
            {
                SendClientMessage(playerid, COLOR_SYSTEM, "You must input the password to your account! If this is not your account, come back with a new name!");
                ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Login / Authentication Process", "Hello there!\n\nPlease enter your password to authenticate.", "Login", "Cancel");
            }
            else
            {
                new buf[129];
                GetFromAccount(PlayerStatistics[playerid][pDatabaseID], "Password", PlayerStatistics[playerid][pPassword]);
                WP_Hash(buf, sizeof (buf), inputtext);
                if(!strcmp(buf, PlayerStatistics[playerid][pPassword], true))
                {
                    PlayerStatistics[playerid][pAuth] = 1;
                    LoadAccountVariables(playerid);

                    format(string, sizeof(string), "You have successfully logged in, %s!", GetName(playerid));
                    SendClientMessage(playerid, COLOR_ORANGE, string);

                    SetPlayerMoney(playerid, PlayerStatistics[playerid][pMoney]);
                    SetPlayerScore(playerid, PlayerStatistics[playerid][pScore]);

                    format(string, sizeof(string), "%s (%d)\n Score: %i", GetName(playerid), playerid, GetPlayerScore(playerid));
                    pNameTag[playerid] = Create3DTextLabel(string, COLOR_YELLOW, 30.0, 40.0, 50.0, 15.0, -1, 1);
                    Attach3DTextLabelToPlayer(pNameTag[playerid], playerid, 0.0, 0.0, 0.2);
                }
                else
                {
                    SendClientMessage(playerid, COLOR_WHITE, "Incorrect password. Please try again!");
                    ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Login / Authentication Process", "Hello there!\n\nPlease enter your password to authenticate.", "Login", "Cancel");
                }
            }
        }
        case 2:
        {
            if(!response)  // If the player hit "exit" we're going to kick them
            {
                SendClientMessage(playerid, COLOR_SYSTEM, "You selected exit, therfore you have been kicked!");
                Kick(playerid);
            }
            else if(strlen(inputtext) < 4 || strlen(inputtext) > 30)
            {
                SendClientMessage(playerid, COLOR_SYSTEM, "Your password must be at least 4, but less than 30 alhpanumeric characters.");
                ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Hello there!\n\nPlease enter a password to register.", "Authenticate", "Cancel");
            }
            else
            {
                new buf[130], EscapedName[MAX_PLAYER_NAME];
                mysql_real_escape_string(GetName(playerid), EscapedName);
                WP_Hash(buf, sizeof (buf), inputtext);

                format(Query, sizeof(Query), "INSERT INTO `Accounts` (Username, Password) VALUES('%s', '%s')", EscapedName, buf);
                mysql_query(Query);

                JustRegistered = 1;
                PlayerStatistics[playerid][pAuth] = 1;

                format(string, sizeof(string), "You have automatically logged in, %s!", GetName(playerid));
                SendClientMessage(playerid, COLOR_ORANGE, string);

                SpawnPlayer(playerid);
            }
        }
    }
}

public OnPlayerClickPlayer(playerid, clickedplayerid, source)
{
    return 1;
}

// -------------------- [MySQL Stocks and Publics] ------------------------ //

stock ResetPlayerVariables(playerid)
{
    PlayerStatistics[playerid][pDatabaseID] = 0;
    PlayerStatistics[playerid][pAdminLevel] = 0;
    PlayerStatistics[playerid][pMoney] = 0;
    PlayerStatistics[playerid][pAuth] = 0;
    PlayerStatistics[playerid][pPositionX] = 0.00;
    PlayerStatistics[playerid][pPositionY] = 0.00;
    PlayerStatistics[playerid][pPositionZ] = 0.00;
    return 1;
}

stock InitPlayerConnection(playerid)
{
    ResetPlayerVariables(playerid);
    new EscapedName[MAX_PLAYER_NAME];

    mysql_real_escape_string(GetName(playerid), EscapedName);

    format(Query, sizeof(Query), "SELECT `UserID` FROM `Accounts` WHERE `Username` = '%s'", EscapedName);
    mysql_query(Query);
    mysql_store_result();

    if(mysql_num_rows() >= 1)
    {
        if(mysql_num_rows() >= 2)
        {
            mysql_free_result();
            SendClientMessage(playerid, COLOR_WHITE, "There seems to be duplicates of your account, please contact a high-level admin.");
            Kick(playerid);
        }
        else
        {
            PlayerStatistics[playerid][pDatabaseID] = mysql_fetch_int();
            mysql_free_result();

            ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Login / Authentication Process", "Hello there!\n\nPlease enter your password to authenticate.", "Login", "Cancel");
            SendClientMessage(playerid, COLOR_WHITE, "Your account exists. Please enter your password to proceed.");
        }
    }
    else
    {
        mysql_free_result();
        SendClientMessage(playerid, COLOR_WHITE, "Your account does not yet exist. Enter a password to register one!");
        ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Hello there!\n\nPlease enter a password to register.", "Register", "Cancel");
    }
}

stock SavePlayerAccount(playerid)
{
    if(PlayerStatistics[playerid][pAuth] == 1)
    {
        PlayerStatistics[playerid][pScore] = GetPlayerScore(playerid);
        PlayerStatistics[playerid][pMoney] = GetPlayerMoney(playerid);
        PlayerStatistics[playerid][pSkin] = GetPlayerSkin(playerid);
        format(Query, sizeof(Query), "UPDATE `Accounts` SET `Password` = '%s', `AdminLevel` = '%d', `AdminDuty` = '%d', `Money` = '%d', `Score` = '%d', `Skin` = '%d' WHERE `UserID` = '%d'", PlayerStatistics[playerid][pPassword], PlayerStatistics[playerid][pAdminLevel], PlayerStatistics[playerid][pAdminDuty], PlayerStatistics[playerid][pMoney], PlayerStatistics[playerid][pScore], PlayerStatistics[playerid][pSkin], PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(Query);

        GetPlayerPos(playerid, PlayerStatistics[playerid][pPositionX], PlayerStatistics[playerid][pPositionY], PlayerStatistics[playerid][pPositionZ]);
        format(Query, sizeof(Query), "UPDATE `Accounts` SET `PositionX` = '%f', `PositionY` = '%f', `PositionZ` = '%f' WHERE `UserID` = '%d'", PlayerStatistics[playerid][pPositionX], PlayerStatistics[playerid][pPositionY], PlayerStatistics[playerid][pPositionZ], PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(Query);
    }
    return 1;
}

stock LoadAccountVariables(playerid)
{
    if(IsPlayerConnected(playerid))
    {
        format(Query, sizeof(Query), "SELECT * FROM `Accounts` WHERE `UserID` = %d", PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(Query);
        mysql_store_result();
        mysql_fetch_row(Query, "|");
        sscanf(Query, "e<p<|>ds[23]s[129]dddddfff>", PlayerStatistics[playerid]);
        SpawnPlayer(playerid);
    }
    else
    {
        printf("[MySQL ERROR] LoadAccountVariables() was called, but to a non-connected ID.", playerid);
    }
    mysql_free_result();
}

stock GetFromAccount(dbid, obtaining[], holdingvar[])
{
    format(Query, sizeof(Query), "SELECT `%s` FROM `Accounts` WHERE `UserID` = '%d'", obtaining, dbid);
    mysql_query(Query);
    mysql_store_result();
    if(mysql_fetch_row(holdingvar) == 1)
    {
        mysql_free_result();
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)