[Include] Top Players
#1

TOP PLAYERS INCLUDE
Hey Guys, This is gonna be my first include.

Info :

With this include, you can sort out Players Data in Ascending/Descending Order.

Stocks :

pawn Код:
t_FeedInfo(playerid,Float:info)
t_GetRank(playerid,order)
t_GetPlayer(rank,order)
Usage

With t_FeedInfo, you shall feed the information which you want to sort out.
pawn Код:
for(new i=0;i<MAX_PLAYERS;++i)
    {
        if(!IsPlayerConnected(i)) continue;
        t_FeedInfo(i,pInfo[i][Kills];
    }
With t_GetRank, it will return the rank of that player according to order(Ascending/Descending) and your data.

Suppose, you have feeded data
Код:
Playerid   Data    Rank (Ascending)    Rank (Descending) 
    0         9              4                         2
    1         1               2                        4
    2         -5              1                        5
    3         10              5                        1
    4         2               3                        3
pawn Код:
new rank = t_GetRank(3,0);
    //0 for ascending order ,1 for descending
Now rank will have value 5
pawn Код:
new rank = t_GetRank(3,1);
And ,now rank will have value 1

pawn Код:
new id = t_GetPlayer(1,0); //id shall be 2
id = t_GetPlayer(1,1);// id shall be 3
Since, this is my first script and it took me 30 min to complete , I am sure it may have lot's of bugs. Report Bugs and Suggestions

Код:
#define ORDER_ASC 0
#define ORDER_DES 1

new Float:t_Data[MAX_PLAYERS];
new bool:t_IsDataEntered = false;
new t_id[MAX_PLAYERS];
new t_Data_Sorted_Manner = -1;

/*
native t_FeedInfo(playerid,Float:info);
native t_GetRank(playerid,order);
native t_GetPlayer(rank,order);
*/

stock t_FeedInfo(playerid,Float:info)
{
	t_Data[playerid] = info;
	t_id[playerid] = playerid;
	t_IsDataEntered = true;
	return 1;
}
stock SortData(order)
{
	if(order == ORDER_ASC)
	{
		for(new i=0;i<MAX_PLAYERS;++i)
		{
		    if(!IsPlayerConnected(i)) continue;
		    for(new j=i;j<MAX_PLAYERS;++j)
		    {
		        if(!IsPlayerConnected(j)) continue;
		        if(t_Data[j]<t_Data[i])
		        {
		            new Float:temp = t_Data[i];
					t_Data[i] = t_Data[j];
					t_Data[j] = temp;

					new temp1 = t_id[i];
					t_id[i] = t_id[j];
					t_id[j] = temp1;
  		        }
		    }
		}
		return 1;
	}
	else
	{
	    for(new i=0;i<MAX_PLAYERS;++i)
		{
		    if(!IsPlayerConnected(i)) continue;
		    for(new j=i;j<MAX_PLAYERS;++j)
		    {
		        if(!IsPlayerConnected(j)) continue;
		        if(t_Data[j]>t_Data[i])
		        {
		            new Float:temp = t_Data[i];
					t_Data[i] = t_Data[j];
					t_Data[j] = temp;

					new temp1 = t_id[i];
					t_id[i] = t_id[j];
					t_id[j] = temp1;

  		        }
		    }
		}
		return 1;
	}
}
stock t_GetRank(playerid,order)
{
    if(t_IsDataEntered == false)
	{
	    printf("TOP_PLAYER [ERROR] : Data is not entered\nAborting process.");
	    return -1;
	}
	if(playerid==INVALID_PLAYER_ID || (!IsPlayerConnected(playerid)))
	{
	    printf("TOP_PLAYER [ERROR] : Invalid ID || Player not connected\nAborting process.");
	    return -1;
	}

	if(order>1 || order < 0)
	{
	    printf("TOP_PLAYER [ERROR] : Wrong Order (only 0,1 allowed)\nAborting process.");
	    return -1;
	}
	else if(order == ORDER_ASC)//Ascending order 1,2,3,4,5
	{
	    if(t_Data_Sorted_Manner != ORDER_ASC)
	    {
	        SortData(ORDER_ASC);
	        t_Data_Sorted_Manner = ORDER_ASC;
	    }

		new r;
		for(new i=0;i<MAX_PLAYERS;++i)
		{
		    if(!IsPlayerConnected(i)) continue;
		    if(t_id[i] == playerid)
		    {
		        r = i+1;
		        break;
		    }
		}
  		return r;
	}

	else if(order == ORDER_DES)//Descending order 5,4,3,2,1
	{
 		if(t_Data_Sorted_Manner != ORDER_DES)
	    {
	        SortData(ORDER_DES);
	        t_Data_Sorted_Manner = ORDER_DES;
	    }

		new r;
		for(new i=0;i<MAX_PLAYERS;++i)
		{
		    if(!IsPlayerConnected(i)) continue;
		    if(t_id[i] == playerid)
		    {
		        r = i+1;
		        break;
		    }
		}
  		return r;
	}
    return  0;
}
stock t_GetPlayer(rank,order)
{
	if(t_IsDataEntered == false)
	{
	    printf("TOP_PLAYER [ERROR] : Data is not entered\nAborting process.");
	    return -1;
	}
	if(rank<1)
	{
	    printf("TOP_PLAYER [ERROR] : Rank can not be less than 1.\nAborting process.");
	    return -1;
	}

	if(order>1 || order < 0)
	{
	    printf("TOP_PLAYER [ERROR] : Wrong Order (only 0,1 allowed)\nAborting process.");
	    return -1;
	}
	else if(order == ORDER_ASC)//Ascending order 1,2,3,4,5
	{
		if(t_Data_Sorted_Manner != ORDER_ASC)
	    {
	        SortData(ORDER_ASC);
	        t_Data_Sorted_Manner = ORDER_ASC;
	    }
  		return t_id[rank-1];
	}

	else if(order == ORDER_DES)//Descending order 5,4,3,2,1
	{
		if(t_Data_Sorted_Manner != ORDER_DES)
	    {
	        SortData(ORDER_DES);
	        t_Data_Sorted_Manner = ORDER_DES;
	    }

		return t_id[rank-1];
	}
    return  0;
}
BUGS KNOWN :
None so far.

Code updated (29/09/2012). Flag has been set to check if data is already arranged. Decreasing the no of loops a lot. Also, now there is no more copying of data , therefore saving memory.
Reply


Messages In This Thread
Top Players - by [MM]RoXoR[FS] - 21.07.2012, 14:26
Re: Top Players - by Spookie98 - 21.07.2012, 14:51
Re: Top Players - by [MM]RoXoR[FS] - 21.07.2012, 15:31
Re: Top Players - by Finn - 21.07.2012, 15:39
Re: Top Players - by Ronaldo_raul™ - 21.07.2012, 16:02
Re: Top Players - by Gangs_Rocks - 21.09.2012, 08:30
Re: Top Players - by DeadLy™ - 21.09.2012, 10:00
Re: Top Players - by Q_Lite - 21.09.2012, 11:05
Re: Top Players - by [MM]RoXoR[FS] - 25.09.2012, 10:15
Re: Top Players - by Slice - 25.09.2012, 11:59
Re: Top Players - by [MM]RoXoR[FS] - 26.09.2012, 14:59
Re: Top Players - by [MM]RoXoR[FS] - 29.09.2012, 13:27
Re: Top Players - by Guest4390857394857 - 17.02.2014, 11:41
Re: Top Players - by fiki574 - 17.02.2014, 19:20
Re: Top Players - by CODRWW - 17.02.2014, 19:27
Re: Top Players - by Guest4390857394857 - 18.02.2014, 11:34

Forum Jump:


Users browsing this thread: 2 Guest(s)