29.01.2017, 20:20
I don't see why you save a cache for later use.
When a player connects, just load his ID in the database that matches his name and load the hashed password and salt, store all 3 (password, salt and ID) in the array pInfo and ShowPlayerDialog to input a password.
Then in that callback (OnDialogResponse), check if the hashed input is the same as the stored hashed password.
If so, send a new query to load the remaining data and store that as well in your pInfo array.
No need to save cache and having to remember to delete it later on.
Also, saving everything inside OnPlayerDisconnect is also questionable.
With so little data, it won't matter that much.
But once you have a huge gamemode, you'll want to split up datasaving.
In my gamemode, I have so much data that my query would be at least 5000 characters long when I would save everything at once in one huge query upon logging out.
Just save whatever changes using a small query and save it immediately.
A player gets money? Save it.
A player receives a scorepoint? Save it.
A player buys a vehicle or a house? Save the money with one query, and update the housedata with a second query.
I'm having a full-blown auction system in my gamemode to auction houses and vehicles to other players, all controlled via one single menu built with textdraws (my vehicle-menu).
Players can place bids on houses and the system also has an automatic system linked to it.
Once a house is put up for auction, the house will automatically transferred to the highest bidder who has the money for it after 7 days, even when both parties (seller and buyer) are offline.
If no bids were there, or nobody has the money for it, the house goes to the bank.
Also vehicles can be browsed that are put up for auction.
House-owners also need to pay a weekly maintenance fee to keep their houses and vehicles.
If they can't afford it, the house is automatically put up for auction along with the vehicles linked to that house.
Doing such a system with queries only inside OnPlayerDisconnect is mindblowing and practically impossible to do.
If I would only save whenever the server shuts down, the server would need several thousand huge queries to update every house on the server,
every house-vehicle (which could be several tens of thousands), just in case something was edited.
Just save whatever needs to be saved at the moment it changes, and use small queries to do it.
Nothing as easy as a function like this:
Whenever a player gets money or pays money, his account in the database has also been updated at once.
That way, you can also lose the Corrupt_Check thing, whatever it does.
The data was already saved while playing, logging in fast after a player disconnects won't corrupt data as there is nothing saved in there.
What if your server suddenly crashes? Then nothing would be saved for hours.
You'll lose hours of progress of your players, who in turn will not be happy because they need to spend hours again to get back what was lost.
The only thing I'm saving in my OnPlayerDisconnect callback, is the fuel for all my house-vehicles, as fuel changes every second while driving, this would mean one query per second per vehicle and is too much at once.
And for the rest of the code in there, is to delete the house-vehicles from the server's memory when a player logs out and clearing variables.
When creating an account, add some default values for values to shorten the INSERT query as well upon registering.
Since score, kills, deaths and cash are 0 to begin with, you can simply have MySQL fill in those for you automatically when your table has default values of 0 assigned to those columns.
Also, clearing an array with an enum is much easier than clearing every variable one by one.
You can clear them all at once for a single player.
Instead of doing:
You can do:
Not much of a space-saver here, but you never have to edit that code again when you add a variable to the enum.
So less likely to forget to clear one.
When a player connects, just load his ID in the database that matches his name and load the hashed password and salt, store all 3 (password, salt and ID) in the array pInfo and ShowPlayerDialog to input a password.
Then in that callback (OnDialogResponse), check if the hashed input is the same as the stored hashed password.
If so, send a new query to load the remaining data and store that as well in your pInfo array.
No need to save cache and having to remember to delete it later on.
Also, saving everything inside OnPlayerDisconnect is also questionable.
With so little data, it won't matter that much.
But once you have a huge gamemode, you'll want to split up datasaving.
In my gamemode, I have so much data that my query would be at least 5000 characters long when I would save everything at once in one huge query upon logging out.
Just save whatever changes using a small query and save it immediately.
A player gets money? Save it.
A player receives a scorepoint? Save it.
A player buys a vehicle or a house? Save the money with one query, and update the housedata with a second query.
I'm having a full-blown auction system in my gamemode to auction houses and vehicles to other players, all controlled via one single menu built with textdraws (my vehicle-menu).
Players can place bids on houses and the system also has an automatic system linked to it.
Once a house is put up for auction, the house will automatically transferred to the highest bidder who has the money for it after 7 days, even when both parties (seller and buyer) are offline.
If no bids were there, or nobody has the money for it, the house goes to the bank.
Also vehicles can be browsed that are put up for auction.
House-owners also need to pay a weekly maintenance fee to keep their houses and vehicles.
If they can't afford it, the house is automatically put up for auction along with the vehicles linked to that house.
Doing such a system with queries only inside OnPlayerDisconnect is mindblowing and practically impossible to do.
If I would only save whenever the server shuts down, the server would need several thousand huge queries to update every house on the server,
every house-vehicle (which could be several tens of thousands), just in case something was edited.
Just save whatever needs to be saved at the moment it changes, and use small queries to do it.
Nothing as easy as a function like this:
PHP Code:
// This function is used to give (or take) money to/from the player
Player_GiveMoney(playerid, amount)
{
// Setup local variables
new query[128];
// Add the given Money to the player's account
APlayerData[playerid][Money] = APlayerData[playerid][Money] + amount;
// Also update the client immediately instead of waiting for the GlobalTimer1000 to update it
ResetPlayerMoney(playerid);
GivePlayerMoney(playerid, APlayerData[playerid][Money]);
// Update money for this player in the player's account in MySQL
mysql_format(SQL_db, query, sizeof(query), "UPDATE playerdata SET Money = '%i' WHERE UserID = '%i'", APlayerData[playerid][Money], APlayerData[playerid][UserID]);
mysql_tquery(SQL_db, query, "", "");
return 1;
}
That way, you can also lose the Corrupt_Check thing, whatever it does.
The data was already saved while playing, logging in fast after a player disconnects won't corrupt data as there is nothing saved in there.
What if your server suddenly crashes? Then nothing would be saved for hours.
You'll lose hours of progress of your players, who in turn will not be happy because they need to spend hours again to get back what was lost.
The only thing I'm saving in my OnPlayerDisconnect callback, is the fuel for all my house-vehicles, as fuel changes every second while driving, this would mean one query per second per vehicle and is too much at once.
And for the rest of the code in there, is to delete the house-vehicles from the server's memory when a player logs out and clearing variables.
When creating an account, add some default values for values to shorten the INSERT query as well upon registering.
Since score, kills, deaths and cash are 0 to begin with, you can simply have MySQL fill in those for you automatically when your table has default values of 0 assigned to those columns.
Also, clearing an array with an enum is much easier than clearing every variable one by one.
You can clear them all at once for a single player.
Instead of doing:
PHP Code:
//Resetting player information.
pInfo[playerid][Kills] = 0;
pInfo[playerid][Deaths] = 0;
pInfo[playerid][PasswordFails] = 0;
PHP Code:
new clean[ENUM_PLAYER_DATA];
pInfo[playerid] = clean;
So less likely to forget to clear one.