Y_INI: Can i get how much is the player's money while he's offline?
#1

I couldn't find a way to increase player's money while he's offline! is it possible already?

Example:
pawn Code:
format(string,sizeof(string),"/JCNR/Users/%s.ini", HouseInfo[h][HouseOwner]);
new INI:File = INI_Open(string);
INI_SetTag(File,"UserData");
INI_WriteInt(File,"Cash", Current Cash + Refund);
INI_Close(File);
Reply
#2

Use INI_ParseFile and INI_Int and store player money in a variable the write it with new value
Reply
#3

Basically, you need to load the user's current cash which can be achieved by using INI_ParseFile. You will then need to save this value to a new variable, which we will define at the top of the script. Feel free to change the name of the variable.
pawn Code:
new AdminCashRefund[MAX_PLAYERS] = {0, ...}; //At the top of your script
Then, when you want to refund cash, check if the player exists first...
pawn Code:
format(string, sizeof(string), "/JCNR/Users/%s.ini", HouseInfo[h][HouseOwner]); //Format the path
    if(!fexist(string)) return SendClientMessage(playerid, -1, "Player does not exist."); //If the user doesn't exist...
Then get ready to load the cash to a variable assigned to the admin/playerid doing the refund.
pawn Code:
INI_ParseFile(string, "RefundUser_%s", .bExtra = true, .extra = playerid);
It will call the following function:
pawn Code:
forward RefundUser_UserData(playerid, name[], value[]);
public RefundUser_UserData(playerid, name[], value[])
{
    INI_Int("Cash", AdminCashRefund[playerid]);
    return 1;
}
The current value of 'Cash' will be saved to the variable 'AdminCashRefund[playerid]', where 'playerid' is the ID of the admin doing the refund.

Then you can process the refund...
pawn Code:
new INI:File = INI_Open(string);
    INI_SetTag(File, "UserData");
    INI_WriteInt(File, "Cash", AdminCashRefund[playerid] + Refund);
    INI_Close(File);
So your final code would look like...
pawn Code:
new AdminCashRefund[MAX_PLAYERS] = {0, ...};

CMD:refund(playerid, params[])
{
    //Stuff should be here...
    format(string, sizeof(string), "/JCNR/Users/%s.ini", HouseInfo[h][HouseOwner]);
    if(!fexist(string)) return SendClientMessage(playerid, -1, "Player does not exist.");
    INI_ParseFile(string, "RefundUser_%s", .bExtra = true, .extra = playerid);
    new INI:File = INI_Open(string);
    INI_SetTag(File, "UserData");
    INI_WriteInt(File, "Cash", AdminCashRefund[playerid] + Refund);
    INI_Close(File);
    return 1;
}

forward RefundUser_UserData(playerid, name[], value[]);
public RefundUser_UserData(playerid, name[], value[])
{
    INI_Int("Cash", AdminCashRefund[playerid]);
    return 1;
}
I've put it in a command called 'refund' just for exemplary purposes.
Reply
#4

Thanks for replying, but i'm a lil bit lost in this....

After loading 1 variable (Cash) by INI_ParseFile from the disconnected player's file, What "playerid" refers to, if the player is not connected? what "playerid" returns in this case?

Reply
#5

Well, because the offline player is... 'offline', you can't exactly assign a variable to their ID, because... they don't have an ID. So what you do is assign a variable to the admin who is processing the refund instead. 'AdminCashRefund[playerid]'

'playerid' is the ID of the player who is doing the refund. Not the player 'being' refunded.
Reply
#6

hmm yea i got that but no, it's not gonna work with this. cuz it's also not a player who is doing a refund, i just said refund as an example. But here what's goin on:

Player 1:
- Disconnected.
- Has a house (For Sale).
- $1m sale price for example.

Player 2:
- Online.
- Bought the house while the owner is disconnected.
- GivePlayerMoney(Player 2, -HouseInfo[h][ForSalePrice]); (-$1m)
- How to give 1m to the old owner of the house!

I can do your method as "Player 2 ID" is the 'playerid',, but i should also take care of what if Player 2 has disconnected from the server and Player 1 didn't join yet, that's the conflict here.

Nevermind ill fix this by making new variable called it "BonusMoney" for example and set 1m in it, so when the player connects it will give him PlayerInfo[playerid][pMoney] + PlayerInfo[playerid][BonusMoney]

If you have another method, please tell me. Thank you!
Reply
#7

It doesn't matter whether they join or not. It updates their user file, so they log in and the money is in their account. Leave it how it is, it's fine..
Reply
#8

Would you please tell how it works step by step on this command? this would be easily to understand.
pawn Code:
CMD:increasecash(playerid, params[]) //admin using the command to increase a disconnected player's money
{
    new string[128], name[64], money;
    if(!IsPlayerAdmin(playerid)) return 0;
    if(sscanf(params, "s[64]i", name, money)) return SendClientMessage(playerid, -1, "/refund [player name] [money]");

    format(string,sizeof(string),"/JCNR/Users/%s.ini", name);
    if(!fexist(string)) return SendClientMessage(playerid, -1, "Invalid Player.");

    INI_ParseFile(string, "LoadUser_%s", .bExtra = true, .extra = playerid);

    new INI:File = INI_Open(string);
    INI_SetTag(File,"UserData");
    INI_WriteInt(File,"Cash", PlayerInfo[playerid][pCash] + money);
    INI_Close(File);
    return 1;
}

forward LoadUser_UserData(playerid, name[], value[]);
public LoadUser_UserData(playerid, name[], value[])
{
    INI_Int("Cash", PlayerInfo[playerid][pCash]);
    return 1;
}
i already have a variable to store the player's money defined through an enum.
- What did you mean by {0 bla bla} after defining the variable?
- Does 'playerid' in INI_ParseFile where it's in the command return the admin's id? if so, it will load the admin's money who used the command, or what
Reply
#9

Don't use a players actual cash variable. You are going to store an offline players cash in that variable, which means your money will be replaced with the offline players money. Use an unused variable or new variable, do not use one that you use regularly. The '{0, ...}' just sets the default value to 0 for all elements when created.
pawn Code:
format(string), sizeof(string), "JCNR/Users/%s.ini", name);
Formats a string to store the file path to the offline players user file.

pawn Code:
if(!fexist(string))
If users file does not exist...

pawn Code:
INI_ParseFile(string, "LoadUser_%s", .bExtra = true, .extra = playerid);
Call the function LoadUser and load all tags. You use the tag 'UserData', so our public function must be LoadUser_UserData.

pawn Code:
INI_Int("Cash", NewVar[playerid]);
Load the cash value and save it to NewVar[playerid] where playerid is the person who typed the command. Because we passed playerid through INI_ParseFile. We only use playerid because that variable is assigned to the admin, and assures us that it's value will not change between loading and using th variable. And no it won't load the admins money, because we passed 'strong' through parsefile, which is the offline players file... Not the admins file...

pawn Code:
INI_WriteInt(File, "Cash", NewVar[playerid] + money);
NewVar now contains the offline players cash, so if we add money into it, we get the total value that the offline players cash should now be. Then you just issue that new value into the offline players account...

It's really very simple. Hopefully I don't need to explain this again, on my phone, at work.
Reply
#10

Yea i think it cleared everything but why did you load the file after checking if the file is not exist?!
pawn Code:
if(!fexist(string)) INI_ParseFile(string, "LoadUser_%s", .bExtra = true, .extra = playerid);
Edit: Thanks bro, i got everything now.
+REP
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)