[TUT] Creating TOP lists. -
[LDT]LuxurY - 12.02.2009
[HowTo] Creating TOP lists.
Most of server admins want to get TOP lists of players on their servers. And you don't need to create hard coded pieces of code. It'll be something like this:
pawn Код:
stock fill ( array[][] )
{
new x_count;
for ( new g = 0; g < MAX_PLAYERS; g++ )
if ( IsPlayerConnected ( g ) )
{
x_count++;
array[g][1] = g;
array[g][0] = GetPlayerScore ( g );
}
return x_count;
}
This function fills the array with our players data. You can use everything instead of GetPlayerScore ( g ); Also it returns count of online players, because we don't want to sort extra sells.
pawn Код:
stock process ( array[][] , size = sizeof ( array ) )
{
if ( size > 1 )
{
new
bool:flag,
tmpx;
do
{
flag = true;
for ( new i = size - 1; i > 0; i-- )
if ( array[i][0] < array[i - 1][0] )
{
tmpx = array[i][0];
array[i][0] = array[i - 1][0];
array[i - 1][0] = tmpx;
tmpx = array[i][1];
array[i][1] = array[i - 1][1];
array[i - 1][1] = tmpx;
flag = false;
}
}
while flag == false;
}
}
This function sorts the array by using bubble sorting method (for more information about this method click
here).
Function will not be processed if there is only 1 player or there is no players on the server.
We'll get array[][1] - TOP playerid and array[][0] - TOP player's score/money/health/time in race etc...
And then, for example to create TOP 10 players, we should write something like this:
pawn Код:
stock CreateTop ( array[][] , max , count = 10 )
{
new
str[64],
pln[24];
for ( new i = 0; i < ( ( max < count) ? max : count ); i++ )
{
GetPlayerName ( array[i][1] , pln , sizeof ( pln ) );
format( str , sizeof ( str ) , "%d.%s: %d" , i + 1 , pln , array[i][0] );
SendClientMessageToAll ( 0x33FF33AA , str );
}
return true;
}
In this function we must check the count of online players, so we'll use the count from fill function.
Online players >= TOP list count -
OK!
Online players < TOP list count -
Failure!
pawn Код:
( ( max < count) ? max : count )
Return the lesser value.
It'll print count lines ( default = 10 ) in game chat if the count of online player will be >= 10.
So the usage:
pawn Код:
new
ar[MAX_PLAYERS][2],
tmpsize;
tmpsize = fill ( ar );
process( ar , tmpsize );
CreateTop ( ar , tmpsize );
It's just 6 lines to create a TOP list

Forget about previous functions and add them in include.
I think it'll help someone.
Cheers,
[LDT]LuxurY.
Re: [HowTo] Creating TOP lists. -
Kanji_Suzuki - 13.02.2009
hmmm sounds good
Re: [HowTo] Creating TOP lists. -
Dinero - 13.02.2009
Very good
Re: [HowTo] Creating TOP lists. -
Celson - 14.02.2009
Man... you fuckin awesome.
Re: [HowTo] Creating TOP lists. -
[LDT]LuxurY - 14.02.2009
Quote:
Originally Posted by ssǝן‾ʎ
Did you do ANY research before posting this or just put what you like?
Quote:
Originally Posted by [LDT
LuxurY ]
This function sorts the array by using classic example of sorting arrays.
|
WHAT "classic" example would that be?
Next time you want to write a tutorial on something like sorting, do some research.
|
It's a bubble sorting. That topic was discussed in my russian forum. And I decided to post it here. It's HowTo isn't about sorting methods. It's about creating TOP lists. And do not please write such posts if you don't know about existing of a research. Of course I'm not so clever as you and that's why I share whatever I know.
Thanks a lot,
[LDT]LuxurY.
Re: [HowTo] Creating TOP lists. -
LarzI - 19.11.2009
Great tutorial!
Old topic, I know, but still...
8/10 (Y)
Re: [HowTo] Creating TOP lists. -
Tigerbeast11 - 19.11.2009
Good Tut, but can you make one for biginners?