#include <a_samp>

#define AFKSeconds 500
#define COLOR_YELLOW 0xFFFF00AA

enum afker
{
pNameEx[MAX_PLAYER_NAME],
TickEx,
ChekEx,
VarEx,
bool:pSpawnEx,
bool:AFKEx,
}

new PlayerEx[MAX_PLAYERS][afker];
new GG,TT;
public OnFilterScriptInit()
{
print("***************************************************");
print("*            AFK System by Crone                  *");
print("*                                 nwo-team.ru     *");
print("***************************************************");
GG = SetTimer("AFKProcessor", 1000,1);
TT = SetTimer("CountEx", 5000,1);
return 1;
}

public OnFilterScriptExit()
{
KillTimer(GG);
KillTimer(TT);
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i) && PlayerEx[i][AFKEx] == true)
{
new str[MAX_PLAYER_NAME];
format(str, sizeof (str), "%s", PlayerEx[i][pNameEx]);
SetPlayerName(i,str);
}
}
return 1;
}

public OnGameModeInit()
{

	return 1;
}

public OnGameModeExit()
{
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i) && PlayerEx[i][AFKEx] == true)
{
new str[MAX_PLAYER_NAME];
format(str, sizeof (str), "%s", PlayerEx[i][pNameEx]);
SetPlayerName(i,str);
}
}
return 1;
}


public OnPlayerConnect(playerid)
{
    new str[MAX_PLAYER_NAME];
    format(str, sizeof (str), "%s", GetPlayerNameEx(playerid));
    new str2[30];
    format(str2, sizeof (str2), "AFK_%s", GetPlayerNameEx(playerid));
    new str3[5];
    format(str3, sizeof (str3), "%s", GetPlayerNameEx(playerid));
    PlayerEx[playerid][pNameEx] = str;
    PlayerEx[playerid][pSpawnEx] = false;
    PlayerEx[playerid][AFKEx] = false;
    if(strfind(str3, "AFK_", true) != -1)
    {
    Kick(playerid);
    }
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
    if(IsPlayerConnected(i) && i != playerid)
    {
    new find[30];
    format(find, sizeof (find), "AFK_%s", GetPlayerNameEx(i));
    if(strfind(find, str2, true) != -1)
    {
    Kick(playerid);
    }
    }
    }
    return 1;
}



public OnPlayerDeath(playerid,killerid, reason)
{
    PlayerEx[playerid][pSpawnEx] = false;
	return 1;
}


forward CountEx();
public CountEx()
{
for(new x = 0; x < MAX_PLAYERS; x++)
{
 if(IsPlayerConnected(x))
  {
  if(PlayerEx[x][ChekEx] == PlayerEx[x][TickEx])
  {
  if(PlayerEx[x][pSpawnEx] == true)
  {
  PlayerEx[x][AFKEx] = true;
  }
  }
  PlayerEx[x][ChekEx] = PlayerEx[x][TickEx];
  }
}
return 1;
}

forward AFKProcessor();
public AFKProcessor()
{
for(new x = 0; x < MAX_PLAYERS; x++)
{
 if(IsPlayerConnected(x))
  {
    if(PlayerEx[x][ChekEx] < PlayerEx[x][TickEx])
    {
    if(PlayerEx[x][AFKEx] == true)
    {
    new str[MAX_PLAYER_NAME];
    format(str, sizeof (str), "%s", PlayerEx[x][pNameEx]);
    SetPlayerName(x,str);
    PlayerEx[x][VarEx] = 0;
    PlayerEx[x][AFKEx] = false;
    }
    }
    if(PlayerEx[x][AFKEx] == true)
    {
	PlayerEx[x][VarEx]++;
    new str[MAX_PLAYER_NAME];
    format(str, sizeof (str), "AFK_%s", PlayerEx[x][pNameEx]);
    SetPlayerName(x,str);
    }
    if(PlayerEx[x][VarEx] > AFKSeconds)
    {
    new string[255];
    format(string,sizeof(string),"[AFK]: Игрок %s кикнут с сервера за долгое пребывание в AFK.", GetPlayerNameEx(x));
    SendClientMessageToAll(COLOR_YELLOW,string);
    Kick(x);
    PlayerEx[x][VarEx] = 0;
    }
    if(PlayerEx[x][VarEx] > 10)
    {
    new stringF[20];
    format(stringF,sizeof(stringF),"ESC [%s]",Convert(PlayerEx[x][VarEx]));
    SetPlayerChatBubble(x, stringF, 0xFFFFFF00, 70.0, 1200);
    }
  }
}
return 1;
}

public OnPlayerSpawn(playerid)
{
    PlayerEx[playerid][pSpawnEx] = true;
    return 1;
}

public OnPlayerUpdate(playerid)
{
	if(IsPlayerConnected(playerid))PlayerEx[playerid][TickEx]++;
	return 1;
}


public OnPlayerDisconnect(playerid, reason)
{
if(PlayerEx[playerid][AFKEx] == true)
{
new str[MAX_PLAYER_NAME];
format(str, sizeof (str), "%s", PlayerEx[playerid][pNameEx]);
SetPlayerName(playerid,str);
}
return 1;
}

GetPlayerNameEx(playerid)
{
	new pname[MAX_PLAYER_NAME];
	GetPlayerName(playerid,pname,MAX_PLAYER_NAME);
	return pname;
}


Convert(number)
{
	new hours = 0, mins = 0, secs = 0, string[100];
	hours = floatround(number / 3600);
	mins = floatround((number / 60) - (hours * 60));
	secs = floatround(number - ((hours * 3600) + (mins * 60)));
	if(hours > 0)
	{
	format(string, 100, "%d:%02dm:%02ds", hours, mins, secs);
	}
	else
	{
	format(string, 100, "%d:%02d", mins, secs);
	}
	return string;
}



