Overwrite an existing Entry in a ini file
#1

I'm trying to save player ranks to a file, and load them when a player connects but it's not doing it properly.

Код:
enum PlayerInfo
{
	Pass[129],
	AdmLevel,
	Money,
	Score,
	Kills,
	Deaths,
	Rank[128],
};
Code for when player first registers, it automatically sets "Rank" to "Player":

Код:
INI_WriteString(file,"Rank","Player");
Code to load the rank when player connects

Код:
format(string2,sizeof(string2),"%s %s (%d) connected:",pInfo[playerid][Rank],Name(playerid),playerid);
Save the rank (if player changes it) to the file:

Код:
INI_WriteString(file,"Rank",pInfo[playerid][Rank]);
This creates a 3d text label with the player's name, id, and whatever rank he/she has in his/her ini file:

Код:
format(ranklabel,sizeof(ranklabel),"%s (%d)\n%s",Name(playerid),playerid,pInfo[playerid][Rank]);
	rank[playerid] = Create3DTextLabel(ranklabel,COLOR_YELLOW,0.0,0.0,0.0,100.0,GetPlayerVirtualWorld(playerid),1);
	Attach3DTextLabelToPlayer(rank[playerid],playerid,0.0,0.0,0.0);
This is the command to change the player's rank:

Код:
CMD:setlabel(playerid,params[])
{
	new ranklabel2[128], plrrank[128],name[MAX_PLAYER_NAME],string[128];
	GetPlayerName(playerid,name,sizeof(name));
	{
		if(sscanf(params,"s[128]",string)) return SendClientMessage(playerid, 0xFF0000FF,"USAGE: /setlabel <text>");
		if(strlen(ranklabel2) > 32) return SendClientMessage(playerid,0xFF0000FF,"ERROR: Rank must be less than 32 characters.");
		{
		    new INI:file = INI_Open(Path(playerid));
			format(ranklabel2,sizeof(ranklabel2),"%s (%d)\n%s",name,playerid,string);
			Update3DTextLabelText(rank[playerid],0xFFFF00FF,ranklabel2);
			format(plrrank,sizeof(plrrank),"You have set your rank to: %s",string);
			SendClientMessage(playerid,0x008000FF,plrrank);
			INI_RemoveEntry(file,"Rank");
			INI_WriteString(file,"Rank",string);
			INI_Close(file);
		}
	}
	return 1;
}
Note: You will see why I have "RemoveEntry" in the command.

Little forward to load user info (yeah i had to resort to a tutorial, I aint pro yet)
Код:
forward loadaccount_user(playerid,name[],value[]);
public loadaccount_user(playerid,name[],value[])
{
	INI_String("Pass",pInfo[playerid][Pass],129);
	INI_Int("AdmLevel",pInfo[playerid][AdmLevel]);
	INI_Int("Money",pInfo[playerid][Money]);
	INI_Int("Score",pInfo[playerid][Score]);
	INI_Int("Kills",pInfo[playerid][Kills]);
	INI_Int("Deaths",pInfo[playerid][Deaths]);
	INI_String("Rank",pInfo[playerid][Rank],128);
	return 1;
}
Now to explain the whole thing....so hold on (I gotta jumble all of this in one post cause I'm traveling and I will not know when I will stop at another stop with internet)

This is the player's ini file when they first register:

Код:
[Player's Data]
Pass = 4038C6D0353755AE40BA7D3EF4B1636380CC6310D7EA4B05636DD0355BF83569BF09C5C18F4738B75902F495F9E78C13DB77E6543DD7BFB3C3DE35AD2277DCED
AdmLevel = 0
Money = 0
Score = 0
Kills = 0
Deaths = 0
Rank = Player
The Rank is clearly specified but look what it shows when I connect:


So I go ahead and change my label with the set label cmd:


Recheck the ini
Код:
Rank = Testing
[Player's Data]
Pass = 4038C6D0353755AE40BA7D3EF4B1636380CC6310D7EA4B05636DD0355BF83569BF09C5C18F4738B75902F495F9E78C13DB77E6543DD7BFB3C3DE35AD2277DCED
AdmLevel = 0
Money = 0
Score = 0
Kills = 0
Deaths = 0
Rank = Player
[Player Data]
AdmLevel = 0
Money = 0
Score = 0
Kills = 0
Deaths = 0
Rank =
I go in game and it still displays no rank (couldnt post the image cause I'm starting to run low on time)

If I changed the second "Rank =" to "Rank = Insert rank here" it actually works. So I have no idea how to make y_ini replace an existing entry or "overwrite" ...cause it just makes a new "Rank" that's not included under the Player Data tag (which I think might be the problem)

To everyone who reads all of this unorganized crap, thanks again for taking the time. I will rep+ you for helping, and all help is greatly appreciated
Reply
#2

Deleted
Reply
#3

Quote:
Originally Posted by Facerafter
Посмотреть сообщение
Try instead of
pawn Код:
INI_WriteString
this
pawn Код:
INI_WriteInt
I had some like that before.
"Rank" is a string, why would you want to write it as an integer?

I'm not very familiarized with y_ini but I think it has something to do with the tags.

Let me quote this from the y_ini thread.

Quote:
Tags

The other feature in yini not in dini is tags within ini files:

Код:
[LVDM]
health = 4.23
pos = 2500 1968 7.3

[SFTDM]
health = 100
pos = -2134 -980 2
If you had two modes (or even just two libraries) in dini, any user system would need to use separate files for each one. Wheras here the different modes' data are separated by tags (the bits in square brackets). This is where the odd syntax in the read function came in.

Could you show us the register part, just to make sure?
Reply
#4

Deleted
Reply
#5

You just forgot to call INI_SetTag in the command
Reply
#6

Register system:

Код:
if(dialogid == dregister) //If dialog id is a register dialog
    {//then
        if(!response) return Kick(playerid); //If they clicked the second button "Quit", we will kick them.
        if(response) //if they clicked the first button "Register"
        {//then
            if(!strlen(inputtext)) //If they didn't enter any password
            {// then we will tell to them to enter the password to register
				SendClientMessage(playerid,COLOR_RED,"ERROR:No password entered.");
                ShowPlayerDialog(playerid,dregister,DIALOG_STYLE_INPUT,"Register","Welcome to Unknown Assassins' Freeroam\nTo play, you must first register an account.","Register","Quit");
            }
            //If they have entered a correct password for his/her account...
            new hashpass[129];
			new string2[128]; //Now we will create a new variable to hash his/her password
            WP_Hash(hashpass,sizeof(hashpass),inputtext);//We will use whirlpool to hash their inputted text
            new INI:file = INI_Open(Path(playerid)); // we will open a new file for them to save their account inside of Scriptfiles/Users folder
            INI_SetTag(file,"Player's Data");//We will set a tag inside of user's account called "Player's Data"
            INI_WriteString(file,"Pass",hashpass);//This will write a hashed password into user's account
            INI_WriteInt(file,"AdmLevel",0); //Write an integer inside of user's account called "AdminLevel". We will set his level to 0 after he registered.
            INI_WriteInt(file,"Money",0);//Write an integer inside of user's account called "Money". We will set their money to 0 after he registered
            INI_WriteInt(file,"Score",0);//Write an integer inside of user's account called "Scores". We will set their score to 0 after he registered
            INI_WriteInt(file,"Kills",0);//As explained above
            INI_WriteInt(file,"Deaths",0);
            INI_WriteString(file,"Rank","Player");
            INI_Close(file);
			format(string2,sizeof(string2),"%s %s (%d) connected:",pInfo[playerid][Rank],Name(playerid),playerid);
			SendClientMessageToAll(COLOR_GREY,string2);//Now after we've done saving their data, we now need to close the file
            SendClientMessage(playerid,COLOR_WHITE,"You have successfully registered!");//Tell to them that they have successfully registered a new account
        }
        return 1;
    }
    if(dialogid == dlogin) //If dialog id is a login dialog
    {//then
    	if(!response) return Kick(playerid); //If they clicked the second button "Quit", we will kick them.
        if(response) //if they clicked the first button "Register"
        {//then
            new hashpass[129]; //Will create a new variable to hash his/her password
            WP_Hash(hashpass,sizeof(hashpass),inputtext); //Will hash inputted password
            if(!strcmp(hashpass,pInfo[playerid][Pass])) //If they have insert their correct password
            {//then
				new string[128];
                INI_ParseFile(Path(playerid),"loadaccount_user",.bExtra = true, .extra = playerid);//We will load his account's data from user's path
                SetPlayerScore(playerid,pInfo[playerid][Score]);//We will get their score inside of his user's account and we will set it here
                GivePlayerMoney(playerid,pInfo[playerid][Money]);
				format(string,sizeof(string),"%s %s (%d) connected:",pInfo[playerid][Rank],Name(playerid),playerid);
				SendClientMessageToAll(COLOR_GREY,string);//As explained above
                SendClientMessage(playerid,COLOR_WHITE,"You have successfully logged in.");
            }
            else //If they've entered an incorrect password
            {//then
                SendClientMessage(playerid,COLOR_RED,"Wrong password!");
                ShowPlayerDialog(playerid,dlogin,DIALOG_STYLE_INPUT,"Login","You entered a wrong password!\nPlease enter the correct password to log in.","Login","Quit");//We will tell to them that they've entered an incorrect password
            }
        }
    }
    return 1;
}
And I will try the SetTag thing
Reply
#7

Thanks for your help guys, using SetTag did the trick.

Rep+
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)