Stats resets upon spawn
#1

Hello, I have used the Y_INI method to make a register system and I am having an issue, I am using an ordinary method to avoid other players from achieving other players stats by accident but for some reason it seems to reset the players stats as soon as he connects and that never happened to me before.

Here is my code.

PHP код:

public OnPlayerConnect(playerid)
{
    new 
name[MAX_PLAYER_NAME];
    
GetPlayerName(playeridnamesizeof(name));
    
    
pInfo[playerid][Admin] = 0;
    
pInfo[playerid][Score] = 0;
    
pInfo[playerid][Money] = 0;
    
pInfo[playerid][VIP] = 0;
    
pInfo[playerid][Kills] = 0;
    
pInfo[playerid][Deaths] = 0;
    
    if(
fexist(Path(playerid)))
    {
        
INI_ParseFile(Path(playerid),"loadaccount_%s", .bExtra true, .extra playerid);
        
ShowPlayerDialog(playerid,login,DIALOG_STYLE_PASSWORD,"Login","Welcome back. This account is registered. \nInsert your password to login to your account","Login","Quit");
    }
    else
    {
        
ShowPlayerDialog(playerid,register,DIALOG_STYLE_PASSWORD,"Register","Welcome! This account is not registered.\nEnter your own password to create a new account.","Register","Quit");
        return 
1;
      }
    return 
1;

if you could point out my mistake, I would really appreciate it.
Reply
#2

There is nothing wrong with anything there, consider publishing further.
Reply
#3

For some reason it writes my stats in the ini file upside down.

Код:
[Player's Data]
Deaths = 0
Kills = 0
VIP = 0
Money = 0
Score = 0
Admin = 5
Password = 988EEC2A02039635B6836CE3700EDA7DB441BE8B4675492A53C20A70CDA2A75C9852FB52DDFF2A0F51BB4A3E1EA46A228B36542B6E9F439382806E3AA80F3260
pawn Код:
public loadaccount_user(playerid, name[], value[])
{
     INI_String("Password", pInfo[playerid][Pass],129);
     INI_Int("Admin",pInfo[playerid][Admin]);
     INI_Int("Score",pInfo[playerid][Score]);
     INI_Int("Money",pInfo[playerid][Money]);
     INI_Int("VIP",pInfo[playerid][VIP]);
     INI_Int("Kills",pInfo[playerid][Kills]);
     INI_Int("Deaths",pInfo[playerid][Deaths]);
     return 1;
}

pawn Код:
public OnPlayerDisconnect(playerid, reason)
{
    new INI:file = INI_Open(Path(playerid));
        INI_SetTag(file,"Player's Data");
        INI_WriteInt(file,"Admin",pInfo[playerid][Admin]);
        INI_WriteInt(file,"Score",GetPlayerScore(playerid));
        INI_WriteInt(file,"Money",GetPlayerMoney(playerid));
        INI_WriteInt(file,"VIP",pInfo[playerid][VIP]);
        INI_WriteInt(file,"Kills",pInfo[playerid][Kills]);
        INI_WriteInt(file,"Deaths",pInfo[playerid][Deaths]);
        INI_Close(file);
    return 1;
}
pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == register)
    {
        if(!response) return Kick(playerid);
        if(response)
        {
            if(!strlen(inputtext))
            {
                ShowPlayerDialog(playerid,register,DIALOG_STYLE_PASSWORD,"Register","Welcome! This account is not registered.\nEnter your own password to create a new account.\nPlease enter the password!","Register","Quit");
                return 1;
            }
            new hashpass[129];
            WP_Hash(hashpass,sizeof(hashpass),inputtext);
            new INI:file = INI_Open(Path(playerid));
            INI_SetTag(file,"Player's Data");
            INI_WriteString(file,"Password",hashpass);
            INI_WriteInt(file,"Admin",0);
            INI_WriteInt(file,"Score",0);
            INI_WriteInt(file,"Money",0);
            INI_WriteInt(file,"VIP",0);
            INI_WriteInt(file,"Kills",0);
            INI_WriteInt(file,"Deaths",0);
            INI_Close(file);
            INI_ParseFile(Path(playerid),"loadaccount_%s", .bExtra = true, .extra = playerid);
            SendClientMessage(playerid,-1,"You have been successfully registered");
            return 1;
        }
    }
    if(dialogid == login)
    {
        if(!response) return Kick(playerid);
        if(response)
        {
            new hashpass[129];
            WP_Hash(hashpass,sizeof(hashpass),inputtext);
            if(!strcmp(hashpass, pInfo[playerid][Pass], false))
            {
                INI_ParseFile(Path(playerid),"loadaccount_%s",.bExtra = true, .extra = playerid);
                SetPlayerScore(playerid,pInfo[playerid][Score]);
                GivePlayerMoney(playerid,pInfo[playerid][Money]);
                SendClientMessage(playerid,-1,"Welcome back! You have successfully logged in");
            }
            else
            {
                ShowPlayerDialog(playerid,login,DIALOG_STYLE_INPUT,"Login","Welcome back. This account is registered. \nInsert your password to login to your account.\nIncorrect password!","Login","Quit");//We will tell to them that they've entered an incorrect password
                return 1;
            }
        }
    }
    return 1;
}
Reply
#4

Take the following script as a reference:
pawn Код:
// [ DEVELOPMENT GAMEMODE ]

// INCLUDES:

#include <a_samp>
#include <YSI\y_ini>

// NATIVES:

// WHIRLPOOL:

native WP_Hash(buffer[], len, const str[]);

// UNDEFINES:

#undef isnull

// DEFINES:

// FUNCTIONS:

#define function%0(%1) forward%0(%1); public%0(%1)
#define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))

// DIALOGS:

#define DIALOG_REGISTER 0
#define DIALOG_LOGIN 1

// DATABASE:

#define USER_TAG "player_data"
#define USER_PASSWORD "password"
#define USER_SCORE "score"
#define USER_CASH "cash"
#define USER_KILLS "kills"
#define USER_DEATHS "deaths"
#define USER_VIP_LEVEL "vip_level"
#define USER_ADMIN_LEVEL "admin_level"

// ARRAYS AND ENUMERATORS:

enum eUserInfo
{
    user_password[129],
    user_score,
    user_cash,
    user_kills,
    user_deaths,
    user_vip_level,
    user_admin_level
}

new aUserInfo[MAX_PLAYERS][eUserInfo];

// VARIABLES:

// STATES:

new bool:sSignedIn[MAX_PLAYERS] = false;

// MAIN:

main()
{
    print("Development Mode: ini_login_system.amx");
}

// CALLBACKS:

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerConnect(playerid)
{
    ResetPlayerVariables(playerid);

    if(fexist(UserPath(playerid)))
    {
        INI_ParseFile(UserPath(playerid), "LoadUserData", .bExtra = true, .extra = playerid);
        ShowLoginDialog(playerid, "");
    }
    else ShowRegisterDialog(playerid, "");
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    if(IsPlayerSignedIn(playerid))
    {
        new INI:file = INI_Open(UserPath(playerid));
        INI_SetTag(file, USER_TAG);
        INI_WriteInt(file, USER_SCORE, GetPlayerScore(playerid));
        INI_WriteInt(file, USER_CASH, GetPlayerMoney(playerid));
        INI_WriteInt(file, USER_KILLS, aUserInfo[playerid][user_kills]);
        INI_WriteInt(file, USER_DEATHS, aUserInfo[playerid][user_deaths]);
        INI_WriteInt(file, USER_VIP_LEVEL, aUserInfo[playerid][user_vip_level]);
        INI_WriteInt(file, USER_ADMIN_LEVEL, aUserInfo[playerid][user_admin_level]);
        INI_Close(file);
    }

    ResetPlayerVariables(playerid);
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case DIALOG_REGISTER:
        {
            if(!response) return KickPlayer(playerid);
            if(response)
            {
                if(isnull(inputtext)) return ShowRegisterDialog(playerid, "Enter a password.");

                new hash[129];
                WP_Hash(hash, sizeof(hash), inputtext);

                new INI:file = INI_Open(UserPath(playerid));
                INI_SetTag(file, USER_TAG);
                INI_WriteString(file, USER_PASSWORD, hash);
                INI_WriteInt(file, USER_SCORE, 0);
                INI_WriteInt(file, USER_CASH, 0);
                INI_WriteInt(file, USER_KILLS, 0);
                INI_WriteInt(file, USER_DEATHS, 0);
                INI_WriteInt(file, USER_VIP_LEVEL, 0);
                INI_WriteInt(file, USER_ADMIN_LEVEL, 0);
                INI_Close(file);

                sSignedIn[playerid] = true;

                SendClientMessage(playerid, -1, "You have successfully registered an account.");
            }
        }
        case DIALOG_LOGIN:
        {
            if(!response) return KickPlayer(playerid);
            if(response)
            {
                if(isnull(inputtext)) return ShowLoginDialog(playerid, "Enter a password.");

                new hash[129];
                WP_Hash(hash, sizeof(hash), inputtext);

                if(strcmp(aUserInfo[playerid][user_password], hash, false) == 0)
                {
                    INI_ParseFile(UserPath(playerid), "LoadUserData", .bExtra = true, .extra = playerid);
                    SetPlayerScore(playerid, aUserInfo[playerid][user_score]);
                    GivePlayerMoney(playerid, aUserInfo[playerid][user_cash]);

                    sSignedIn[playerid] = true;

                    SendClientMessage(playerid, -1, "You have successfully logged into your account.");
                }
                else return ShowLoginDialog(playerid, "Incorrect password.");
            }
        }
    }
    return 1;
}

// FUNCTIONS:

stock ResetPlayerVariables(playerid)
{
    // ARRAYS AND ENUMERATORS:
    aUserInfo[playerid][user_password] = EOS;
    aUserInfo[playerid][user_score] = 0;
    aUserInfo[playerid][user_cash] = 0;
    aUserInfo[playerid][user_kills] = 0;
    aUserInfo[playerid][user_deaths] = 0;
    aUserInfo[playerid][user_vip_level] = 0;
    aUserInfo[playerid][user_admin_level] = 0;

    // PER-PLAYER VARIABLES:

    // STATES:

    sSignedIn[playerid] = false;
}

function LoadUserData(playerid, name[], value[])
{
    INI_String(USER_PASSWORD, aUserInfo[playerid][user_password], 129);
    INI_Int(USER_SCORE, aUserInfo[playerid][user_score]);
    INI_Int(USER_CASH, aUserInfo[playerid][user_cash]);
    INI_Int(USER_KILLS, aUserInfo[playerid][user_kills]);
    INI_Int(USER_DEATHS, aUserInfo[playerid][user_deaths]);
    INI_Int(USER_VIP_LEVEL, aUserInfo[playerid][user_vip_level]);
    INI_Int(USER_ADMIN_LEVEL, aUserInfo[playerid][user_admin_level]);
    return 1;
}

stock ShowRegisterDialog(playerid, error[])
{
    new string[256];
    strcat(string, "Welcome. This account is not registered.\n");
    strcat(string, "Enter a password below to register an account.");
    if(!isnull(error)) format(string, sizeof(string), "%s\n\n{FF0000}%s", string, error);
    return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "Register", string, "Register", "Quit");
}

stock ShowLoginDialog(playerid, error[])
{
    new string[256];
    strcat(string, "Welcome back. This account is already registered.\n");
    strcat(string, "Insert your password to log into your account.");
    if(!isnull(error)) format(string, sizeof(string), "%s\n\n{FF0000}%s", string, error);
    return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", string, "Login", "Quit");
}

stock KickPlayer(playerid) return SetTimerEx("KickPlayerEx", 100, false, "i", playerid);

function KickPlayerEx(playerid) return Kick(playerid);

stock UserPath(playerid)
{
    new string[128], name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    format(string, sizeof(string), "/Users/%s.ini", name);
    return string;
}

stock IsPlayerSignedIn(playerid)
{
    if(sSignedIn[playerid]) return true;
    return false;
}
Reply
#5

Thanks, SickAttack you saved me a lot of trouble. +REP
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)