[Tutorial] Temporary Ban Commands (MySQL, ZCMD, sscanf)
#6

It's not recommended by most programmers to save one thing in a database and the other in files.
Either completely use a database, or keep using files.

I also had issues learning MySQL, as I figured installing a 500Mb+ package to hold only a few hundred kilobytes on data was just stupid.

But I went MySQL all the way now, and I find it alot easier now, compared to using files.
Also, certain things you can't simply do with files.

Gathering a list of players with the most money (getting the 25 richest players on your server, even the offline ones for example) is virtually impossible with files.
And if you can do it, it will certainly add alot of lag to your server, as you would need to open all player-files, read them all to read the money, sort them yourself manually and keep only the 25 richest players.
It will add alot of lag, as this cannot be threaded.
The script needs to wait to run other code until all files have been read, data sorted and such.

All that would also require alot of code.

Using MySQL, it's only a matter of creating one simple query like this:
pawn Код:
SELECT Name FROM players ORDER BY Money DESC LIMIT 25
This gathers the playernames (Name) from the table "players", orders it in a descending order using the "Money" column and gathers only 25 rows.
By ordering them descending, your sorting them from richest to poorest.

This single line replaces your entire loop to read all player-files line by line to find the money of each player, it also replaces your entire sort-function to sort them.

Now you only need a loop to read the data from the result and display it.
Pseudo-code (really short code):
pawn Код:
new DialogContents[2000], NameToAdd[24];
for (new Row; Row < RowsFromResult; Row++)
{
    cache_get_field_content(Row, "Name", NameToAdd, SQL_connection, 24); // Read the Name from each row and put it inside variable NameToAdd
    format(DialogContents, sizeof(DialogContents), "%s%s\n", DialogContents, NameToAdd); // Add the name to a string, which will be used inside the dialog for showing
}
ShowPlayerDialog(playerid, 100, DIALOG_STYLE_MSGBOX, "Richest players:", DialogContents, "OK");
No need to do the sorting manually, no need to open all files and read them line by line.

And the best of it: it won't lag at all, as you can use threaded queries.
MySQL will gather the data in the background while your script continues to run other stuff.
When MySQL has gathered all the data, your callback (which you specified) will be called automatically, and inside the callback you read the results and display it.

As you see, you only need one query and only about 7 lines of code to get all that data, sort it and display it.
And in a threaded way so you won't get any lag.

Try the same with files (8 lines of code to read all your files, sort them descending by each player's money value, keep only 25 results and display it).
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)