#include /*BRICS*/<a_samp>
new Top_Player[MAX_PLAYERS] = 3;
new players_Data[MAX_PLAYERS][2], tempString[MAX_PLAYERS][128], tempVar[MAX_PLAYERS];
#define TOP_CMD "/topmenu" //Команда вызова топ-меню!
#define TOP_SCORE_GEN "» %d. Игрок: %s [id: %d] - Фраги: %d\n"
#define TOP_MONEY_GEN "» %d. Игрок: %s [id: %d] - Деньги: %d\n"
#define TOP_WANTED_GEN "» %d. Игрок: %s [id: %d] - Уровень розыска: %d\n"
#define TOP_PING_GEN "» %d. Игрок: %s [id: %d] - Пинг: %d\n"
#define TOP_DISTANCE_GEN "» %d. Игрок: %s [id: %d] - Расстояние: %d метров\n"
#define TOP_TITLE "» Топ %d игроков по %s:"
#define TOP_LIST_ELEMENT_SCORE "фрагам"
#define TOP_LIST_ELEMENT_MONEY "деньгам"
#define TOP_LIST_ELEMENT_WL "уровню розыска"
#define TOP_LIST_ELEMENT_PING "пингу"
#define TOP_LIST_ELEMENT_DIS "расстоянию от меня"
#define KEY_BACK "Назад"
#define KEY_SELECT "Выбрать"
#define KEY_EXIT "Выход"
#define KEY_INPUT "Ввод"
#define GENERAL_DIALOG_ID 300
#define GENERAL_DIALOG_TITLE "Меню списков"
#define GENERAL_DIALOG_CONTENT "Игроков в топ: %d\nТоп %d игроков по фрагам\nТоп %d игроков по деньгам\nТоп %d игроков по уровням розыска\nТоп %d игроков по пингу\nТоп %d игроков по расстоянию от меня"
#define  /*BRICS*/DIALOG_TOP_ID GENERAL_DIALOG_ID+1
#define  /*BRICS*/VALUE_CHANGED "Значение вывода игроков было изменено на %d"
#define  /*BRICS*/DIALOG_GHANGE_TOP_ID GENERAL_DIALOG_ID+2
#define NOT_FINDED "Никого не найдено"
#define DIALOG_CHANGE_ID_TITLE "Смена вывода игоков в топ"
#define DIALOG_CHANGE_ID_CONTENT "Введите новое число вывода игроков в топ"
#define DIALOG_CHANGE_ID_CONTENT_ERROR_NOT_LITLE_ONE "Ошибка: Число не должно быть меньше 1 и не больше максимума игроков!\nВведите новое число вывода игроков в топ"
public OnFilterScriptInit()
{
	print("\n   Скрипт [Top Lists] от BRICS'a (aka Nеtflаshmаn) был загружен\n");
	return 1;
}




stock  /*BRICS*/TopCycle(playerid)
{
   for(new i = 0,j = 0; i < tempVar[playerid]; ++i)
   {
           j = players_Data[i][0];

           for(new k = i - 1; k > -1; --k)
           {
               if(j > players_Data[k][0])//спасибо чуваку с офф форума - RyDER за это
               {
                   players_Data[k][0] ^= players_Data[k + 1][0], players_Data[k + 1][0] ^= players_Data[k][0], players_Data[k][0] ^= players_Data[k + 1][0];
                   players_Data[k][1] ^= players_Data[k + 1][1], players_Data[k + 1][1] ^= players_Data[k][1], players_Data[k][1] ^= players_Data[k + 1][1];
               }
           }
   }
   return 1;
}
stock  /*BRICS*/Bar(barid,playerid,top)
{
	   new nametop[64];
	   new block[128];
	   new big_buff[512];
	   switch(barid)
	   {
		   case 1: block = TOP_SCORE_GEN;
		   case 2: block = TOP_MONEY_GEN;
		   case 3: block = TOP_WANTED_GEN;
		   case 4: block = TOP_PING_GEN;
		   case 5: block = TOP_DISTANCE_GEN;
	   }
	   
       for(new i = 0; i < top; ++i)
       {
           if(tempVar[playerid] <= i)break;
           GetPlayerName(players_Data[i][1], tempString[playerid], MAX_PLAYER_NAME);
           format(tempString[playerid], 128, block, i+1, tempString[playerid], players_Data[i][1], players_Data[i][0]);
           strcat(big_buff,tempString[playerid],sizeof(big_buff));
       }
       switch(barid)
       {
		   case 1: nametop = TOP_LIST_ELEMENT_SCORE;
		   case 2: nametop = TOP_LIST_ELEMENT_MONEY;
		   case 3: nametop = TOP_LIST_ELEMENT_WL;
		   case 4: nametop = TOP_LIST_ELEMENT_PING;
		   case 5: nametop = TOP_LIST_ELEMENT_DIS;
       }
       format(tempString[playerid], 129, TOP_TITLE, top, nametop);
       if(tempVar[playerid] == 0)big_buff = NOT_FINDED;
       ShowPlayerDialog(playerid,DIALOG_TOP_ID,DIALOG_STYLE_MSGBOX,tempString[playerid],big_buff,KEY_BACK,"");
       return 1;
}

stock OpenGen(playerid,key[],key2[],str[]=GENERAL_DIALOG_TITLE)
{
	 new megabuff[320];
	 format(megabuff,sizeof(megabuff),GENERAL_DIALOG_CONTENT,Top_Player[playerid],Top_Player[playerid],Top_Player[playerid],Top_Player[playerid],Top_Player[playerid],Top_Player[playerid]);
     ShowPlayerDialog(playerid,GENERAL_DIALOG_ID,DIALOG_STYLE_LIST,str,megabuff,key,key2);
     return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	if(!strcmp(cmdtext, TOP_CMD, true))return OpenGen(playerid,KEY_SELECT,KEY_EXIT);
	return 0;
}
stock GetTop(list,playerid,top)
{
       tempVar[playerid] = 0;
       for(new i = 0, j = GetMaxPlayers(); i < j; ++i)
       {
               if(!IsPlayerConnected(i) || IsPlayerNPC(i))continue;
               if(list == 5 && i == playerid)continue;
               switch(list)
               {
                  case 1: players_Data[tempVar[playerid]][0] = GetPlayerScore(i);
                  case 2: players_Data[tempVar[playerid]][0] = GetPlayerMoney(i);
                  case 3: players_Data[tempVar[playerid]][0] = GetPlayerWantedLevel(i);
                  case 4: players_Data[tempVar[playerid]][0] = GetPlayerPing(i);
                  case 5: {new Float: X,Float: Y,Float: Z;GetPlayerPos(playerid,X,Y,Z);players_Data[tempVar[playerid]][0] = floatround(GetPlayerDistanceFromPoint(i,X,Y,Z));}
               }
               players_Data[tempVar[playerid]++][1] = i;
       }
       TopCycle(playerid);//запустить основной цикл
       Bar(list,playerid,top);
       return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	if(dialogid == DIALOG_TOP_ID)return OpenGen(playerid,KEY_SELECT,KEY_EXIT);
	if(dialogid == GENERAL_DIALOG_ID)
	{
		if(!response)return 1;
		if(listitem != 0)return GetTop(listitem,playerid,Top_Player[playerid]);
		ShowPlayerDialog(playerid,DIALOG_GHANGE_TOP_ID,DIALOG_STYLE_INPUT,DIALOG_CHANGE_ID_TITLE,DIALOG_CHANGE_ID_CONTENT,KEY_INPUT,KEY_BACK);
	}
	if(dialogid == DIALOG_GHANGE_TOP_ID)
	{
		if(!response)return OpenGen(playerid,KEY_SELECT,KEY_EXIT);
		if(!isNumeric(inputtext))return ShowPlayerDialog(playerid,DIALOG_GHANGE_TOP_ID,DIALOG_STYLE_INPUT,DIALOG_CHANGE_ID_TITLE,DIALOG_CHANGE_ID_CONTENT,KEY_INPUT,KEY_BACK);
		if(strval(inputtext) < 1 || strval(inputtext) > GetMaxPlayers())return ShowPlayerDialog(playerid,DIALOG_GHANGE_TOP_ID,DIALOG_STYLE_INPUT,DIALOG_CHANGE_ID_TITLE,DIALOG_CHANGE_ID_CONTENT_ERROR_NOT_LITLE_ONE,KEY_INPUT,KEY_BACK);
		Top_Player[playerid] = strval(inputtext);
		format(tempString[playerid],64,VALUE_CHANGED,strval(inputtext));
		OpenGen(playerid,KEY_SELECT,KEY_EXIT,tempString[playerid]);
	}
	return 1;
}
stock isNumeric(const string[]) {
	new length=strlen(string);
	if (length==0) return false;
	for (new i = 0; i < length; i++) {
		if (
		(string[i] > '9' || string[i] < '0' && string[i]!='-' && string[i]!='+') // Not a number,'+' or '-'
		|| (string[i]=='-' && i!=0)                                             // A '-' but not at first.
		|| (string[i]=='+' && i!=0)                                             // A '+' but not at first.
		) return false;
	}
	if (length==1 && (string[0]=='-' || string[0]=='+')) return false;
	return true;
}
