/*
	version system --------------------    [ v 0.2.3 ]  -------------------- version system
*/
#include a_samp
#include dc_cmd
#include sscanf2
#include a_mysql
#include streamer
#include foreach

#define MAX_PARTY 15

#define DIALOG_PARTY_INFO   9991
#define DIALOG_PARTY_CREATE   9992
#define DIALOG_PARTY_PANEL   9993
#define DIALOG_PARTY_DESCRIPTION 9994
#define DIALOG_PARTY_NAME   9995

main()
{
	print("");
}
enum playerinfo
{
    pName[MAX_PLAYER_NAME char],
	pParty
}
/* [ new ] */
// ===== [ сисма партий ] ====
new
	partypickup;
static	stock
		PartyName[MAX_PARTY][32];
		//
static  stock
		NameCreate[MAX_PARTY][MAX_PLAYER_NAME + 1 char];
		//
static  stock
		TotalParty = 0;
		//
static 	stock
		CreateData[MAX_PARTY][22];
static  stock
        PartyText[MAX_PARTY][144];
        //
new
	create_time_date[6];
// =========================

new pinfo[MAX_PLAYERS char][playerinfo];
new bd_connect = -1;
new PlayerName[MAX_PLAYERS char][MAX_PLAYER_NAME + 1 char];
static
	stock
	Text3D:textparty[MAX_PLAYERS  char] ={Text3D:-1,...};
/*         */

public OnFilterScriptInit()
{
    bd_connect = mysql_connect("127.0.0.1", "root", "arizona", ""); // подключаем бд
	//
	partypickup = CreatePickup(19131, 23, 2033.1632, 1346.0590, 10.8203); // создали пикап
	//
	Create3DTextLabel ("{8DFDFF}Информация о партиях", -1, 2033.1632, 1346.0590, 10.8203,20,0); // текс для инфы че за пикап
	//
	mysql_tquery(bd_connect, "SELECT * FROM `party`", "partyload", ""); // вытаскием все что есть в таблице и загружаем паблик
	return true;
}

public OnFilterScriptExit()
{
	return true;
}

public OnPlayerConnect(playerid)
{
    GetPlayerName(playerid, PlayerName[playerid], 24);
	return true;
}

public OnPlayerPickUpPickup(playerid, pickupid)
{
	if (pickupid == partypickup)
	{
		ShowPlayerDialog
		(
			playerid,
			DIALOG_PARTY_INFO,
			DIALOG_STYLE_LIST,
			"{C697E5}Партии",
			"{DEF78C}1. {ffffff}Информация о партии\
			\n{DEF78C}2. {ffffff}Создание партии\
			\n{DEF78C}3. {ffffff}Список партий",
			"Далее", "Закрыть"
		);
	}
	return true;
}
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if (newkeys & KEY_YES)
	{
		if (GetPVarInt(playerid, "listparty")) mysql_function_query(bd_connect, "SELECT * FROM `party` ORDER BY `party`. `NameCreate` DESC LIMIT 0, 6", true, "listovkaparty", "i", playerid);
	}
	return true;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	switch(dialogid)
	{
		case DIALOG_PARTY_INFO:
		{
			if (!response) return true;
			switch(listitem)
			{
				case 0: mysql_function_query(bd_connect, "SELECT * FROM `party` LIMIT 15", true, "search_party", "i", playerid);
				case 1:
				{
					ShowPlayerDialog
					(
						playerid,
						DIALOG_PARTY_CREATE,
						DIALOG_STYLE_INPUT,
						"{C697E5}Создание партии",
		            	"\n\n{ffffff}Стоимость создания партии: {047D12}$1000000\n\n\
						{89B1F2}Введите название партии:\n\n",
						"Далее", "Закрыть"
					);
				}
				case 2: mysql_function_query(bd_connect, "SELECT * FROM `party` LIMIT 15", true, "allparty", "i", playerid);
			}
		}
		case DIALOG_PARTY_CREATE:
		{
			if (!response) return true;
			
            if (!strlen(inputtext) || strlen(inputtext)>32)
				return SendClientMessage(playerid, -1, "Вы не можете использовать больше 32 символов");
				
			static const
			    str_party_create[] = "{047D12}[Успешно] {ffffff}Вы успешно создали партию %s. Для управление используйте: {FFA20D}/party";
    		new
				str_party[sizeof(str_party_create) + 32 - 2];
 			format
			(
				str_party,
				sizeof(str_party),
				str_party_create,
				inputtext
			);
			SendClientMessage(playerid, -1, str_party);
  			////////
			static const
			    add_party[] = "SELECT `ID` FROM `party` WHERE `ID` = '%i' LIMIT 15";
			new
			    party_add[sizeof(add_party) + 5 - 2];
 			format
			(
				party_add,
				sizeof(party_add),
				add_party,
				TotalParty
			);
			mysql_tquery(bd_connect, party_add, "create_party", "ii", playerid, TotalParty);
			

			new
			    str_add[22 + 1];
			    
			strmid(PartyName[TotalParty], inputtext, 0, strlen(inputtext));
			strmid(NameCreate[TotalParty], PlayerName[playerid], 0, strlen(PlayerName[playerid]), MAX_PLAYER_NAME);
			//
			getdate(create_time_date[0], create_time_date[1], create_time_date[2]);
			gettime(create_time_date[3], create_time_date[4], create_time_date[5]);
			format
			(
				str_add,
				20,
				"%02d/%02d/%d в %02d:%02d",
				create_time_date[0],
				create_time_date[1],
				create_time_date[2],
				create_time_date[3],
				create_time_date[4],
				create_time_date[5]
			);
			strmid(CreateData[TotalParty], str_add, 0, 20, 31);
			pinfo[playerid][pParty] = TotalParty + 1;
		}
		case DIALOG_PARTY_PANEL:
		{
		    if (!response) return true;
		    new
				party_members_online = 0;
			switch(listitem)
			{
				case 0: mysql_function_query(bd_connect, "SELECT * FROM `party` ORDER BY `party`. `NameCreate` DESC LIMIT 0, 6", true, "infoparty", "i", playerid);
				case 1:
				{
					static const
					    str_all_members[] = "{A92F03}Всего {ffffff}%i {A92F03}членов в партии";
					new
						all_members[sizeof(str_all_members) + 3 - 2];
    		        format
					(
						all_members,
						sizeof(all_members),
						str_all_members,
						party_members_online
					); // это ебатня надо еще решить, сделано не до конца
					SendClientMessage(playerid, -1, all_members);
				}
				case 2:
				{
					foreach (new i : Player)
					{
						if (pinfo[i][pParty] == pinfo[playerid][pParty])
						{
						    new strings[120];
					        format(strings, sizeof(strings), "%s {A92F03}%i. {ffffff}%s\n", strings, party_members_online + 1, PlayerName[i]); // это ебатня такая же, чет придумаем
					        ShowPlayerDialog
							(
								playerid,
								0000,
								DIALOG_STYLE_MSGBOX,
								"{ffffff}Состав партии {047D12}online", strings,
								"Закрыть", ""
							);
					        party_members_online ++;
					        break;
			    		}
					}
				}
				case 3:
				{
					ShowPlayerDialog
					(
						playerid,
						DIALOG_PARTY_DESCRIPTION,
						DIALOG_STYLE_INPUT,
						"{C697E5}Изменение описания партии",
						"\n\n{89B1F2}Введите новое описание партии:\n\n",
						"Далее", "Закрыть"
					);
				}
				case 4:
				{
					ShowPlayerDialog
					(
						playerid,
						DIALOG_PARTY_NAME,
						DIALOG_STYLE_INPUT,
						"{C697E5}Изменение названия партии",
						"\n\n{89B1F2}Введите новое название партии:\n\n",
						"Далее", "Закрыть"
					);
				}
			    // ебала какая - то	case 5:
				case 6:
				{
					SendClientMessage(playerid, -1, "Вы покинули партию");
				    pinfo[playerid][pParty] = TotalParty - 1;
				}
			}
		}
		case DIALOG_PARTY_DESCRIPTION:
		{
		    if (!response) return true;
		    
		    if (!strlen(inputtext) || strlen(inputtext) > 144)
				return SendClientMessage(playerid, -1, "Вы не можете использовать больше 144 символов");

		    static const
			   	str_opis_party[] = "{047D12}[Успешно] {ffffff}Вы успешно изменили описание партии на: {C697E5}%s.";
			new
				str_opis[sizeof(str_opis_party) + 128 - 2];
			format
			(
				str_opis,
				sizeof(str_opis),
				str_opis_party,
				inputtext
			);
			SendClientMessage(playerid, -1, str_opis);
			//
  		    static const
			    str_opisparty[] = "UPDATE `party` SET `PartyText`= '%s'";
			new
			    opis_party_str[sizeof(str_opisparty) + 128 - 2];
		  	format
			(
				opis_party_str,
				sizeof(opis_party_str),
				str_opisparty,
    			inputtext
			);
			mysql_tquery(bd_connect, opis_party_str, "", "");
		}
		case DIALOG_PARTY_NAME:
		{
		    if (!response) return true;

		    if (!strlen(inputtext) || strlen(inputtext)>32)
				return SendClientMessage(playerid, -1, "Вы не можете использовать больше 32 символов");

		    static const
			   	str_nazv_party[] = "{047D12}[Успешно] {ffffff}Вы успешно изменили название партии на: {C697E5}%s.";
			new
				str_naz[sizeof(str_nazv_party) + 32 - 2];
			format
			(
				str_naz,
				sizeof(str_naz),
				str_nazv_party,
				inputtext
			);
			SendClientMessage(playerid, -1, str_naz);
			//
		    static const
			    str_nazparty[] = "UPDATE `party` SET `PartyName`= '%s'";
			new
			    naz_party_str[sizeof(str_nazparty) + 32 - 2];
		  	format
			(
				naz_party_str,
				sizeof(naz_party_str),
				str_nazparty,
    			inputtext
			);
			mysql_tquery(bd_connect, naz_party_str, "", "");
		}
	}
	return true;
}
forward partyload();
public partyload()
{
	new
		rows,
		fields;
 	cache_get_data(rows, fields, bd_connect);
 	//
	if (!rows)
		return printf("[info] Party не загружены, причины кол-во партий в таблице равно - 0, ");
 	for(new f = 0; f < rows; f++)
    {
        if (f > MAX_PARTY -1) break;
        cache_get_row(f, 1, NameCreate[f], bd_connect, 32);
        cache_get_row(f, 2, CreateData[f], bd_connect, 22);
        cache_get_row(f, 3, PartyName[f], bd_connect, 24);
        cache_get_row(f, 4, PartyText[f], bd_connect, 144);
        
        TotalParty++;
    }
    printf ("[info] Party успешно загружены, кол-во партий [%d]", rows);
	return true;
}
forward create_party(playerid, parid);
public create_party(playerid, parid)
{
	new
		num,
		fields;
	cache_get_data(num, fields);
	//
	if (!num)
	{
	    if (TotalParty >= (MAX_PARTY - 2))
			return SendClientMessage(playerid, -1,"Превышено колочество");
			
		static const
		    str_create_party[] =
				"\
					INSERT INTO `party`\
					(`ID`,\
					`NameCreate`,\
					`CreateData`,\
					`PartyName`)\
					 VALUES\
					 ('%i',\
					 '%s',\
					 '%s',\
					 '%s')\
				";
		new
		    create_party_str[sizeof(str_create_party) + 3 + MAX_PLAYER_NAME + 1 + 22  + 32 + 2 - 10];
   		format
		(
			create_party_str,
			sizeof(create_party_str),
			str_create_party,
			TotalParty,
			NameCreate[TotalParty],
			CreateData[TotalParty],
			PartyName[TotalParty]
		);
		mysql_tquery(bd_connect, create_party_str, "", "");
		TotalParty++;
	}
	else
	{
	    TotalParty++;
	    static const
      		str_create_party2[] = "SELECT `ID` FROM `party` WHERE `ID` = '%i' LIMIT 15";
		new
            create_party2[sizeof(str_create_party2) + 2 - 2];
     	format
		(
			create_party2,
			sizeof(create_party2),
			str_create_party2,
			TotalParty
		);
     	mysql_tquery(bd_connect, create_party2, "create_party", "ii", playerid, TotalParty);
	}
	return true;
}
forward search_party(playerid);
public search_party(playerid)
{
    new
		partyall = cache_num_rows();
	//
	if (partyall > 0)
	{
        static const
	    	str_search_party[] =
				"\
					\n\n\n\n{ffffff}Зарегистрировано партий: {610BC4}%d\n\n\
					{89B1F2}Введите название партии:\n\n\n\n\
				";
		new
		    search_party_str[sizeof(str_search_party) + 10 - 2];
		format
		(
			search_party_str,
			sizeof(search_party_str),
			str_search_party,
			partyall
		);
		ShowPlayerDialog(playerid, 0000, DIALOG_STYLE_INPUT, "{DEF78C}Поиск партии", search_party_str, "Далее", "Закрыть");
	}
}
forward allparty(playerid);
public allparty(playerid)
{
    new
		rows,
		fields,
		string[500],
		partyname[32 + 1];
	cache_get_data(rows, fields);
	//
	if (rows < 1)
		return true;
    for(new i; i < rows; i++)
    {
        cache_get_field_content(i, "PartyName", partyname);
        
 		format(string, sizeof(string), "{ffffff}%s{A92F03}%i. \t\t\t{ffffff}%s\n", string, i + 1, partyname);
		ShowPlayerDialog(playerid, 0000, DIALOG_STYLE_LIST, "{F7E388}Список партий:", string, "Далее", "Закрыть");
	}
	return true;
}
forward listovkaparty(playerid);
public listovkaparty(playerid)
{
    new
		rows,
		fields,
		string[227],
		partyname[32 + 1],
		nickname[MAX_PLAYER_NAME + 1],
		dateparty[22],
		text[144],
		party_members_online = 0;
	cache_get_data(rows, fields);
	//
	if (rows < 1)
		return true;
    for(new i; i < rows; i++)
    {
        cache_get_field_content(i, "NameCreate", nickname);
        cache_get_field_content(i, "PartyName", partyname);
        cache_get_field_content(i, "CreateData", dateparty);
		cache_get_field_content(i, "PartyText", text);
		
		if (pinfo[i][pParty] == pinfo[playerid][pParty]) party_members_online ++;

		//
		format
		(
			string,
			sizeof(string), // эмоции выше
			"%s\n\n{ffffff}Партия {C47A04}%s\n\n\
			{ffffff}Состав партии: {DEF78C}%i {ffffff}участник(ов)\n\
			{ffffff}Дата образования: {DEF78C}%s\n\n\
			{ffffff}Руководитель партии: {89B1F2}%s\n\n\n\n\n\
			{89B1F2}%s",
			string,
			partyname,
			party_members_online,
			dateparty,
			nickname,
			text
		);
	}
	DeletePVar(playerid, "listparty");
 	ShowPlayerDialog(playerid, 0000, DIALOG_STYLE_MSGBOX, "{DEF78C}Листовка", string, "Закрыть", "");
	return true;
}
forward infoparty(playerid);
public infoparty(playerid)
{
    new
		numb,
		fields,
		string2[227],
		partyname[32 + 1],
		nickname[MAX_PLAYER_NAME + 1],
		dateparty[22],
		text[144],
		party_members_online = 0;
	cache_get_data(numb, fields);
	//
	if (numb < 1)
		return true;
    for(new i; i < numb; i++)
    {
        cache_get_field_content(i, "NameCreate", nickname);
        cache_get_field_content(i, "PartyName", partyname);
        cache_get_field_content(i, "CreateData", dateparty);
		cache_get_field_content(i, "PartyText", text);
		
		if (pinfo[i][pParty] == pinfo[playerid][pParty]) party_members_online ++;
		//
		format
		(
			string2,
			sizeof(string2), // эмоции выше
			"%s\n\n{ffffff}Партия {C47A04}%s\n\n\
			{ffffff}Состав партии: {DEF78C}%i {ffffff}участник(ов)\n\
			{ffffff}Дата образования: {DEF78C}%s\n\n\
			{ffffff}Руководитель партии: {89B1F2}%s\n\n\n\n\n\
			{89B1F2}%s",
			string2,
			partyname,
			party_members_online,
			dateparty,
			nickname,
			text
		);
	}
 	ShowPlayerDialog(playerid, 0000, DIALOG_STYLE_MSGBOX, "{DEF78C}Информация о партии", string2, "Закрыть", "");
	return true;
}
/* [ команды ] */
CMD:party(playerid,params[])
{
    if (!pinfo[playerid][pParty])
		return SendClientMessage(playerid, -1,"Вы не состоите в партии");

    ShowPlayerDialog
	(
		playerid,
		DIALOG_PARTY_PANEL,
		DIALOG_STYLE_LIST,
		"{DEF78C}Партия",
		"{DEF78C}1. {ffffff}Информация о партии\
		\n{DEF78C}2. {ffffff}Состав партии\
		\n{DEF78C}3. {ffffff}Состав партии {047D12}online\
		\n{DEF78C}4. {ffffff}Изменить описание партии\
		\n{DEF78C}5. {ffffff}Изменить название партии\
		\n{DEF78C}6. {ffffff}Изменить цвет партии\
		\n{DEF78C}7. {BD0707}Покинуть партию",
		"Далее", "Закрыть"
	);
    return true;
}
CMD:pobject(playerid,params[])
{
    if (!pinfo[playerid][pParty])
		return SendClientMessage(playerid, -1, "Вы не состоите в партии");
		
	if (sscanf(params, "i", params[0]))
		return SendClientMessage(playerid, -1, "{FBFF09}[Подсказка] {ffffff}/pobject [objectid (1, 3)]");
	#define objectid params[0]
	
	new
		Float:x,
		Float:y,
		Float:z,
		Float:angle;
		
	if (objectid < 1 || objectid > 3)
		return true;

	if (GetPVarInt(playerid,"pobject") == 1)
	{
        DestroyDynamicObject(GetPVarInt(playerid, "p_object"));
        DeletePVar(playerid, "p_object");
        DeletePVar(playerid, "pobject");
        Delete3DTextLabel(Text3D:textparty[playerid]);
        textparty[playerid] = Text3D: -1;
		SendClientMessage(playerid, -1, "Вы убрали предмет");
		return true;
	}
	//
	GetPlayerPos(playerid, x, y, z);
    GetPlayerFacingAngle(playerid, angle);
    x += floatsin(-angle, degrees);
    y += floatcos(-angle, degrees);
	ApplyAnimation(playerid, "BOMBER", "BOM_Plant", 2, 0, 0, 0, 0, 0);
	//
    if (GetPVarInt(playerid,"pobject") == 0)
	{
		switch(objectid)
		{
			case 1: SetPVarInt(playerid, "p_object", CreateDynamicObject(3862, x, y, z, 0, 0, 0));
			case 2: SetPVarInt(playerid, "p_object", CreateDynamicObject(1955, x, y, z, 0, 0, 0));
			case 3: SetPVarInt(playerid, "p_object", CreateDynamicObject(2773, x, y, z-0.49, 0, 0, 0));
		}
		static const
		    str_p_object[] = "%d";
		    
		new
			p_object_str[sizeof(str_p_object) + 3 - 2];
			
        format(p_object_str, sizeof(p_object_str), str_p_object, playerid);
        
        textparty[playerid] = Create3DTextLabel(p_object_str, -1, x, y, z-0.7, 30.0, 0, 0);
        SetPVarInt(playerid,"pobject", 1);
		SendClientMessage(playerid, -1, "Вы установили предмет. Используйте {ffffff}'{610BC4}/pobject{ffffff}' ещё раз, чтобы убрать его");
	}
	#undef objectid
    return true;
}
CMD:partylist(playerid,params[])
{
	if (!pinfo[playerid][pParty])
		return SendClientMessage(playerid, -1, "Вы не состоите в партии");

	if (sscanf(params, "d", params[0]))
		return SendClientMessage(playerid, -1, "{FBFF09}[Подсказка] {ffffff}/partylist [playerid]");
	#define targetid params[0]
	
	static const
	    str_party_list[] = "{269BD8}Вы предложили {ffffff}%s {269BD8}взять листовку";
	new
	    party_list_str[sizeof(str_party_list) + MAX_PLAYER_NAME + 1 - 2];
	format(party_list_str, sizeof(party_list_str), str_party_list, PlayerName[targetid]);
	SendClientMessage(playerid, -1, party_list_str);
	//
	static const
	    str_party_list2[] = "%s {269BD8}предложил Вам взять партийную листовку. Чтобы подтвердить нажмите {047D12}Y, {269BD8}иначе {9F0000}N";
	new
	    party_list_str2[sizeof(str_party_list2) + MAX_PLAYER_NAME + 1 - 2];
	format(party_list_str2, sizeof(party_list_str2), str_party_list2, PlayerName[playerid]);
	SetPVarInt(targetid, "listparty", 1);
	SendClientMessage(targetid, -1, party_list_str2);
	
	#undef targetid
   	return true;
}
