SA-MP Forums Archive
Sorting Players According to Kills - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Sorting Players According to Kills (/showthread.php?tid=399877)



Sorting Players According to Kills - [HK]Ryder[AN] - 16.12.2012

Hello Guys and Gals,
I have just released my TDM gamemode and my friend Lordz gave the suggestion to add top 5 killers also.

I started working on that but figured out that it would be too difficult using y_ini..i am starting to switch the gamemode to sqlite,but i am a newb in it and maybe someone can give me an example how to sort players according to their kills if the database name is database.db
table is users
and field is kills

mayb something like
SELECT * FROM `Users` WHERE `Kills` ....
what to do after that?
thanks.


Re: Sorting Players According to Kills - RajatPawar - 16.12.2012

I don't know this well, too, but I suppose you could SELECT kills in a 'while' loop replacing a variable say, 'maxkills' when the next player's kills exceed the previous 'maxkills' value. Correct me someone, if I am wrong! (Apologies if.)


Re: Sorting Players According to Kills - RedCrossER - 16.12.2012

Well why not mysql instead of sqlite?
Well i think i can help you with this , i will tell when i reach at home.


Re: Sorting Players According to Kills - iggy1 - 16.12.2012

Quote:
Originally Posted by RedCrossER
Посмотреть сообщение
Well why not mysql instead of sqlite?.
What difference will that make?

OP: Get top 5 user kills with names.
pawn Код:
"SELECT username, kills FROM users ORDER BY kills DESC LIMIT 5"
Very easy to add that query to a function that gets the top 'number' anything.

EDIT:

That is a pretty slow query if the database is large, so it's a good idea to create an index on the kills column. Especially if you run this query often.


Re: Sorting Players According to Kills - RajatPawar - 16.12.2012

Quote:
Originally Posted by iggy1
Посмотреть сообщение
pawn Код:
"SELECT username, kills FROM users ORDER BY kills DESC LIMIT 5"
Very easy to add that query to a function that gets the top 'number' anything.
That is so awesome. MySQL rocks! One line and boom :0 Thanks for sharing.


Re: Sorting Players According to Kills - [HK]Ryder[AN] - 16.12.2012

Quote:
Originally Posted by iggy1
Посмотреть сообщение
What difference will that make?

OP: Get top 5 user kills with names.
pawn Код:
"SELECT username, kills FROM users ORDER BY kills DESC LIMIT 5"
Very easy to add that query to a function that gets the top 'number' anything.

EDIT:

That is a pretty slow query if the database is large, so it's a good idea to create an index on the kills column. Especially if you run this query often.
Hello,
Can you tell me that is i use the code you provided how do i run it?mysql_query?
sorry am a newb in mysql and sqlite.

also can you show me a full working script for example i wanna print the top 5 players name and their kills, what will i do after querying(please provide code if possible)
thanks.


Re: Sorting Players According to Kills - iggy1 - 16.12.2012

I made a snippet but it is untested. You need to change one variable i commented it at the top. The var 'gDatabaseHandle' should be the connection handle you use in your code.

pawn Код:
#define TOPLIST_DIALOG      (666)

//The following variable must be YOUR connection handle
//new gDatabaseHandle = mysql_connect(...);

forward OnTopKillsRetreived( playerid );

public OnTopKillsRetreived( playerid )
{
    new
        rows=0,//amount of rows returned
        cols=0,//amount of columns returned
        cur_row=0,//current row being processed
        tmpKills[12],//amount of kills returned
        tmpStr[512],//string to output to dialog
        tmpName[MAX_PLAYER_NAME]//to store the name
    ;
    cache_get_data( rows, cols );

    while( rows )
    {
        cache_get_row(cur_row, 0, tmpName);//gets users name
        cache_get_row(cur_row, 1, tmpKills);//get kills
        format(tmpStr, sizeof(tmpStr), "%s%d: %s Kills = %s\n", tmpStr, cur_row+1, tmpName, tmpKills);
        ++cur_row;//jump to next row
        --rows;//reduce total rows
    }
    ShowPlayerDialog(playerid, TOPLIST_DIALOG, DIALOG_STYLE_MSGBOX, "Here is top five killers on the server", tmpStr, "Close", "");
    return 1;
}

stock ShowPlayerTopKillsDialog(playerid)
{
    mysql_function_query( gDatabaseHandle,
        "SELECT username, kills FROM users ORDER BY kills DESC LIMIT 5",
        true, "OnTopKillsRetreived", "i", playerid
    );
}
I hope it works for you. Use 'ShowPlayerTopKillsDialog' to show the dialog.