15.01.2015, 01:35
(
Последний раз редактировалось vassilis; 15.01.2015 в 19:44.
)
solved
// Sets the admin-level of another player
COMMAND:setlevel(playerid, params[])
{
// If the player has an insufficient admin-level (he needs level 6), exit the command
if (APlayerData[playerid][AdminLevel] < 6) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}Only admins level 6 and higher can use this command");
// Setup local variables
new OtherPlayer, Level, Msg[128], OldLevel, Query[128];
// Split parameters
if (sscanf(params, "ui", OtherPlayer, Level)) return SendClientMessage(playerid, 0xFF0000AA, "Usage: \"/setlevel <OtherPlayer> <AdminLevel>\"");
// If the other player has an invalid ID (sscanf returns 65535 as playerid if the player cannot be found), exit the command
if (OtherPlayer >= MAX_PLAYERS) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}That player is not online");
// If the other player hasn't logged in yet, exit the command
if (APlayerData[OtherPlayer][LoggedIn] == false) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}That player hasn't logged in properly yet");
// If "Level" is invalid, exit the command
if ((Level < 0) || (Level > 6)) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}Use admin-levels ranging from 0 to 6");
// Get the old level of the other player
OldLevel = APlayerData[OtherPlayer][AdminLevel];
// If the level is still the same, exit the command
if (OldLevel == Level) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}Other player's level hasn't been changed");
// Store the new level of the player and save it to MySQL
APlayerData[OtherPlayer][AdminLevel] = Level;
mysql_format(SQL_db, Query, sizeof(Query), "UPDATE playerdata SET AdminLevel = '%i' WHERE ID = '%i'", APlayerData[OtherPlayer][AdminLevel], APlayerData[OtherPlayer][SQLID]);
mysql_tquery(SQL_db, Query, "", "");
// Check if the player has been promoted or demoted
if (OldLevel < Level)
format(Msg, 128, "Player %s has been promoted to %s by %s: %s", APlayerData[OtherPlayer][PlayerName], AdminLevelName[Level], AdminLevelName[APlayerData[playerid][AdminLevel]], APlayerData[playerid][PlayerName]);
if (OldLevel > Level)
format(Msg, 128, "Player %s has been demoted to %s by %s: %s", APlayerData[OtherPlayer][PlayerName], AdminLevelName[Level], AdminLevelName[APlayerData[playerid][AdminLevel]], APlayerData[playerid][PlayerName]);
SendClientMessageToAll(0x00FF00FF, Msg);
// Let the server know that this was a valid command
return 1;
}
// This function adds the given Money and Score values to the given player
Player_Reward(playerid, Money, Score)
{
new Query[128];
// Add the given Money and Score to the player's account
APlayerData[playerid][PlayerMoney] = APlayerData[playerid][PlayerMoney] + Money;
APlayerData[playerid][PlayerScore] = APlayerData[playerid][PlayerScore] + Score;
// Update money and score for this player in the player's account in MySQL
mysql_format(SQL_db, Query, sizeof(Query), "UPDATE playerdata SET Money = '%i', Score = '%i' WHERE ID = '%i'", APlayerData[playerid][PlayerMoney], APlayerData[playerid][PlayerScore], APlayerData[playerid][SQLID]);
mysql_tquery(SQL_db, Query, "", "");
return 1;
}
// This command allows the player to toggle his speedometer between kph and mph
COMMAND:units(playerid, params[])
{
// Setup local variables
new Query[128];
// Toggle the units for this player
if (APlayerData[playerid][SpeedInMph] == false)
{
// Set the player's speed read-out to mph
APlayerData[playerid][SpeedInMph] = true;
// Save the data to MySQL
mysql_format(SQL_db, Query, sizeof(Query), "UPDATE playerdata SET SpeedInMph = '1' WHERE ID = '%i'", APlayerData[playerid][SQLID]);
mysql_tquery(SQL_db, Query, "", "");
// Inform the player
SendClientMessage(playerid, 0xFFFFFFFF, "{00FF00}You've set your speedometer read-out to mph");
}
else
{
// Set the player's speed read-out to kph
APlayerData[playerid][SpeedInMph] = false;
// Save the data to MySQL
mysql_format(SQL_db, Query, sizeof(Query), "UPDATE playerdata SET SpeedInMph = '0' WHERE ID = '%i'", APlayerData[playerid][SQLID]);
mysql_tquery(SQL_db, Query, "", "");
// Inform the player
SendClientMessage(playerid, 0xFFFFFFFF, "{00FF00}You've set your speedometer read-out to kph");
}
// Let the server know that this was a valid command
return 1;
}
You didn't give the code where you set the level to 4, can we see that as well?
Also a suggestion: Make multiple smaller queries to only update the fields that change. It has no use to ALWAYS update ALL fields when only 1 value changes. pawn Код:
The first is my /setlevel command, the second is a function which I can use to give the player some money and/or scorepoints. That last function is called everywhere in the script where the player earns money or scorepoints. Maintaining such a small query is not a problem, and as soon as it's written, you never have to edit it again, as it's doing whatever it is supposed to be doing. You have to keep updating yours everytime you wanna save an additional field, and because of it's length, you can make mistakes easily. Updating money doesn't require overwriting the adminlevel, or wanted level, or muted status, or my deaths/kills, or even my banned status, they don't relate to eachother at all. Imagine that your table will grow ALOT (say to 150 fields), would you rather make one HUGE query to update all 150 fields when only 1 value changes and where the other 149 values just stay the same? Another comparison: If you type a big post onto this forum and you make a typo (wrong character or wrong word), would you retype the entire post to fix the typo, or just edit that one character or word to fix the typo? I guess you choose the second one. Making such huge queries puts extra stress on your database and harddrive when it's not even needed. MySQL needs to break up your query in different parts, analyze it word by word, search for every required column, overwrite each field to finish your query. All this takes time, and the longer your query, the longer it takes. It can work much faster if you only update the fields that need to be updated. Leave the rest alone, as it stays the same anyway. Of course, you'll have multiple queries all over your script instead of only one huge function. You might as well use files, which NEED to be overwritten completely when only 1 values changes (those might actually be faster compared to updating all fields everytime in a database). A database is created because you can update whatever you want, when you want and to do it fast, because you have access to each field separately. That's their main function. |
A combination of both would be the best I guess.
Theres "slow" data like properties, that barely changes. This could be stored on every change very well. And then theres "fast" data, e.g. weapon data, health, position, etc. As this might change very often it would be quite stupid to save these values on every update of course. So theres no way that works best for everyone. Youll rather have to decide when to store which values. |
Guys first of all thanks for your feedback. Secondly the database was working properly before but yesterday i tried to add skin saving and when i saw it didn't work fine i removed it again and i think by mistake i deleted something that i shouldn't delete(i guess??) so that's my problem..
EDIT: I read whole statement of PPC so you tell me that instead of making a whole function for disconnect it would be better saving each query to each function or command it is neccessary? |