t_FeedInfo(playerid,Float:info)
t_GetRank(playerid,order)
t_GetPlayer(rank,order)
for(new i=0;i<MAX_PLAYERS;++i)
{
if(!IsPlayerConnected(i)) continue;
t_FeedInfo(i,pInfo[i][Kills];
}
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
new rank = t_GetRank(3,0);
//0 for ascending order ,1 for descending
new rank = t_GetRank(3,1);
new id = t_GetPlayer(1,0); //id shall be 2
id = t_GetPlayer(1,1);// id shall be 3
#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; }
Looking good, it can be used maybe in a Top Kill ? So it can be shown in a textdraw..
|
stock GetSpots()
{
for(new i = 0; i < MAX_PLAYERS; i++)
{
FeedInfo(i, pStats[i][MapKills]);
}
return 1;
}
Can you explain how can I make a top 3 players online with this?
What I'm trying to do is I've made a textdraw which should show 1. [NAME] [MapKills] 2. [NAME] [MapKills] 3. [NAME] [MapKills] You [MapKills] Now, the you part is okay But BUT BUT The 1-2-3 all show ID 0. Now I'm trying to use your include to do it and here's my stock pawn Код:
|
stock GetSpots()
{
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i)) FeedInfo(i, pStats[i][MapKills]);
}
new rank[3];
for(new i=0;i<3;++i) rank[i] = t_GetPlayer(i+1, 1)//Descending as we wish player with max kills to be first
//Now rank[0] have player whose rank is first, rank[1] with second and so on.
//You can now get PlayerName by using
GetPlayerName(rank[0]..
return 1;
}
I do like the concept of this - too few people use sorting instead of naive searching to get the top whatever. In fact that's what I assumed would have been done when I saw the topic simply because that's what most people do, I was pleasantly surprised!
Having said that, there are a few bugs: Most notably, this relies too much on global state. Say you start calling "t_FeedInfo", then before calling "t_GetPlayer", you call one of my functions for some reason (to get more data on something). Then I use your include to sort some data and return the result, now when you call "t_GetPlayer" all the data is wrong and corrupted. Not a bug, but you might want to consider sorting the data and setting a flag. When "t_FeedInfo" is called set the flag to false. When "t_GetPlayer" is called check the flag - if it's true the data is already sorted and you can use it. If it is false then sort the data, set the flag to true, and return your result. Also, you might want to look in to Slice's sort include, which uses a very efficient implementation of QuickSort for exactly this sort of task. |
Everthing is understandable!!!
But can you make me a onplayercommandtext with strcmp pls bro roxor! |