[Include] Ban System [MySQL R39-4 + TextDraw]
#1

Sei que jб hб sistemas assim, mas esse й meu primeiro sistema em MySQL.

Esse sistema em MySQL, do meu ponto de vista, ficou melhor que o meu anterior em SQLite e para dar uma diferenciada coloquei TextDraws no lugar de mensagens ou diбlogo.

TABELA E CONEXГO
A primeira coisa й conectar no banco de dados e deve ser inserido normalmente em OnGameModeInit/OnFilterScriptInit.
A Conexгo й estabelecida e a tabela й criada automaticamente, apenas forneзa as informaзхes corretas.
Код:
mysql_ban_connect(const host[], const user[], const database[], const password[]);
Exemplo:
Код:
mysql_ban_connect("localhost", "root", "server_samp", "");
VERIFICAЗГO DO JOGADOR
Podemos verificar se o jogador que serб banido estб online ou nгo com a seguinte funзгo (lembrando que a verificaзгo й em string e nгo inteiro como IsPlayerConnected):
Код:
bool:mysql_ban_player_check(Find[])
Exemplo:
Код:
//VERIFICANDO PELO NOME
GetPlayerName(playerid, Nome, 24);
if(mysql_ban_player_check(Nome) == true)
{
     //Player On
}
else
{
     //Player Off
}

//VERIFICANDO PELO IP
GetPlayerIp(playerid, IP, 16);
if(mysql_ban_player_check(IP) == true)
{
     //Player On
}
else
{
     //Player Off
}
BANINDO O JOGADOR
Код:
mysql_ban_player(Player_Name[], Player_IP[], Ban_By[], Reason[], Ban_Type, Time);
Exemplo:
Код:
mysql_ban_player("WesleyScript", "127.0.0.1", "F1N4L", "LIXO", PERMANENTE, 0);
OBS: Quando colocarem como Ban permanente, independentemente do tempo que vcs colocarem, nгo vai mudar nada.
O Ban permanente й basicamente 20 anos banido, ou seja, tecnicamente permanente mesmo. A verificaзгo se й permanente ou nгo й se caso os dias forem maiores que 3650, ou seja, +- 10 anos.

Por padrгo jб defini macros e podem rodar no GM/FS sem problemas, apenas serб mais fбcil de alternar entre o tipo de ban:
Код:
#define				PERMANENTE			0
#define				MINUTOS				1
#define				HORAS				2
#define				DIAS				3
DESBANINDO O JOGADOR
Код:
mysql_ban_remove(Banned[]);
Para evitar de fazer a verificaзгo no banco de dados se existe ou nгo atravйs dos comando SQL fiz a seguinte stock:
Код:
bool:mysql_ban_remove_check(Banned[])
Exemplo:
Код:
//VERIFICANDO PELO NOME
if(mysql_ban_player_check("F1N4L") == true)
{
     //Coluna encontrada
}
else
{
     //Coluna nгo encontrada
}

//VERIFICANDO PELO IP
if(mysql_ban_player_check("127.0.0.1") == true)
{
     //Coluna encontrada
}
else
{
     //Coluna nгo encontrada
}
VERIFICANDO AO CONECTAR
Eu criei o hook na include propositalmente, assim as ъnicas coisas necessбrias e principais sгo as stocks de conexгo ao banco de dados e a de banir, portanto, a verificaзгo vai ocorrer na include mesmo.
Код:
mysql_banned_check(playerid);
Conexгo:
APLICANDO NA PRБTICA:
Код:
public OnFilterScriptInit() // ou OnGameModeInit
{
	mysql_ban_connect("localhost", "root", "server_samp", "");
	
	return 1;
}
NOS COMANDOS ABAIXO TEM EXEMPLOS CORRETOS DA UTILIZAЗГO DO SISTEMA

Comandos:
Код:
CMD:banname(playerid, params[])
{
	new NomeBanido[24], NomeBanidoPor[24], Motivo[20], TipoBanimento, Tempo,String[128];
	
	if(sscanf(params, "s[24]iis[20]", NomeBanido, TipoBanimento, Tempo, Motivo)) return SendClientMessage(playerid, -1, "/ban [nome] [Tipo: 0 = permanente | 1 = minutos | 2 = horas | 3 = dias] [tempo] [motivo]");
	
	if(TipoBanimento < 0 || TipoBanimento > 3) return SendClientMessage(playerid, -1, "Tipo de banimento incorreto! Utilize: 0 = permanente | 1 = minutos | 2 = horas | 3 = dias");
	
	GetPlayerName(playerid, NomeBanidoPor, sizeof NomeBanidoPor);
	
	if(mysql_ban_player_check(NomeBanido) == true)
	{
		switch(TipoBanimento)
		{
			case 0: format(String, sizeof String, "Admin %s baniu Online o jogador %s. Motivo: %s. Tempo: PERMANENTE", NomeBanidoPor, NomeBanido, Motivo);
			case 1: format(String, sizeof String, "Admin %s baniu Online o jogador %s. Motivo: %s. Tempo: %i Minutos", NomeBanidoPor, NomeBanido, Motivo, Tempo);
			case 2: format(String, sizeof String, "Admin %s baniu Online o jogador %s. Motivo: %s. Tempo: %i Horas", NomeBanidoPor, NomeBanido, Motivo, Tempo);
			case 3: format(String, sizeof String, "Admin %s baniu Online o jogador %s. Motivo: %s. Tempo: %i Dias", NomeBanidoPor, NomeBanido, Motivo, Tempo);
		}
	}
	else
	{
		switch(TipoBanimento)
		{
			case 0: format(String, sizeof String, "Admin %s baniu Offline o jogador %s. Motivo: %s. Tempo: PERMANENTE", NomeBanidoPor, NomeBanido, Motivo);
			case 1: format(String, sizeof String, "Admin %s baniu Offline o jogador %s. Motivo: %s. Tempo: %i Minutos", NomeBanidoPor, NomeBanido, Motivo, Tempo);
			case 2: format(String, sizeof String, "Admin %s baniu Offline o jogador %s. Motivo: %s. Tempo: %i Horas", NomeBanidoPor, NomeBanido, Motivo, Tempo);
			case 3: format(String, sizeof String, "Admin %s baniu Offline o jogador %s. Motivo: %s. Tempo: %i Dias", NomeBanidoPor, NomeBanido, Motivo, Tempo);
		}
	}
	SendClientMessageToAll(-1, String);
	
	mysql_ban_player(NomeBanido, "0.0.0.0", NomeBanidoPor, Motivo, TipoBanimento, Tempo);
	
	return 1;
}

CMD:banip(playerid, params[])
{
	new IpBanido[24], NomeBanidoPor[24], Motivo[20], TipoBanimento, Tempo, String[128];
	
	if(sscanf(params, "s[16]iis[20]", IpBanido, TipoBanimento, Tempo, Motivo)) return SendClientMessage(playerid, -1, "/ban [nome] [Tipo: 0 = permanente | 1 = minutos | 2 = horas | 3 = dias] [tempo] [motivo]");
	
	if(TipoBanimento < 0 || TipoBanimento > 3) return SendClientMessage(playerid, -1, "Tipo de banimento incorreto! Utilize: 0 = permanente | 1 = minutos | 2 = horas | 3 = dias");
	
	GetPlayerName(playerid, NomeBanidoPor, sizeof NomeBanidoPor);
	
	if(mysql_ban_player_check(IpBanido) == true)
	{
		switch(TipoBanimento)
		{
			case 0: format(String, sizeof String, "Admin %s baniu Online a faixa de IP %s. Motivo: %s. Tempo: PERMANENTE", NomeBanidoPor, IpBanido, Motivo);
			case 1: format(String, sizeof String, "Admin %s baniu Online a faixa de IP %s. Motivo: %s. Tempo: %i Minutos", NomeBanidoPor, IpBanido, Motivo, Tempo);
			case 2: format(String, sizeof String, "Admin %s baniu Online a faixa de IP %s. Motivo: %s. Tempo: %i Horas", NomeBanidoPor, IpBanido, Motivo, Tempo);
			case 3: format(String, sizeof String, "Admin %s baniu Online a faixa de IP %s. Motivo: %s. Tempo: %i Dias", NomeBanidoPor, IpBanido, Motivo, Tempo);
		}
	}
	else
	{
		switch(TipoBanimento)
		{
			case 0: format(String, sizeof String, "Admin %s baniu Offline a faixa de IP %s. Motivo: %s. Tempo: PERMANENTE", NomeBanidoPor, IpBanido, Motivo);
			case 1: format(String, sizeof String, "Admin %s baniu Offline a faixa de IP %s. Motivo: %s. Tempo: %i Minutos", NomeBanidoPor, IpBanido, Motivo, Tempo);
			case 2: format(String, sizeof String, "Admin %s baniu Offline a faixa de IP %s. Motivo: %s. Tempo: %i Horas", NomeBanidoPor, IpBanido, Motivo, Tempo);
			case 3: format(String, sizeof String, "Admin %s baniu Offline a faixa de IP %s. Motivo: %s. Tempo: %i Dias", NomeBanidoPor, IpBanido, Motivo, Tempo);
		}
	}
	SendClientMessageToAll(-1, String);
	
	mysql_ban_player("N/A", IpBanido, NomeBanidoPor, Motivo, TipoBanimento, Tempo);
	
	return 1;
}

CMD:banid(playerid, params[])
{
	new Banido, IpBanido[24], NomeBanido[24], NomeBanidoPor[24], Motivo[20], TipoBanimento, Tempo, String[128];
	
	if(sscanf(params, "uiis[20]", Banido, TipoBanimento, Tempo, Motivo)) return SendClientMessage(playerid, -1, "/ban [id/nome] [Tipo: 0 = permanente | 1 = minutos | 2 = horas | 3 = dias] [tempo] [motivo]");
	
	if(!IsPlayerConnected(Banido)) return SendClientMessage(playerid, -1, "Jogador nгo conectado!");
	
	if(TipoBanimento < 0 || TipoBanimento > 3) return SendClientMessage(playerid, -1, "Tipo de banimento incorreto! Utilize: 0 = permanente | 1 = minutos | 2 = horas | 3 = dias");
	
	GetPlayerName(playerid, NomeBanidoPor, sizeof NomeBanidoPor);
	
	GetPlayerName(Banido, NomeBanido, sizeof NomeBanido);
	GetPlayerIp(Banido, IpBanido, sizeof IpBanido);
	
	if(mysql_ban_player_check(IpBanido) == true)
	{
		switch(TipoBanimento)
		{
			case 0: format(String, sizeof String, "Admin %s baniu Online o jogador %s [IP: %s]. Motivo: %s. Tempo: PERMANENTE", NomeBanidoPor, NomeBanido,  IpBanido, Motivo);
			case 1: format(String, sizeof String, "Admin %s baniu Online o jogador %s [IP: %s]. Motivo: %s. Tempo: %i Minutos", NomeBanidoPor, NomeBanido, IpBanido, Motivo, Tempo);
			case 2: format(String, sizeof String, "Admin %s baniu Online o jogador %s [IP: %s]. Motivo: %s. Tempo: %i Horas", NomeBanidoPor, NomeBanido, IpBanido, Motivo, Tempo);
			case 3: format(String, sizeof String, "Admin %s baniu Online o jogador %s [IP: %s]. Motivo: %s. Tempo: %i Dias", NomeBanidoPor, NomeBanido, IpBanido, Motivo, Tempo);
		}
		SendClientMessageToAll(-1, String);
	}
	else SendClientMessage(playerid, -1, "Player ID informado nгo estб conectado!");
	
	mysql_ban_player(NomeBanido, IpBanido, NomeBanidoPor, Motivo, TipoBanimento, Tempo);
	
	return 1;
}

CMD:banremove(playerid, params[])
{
	if(isnull(params)) return SendClientMessage(playerid, -1, "/banremove [name/ip]");
	
	if(mysql_ban_remove_check(params) == true)
	{
		mysql_ban_remove(params);
		SendClientMessage(playerid, -1, "Jogador/IP encontrado e desbanido com sucesso!");
	}
	else SendClientMessage(playerid, -1, "Jogador/IP nгo foi encontrado no Banco de Dados!");
	
	return 1;
}



O sistema foi atualizado [26/05/2016 - 21:55]:
*TextDraws se resumem em apenas 4 - 2 box e 2 msg.
*Sistema bem otimizado e identado.


Download:
PASTEBIN

Crйditos:
a_samp // By SA-MP Team
zcmd // By Zeex
sscanf // By ******
a_mysql // By BlueG
Script // By F1N4L
Reply
#2

Muito bom, parabйns.
Reply
#3

Quote:
Originally Posted by LockedLucas
Посмотреть сообщение
Muito bom, parabйns.
Thx bro!
Reply
#4

Gostei, to aprendendo mysql esse sistema vai me ajuda vlw.
Reply
#5

Quote:
Originally Posted by _Play_
Посмотреть сообщение
Gostei, to aprendendo mysql esse sistema vai me ajuda vlw.
Vlw, _Play_!
Reply
#6

Ficou уtimo, Parabйns
Reply
#7

Quote:
Originally Posted by Wellington1999
Посмотреть сообщение
Ficou уtimo, Parabйns
Obg, Wellington1999!
Reply
#8

Porra!

Meus parabйns em cara, Alйm de vocк chega no ponto aonde eu tava batendo cabeзa vocк acabou me ajudando que foi na parte do CheckBan Thank
Reply
#9

Quote:
Originally Posted by ZEDD666
Посмотреть сообщение
Porra!

Meus parabйns em cara, Alйm de vocк chega no ponto aonde eu tava batendo cabeзa vocк acabou me ajudando que foi na parte do CheckBan Thank
Vlw, ZEDD666!
Reply
#10

Ficou legalzinho, mas vocк pode melhorar algumas coisas, por exemplo:

Код:
stock ConvertTime(SEGUNDOS_)
{
	new MINUTOS_, HORAS_, DIAS_, String[200];
	
	if(SEGUNDOS_ > 59)
	{
	    MINUTOS_ = SEGUNDOS_ / 60;
		SEGUNDOS_ = SEGUNDOS_ - MINUTOS_ * 60;
	}
	if(MINUTOS_ > 59)
	{
	    HORAS_ = MINUTOS_ / 60;
		MINUTOS_ = MINUTOS_ - HORAS_ * 60;
	}
	if(HORAS_ > 23)
	{
	    DIAS_ = HORAS_ / 24;
		HORAS_ = HORAS_ - DIAS_ * 24;
	}
	
	format(String, sizeof(String), "%02d Dias e %02d Horas e %02d Minutos e %02d Segundos", DIAS_, HORAS_, MINUTOS_, SEGUNDOS_);
	if(DIAS_ < 1) format(String, sizeof(String), "%02d Horas e %02d Minutos e %02d Segundos", HORAS_, MINUTOS_, SEGUNDOS_);
	if(DIAS_ < 1 && HORAS_ < 1) format(String, sizeof(String), "%02d Minutos e %02d Segundos", MINUTOS_, SEGUNDOS_);
	if(DIAS_ < 1 && HORAS_ < 1 && MINUTOS_ < 1) format(String, sizeof(String), "%02d Segundos", SEGUNDOS_);
	if(DIAS_ > 3650) format(String, sizeof(String), "Permanente");
	
	return String;
}
Isso й um cуdigo feio e desnecessбrio, uma ferramenta tгo rica quanto o MySQL te traz a oportunidade de trabalhar com timestamp, veja por exemplo um cуdigo simples que fiz usando o plugin CTime Library do Ryder`:

Код:
#define DaysToSeconds(%0)	(%0 * 86400)

GetDateFromTime(timestamp)
{
	new	tm <tmStamp>, string[32];
	localtime(Time:timestamp, tmStamp);
		
	strftime(string, sizeof(string), "%d/%m/%y", tmStamp); 
	return string;
}
Entгo para um ban de 30 dias por exemplo, vocк poderia usar o timestamp:

Код:
new ban_de_30_dias = DaysToSeconds(30) + gettime();
E voilб, vocк tem o timestamp de 30 dias


Uma outra coisa que poderia ser melhorada:

Код:
switch(Ban_Type)
	{
		case PERMANENTE: mysql_format(MyConection, MySQL_Format, sizeof MySQL_Format, "INSERT INTO `"TABLE_BAN"` (`"NOME"`, `"IP"`, `"DATA"`, `"BANIDO_POR"`, `"MOTIVO"`, `"TEMPO"`) VALUES ('%s', '%s', '%s', '%s', '%s', '%i')", Player_Name, Player_IP, FormatTimeDate, Ban_By, Reason, 20 * 12 * 30 * 24 * 60 * 60 + gettime());
		case MINUTOS: mysql_format(MyConection, MySQL_Format, sizeof MySQL_Format, "INSERT INTO `"TABLE_BAN"` (`"NOME"`, `"IP"`, `"DATA"`, `"BANIDO_POR"`, `"MOTIVO"`, `"TEMPO"`) VALUES ('%s', '%s', '%s', '%s', '%s', '%i')", Player_Name, Player_IP, FormatTimeDate, Ban_By, Reason, Time * 60 + gettime());
		case HORAS: mysql_format(MyConection, MySQL_Format, sizeof MySQL_Format, "INSERT INTO `"TABLE_BAN"` (`"NOME"`, `"IP"`, `"DATA"`, `"BANIDO_POR"`, `"MOTIVO"`, `"TEMPO"`) VALUES ('%s', '%s', '%s', '%s', '%s', '%i')", Player_Name, Player_IP, FormatTimeDate, Ban_By, Reason, Time * 60 * 60 + gettime());
		case DIAS: mysql_format(MyConection, MySQL_Format, sizeof MySQL_Format, "INSERT INTO `"TABLE_BAN"` (`"NOME"`, `"IP"`, `"DATA"`, `"BANIDO_POR"`, `"MOTIVO"`, `"TEMPO"`) VALUES ('%s', '%s', '%s', '%s', '%s', '%i')", Player_Name, Player_IP, FormatTimeDate, Ban_By, Reason, Time * 24 * 60 * 60 + gettime());
	}
Jб que se trata de um cуdigo repetitivo, bastava vocк usar uma query dessas e uma prй-definiзгo de cada tipo de fуrmula a ser retornado ao multiplicar-se o timestamp obtido com o gettime, fazendo com que assim esse switch fosse desnecessбrio, e vocк sу utilizaria 1 mysql_format com a funзгo de retornar o tempo no formato correto, e entгo seu cуdigo ficaria muito mais legнvel e mais bem modelado.

Gostei da include, acho interessante que os usuбrios venham a utilizar a cada vez mais SQLite, MSSQL, MySQL, MariaDB, e atй SQLServer, parece algo bobo quando se trata de SAMP mas no mercado de trabalho saber a linguagem SQL e suas ferramentas й um grande diferencial, parabйns pela include
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)