Added chicken things. Awards need to be fixed

#pragma semicolon 1

#include <string>
#include <sourcemod>
#include <sdktools>
#include <SteamWorks>
#include <smjansson>
#include <Leet>

//Uncomment to enable debug mode.
#define DEBUG

#define PLUGIN_NAME		"Leet GG"
#define PLUGIN_AUTHOR	"Leetcoin Team"
#define PLUGIN_DES		"The official Leet.gg plugin for Sourcemod. This plugin takes advantage of our native API."
#define PLUGIN_VERSION	"1.0.0"
#define PLUGIN_URL		"https://www.leet.gg/"

//#define API_URL			"api-dot-1337coin.appspot.com"
#define API_URL			"http://apitest-dot-1337coin.appspot.com"
#define API_URL_BLEED	"apitest-dot-1337coin.appspot.com"
#define TEST_URL		"https://www.leet.gg/server/view/agpzfjEzMzdjb2luchMLEgZTZXJ2ZXIYgICA9MD-_gsM"

#define API_URL_GET_SERVER_INFO		"/api/get_server_info"
#define API_URL_ACTIVATE_PLAYER		"/api/activate_player"
#define API_URL_PUT_MATCH_RESULTS	"/api/put_match_results"
#define API_URL_ISSUE_AWARD			"/api/issue_award"
#define API_URL_DEACTIVATE_PLAYER	"/api/deactivate_player"

Handle hConVars[3];
bool cv_bStatus;
char cv_sAPIKey[256];
char cv_sServerSecret[256];

bool bServerSetup;

//On Server Start
int g_minimumBTCHold;
bool g_no_death_penalty;
bool g_allow_non_authorized_players;
int g_admissionFee;
float g_serverRakeBTCPercentage;
char g_api_version[256];
int g_incrementBTC;
float g_leetcoinRakePercentage;
bool g_authorization;

//On Client Connect
int g_player_btchold[MAXPLAYERS + 1];
char g_player_name[MAXPLAYERS + 1][512];
char g_player_platformid[MAXPLAYERS + 1][64];
char g_player_key[MAXPLAYERS + 1][128];
bool g_player_previously_active[MAXPLAYERS + 1];
int g_player_rank[MAXPLAYERS + 1];
bool g_player_authorized[MAXPLAYERS + 1];
char g_default_currency_conversion[MAXPLAYERS + 1][64];
char g_default_currency_display[MAXPLAYERS + 1][32];
bool g_authorization_client[MAXPLAYERS + 1];

int kill_reward;
float rake;
char iCommunityID[MAXPLAYERS + 1][32];
int iClientRank[MAXPLAYERS + 1];
int iStatsKills[MAXPLAYERS + 1];
int iStatsDeaths[MAXPLAYERS + 1];

public Plugin myinfo =
{
	name = PLUGIN_NAME,
	author = PLUGIN_AUTHOR,
	description = PLUGIN_DES,
	version = PLUGIN_VERSION,
	url = PLUGIN_URL
};

public void OnPluginStart()
{
	CreateConVar("leet_gg_version", PLUGIN_VERSION, PLUGIN_DES, FCVAR_REPLICATED | FCVAR_NOTIFY | FCVAR_SPONLY | FCVAR_DONTRECORD);

	hConVars[0] = CreateConVar("sm_leetgg_status", "1", "Status of the plugin.", FCVAR_NOTIFY, true, 0.0, true, 1.0);
	hConVars[1] = CreateConVar("sm_leetgg_api", "", "Server API key to use.", FCVAR_PROTECTED);
	hConVars[2] = CreateConVar("sm_leetgg_sever_secret", "", "Server Secret to use", FCVAR_PROTECTED);

	HookEvent("player_death", OnPlayerDeath);
	HookEvent("round_end", OnRoundEnd);

	RegAdminCmd("sm_chicken", OnSpawnChicken, ADMFLAG_ROOT, "Spawn a chicken where youre looking.");

	CreateTimer(30.0, SubmitPlayerInformation, _, TIMER_REPEAT);

	HookEntityOutput("chicken", "OnBreak", OnChickenKill);

	AutoExecConfig();
}

public Action OnRoundEnd(Event event, const char[] name, bool dontBroadcast) {
	for (int i = 1; i <= MaxClients; i++)
		if (IsClientInGame(i) && !IsFakeClient(i))
			PrintToChat(i, "Your current balance is: %i satoshi.", g_player_btchold[i]);
	return Plugin_Continue;
}

public void OnConfigsExecuted()
{
	cv_bStatus = GetConVarBool(hConVars[0]);
	GetConVarString(hConVars[1], cv_sAPIKey, 256);
	GetConVarString(hConVars[2], cv_sServerSecret, 256);

	if (!cv_bStatus)
	{
		bServerSetup = false;
		return;
	}

	if (strlen(cv_sAPIKey) == 0 || strlen(cv_sServerSecret) == 0)
	{
		Leet_Log("Error retrieving server information, API key or Server secret is missing.");
		Leet_Log("Api Key: %s", cv_sAPIKey);
		Leet_Log("Server Secret: %s", cv_sServerSecret);
		bServerSetup = false;
		return;
	}

	Leet_Log("Requesting server information...");

	char sURL[512];
	Format(sURL, sizeof(sURL), "%s%s", API_URL, API_URL_GET_SERVER_INFO);

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sURL);

	float fTime = float(GetTime());

	char sTime[128];
	FloatToString(fTime, sTime, sizeof(sTime));
	char params[4096];
	Format(params, sizeof(params), "nonce=%s", sTime);

	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "nonce", sTime);

	char sHash[2048];
	digest_string_with_key(cv_sServerSecret, params, sHash, 2048);

	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-type", "application/x-www-form-urlencoded");
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Key", cv_sAPIKey);
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Sign", sHash);
	SteamWorks_SetHTTPCallbacks(hRequest, OnPullingServerInfo);
	SteamWorks_SendHTTPRequest(hRequest);
}

public int OnPullingServerInfo(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode)
{
	if (bFailure || !bRequestSuccessful)
	{
		bServerSetup = false;
		Leet_Log("Error retrieving server information. Error code: %i", view_as<int>(eStatusCode));
		#if defined DEBUG
		Leet_DebugLog("Pulling server information failure:\n-bFailure = %s\n-bRequestSuccessful = %s\n-eStatusCode = %i", bFailure ? "True" : "False", bRequestSuccessful ? "True" : "False", view_as<int>(eStatusCode));
		Leet_Log("Request %s", hRequest);
		#endif
		return;
	}

	int size = 0;
	SteamWorks_GetHTTPResponseBodySize(hRequest, size);

	char[] sBuffer = new char[size];
	SteamWorks_GetHTTPResponseBodyData(hRequest, sBuffer, size);
	Leet_Log("Json string: %s ",sBuffer);

	Handle hJSON = json_load(sBuffer);
	g_minimumBTCHold = json_object_get_int(hJSON, "minimumBTCHold");
	g_no_death_penalty = json_object_get_bool(hJSON, "no_death_penalty");
	g_allow_non_authorized_players = json_object_get_bool(hJSON, "allow_non_authorized_players");
	g_admissionFee = json_object_get_int(hJSON, "admissionFee");
	g_serverRakeBTCPercentage = json_object_get_float(hJSON, "serverRakeBTCPercentage");
	json_object_get_string(hJSON, "api_version", g_api_version, sizeof(g_api_version));
	g_incrementBTC = json_object_get_int(hJSON, "incrementBTC");
	g_leetcoinRakePercentage = json_object_get_float(hJSON, "leetcoinRakePercentage");
	g_authorization = json_object_get_bool(hJSON, "authorization");

	#if defined DEBUG
	Leet_DebugLog("Server information pulled:\n-minimumBTCHold = %i\n-no_death_penalty = %s\n-allow_non_authorized_players = %s\n-admissionFee = %i\n-serverRakeBTCPercentage = %f\n-api_version = %s\n-incrementBTC = %i\n-leetcoinRakePercentage = %f\n-authorization = %s", g_minimumBTCHold, g_no_death_penalty ? "True" : "False", g_allow_non_authorized_players ? "True" : "False", g_admissionFee, g_serverRakeBTCPercentage, g_api_version, g_incrementBTC, g_leetcoinRakePercentage, g_authorization ? "True" : "False");
	#endif

	rake = g_serverRakeBTCPercentage + g_leetcoinRakePercentage;
	kill_reward = RoundToCeil(g_incrementBTC - (g_incrementBTC * rake));

	Leet_Log("Server information retrieval successful.");
	bServerSetup = true;
}

public void OnClientAuthorized(int client, const char[] sAuth)
{
	if (!cv_bStatus || !bServerSetup)
	{
		return;
	}

	iCommunityID[client] = "";
	iClientRank[client] = 0;
	iStatsKills[client] = 0;
	iStatsDeaths[client] = 0;

	char sCommunityID[128];
	GetClientAuthId(client, AuthId_SteamID64, sCommunityID, sizeof(sCommunityID));

	Leet_Log("Community ID one: %s", sCommunityID);
	strcopy(iCommunityID[client], 32, sCommunityID);

	char sURL[512];
	Format(sURL, sizeof(sURL), "%s%s", API_URL, API_URL_ACTIVATE_PLAYER);

	Leet_Log("On client authorized.");

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sURL);

	float fTime = float(GetTime());

	char sTime[128];
	FloatToString(fTime, sTime, sizeof(sTime));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "nonce", sTime);

	char sAuthID[128];
	GetClientAuthId(client, AuthId_SteamID64, sAuthID, sizeof(sAuthID));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "platformid", sAuthID);

	char params[2048];
	Format(params, sizeof(params), "nonce=%s&platformid=%s", sTime, sAuthID);

	char sHash[2048];
	digest_string_with_key(cv_sServerSecret, params, sHash, 2048);

	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-type", "application/x-www-form-urlencoded");
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Key", cv_sAPIKey);
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Sign", sHash);

	SteamWorks_SetHTTPRequestContextValue(hRequest, GetClientUserId(client));

	SteamWorks_SetHTTPCallbacks(hRequest, OnPullingClientInfo);
	SteamWorks_SendHTTPRequest(hRequest);
}

public int OnPullingClientInfo(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, any data1)
{
	int client = GetClientOfUserId(data1);

	if (client < 1)
	{
		Leet_Log("Error retrieving client information, client index is invalid.");
		return;
	}

	if (bFailure || !bRequestSuccessful)
	{
		Leet_Log("Error retrieving client information for %L. Error code: %i", client, view_as<int>(eStatusCode));
		#if defined DEBUG
		Leet_DebugLog("Pulling client information failure for %L:\n-bFailure = %s\n-bRequestSuccessful = %s\n-eStatusCode = %i", client, bFailure ? "True" : "False", bRequestSuccessful ? "True" : "False", view_as<int>(eStatusCode));
		#endif
		return;
	}

	int size = 0;
	SteamWorks_GetHTTPResponseBodySize(hRequest, size);

	char[] sBuffer = new char[size];
	SteamWorks_GetHTTPResponseBodyData(hRequest, sBuffer, size);

	Handle hJSON = json_load(sBuffer);
	g_player_btchold[client] = json_object_get_int(hJSON, "player_btchold");
	json_object_get_string(hJSON, "player_name", g_player_name[client], 512);
	json_object_get_string(hJSON, "player_platformid", g_player_platformid[client], 64);
	json_object_get_string(hJSON, "player_key", g_player_key[client], 128);
	g_player_previously_active[client] = json_object_get_bool(hJSON, "player_previously_active");
	g_player_rank[client] = json_object_get_int(hJSON, "player_rank");
	g_player_authorized[client] = json_object_get_bool(hJSON, "player_authorized");
	//json_object_get_string(hJSON, "g_default_currency_conversion", g_default_currency_conversion[client], 64);
	json_object_get_string(hJSON, "default_currency_display", g_default_currency_display[client], 32);
	g_authorization_client[client] = json_object_get_bool(hJSON, "authorization");

	if (g_authorization && !g_player_authorized[client])
	{
		KickClient(client, "You are not authorized to join this server.");
	}

	#if defined DEBUG
	Leet_DebugLog("Client information pulled:\n-player_btchold = %i\n-player_name = %s\n-player_platformid = %s\n-player_key = %s\n-player_previously_active = %s\n-player_rank = %i\n-player_authorized = %s\n-g_default_currency_conversion = %s\n-default_currency_display = %s\n-authorization = %s", g_player_btchold[client], g_player_name[client], g_player_platformid[client], g_player_key[client], g_player_previously_active[client] ? "True" : "False", g_player_rank[client], g_player_authorized[client] ? "True" : "False", g_default_currency_conversion[client], g_default_currency_display[client], g_authorization_client[client] ? "True" : "False");
	#endif

	Leet_Log("Client '%N' information retrieval successful.", client);
}

public void OnClientDisconnect(int client)
{

	Leet_Log("On client disconnect.");

	if (!cv_bStatus || !bServerSetup)
	{
		return;
	}

	iCommunityID[client] = "";
	iClientRank[client] = 0;
	iStatsKills[client] = 0;
	iStatsDeaths[client] = 0;

	char sURL[512];
	Format(sURL, sizeof(sURL), "%s%s", API_URL, API_URL_DEACTIVATE_PLAYER);

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sURL);

	float fTime = float(GetTime());

	char sTime[128];
	FloatToString(fTime, sTime, sizeof(sTime));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "nonce", sTime);

	char sAuthID[128];
	GetClientAuthId(client, AuthId_SteamID64, sAuthID, sizeof(sAuthID));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "platformid", sAuthID);

	char params[4096];
	Format(params, sizeof(params), "nonce=%s&platformid=%s", sTime, sAuthID);

	char sHash[2048];
	digest_string_with_key(cv_sServerSecret, params, sHash, 2048);

	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-type", "application/x-www-form-urlencoded");
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Key", cv_sAPIKey);
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Sign", sHash);

	SteamWorks_SetHTTPRequestContextValue(hRequest, GetClientUserId(client));

	SteamWorks_SetHTTPCallbacks(hRequest, OnDeactivatingPlayer);
	SteamWorks_SendHTTPRequest(hRequest);
}

public int OnDeactivatingPlayer(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, any data1)
{
	int client = GetClientOfUserId(data1);

	if (client < 1)
	{
		Leet_Log("Error deactivating client, client index is invalid.");
		return;
	}

	if (bFailure || !bRequestSuccessful)
	{
		Leet_Log("Error deactivating client for %L. Error code: %i", client, view_as<int>(eStatusCode));
		#if defined DEBUG
		Leet_DebugLog("Pulling client deactivation failure for %L:\n-bFailure = %s\n-bRequestSuccessful = %s\n-eStatusCode = %i", client, bFailure ? "True" : "False", bRequestSuccessful ? "True" : "False", view_as<int>(eStatusCode));
		#endif
		return;
	}

	bool g_player_previously_active2;
	bool g_player_authorized2;
	char g_player_key2[256];
	bool g_authorization2;

	int size = 0;
	SteamWorks_GetHTTPResponseBodySize(hRequest, size);

	char[] sBuffer = new char[size];
	SteamWorks_GetHTTPResponseBodyData(hRequest, sBuffer, size);

	Handle hJSON = json_load(sBuffer);
	g_player_previously_active2 = json_object_get_bool(hJSON, "player_previously_active");
	g_player_authorized2 = json_object_get_bool(hJSON, "player_authorized");
	json_object_get_string(hJSON, "player_key", g_player_key2, sizeof(g_player_key2));
	g_authorization2 = json_object_get_bool(hJSON, "authorization");

	#if defined DEBUG
	Leet_DebugLog("Client deactivation data:\n-player_previously_active = %s\n-player_authorized = %s\n-player_key = %s\n-authorization = %s", g_player_previously_active2 ? "True" : "False", g_player_authorized2 ? "True" : "False", g_player_key2, g_authorization2 ? "True" : "False");
	#endif

	Leet_Log("Client '%N' deactivation successful.", client);
}

public void OnPlayerDeath(Handle event, const char[] name, bool dontBroadcast)
{
	if (!cv_bStatus || !bServerSetup)
		return;

	int client = GetClientOfUserId(GetEventInt(event, "userid"));
	int attacker = GetClientOfUserId(GetEventInt(event, "attacker"));

	if (client != attacker)
	{
		if (g_player_authorized[client] && g_player_authorized[attacker])
		{
			g_player_btchold[attacker] += kill_reward;

			if (!g_no_death_penalty)
				g_player_btchold[client] -= (kill_reward + rake);

			CalculateEloRank(attacker, client, false);

			if (g_player_btchold[client] < g_minimumBTCHold)
				KickClient(client, "Your balance is too low to continue playing.  Go to leet.gg to add more btc to your server hold.");

			PrintToChatAll("%N earned: %i Satoshi for killing %N", attacker, kill_reward, client);
		}

		iStatsKills[attacker]++;
		iStatsDeaths[client]++;
	}
}

public Action OnClientSayCommand(int client, const char[] command, const char[] sArgs)
{
	if (strcmp(sArgs, "balance", false) == 0 || strcmp(sArgs, "!balance", false) == 0)
	{
		PrintToChat(client, "Your current balance is: %i satoshi.", g_player_btchold[client]);
 		return Plugin_Handled;
	}
	return Plugin_Continue;
}

public Action SubmitPlayerInformation(Handle timer, any data)
{
	if (!cv_bStatus || !bServerSetup)
	{
		return Plugin_Continue;
	}

	char sURL[512];
	Format(sURL, sizeof(sURL), "%s%s", API_URL, API_URL_PUT_MATCH_RESULTS);

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sURL);

	float fTime = float(GetTime());

	char sTime[128];
	FloatToString(fTime, sTime, sizeof(sTime));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "nonce", sTime);

	char sMapname[64];
	GetCurrentMap(sMapname, sizeof(sMapname));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "map_title", sMapname);

	Handle hPlayerArray = json_array();

	for (int i = 1; i <= MaxClients; i++)
	{
		if (IsClientInGame(i) && !IsFakeClient(i))
		{
			Handle hSingle = json_object(); char sBuffer[2048];

			//PlatformID
			Format(sBuffer, sizeof(sBuffer), "%s", iCommunityID[i]);
			json_object_set_new(hSingle, "platformID", json_string(sBuffer));

			//Kills
			Format(sBuffer, sizeof(sBuffer), "%i", iStatsKills[i]);
			json_object_set_new(hSingle, "kills", json_string(sBuffer));

			//Deaths
			Format(sBuffer, sizeof(sBuffer), "%i", iStatsDeaths[i]);
			json_object_set_new(hSingle, "deaths", json_string(sBuffer));

			//Name
			GetClientName(i, sBuffer, sizeof(sBuffer));
			json_object_set_new(hSingle, "name", json_string(sBuffer));

			//Rank
			Format(sBuffer, sizeof(sBuffer), "%i", iClientRank[i]);
			json_object_set_new(hSingle, "rank", json_string(sBuffer));

			//Weapon
			new String:sWeaponName[64];
			GetClientWeapon(i, sWeaponName, sizeof(sWeaponName));
			json_object_set_new(hSingle, "weapon", json_string(sWeaponName));

			json_array_append(hPlayerArray, hSingle);
		}
	}

	char sJSONList[10000];
	json_dump(hPlayerArray, sJSONList, sizeof(sJSONList));

	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "player_dict_list", sJSONList);

	char json_escaped[10000];
	int output_len = url_escape(json_escaped, sizeof(json_escaped), sJSONList);

	char params[10000];
	Format(params, sizeof(params), "nonce=%s&map_title=%s&player_dict_list=", sTime, sMapname);
	int len = output_len + strlen(params);

	append_string(params, strlen(params), json_escaped, output_len);

	char sHash[2048];
	digest_string_with_key_length(cv_sServerSecret, params, sHash, 2048, len);

	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-type", "application/x-www-form-urlencoded");
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Key", cv_sAPIKey);
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Sign", sHash);

	SteamWorks_SetHTTPCallbacks(hRequest, OnSubmittingMatchResults);
	SteamWorks_SendHTTPRequest(hRequest);

	return Plugin_Continue;
}

public int OnSubmittingMatchResults(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode)
{
	if (bFailure || !bRequestSuccessful)
	{
		Leet_Log("Error submitting match results. Error code: %i", view_as<int>(eStatusCode));
		#if defined DEBUG
		Leet_DebugLog("Error submitting match results:\n-bFailure = %s\n-bRequestSuccessful = %s\n-eStatusCode = %i", bFailure ? "True" : "False", bRequestSuccessful ? "True" : "False", view_as<int>(eStatusCode));
		#endif
		return;
	}

	int size = 0;
	SteamWorks_GetHTTPResponseBodySize(hRequest, size);

	char[] sBuffer = new char[size];
	SteamWorks_GetHTTPResponseBodyData(hRequest, sBuffer, size);

	Handle hJSON = json_load(sBuffer);

	for (int i = 0; i < json_array_size(hJSON); i++)
	{
		Handle hSteamID = json_array_get(hJSON, i);

		char sBuffer2[1024];
		json_string_value(hSteamID, sBuffer2, sizeof(sBuffer2));

		int retrieve = CheckAgainstCommunityID(sBuffer2);

		if (retrieve > 0)
			KickClient(retrieve, "Please go to Leet.gg and register for the server.");
	}
}

int CheckAgainstCommunityID(const char[] sCommunityID)
{
	for (int i = 1; i <= MaxClients; i++)
		if (IsClientInGame(i) && !IsFakeClient(i))
			if (strcmp(iCommunityID[i], sCommunityID))
				return i;
	return 0;
}

void IssuePlayerAward(int client, int amount, const char[] sReason)
{
	if (!cv_bStatus || !bServerSetup)
		return;

	char sURL[512];
	Format(sURL, sizeof(sURL), "%s%s", API_URL, API_URL_PUT_MATCH_RESULTS);

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sURL);

	float fTime = float(GetTime());

	char sTime[128];
	FloatToString(fTime, sTime, sizeof(sTime));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "nonce", sTime);

	Handle hArray = json_array(); char sBuffer[1024];

	Format(sBuffer, sizeof(sBuffer), "%i", g_player_platformid[client]);
	json_array_append_new(hArray, json_string(sBuffer));

	GetClientName(client, sBuffer, sizeof(sBuffer));
	json_array_append_new(hArray, json_string(sBuffer));

	Format(sBuffer, sizeof(sBuffer), "%i", amount);
	json_array_append_new(hArray, json_string(sBuffer));

	Format(sBuffer, sizeof(sBuffer), "%s", sReason);
	json_array_append_new(hArray, json_string(sBuffer));

	char sAward[2048];
	json_dump(hArray, sAward, sizeof(sAward));

	char params[2048];
	Format(params, sizeof(params), "award=%s", sAward);

	char sHash[2048];
	digest_string_with_key(cv_sServerSecret, params, sHash, 2048);

	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "award", sAward);

	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-type", "application/x-www-form-urlencoded");
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Key", cv_sAPIKey);
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Sign", sHash);

	Handle hPack = CreateDataPack();
	WritePackCell(hPack, GetClientUserId(client));
	WritePackCell(hPack, amount);
	WritePackString(hPack, sReason);

	SteamWorks_SetHTTPRequestContextValue(hRequest, GetClientUserId(client));

	SteamWorks_SetHTTPCallbacks(hRequest, OnIssueAward);
	SteamWorks_SendHTTPRequest(hRequest);
}

public int OnIssueAward(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, any data1)
{
	ResetPack(data1);

	int client = GetClientOfUserId(ReadPackCell(data1));
	int amount = ReadPackCell(data1);

	char sReason[1024];
	ReadPackString(data1, sReason, sizeof(sReason));

	CloseHandle(data1);

	if (client < 1)
	{
		Leet_Log("Error issuing a reward to a client, client index is invalid.");
		return;
	}

	if (bFailure || !bRequestSuccessful)
	{
		Leet_Log("Error issuing a reward to the client '%L'. Error code: %i", client, view_as<int>(eStatusCode));
		#if defined DEBUG
		Leet_DebugLog("Pulling client rewards issued failure for %L:\n-bFailure = %s\n-bRequestSuccessful = %s\n-eStatusCode = %i", client, bFailure ? "True" : "False", bRequestSuccessful ? "True" : "False", view_as<int>(eStatusCode));
		#endif
		return;
	}

	int size = 0;
	SteamWorks_GetHTTPResponseBodySize(hRequest, size);

	char[] sBuffer = new char[size];
	SteamWorks_GetHTTPResponseBodyData(hRequest, sBuffer, size);

	Handle hJSON = json_load(sBuffer);

	if (json_object_get_bool(hJSON, "award_authorized"))
	{
		g_player_btchold[client] += amount;
		PrintToChatAll("%N earned: %i Satoshi for: %s", client, amount, sReason);
	}
}

public void OnEntityCreated(int entity, const char[] sClassname)
{
	Leet_Log("entity %s\n", sClassname);
	if (StrEqual(sClassname, "chicken")) {
		SetEntPropFloat(entity, Prop_Data, "m_explodeDamage", float(4000));
		SetEntPropFloat(entity, Prop_Data, "m_explodeRadius", float(4000));
		HookSingleEntityOutput(entity, "OnBreak", OnChickenKill);
	}
}

public void OnChickenKill(const char[] output, int caller, int activator, float delay)
{
	Leet_Log("On Chicken Kill.");
	char name[64];
	GetClientName(activator, name, sizeof(name));
	PrintToChatAll("%s, otherise known as %s killed a chicken. Not vegan confirmed.", name, g_player_name[activator]);
	SetEntPropFloat(caller, Prop_Data, "m_explodeDamage", float(1000));
	SetEntPropFloat(caller, Prop_Data, "m_explodeRadius", float(400));
	IssuePlayerAward(activator, 100, "Chicken Kill");
}

public Action OnSpawnChicken(int client, int args)
{
	if (!cv_bStatus || !bServerSetup)
		return Plugin_Handled;

	float fPos[3];
	if (!CalculateLookPosition(client, fPos))
	{
		PrintToChat(client, "Error spawning a chicken, invalid look position.");
		return Plugin_Handled;
	}

	int entity = CreateEntityByName("chicken");

	if (IsValidEntity(entity))
	{
		DispatchSpawn(entity);
		TeleportEntity(entity, fPos, NULL_VECTOR, NULL_VECTOR);

		CreateChickenParticle(entity);
		PrintToChat(client, "You have spawned a chicken, congratulations...");
	}

	return Plugin_Handled;
}

bool CalculateLookPosition(int client, float fPos[3], int entity = -1)
{
	float fEyePosition[3];
	GetClientEyePosition(client, fEyePosition);

	float fEyeAngles[3];
	GetClientEyeAngles(client, fEyeAngles);

	Handle hTrace = TR_TraceRayFilterEx(fEyePosition, fEyeAngles, MASK_SOLID, RayType_Infinite, OnHitPlayers);
	TR_GetEndPosition(fPos, hTrace);

	int collision = TR_GetEntityIndex(hTrace);
	bool bHit = TR_DidHit(hTrace);

	if (entity != -1 && entity == collision)
	{
		return false;
	}

	CloseHandle(hTrace);

	return bHit;
}

public bool OnHitPlayers(int entity, int contentsmask, any data)
{
	return !((entity > 0) && (entity <= MaxClients));
}

void CreateChickenParticle(int chicken)
{
	int entity = CreateEntityByName("info_particle_system");

	if (IsValidEntity(entity))
	{
		float fPos[3];
		GetEntPropVector(chicken, Prop_Send, "m_vecOrigin", fPos);
		TeleportEntity(entity, fPos, NULL_VECTOR, NULL_VECTOR);

		DispatchKeyValue(entity, "effect_name", "chicken_gone_feathers");

		DispatchKeyValue(entity, "angles", "-90 0 0");
		DispatchSpawn(entity);
		ActivateEntity(entity);

		AcceptEntityInput(entity, "Start");

		CreateTimer(5.0, KillEntity, EntIndexToEntRef(entity));
	}
}

public Action KillEntity(Handle timer, any data)
{
	int entity = EntRefToEntIndex(data);

	if (IsValidEntity(entity))
		AcceptEntityInput(entity, "Kill");
}

void Leet_Log(const char[] format, any...)
{
	char buffer[256]; char path[PLATFORM_MAX_PATH];
	VFormat(buffer, sizeof(buffer), format, 2);
	BuildPath(Path_SM, path, sizeof(path), "logs/Leet_Logs.log");
	LogToFileEx(path, "%s", buffer);
}

#if defined DEBUG
void Leet_DebugLog(const char[] format, any...)
{
	char buffer[256]; char path[PLATFORM_MAX_PATH];
	VFormat(buffer, sizeof(buffer), format, 2);
	BuildPath(Path_SM, path, sizeof(path), "logs/Leet_Debug.log");
	LogToFileEx(path, "%s", buffer);
}
#endif

void CalculateEloRank(int winner, int loser, bool penalize_loser = true)
{
	int winner_rank = iClientRank[winner];
	int rank_diff = iClientRank[winner] - iClientRank[loser];

	float exp = (rank_diff * -1) / 400.0;

	float odds = 1.0 / (1.0 + Pow(10.0, exp));

	int k;
	if (iClientRank[winner] < 2100)
		k = 32;
	else if (iClientRank[winner] >= 2100 && iClientRank[winner] < 2400)
		k = 24;
	else
		k = 16;

	iClientRank[winner] = RoundFloat(iClientRank[winner] + (k * (1 - odds)));

	if (penalize_loser)
	{
		int new_rank_diff = iClientRank[winner] - winner_rank;
		iClientRank[loser] -= new_rank_diff;
	}

	if (iClientRank[loser] < 1)
		iClientRank[loser] = 1;
}

#pragma semicolon 1

#include <string>
#include <sourcemod>
#include <sdktools>
#include <SteamWorks>
#include <smjansson>
#include <Leet>

//Uncomment to enable debug mode.
#define DEBUG

#define PLUGIN_NAME		"Leet GG"
#define PLUGIN_AUTHOR	"Leetcoin Team"
#define PLUGIN_DES		"The official Leet.gg plugin for Sourcemod. This plugin takes advantage of our native API."
#define PLUGIN_VERSION	"1.0.0"
#define PLUGIN_URL		"https://www.leet.gg/"

//#define API_URL			"api-dot-1337coin.appspot.com"
#define API_URL			"http://apitest-dot-1337coin.appspot.com"
#define API_URL_BLEED	"apitest-dot-1337coin.appspot.com"
#define TEST_URL		"https://www.leet.gg/server/view/agpzfjEzMzdjb2luchMLEgZTZXJ2ZXIYgICA9MD-_gsM"

#define API_URL_GET_SERVER_INFO		"/api/get_server_info"
#define API_URL_ACTIVATE_PLAYER		"/api/activate_player"
#define API_URL_PUT_MATCH_RESULTS	"/api/put_match_results"
#define API_URL_ISSUE_AWARD			"/api/issue_award"
#define API_URL_DEACTIVATE_PLAYER	"/api/deactivate_player"

Handle hConVars[3];
bool cv_bStatus;
char cv_sAPIKey[256];
char cv_sServerSecret[256];

bool bServerSetup;

//On Server Start
int g_minimumBTCHold;
bool g_no_death_penalty;
bool g_allow_non_authorized_players;
int g_admissionFee;
float g_serverRakeBTCPercentage;
char g_api_version[256];
int g_incrementBTC;
float g_leetcoinRakePercentage;
bool g_authorization;

//On Client Connect
int g_player_btchold[MAXPLAYERS + 1];
char g_player_name[MAXPLAYERS + 1][512];
char g_player_platformid[MAXPLAYERS + 1][64];
char g_player_key[MAXPLAYERS + 1][128];
bool g_player_previously_active[MAXPLAYERS + 1];
int g_player_rank[MAXPLAYERS + 1];
bool g_player_authorized[MAXPLAYERS + 1];
char g_default_currency_conversion[MAXPLAYERS + 1][64];
char g_default_currency_display[MAXPLAYERS + 1][32];
bool g_authorization_client[MAXPLAYERS + 1];

int kill_reward;
float rake;
char iCommunityID[MAXPLAYERS + 1][32];
int iClientRank[MAXPLAYERS + 1];
int iStatsKills[MAXPLAYERS + 1];
int iStatsDeaths[MAXPLAYERS + 1];

public Plugin myinfo =
{
	name = PLUGIN_NAME,
	author = PLUGIN_AUTHOR,
	description = PLUGIN_DES,
	version = PLUGIN_VERSION,
	url = PLUGIN_URL
};

public void OnPluginStart()
{
	CreateConVar("leet_gg_version", PLUGIN_VERSION, PLUGIN_DES, FCVAR_REPLICATED | FCVAR_NOTIFY | FCVAR_SPONLY | FCVAR_DONTRECORD);

	hConVars[0] = CreateConVar("sm_leetgg_status", "1", "Status of the plugin.", FCVAR_NOTIFY, true, 0.0, true, 1.0);
	hConVars[1] = CreateConVar("sm_leetgg_api", "", "Server API key to use.", FCVAR_PROTECTED);
	hConVars[2] = CreateConVar("sm_leetgg_sever_secret", "", "Server Secret to use", FCVAR_PROTECTED);

	HookEvent("player_death", OnPlayerDeath);
	HookEvent("round_end", OnRoundEnd);

	RegAdminCmd("sm_chicken", OnSpawnChicken, ADMFLAG_ROOT, "Spawn a chicken where youre looking.");

	CreateTimer(30.0, SubmitPlayerInformation, _, TIMER_REPEAT);

	AutoExecConfig();
}

public Action OnRoundEnd(Event event, const char[] name, bool dontBroadcast) {
	for (int i = 1; i <= MaxClients; i++)
		if (IsClientInGame(i) && !IsFakeClient(i))
			PrintToChat(i, "Your current balance is: %i satoshi.", g_player_btchold[i]);
	return Plugin_Continue;
}

public void OnConfigsExecuted()
{
	cv_bStatus = GetConVarBool(hConVars[0]);
	GetConVarString(hConVars[1], cv_sAPIKey, 256);
	GetConVarString(hConVars[2], cv_sServerSecret, 256);

	if (!cv_bStatus)
	{
		bServerSetup = false;
		return;
	}

	if (strlen(cv_sAPIKey) == 0 || strlen(cv_sServerSecret) == 0)
	{
		Leet_Log("Error retrieving server information, API key or Server secret is missing.");
		Leet_Log("Api Key: %s", cv_sAPIKey);
		Leet_Log("Server Secret: %s", cv_sServerSecret);
		bServerSetup = false;
		return;
	}

	Leet_Log("Requesting server information...");

	char sURL[512];
	Format(sURL, sizeof(sURL), "%s%s", API_URL, API_URL_GET_SERVER_INFO);

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sURL);

	float fTime = float(GetTime());

	char sTime[128];
	FloatToString(fTime, sTime, sizeof(sTime));
	char params[4096];
	Format(params, sizeof(params), "nonce=%s", sTime);

	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "nonce", sTime);

	char sHash[2048];
	digest_string_with_key(cv_sServerSecret, params, sHash, 2048);

	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-type", "application/x-www-form-urlencoded");
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Key", cv_sAPIKey);
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Sign", sHash);
	SteamWorks_SetHTTPCallbacks(hRequest, OnPullingServerInfo);
	SteamWorks_SendHTTPRequest(hRequest);
}

public int OnPullingServerInfo(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode)
{
	if (bFailure || !bRequestSuccessful)
	{
		bServerSetup = false;
		Leet_Log("Error retrieving server information. Error code: %i", view_as<int>(eStatusCode));
		#if defined DEBUG
		Leet_DebugLog("Pulling server information failure:\n-bFailure = %s\n-bRequestSuccessful = %s\n-eStatusCode = %i", bFailure ? "True" : "False", bRequestSuccessful ? "True" : "False", view_as<int>(eStatusCode));
		Leet_Log("Request %s", hRequest);
		#endif
		return;
	}

	int size = 0;
	SteamWorks_GetHTTPResponseBodySize(hRequest, size);

	char[] sBuffer = new char[size];
	SteamWorks_GetHTTPResponseBodyData(hRequest, sBuffer, size);
	Leet_Log("Json string: %s ",sBuffer);

	Handle hJSON = json_load(sBuffer);
	g_minimumBTCHold = json_object_get_int(hJSON, "minimumBTCHold");
	g_no_death_penalty = json_object_get_bool(hJSON, "no_death_penalty");
	g_allow_non_authorized_players = json_object_get_bool(hJSON, "allow_non_authorized_players");
	g_admissionFee = json_object_get_int(hJSON, "admissionFee");
	g_serverRakeBTCPercentage = json_object_get_float(hJSON, "serverRakeBTCPercentage");
	json_object_get_string(hJSON, "api_version", g_api_version, sizeof(g_api_version));
	g_incrementBTC = json_object_get_int(hJSON, "incrementBTC");
	g_leetcoinRakePercentage = json_object_get_float(hJSON, "leetcoinRakePercentage");
	g_authorization = json_object_get_bool(hJSON, "authorization");

	#if defined DEBUG
	Leet_DebugLog("Server information pulled:\n-minimumBTCHold = %i\n-no_death_penalty = %s\n-allow_non_authorized_players = %s\n-admissionFee = %i\n-serverRakeBTCPercentage = %f\n-api_version = %s\n-incrementBTC = %i\n-leetcoinRakePercentage = %f\n-authorization = %s", g_minimumBTCHold, g_no_death_penalty ? "True" : "False", g_allow_non_authorized_players ? "True" : "False", g_admissionFee, g_serverRakeBTCPercentage, g_api_version, g_incrementBTC, g_leetcoinRakePercentage, g_authorization ? "True" : "False");
	#endif

	rake = g_serverRakeBTCPercentage + g_leetcoinRakePercentage;
	kill_reward = RoundToCeil(g_incrementBTC - (g_incrementBTC * rake));

	Leet_Log("Server information retrieval successful.");
	bServerSetup = true;
}

public void OnClientAuthorized(int client, const char[] sAuth)
{
	if (!cv_bStatus || !bServerSetup)
	{
		return;
	}

	iCommunityID[client] = "";
	iClientRank[client] = 0;
	iStatsKills[client] = 0;
	iStatsDeaths[client] = 0;

	char sCommunityID[128];
	GetClientAuthId(client, AuthId_SteamID64, sCommunityID, sizeof(sCommunityID));

	Leet_Log("Community ID one: %s", sCommunityID);
	strcopy(iCommunityID[client], 32, sCommunityID);

	char sURL[512];
	Format(sURL, sizeof(sURL), "%s%s", API_URL, API_URL_ACTIVATE_PLAYER);

	Leet_Log("On client authorized.");

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sURL);

	float fTime = float(GetTime());

	char sTime[128];
	FloatToString(fTime, sTime, sizeof(sTime));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "nonce", sTime);

	char sAuthID[128];
	GetClientAuthId(client, AuthId_SteamID64, sAuthID, sizeof(sAuthID));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "platformid", sAuthID);

	char params[2048];
	Format(params, sizeof(params), "nonce=%s&platformid=%s", sTime, sAuthID);

	char sHash[2048];
	digest_string_with_key(cv_sServerSecret, params, sHash, 2048);

	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-type", "application/x-www-form-urlencoded");
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Key", cv_sAPIKey);
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Sign", sHash);

	SteamWorks_SetHTTPRequestContextValue(hRequest, GetClientUserId(client));

	SteamWorks_SetHTTPCallbacks(hRequest, OnPullingClientInfo);
	SteamWorks_SendHTTPRequest(hRequest);
}

public int OnPullingClientInfo(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, any data1)
{
	int client = GetClientOfUserId(data1);

	if (client < 1)
	{
		Leet_Log("Error retrieving client information, client index is invalid.");
		return;
	}

	if (bFailure || !bRequestSuccessful)
	{
		Leet_Log("Error retrieving client information for %L. Error code: %i", client, view_as<int>(eStatusCode));
		#if defined DEBUG
		Leet_DebugLog("Pulling client information failure for %L:\n-bFailure = %s\n-bRequestSuccessful = %s\n-eStatusCode = %i", client, bFailure ? "True" : "False", bRequestSuccessful ? "True" : "False", view_as<int>(eStatusCode));
		#endif
		return;
	}

	int size = 0;
	SteamWorks_GetHTTPResponseBodySize(hRequest, size);

	char[] sBuffer = new char[size];
	SteamWorks_GetHTTPResponseBodyData(hRequest, sBuffer, size);

	Handle hJSON = json_load(sBuffer);
	g_player_btchold[client] = json_object_get_int(hJSON, "player_btchold");
	json_object_get_string(hJSON, "player_name", g_player_name[client], 512);
	json_object_get_string(hJSON, "player_platformid", g_player_platformid[client], 64);
	json_object_get_string(hJSON, "player_key", g_player_key[client], 128);
	g_player_previously_active[client] = json_object_get_bool(hJSON, "player_previously_active");
	g_player_rank[client] = json_object_get_int(hJSON, "player_rank");
	g_player_authorized[client] = json_object_get_bool(hJSON, "player_authorized");
	//json_object_get_string(hJSON, "g_default_currency_conversion", g_default_currency_conversion[client], 64);
	json_object_get_string(hJSON, "default_currency_display", g_default_currency_display[client], 32);
	g_authorization_client[client] = json_object_get_bool(hJSON, "authorization");

	if (g_authorization && !g_player_authorized[client])
	{
		KickClient(client, "You are not authorized to join this server.");
	}

	#if defined DEBUG
	Leet_DebugLog("Client information pulled:\n-player_btchold = %i\n-player_name = %s\n-player_platformid = %s\n-player_key = %s\n-player_previously_active = %s\n-player_rank = %i\n-player_authorized = %s\n-g_default_currency_conversion = %s\n-default_currency_display = %s\n-authorization = %s", g_player_btchold[client], g_player_name[client], g_player_platformid[client], g_player_key[client], g_player_previously_active[client] ? "True" : "False", g_player_rank[client], g_player_authorized[client] ? "True" : "False", g_default_currency_conversion[client], g_default_currency_display[client], g_authorization_client[client] ? "True" : "False");
	#endif

	Leet_Log("Client '%N' information retrieval successful.", client);
}

public void OnClientDisconnect(int client)
{

	Leet_Log("On client disconnect.");

	if (!cv_bStatus || !bServerSetup)
	{
		return;
	}

	iCommunityID[client] = "";
	iClientRank[client] = 0;
	iStatsKills[client] = 0;
	iStatsDeaths[client] = 0;

	char sURL[512];
	Format(sURL, sizeof(sURL), "%s%s", API_URL, API_URL_DEACTIVATE_PLAYER);

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sURL);

	float fTime = float(GetTime());

	char sTime[128];
	FloatToString(fTime, sTime, sizeof(sTime));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "nonce", sTime);

	char sAuthID[128];
	GetClientAuthId(client, AuthId_SteamID64, sAuthID, sizeof(sAuthID));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "platformid", sAuthID);

	char params[4096];
	Format(params, sizeof(params), "nonce=%s&platformid=%s", sTime, sAuthID);

	char sHash[2048];
	digest_string_with_key(cv_sServerSecret, params, sHash, 2048);

	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-type", "application/x-www-form-urlencoded");
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Key", cv_sAPIKey);
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Sign", sHash);

	SteamWorks_SetHTTPRequestContextValue(hRequest, GetClientUserId(client));

	SteamWorks_SetHTTPCallbacks(hRequest, OnDeactivatingPlayer);
	SteamWorks_SendHTTPRequest(hRequest);
}

public int OnDeactivatingPlayer(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, any data1)
{
	int client = GetClientOfUserId(data1);

	if (client < 1)
	{
		Leet_Log("Error deactivating client, client index is invalid.");
		return;
	}

	if (bFailure || !bRequestSuccessful)
	{
		Leet_Log("Error deactivating client for %L. Error code: %i", client, view_as<int>(eStatusCode));
		#if defined DEBUG
		Leet_DebugLog("Pulling client deactivation failure for %L:\n-bFailure = %s\n-bRequestSuccessful = %s\n-eStatusCode = %i", client, bFailure ? "True" : "False", bRequestSuccessful ? "True" : "False", view_as<int>(eStatusCode));
		#endif
		return;
	}

	bool g_player_previously_active2;
	bool g_player_authorized2;
	char g_player_key2[256];
	bool g_authorization2;

	int size = 0;
	SteamWorks_GetHTTPResponseBodySize(hRequest, size);

	char[] sBuffer = new char[size];
	SteamWorks_GetHTTPResponseBodyData(hRequest, sBuffer, size);

	Handle hJSON = json_load(sBuffer);
	g_player_previously_active2 = json_object_get_bool(hJSON, "player_previously_active");
	g_player_authorized2 = json_object_get_bool(hJSON, "player_authorized");
	json_object_get_string(hJSON, "player_key", g_player_key2, sizeof(g_player_key2));
	g_authorization2 = json_object_get_bool(hJSON, "authorization");

	#if defined DEBUG
	Leet_DebugLog("Client deactivation data:\n-player_previously_active = %s\n-player_authorized = %s\n-player_key = %s\n-authorization = %s", g_player_previously_active2 ? "True" : "False", g_player_authorized2 ? "True" : "False", g_player_key2, g_authorization2 ? "True" : "False");
	#endif

	Leet_Log("Client '%N' deactivation successful.", client);
}

public void OnPlayerDeath(Handle event, const char[] name, bool dontBroadcast)
{
	if (!cv_bStatus || !bServerSetup)
		return;

	int client = GetClientOfUserId(GetEventInt(event, "userid"));
	int attacker = GetClientOfUserId(GetEventInt(event, "attacker"));

	if (client != attacker)
	{
		if (g_player_authorized[client] && g_player_authorized[attacker])
		{
			g_player_btchold[attacker] += kill_reward;

			if (!g_no_death_penalty)
				g_player_btchold[client] -= (kill_reward + rake);

			CalculateEloRank(attacker, client, false);

			if (g_player_btchold[client] < g_minimumBTCHold)
				KickClient(client, "Your balance is too low to continue playing.  Go to leet.gg to add more btc to your server hold.");

			PrintToChatAll("%N earned: %i Satoshi for killing %N", attacker, kill_reward, client);
		}

		iStatsKills[attacker]++;
		iStatsDeaths[client]++;
	}
}

public Action OnClientSayCommand(int client, const char[] command, const char[] sArgs)
{
	if (strcmp(sArgs, "balance", false) == 0 || strcmp(sArgs, "!balance", false) == 0)
	{
		PrintToChat(client, "Your current balance is: %i satoshi.", g_player_btchold[client]);
 		return Plugin_Handled;
	}
	return Plugin_Continue;
}

public Action SubmitPlayerInformation(Handle timer, any data)
{
	if (!cv_bStatus || !bServerSetup)
	{
		return Plugin_Continue;
	}

	char sURL[512];
	Format(sURL, sizeof(sURL), "%s%s", API_URL, API_URL_PUT_MATCH_RESULTS);

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sURL);

	float fTime = float(GetTime());

	char sTime[128];
	FloatToString(fTime, sTime, sizeof(sTime));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "nonce", sTime);

	char sMapname[64];
	GetCurrentMap(sMapname, sizeof(sMapname));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "map_title", sMapname);

	Handle hPlayerArray = json_array();

	for (int i = 1; i <= MaxClients; i++)
	{
		if (IsClientInGame(i) && !IsFakeClient(i))
		{
			Handle hSingle = json_object(); char sBuffer[2048];

			//PlatformID
			Format(sBuffer, sizeof(sBuffer), "%s", iCommunityID[i]);
			json_object_set_new(hSingle, "platformID", json_string(sBuffer));

			//Kills
			Format(sBuffer, sizeof(sBuffer), "%i", iStatsKills[i]);
			json_object_set_new(hSingle, "kills", json_string(sBuffer));

			//Deaths
			Format(sBuffer, sizeof(sBuffer), "%i", iStatsDeaths[i]);
			json_object_set_new(hSingle, "deaths", json_string(sBuffer));

			//Name
			GetClientName(i, sBuffer, sizeof(sBuffer));
			json_object_set_new(hSingle, "name", json_string(sBuffer));

			//Rank
			Format(sBuffer, sizeof(sBuffer), "%i", iClientRank[i]);
			json_object_set_new(hSingle, "rank", json_string(sBuffer));

			//Weapon
			new String:sWeaponName[64];
			GetClientWeapon(i, sWeaponName, sizeof(sWeaponName));
			json_object_set_new(hSingle, "weapon", json_string(sWeaponName));

			json_array_append(hPlayerArray, hSingle);
		}
	}

	char sJSONList[10000];
	json_dump(hPlayerArray, sJSONList, sizeof(sJSONList));

	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "player_dict_list", sJSONList);

	char json_escaped[10000];
	int output_len = url_escape(json_escaped, sizeof(json_escaped), sJSONList);

	char params[10000];
	Format(params, sizeof(params), "nonce=%s&map_title=%s&player_dict_list=", sTime, sMapname);
	int len = output_len + strlen(params);

	append_string(params, strlen(params), json_escaped, output_len);

	char sHash[2048];
	digest_string_with_key_length(cv_sServerSecret, params, sHash, 2048, len);

	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-type", "application/x-www-form-urlencoded");
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Key", cv_sAPIKey);
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Sign", sHash);

	SteamWorks_SetHTTPCallbacks(hRequest, OnSubmittingMatchResults);
	SteamWorks_SendHTTPRequest(hRequest);

	return Plugin_Continue;
}

public int OnSubmittingMatchResults(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode)
{
	if (bFailure || !bRequestSuccessful)
	{
		Leet_Log("Error submitting match results. Error code: %i", view_as<int>(eStatusCode));
		#if defined DEBUG
		Leet_DebugLog("Error submitting match results:\n-bFailure = %s\n-bRequestSuccessful = %s\n-eStatusCode = %i", bFailure ? "True" : "False", bRequestSuccessful ? "True" : "False", view_as<int>(eStatusCode));
		#endif
		return;
	}

	int size = 0;
	SteamWorks_GetHTTPResponseBodySize(hRequest, size);

	char[] sBuffer = new char[size];
	SteamWorks_GetHTTPResponseBodyData(hRequest, sBuffer, size);

	Handle hJSON = json_load(sBuffer);

	for (int i = 0; i < json_array_size(hJSON); i++)
	{
		Handle hSteamID = json_array_get(hJSON, i);

		char sBuffer2[1024];
		json_string_value(hSteamID, sBuffer2, sizeof(sBuffer2));

		int retrieve = CheckAgainstCommunityID(sBuffer2);

		if (retrieve > 0)
			KickClient(retrieve, "Please go to Leet.gg and register for the server.");
	}
}

int CheckAgainstCommunityID(const char[] sCommunityID)
{
	for (int i = 1; i <= MaxClients; i++)
		if (IsClientInGame(i) && !IsFakeClient(i))
			if (strcmp(iCommunityID[i], sCommunityID))
				return i;
	return 0;
}

void IssuePlayerAward(int client, int amount, const char[] sReason)
{
	if (!cv_bStatus || !bServerSetup)
		return;

	char sURL[512];
	Format(sURL, sizeof(sURL), "%s%s", API_URL, API_URL_PUT_MATCH_RESULTS);

	Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, sURL);

	float fTime = float(GetTime());

	char sTime[128];
	FloatToString(fTime, sTime, sizeof(sTime));
	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "nonce", sTime);

	Handle hArray = json_array(); char sBuffer[1024];

	Format(sBuffer, sizeof(sBuffer), "%i", g_player_platformid[client]);
	json_array_append_new(hArray, json_string(sBuffer));

	GetClientName(client, sBuffer, sizeof(sBuffer));
	json_array_append_new(hArray, json_string(sBuffer));

	Format(sBuffer, sizeof(sBuffer), "%i", amount);
	json_array_append_new(hArray, json_string(sBuffer));

	Format(sBuffer, sizeof(sBuffer), "%s", sReason);
	json_array_append_new(hArray, json_string(sBuffer));

	char sAward[2048];
	json_dump(hArray, sAward, sizeof(sAward));

	char params[2048];
	Format(params, sizeof(params), "award=%s", sAward);

	char sHash[2048];
	digest_string_with_key(cv_sServerSecret, params, sHash, 2048);

	SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "award", sAward);

	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Content-type", "application/x-www-form-urlencoded");
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Key", cv_sAPIKey);
	SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Sign", sHash);

	Handle hPack = CreateDataPack();
	WritePackCell(hPack, GetClientUserId(client));
	WritePackCell(hPack, amount);
	WritePackString(hPack, sReason);

	SteamWorks_SetHTTPRequestContextValue(hRequest, GetClientUserId(client));

	SteamWorks_SetHTTPCallbacks(hRequest, OnIssueAward);
	SteamWorks_SendHTTPRequest(hRequest);
}

public int OnIssueAward(Handle hRequest, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, any data1)
{
	ResetPack(data1);

	int client = GetClientOfUserId(ReadPackCell(data1));
	int amount = ReadPackCell(data1);

	char sReason[1024];
	ReadPackString(data1, sReason, sizeof(sReason));

	CloseHandle(data1);

	if (client < 1)
	{
		Leet_Log("Error issuing a reward to a client, client index is invalid.");
		return;
	}

	if (bFailure || !bRequestSuccessful)
	{
		Leet_Log("Error issuing a reward to the client '%L'. Error code: %i", client, view_as<int>(eStatusCode));
		#if defined DEBUG
		Leet_DebugLog("Pulling client rewards issued failure for %L:\n-bFailure = %s\n-bRequestSuccessful = %s\n-eStatusCode = %i", client, bFailure ? "True" : "False", bRequestSuccessful ? "True" : "False", view_as<int>(eStatusCode));
		#endif
		return;
	}

	int size = 0;
	SteamWorks_GetHTTPResponseBodySize(hRequest, size);

	char[] sBuffer = new char[size];
	SteamWorks_GetHTTPResponseBodyData(hRequest, sBuffer, size);

	Handle hJSON = json_load(sBuffer);

	if (json_object_get_bool(hJSON, "award_authorized"))
	{
		g_player_btchold[client] += amount;
		PrintToChatAll("%N earned: %i Satoshi for: %s", client, amount, sReason);
	}
}

public void OnEntityCreated(int entity, const char[] sClassname)
{
	if (StrEqual(sClassname, "chicken"))
	{
		HookSingleEntityOutput(entity, "OnBreak", OnChickenKill);
	}
}

public void OnChickenKill(const char[] output, int caller, int activator, float delay)
{
	IssuePlayerAward(activator, 100, "Chicken Kill");
}

public Action OnSpawnChicken(int client, int args)
{
	if (!cv_bStatus || !bServerSetup)
		return Plugin_Handled;

	float fPos[3];
	if (!CalculateLookPosition(client, fPos))
	{
		PrintToChat(client, "Error spawning a chicken, invalid look position.");
		return Plugin_Handled;
	}

	int entity = CreateEntityByName("chicken");

	if (IsValidEntity(entity))
	{
		DispatchSpawn(entity);
		TeleportEntity(entity, fPos, NULL_VECTOR, NULL_VECTOR);

		CreateChickenParticle(entity);
		PrintToChat(client, "You have spawned a chicken, congratulations...");
	}

	return Plugin_Handled;
}

bool CalculateLookPosition(int client, float fPos[3], int entity = -1)
{
	float fEyePosition[3];
	GetClientEyePosition(client, fEyePosition);

	float fEyeAngles[3];
	GetClientEyeAngles(client, fEyeAngles);

	Handle hTrace = TR_TraceRayFilterEx(fEyePosition, fEyeAngles, MASK_SOLID, RayType_Infinite, OnHitPlayers);
	TR_GetEndPosition(fPos, hTrace);

	int collision = TR_GetEntityIndex(hTrace);
	bool bHit = TR_DidHit(hTrace);

	if (entity != -1 && entity == collision)
	{
		return false;
	}

	CloseHandle(hTrace);

	return bHit;
}

public bool OnHitPlayers(int entity, int contentsmask, any data)
{
	return !((entity > 0) && (entity <= MaxClients));
}

void CreateChickenParticle(int chicken)
{
	int entity = CreateEntityByName("info_particle_system");

	if (IsValidEntity(entity))
	{
		float fPos[3];
		GetEntPropVector(chicken, Prop_Send, "m_vecOrigin", fPos);
		TeleportEntity(entity, fPos, NULL_VECTOR, NULL_VECTOR);

		DispatchKeyValue(entity, "effect_name", "chicken_gone_feathers");

		DispatchKeyValue(entity, "angles", "-90 0 0");
		DispatchSpawn(entity);
		ActivateEntity(entity);

		AcceptEntityInput(entity, "Start");

		CreateTimer(5.0, KillEntity, EntIndexToEntRef(entity));
	}
}

public Action KillEntity(Handle timer, any data)
{
	int entity = EntRefToEntIndex(data);

	if (IsValidEntity(entity))
		AcceptEntityInput(entity, "Kill");
}

void Leet_Log(const char[] format, any...)
{
	char buffer[256]; char path[PLATFORM_MAX_PATH];
	VFormat(buffer, sizeof(buffer), format, 2);
	BuildPath(Path_SM, path, sizeof(path), "logs/Leet_Logs.log");
	LogToFileEx(path, "%s", buffer);
}

#if defined DEBUG
void Leet_DebugLog(const char[] format, any...)
{
	char buffer[256]; char path[PLATFORM_MAX_PATH];
	VFormat(buffer, sizeof(buffer), format, 2);
	BuildPath(Path_SM, path, sizeof(path), "logs/Leet_Debug.log");
	LogToFileEx(path, "%s", buffer);
}
#endif

void CalculateEloRank(int winner, int loser, bool penalize_loser = true)
{
	int winner_rank = iClientRank[winner];
	int rank_diff = iClientRank[winner] - iClientRank[loser];

	float exp = (rank_diff * -1) / 400.0;

	float odds = 1.0 / (1.0 + Pow(10.0, exp));

	int k;
	if (iClientRank[winner] < 2100)
		k = 32;
	else if (iClientRank[winner] >= 2100 && iClientRank[winner] < 2400)
		k = 24;
	else
		k = 16;

	iClientRank[winner] = RoundFloat(iClientRank[winner] + (k * (1 - odds)));

	if (penalize_loser)
	{
		int new_rank_diff = iClientRank[winner] - winner_rank;
		iClientRank[loser] -= new_rank_diff;
	}

	if (iClientRank[loser] < 1)
		iClientRank[loser] = 1;
}


checker plugin: added count of comms which are shown when a user joins

// *************************************************************************
//  This file is part of SourceBans++.
//
//  Copyright (C) 2014-2016 SourceBans++ Dev Team <https://github.com/sbpp>
//
//  SourceBans++ is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, per version 3 of the License.
//
//  SourceBans++ is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with SourceBans++. If not, see <http://www.gnu.org/licenses/>.
//
//  This file is based off work(s) covered by the following copyright(s):
//
//   SourceBans Checker 1.0.2
//   Copyright (C) 2010-2013 Nicholas Hastings
//   Licensed under GNU GPL version 3, or later.
//   Page: <https://forums.alliedmods.net/showthread.php?p=1288490>
//
// *************************************************************************

#include <sourcemod>

#define VERSION "1.6.3-PRE"
#define LISTBANS_USAGE "sm_listbans <#userid|name> - Lists a user's prior bans from Sourcebans"
#define LISTCOMMS_USAGE "sm_listcomms <#userid|name> - Lists a user's prior comms from Sourcebans"
#define INVALID_TARGET -1

new String:g_DatabasePrefix[10] = "sb";
new Handle:g_ConfigParser;
new Handle:g_DB;

public Plugin:myinfo =
{
	name = "SourceBans++: Bans Checker",
	author = "psychonic, Ca$h Munny, SourceBans++ Dev Team",
	description = "Notifies admins of prior bans from Sourcebans upon player connect.",
	version = VERSION,
	url = "https://sbpp.github.io"
};

public OnPluginStart()
{
	LoadTranslations("common.phrases");

	CreateConVar("sbchecker_version", VERSION, "", FCVAR_NOTIFY);

	RegAdminCmd("sm_listsbbans", OnListSourceBansCmd, ADMFLAG_BAN, LISTBANS_USAGE); // for backward compatibility with the old version
	RegAdminCmd("sm_listbans", OnListSourceBansCmd, ADMFLAG_BAN, LISTBANS_USAGE);
	RegAdminCmd("sm_listcomms", OnListSourceCommsCmd, ADMFLAG_BAN, LISTCOMMS_USAGE);
	RegAdminCmd("sb_reload", OnReloadCmd, ADMFLAG_RCON, "Reload sourcebans config and ban reason menu options");

	SQL_TConnect(OnDatabaseConnected, "sourcebans");
}

public OnMapStart()
{
	ReadConfig();
}

public Action:OnReloadCmd(client, args)
{
	ReadConfig();
	return Plugin_Handled;
}

public OnDatabaseConnected(Handle:owner, Handle:hndl, const String:error[], any:data)
{
	if (hndl == INVALID_HANDLE)
		SetFailState("Failed to connect to SourceBans DB, %s", error);

	g_DB = hndl;
}

public OnClientAuthorized(client, const String:auth[])
{
    if (g_DB == INVALID_HANDLE)
        return;
    /* Do not check bots nor check player with lan steamid. */
    if (auth[0] == 'B' || auth[9] == 'L')
        return;
    decl String:query[512], String:ip[30];
    GetClientIP(client, ip, sizeof(ip));
    FormatEx(query, sizeof(query), "SELECT COUNT(bid) FROM %s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) UNION SELECT COUNT(bid) FROM %s_comms WHERE authid REGEXP '^STEAM_[0-9]:%s$'", g_DatabasePrefix, auth[8], ip, g_DatabasePrefix,auth[8]);
    SQL_TQuery(g_DB, OnConnectBanCheck, query, GetClientUserId(client), DBPrio_Low);
}
public OnConnectBanCheck(Handle:owner, Handle:hndl, const String:error[], any:userid)
{
    new client = GetClientOfUserId(userid);
    if (!client || hndl == INVALID_HANDLE || !SQL_FetchRow(hndl))
        return;
    new bancount = SQL_FetchInt(hndl, 0);
    new commcount = 0;
    if(SQL_FetchRow(hndl)){
        commcount = SQL_FetchInt(hndl, 0);
    }
    if (bancount > 0 || commcount > 0)
    {
        if(bancount == 0){
            PrintToBanAdmins("\x04[SourceBans++]\x01 Warning: Player \"%N\" has %d previous commban%s.",
                client,commcount,((commcount > 1 || commcount == 0) ? "s":""));
        }
        else if(commcount == 0){
            PrintToBanAdmins("\x04[SourceBans++]\x01 Warning: Player \"%N\" has %d previous ban%s.",
                client, bancount, ((bancount > 1 || bancount == 0) ? "s":""));
        }
        else{
            PrintToBanAdmins("\x04[SourceBans++]\x01 Warning: Player \"%N\" has %d previous ban%s and %d previous commban%s.",
                client, bancount, ((bancount > 1 || bancount == 0) ? "s":""),commcount,((commcount > 1 || commcount == 0) ? "s":""));
        }
    }
}

public Action:OnListSourceBansCmd(client, args)
{
	if (args < 1)
	{
		ReplyToCommand(client, LISTBANS_USAGE);
	}

	if (g_DB == INVALID_HANDLE)
	{
		ReplyToCommand(client, "Error: Database not ready.");
		return Plugin_Handled;
	}

	decl String:targetarg[64];
	GetCmdArg(1, targetarg, sizeof(targetarg));

	new target = FindTarget(client, targetarg, true, true);
	if (target == INVALID_TARGET)
	{
		ReplyToCommand(client, "Error: Could not find a target matching '%s'.", targetarg);
		return Plugin_Handled;
	}

	decl String:auth[32];
	if (!GetClientAuthId(target, AuthId_Steam2, auth, sizeof(auth))
		 || auth[0] == 'B' || auth[9] == 'L')
	{
		ReplyToCommand(client, "Error: Could not retrieve %N's steam id.", target);
		return Plugin_Handled;
	}

	decl String:query[1024], String:ip[30];
	GetClientIP(target, ip, sizeof(ip));
	FormatEx(query, sizeof(query), "SELECT created, %s_admins.user, ends, length, reason, RemoveType FROM %s_bans LEFT JOIN %s_admins ON %s_bans.aid = %s_admins.aid WHERE ((type = 0 AND %s_bans.authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, auth[8], ip);

	decl String:targetName[MAX_NAME_LENGTH];
	GetClientName(target, targetName, sizeof(targetName));

	new Handle:pack = CreateDataPack();
	WritePackCell(pack, (client == 0) ? 0 : GetClientUserId(client));
	WritePackString(pack, targetName);

	SQL_TQuery(g_DB, OnListBans, query, pack, DBPrio_Low);

	if (client == 0)
	{
		ReplyToCommand(client, "[SourceBans++] Note: if you are using this command through an rcon tool, you will not see results.");
	}
	else
	{
		ReplyToCommand(client, "\x04[SourceBans++]\x01 Look for %N's ban results in console.", target);
	}

	return Plugin_Handled;
}

public OnListBans(Handle:owner, Handle:hndl, const String:error[], any:pack)
{
	ResetPack(pack);
	new clientuid = ReadPackCell(pack);
	new client = GetClientOfUserId(clientuid);
	decl String:targetName[MAX_NAME_LENGTH];
	ReadPackString(pack, targetName, sizeof(targetName));
	CloseHandle(pack);

	if (clientuid > 0 && client == 0)
		return;

	if (hndl == INVALID_HANDLE)
	{
		PrintListResponse(clientuid, client, "[SourceBans++] DB error while retrieving bans for %s:\n%s", targetName, error);
		return;
	}

	if (SQL_GetRowCount(hndl) == 0)
	{
		PrintListResponse(clientuid, client, "[SourceBans++] No bans found for %s.", targetName);
		return;
	}

	PrintListResponse(clientuid, client, "[SourceBans++] Listing bans for %s", targetName);
	PrintListResponse(clientuid, client, "Ban Date    Banned By   Length      End Date    R  Reason");
	PrintListResponse(clientuid, client, "-------------------------------------------------------------------------------");
	while (SQL_FetchRow(hndl))
	{
		new String:createddate[11] = "<Unknown> ";
		new String:bannedby[11] = "<Unknown> ";
		new String:lenstring[11] = "N/A       ";
		new String:enddate[11] = "N/A       ";
		new String:reason[28];
		new String:RemoveType[2] = " ";

		if (!SQL_IsFieldNull(hndl, 0))
		{
			FormatTime(createddate, sizeof(createddate), "%Y-%m-%d", SQL_FetchInt(hndl, 0));
		}

		if (!SQL_IsFieldNull(hndl, 1))
		{
			new size_bannedby = sizeof(bannedby);
			SQL_FetchString(hndl, 1, bannedby, size_bannedby);
			new len = SQL_FetchSize(hndl, 1);
			if (len > size_bannedby - 1)
			{
				reason[size_bannedby - 4] = '.';
				reason[size_bannedby - 3] = '.';
				reason[size_bannedby - 2] = '.';
			}
			else
			{
				for (new i = len; i < size_bannedby - 1; i++)
				{
					bannedby[i] = ' ';
				}
			}
		}

		// NOT NULL
		new size_lenstring = sizeof(lenstring);
		new length = SQL_FetchInt(hndl, 3);
		if (length == 0)
		{
			strcopy(lenstring, size_lenstring, "Permanent ");
		}
		else
		{
			new len = IntToString(length, lenstring, size_lenstring);
			if (len < size_lenstring - 1)
			{
				// change the '\0' to a ' '. the original \0 at the end will still be there
				lenstring[len] = ' ';
			}
		}

		if (!SQL_IsFieldNull(hndl, 2))
		{
			FormatTime(enddate, sizeof(enddate), "%Y-%m-%d", SQL_FetchInt(hndl, 2));
		}

		// NOT NULL
		new reason_size = sizeof(reason);
		SQL_FetchString(hndl, 4, reason, reason_size);
		new len = SQL_FetchSize(hndl, 4);
		if (len > reason_size - 1)
		{
			reason[reason_size - 4] = '.';
			reason[reason_size - 3] = '.';
			reason[reason_size - 2] = '.';
		}
		else
		{
			for (new i = len; i < reason_size - 1; i++)
			{
				reason[i] = ' ';
			}
		}

		if (!SQL_IsFieldNull(hndl, 5))
		{
			SQL_FetchString(hndl, 5, RemoveType, sizeof(RemoveType));
		}

		PrintListResponse(clientuid, client, "%s  %s  %s  %s  %s  %s", createddate, bannedby, lenstring, enddate, RemoveType, reason);
	}
}

public Action:OnListSourceCommsCmd(client, args)
{
	if (args < 1)
	{
		ReplyToCommand(client, LISTCOMMS_USAGE);
	}

	if (g_DB == INVALID_HANDLE)
	{
		ReplyToCommand(client, "Error: Database not ready.");
		return Plugin_Handled;
	}

	decl String:targetarg[64];
	GetCmdArg(1, targetarg, sizeof(targetarg));

	new target = FindTarget(client, targetarg, true, true);
	if (target == INVALID_TARGET)
	{
		ReplyToCommand(client, "Error: Could not find a target matching '%s'.", targetarg);
		return Plugin_Handled;
	}

	decl String:auth[32];
	if (!GetClientAuthId(target, AuthId_Steam2, auth, sizeof(auth))
		 || auth[0] == 'B' || auth[9] == 'L')
	{
		ReplyToCommand(client, "Error: Could not retrieve %N's steam id.", target);
		return Plugin_Handled;
	}

	decl String:query[1024];
	FormatEx(query, sizeof(query), "SELECT created, %s_admins.user, ends, length, reason, RemoveType, type FROM %s_comms LEFT JOIN %s_admins ON %s_comms.aid = %s_admins.aid WHERE %s_comms.authid REGEXP '^STEAM_[0-9]:%s$' AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, auth[8]);

	decl String:targetName[MAX_NAME_LENGTH];
	GetClientName(target, targetName, sizeof(targetName));

	new Handle:pack = CreateDataPack();
	WritePackCell(pack, (client == 0) ? 0 : GetClientUserId(client));
	WritePackString(pack, targetName);

	SQL_TQuery(g_DB, OnListComms, query, pack, DBPrio_Low);

	if (client == 0)
	{
		ReplyToCommand(client, "[SourceBans++] Note: if you are using this command through an rcon tool, you will not see results.");
	}
	else
	{
		ReplyToCommand(client, "\x04[SourceBans++]\x01 Look for %N's comm results in console.", target);
	}

	return Plugin_Handled;
}

public OnListComms(Handle:owner, Handle:hndl, const String:error[], any:pack)
{
	ResetPack(pack);
	new clientuid = ReadPackCell(pack);
	new client = GetClientOfUserId(clientuid);
	decl String:targetName[MAX_NAME_LENGTH];
	ReadPackString(pack, targetName, sizeof(targetName));
	CloseHandle(pack);

	if (clientuid > 0 && client == 0)
		return;

	if (hndl == INVALID_HANDLE)
	{
		PrintListResponse(clientuid, client, "[SourceBans++] DB error while retrieving comms for %s:\n%s", targetName, error);
		return;
	}

	if (SQL_GetRowCount(hndl) == 0)
	{
		PrintListResponse(clientuid, client, "[SourceBans++] No comms found for %s.", targetName);
		return;
	}

	PrintListResponse(clientuid, client, "[SourceBans++] Listing comms for %s", targetName);
	PrintListResponse(clientuid, client, "Ban Date    Banned By   Length      End Date    T  R  Reason");
	PrintListResponse(clientuid, client, "-------------------------------------------------------------------------------");
	while (SQL_FetchRow(hndl))
	{
		new String:createddate[11] = "<Unknown> ";
		new String:bannedby[11] = "<Unknown> ";
		new String:lenstring[11] = "N/A       ";
		new String:enddate[11] = "N/A       ";
		new String:reason[23];
		new String:CommType[2] = " ";
		new String:RemoveType[2] = " ";

		if (!SQL_IsFieldNull(hndl, 0))
		{
			FormatTime(createddate, sizeof(createddate), "%Y-%m-%d", SQL_FetchInt(hndl, 0));
		}

		if (!SQL_IsFieldNull(hndl, 1))
		{
			new size_bannedby = sizeof(bannedby);
			SQL_FetchString(hndl, 1, bannedby, size_bannedby);
			new len = SQL_FetchSize(hndl, 1);
			if (len > size_bannedby - 1)
			{
				reason[size_bannedby - 4] = '.';
				reason[size_bannedby - 3] = '.';
				reason[size_bannedby - 2] = '.';
			}
			else
			{
				for (new i = len; i < size_bannedby - 1; i++)
				{
					bannedby[i] = ' ';
				}
			}
		}

		// NOT NULL
		new size_lenstring = sizeof(lenstring);
		new length = SQL_FetchInt(hndl, 3);
		if (length == 0)
		{
			strcopy(lenstring, size_lenstring, "Permanent ");
		}
		else
		{
			new len = IntToString(length, lenstring, size_lenstring);
			if (len < size_lenstring - 1)
			{
				// change the '\0' to a ' '. the original \0 at the end will still be there
				lenstring[len] = ' ';
			}
		}

		if (!SQL_IsFieldNull(hndl, 2))
		{
			FormatTime(enddate, sizeof(enddate), "%Y-%m-%d", SQL_FetchInt(hndl, 2));
		}

		// NOT NULL
		new reason_size = sizeof(reason);
		SQL_FetchString(hndl, 4, reason, reason_size);
		new len = SQL_FetchSize(hndl, 4);
		if (len > reason_size - 1)
		{
			reason[reason_size - 4] = '.';
			reason[reason_size - 3] = '.';
			reason[reason_size - 2] = '.';
		}
		else
		{
			for (new i = len; i < reason_size - 1; i++)
			{
				reason[i] = ' ';
			}
		}

		if (!SQL_IsFieldNull(hndl, 5))
		{
			SQL_FetchString(hndl, 5, RemoveType, sizeof(RemoveType));
		}
		// NOT NULL
		SQL_FetchString(hndl, 6, CommType, sizeof(RemoveType));
		if(StrEqual(CommType,"1"))
			strcopy(CommType, sizeof(CommType), "M");
		if(StrEqual(CommType,"2"))
			strcopy(CommType, sizeof(CommType), "G");

		PrintListResponse(clientuid, client, "%s  %s  %s  %s  %s  %s  %s", createddate, bannedby, lenstring, enddate, CommType, RemoveType, reason);

	}
}

PrintListResponse(userid, client, const String:format[], any:...)
{
	decl String:msg[192];
	VFormat(msg, sizeof(msg), format, 4);

	if (userid == 0)
	{
		PrintToServer("%s", msg);
	}
	else
	{
		PrintToConsole(client, "%s", msg);
	}
}

PrintToBanAdmins(const String:format[], any:...)
{
	decl String:msg[128];
	VFormat(msg, sizeof(msg), format, 2);

	for (new i = 1; i <= MaxClients; i++)
	{
		if (IsClientInGame(i) && !IsFakeClient(i)
			 && CheckCommandAccess(i, "sm_listsourcebans", ADMFLAG_BAN)
			)
		{
			PrintToChat(i, "%s", msg);
		}
	}
}

stock ReadConfig()
{
	InitializeConfigParser();

	if (g_ConfigParser == INVALID_HANDLE)
	{
		return;
	}

	decl String:ConfigFile[PLATFORM_MAX_PATH];
	BuildPath(Path_SM, ConfigFile, sizeof(ConfigFile), "configs/sourcebans/sourcebans.cfg");

	if (FileExists(ConfigFile))
	{
		InternalReadConfig(ConfigFile);
	}
	else
	{
		decl String:Error[PLATFORM_MAX_PATH + 64];
		FormatEx(Error, sizeof(Error), "FATAL *** ERROR *** can not find %s", ConfigFile);
		SetFailState(Error);
	}
}

static InitializeConfigParser()
{
	if (g_ConfigParser == INVALID_HANDLE)
	{
		g_ConfigParser = SMC_CreateParser();
		SMC_SetReaders(g_ConfigParser, ReadConfig_NewSection, ReadConfig_KeyValue, ReadConfig_EndSection);
	}
}

static InternalReadConfig(const String:path[])
{
	new SMCError:err = SMC_ParseFile(g_ConfigParser, path);

	if (err != SMCError_Okay)
	{
		decl String:buffer[64];
		PrintToServer("%s", SMC_GetErrorString(err, buffer, sizeof(buffer)) ? buffer : "Fatal parse error");
	}
}

public SMCResult:ReadConfig_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
{
	return SMCParse_Continue;
}

public SMCResult:ReadConfig_KeyValue(Handle:smc, const String:key[], const String:value[], bool:key_quotes, bool:value_quotes)
{
	if (strcmp("DatabasePrefix", key, false) == 0)
	{
		strcopy(g_DatabasePrefix, sizeof(g_DatabasePrefix), value);

		if (g_DatabasePrefix[0] == '\0')
		{
			g_DatabasePrefix = "sb";
		}
	}

	return SMCParse_Continue;
}

public SMCResult:ReadConfig_EndSection(Handle:smc)
{
	return SMCParse_Continue;
}

// *************************************************************************
//  This file is part of SourceBans++.
//
//  Copyright (C) 2014-2016 SourceBans++ Dev Team <https://github.com/sbpp>
//
//  SourceBans++ is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, per version 3 of the License.
//
//  SourceBans++ is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with SourceBans++. If not, see <http://www.gnu.org/licenses/>.
//
//  This file is based off work(s) covered by the following copyright(s):
//
//   SourceBans Checker 1.0.2
//   Copyright (C) 2010-2013 Nicholas Hastings
//   Licensed under GNU GPL version 3, or later.
//   Page: <https://forums.alliedmods.net/showthread.php?p=1288490>
//
// *************************************************************************

#include <sourcemod>

#define VERSION "1.6.3-PRE"
#define LISTBANS_USAGE "sm_listbans <#userid|name> - Lists a user's prior bans from Sourcebans"
#define LISTCOMMS_USAGE "sm_listcomms <#userid|name> - Lists a user's prior comms from Sourcebans"
#define INVALID_TARGET -1

new String:g_DatabasePrefix[10] = "sb";
new Handle:g_ConfigParser;
new Handle:g_DB;

ConVar ShortMessage;

public Plugin:myinfo =
{
	name = "SourceBans++: Bans Checker",
	author = "psychonic, Ca$h Munny, SourceBans++ Dev Team",
	description = "Notifies admins of prior bans from Sourcebans upon player connect.",
	version = VERSION,
	url = "https://sbpp.github.io"
};

public OnPluginStart()
{
	LoadTranslations("common.phrases");

	CreateConVar("sbchecker_version", VERSION, "", FCVAR_NOTIFY);

	ShortMessage = CreateConVar("sb_short_message", "0", "Use shorter message for displying prev bans", _, true, 0.0, true, 1.0);

	RegAdminCmd("sm_listsbbans", OnListSourceBansCmd, ADMFLAG_BAN, LISTBANS_USAGE); // for backward compatibility with the old version
	RegAdminCmd("sm_listbans", OnListSourceBansCmd, ADMFLAG_BAN, LISTBANS_USAGE);
	RegAdminCmd("sm_listcomms", OnListSourceCommsCmd, ADMFLAG_BAN, LISTCOMMS_USAGE);
	RegAdminCmd("sb_reload", OnReloadCmd, ADMFLAG_RCON, "Reload sourcebans config and ban reason menu options");

	SQL_TConnect(OnDatabaseConnected, "sourcebans");
}

public OnMapStart()
{
	ReadConfig();
}

public Action:OnReloadCmd(client, args)
{
	ReadConfig();
	return Plugin_Handled;
}

public OnDatabaseConnected(Handle:owner, Handle:hndl, const String:error[], any:data)
{
	if (hndl == INVALID_HANDLE)
		SetFailState("Failed to connect to SourceBans DB, %s", error);

	g_DB = hndl;
}

public OnClientAuthorized(client, const String:auth[])
{
	if (g_DB == INVALID_HANDLE)
		return;

	/* Do not check bots nor check player with lan steamid. */
	if (auth[0] == 'B' || auth[9] == 'L')
		return;

	decl String:query[512], String:ip[30];
	GetClientIP(client, ip, sizeof(ip));
	FormatEx(query, sizeof(query), "SELECT COUNT(bid) FROM %s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, auth[8], ip);

	SQL_TQuery(g_DB, OnConnectBanCheck, query, GetClientUserId(client), DBPrio_Low);
}

public OnConnectBanCheck(Handle:owner, Handle:hndl, const String:error[], any:userid)
{
	new client = GetClientOfUserId(userid);

	if (!client || hndl == INVALID_HANDLE || !SQL_FetchRow(hndl))
		return;

	new bancount = SQL_FetchInt(hndl, 0);
	if (bancount > 0)
	{
		if (ShortMessage.BoolValue)
		{
			PrintToBanAdmins("\x04[SB++]\x01Player \"%N\" has %d previous ban%s.",
				client, bancount, ((bancount > 0) ? "s":""));
		}
		else
		{
			PrintToBanAdmins("\x04[SourceBans++]\x01 Warning: Player \"%N\" has %d previous ban%s on record.",
				client, bancount, ((bancount > 0) ? "s":""));
		}
	}
}

public Action:OnListSourceBansCmd(client, args)
{
	if (args < 1)
	{
		ReplyToCommand(client, LISTBANS_USAGE);
	}

	if (g_DB == INVALID_HANDLE)
	{
		ReplyToCommand(client, "Error: Database not ready.");
		return Plugin_Handled;
	}

	decl String:targetarg[64];
	GetCmdArg(1, targetarg, sizeof(targetarg));

	new target = FindTarget(client, targetarg, true, true);
	if (target == INVALID_TARGET)
	{
		ReplyToCommand(client, "Error: Could not find a target matching '%s'.", targetarg);
		return Plugin_Handled;
	}

	decl String:auth[32];
	if (!GetClientAuthId(target, AuthId_Steam2, auth, sizeof(auth))
		 || auth[0] == 'B' || auth[9] == 'L')
	{
		ReplyToCommand(client, "Error: Could not retrieve %N's steam id.", target);
		return Plugin_Handled;
	}

	decl String:query[1024], String:ip[30];
	GetClientIP(target, ip, sizeof(ip));
	FormatEx(query, sizeof(query), "SELECT created, %s_admins.user, ends, length, reason, RemoveType FROM %s_bans LEFT JOIN %s_admins ON %s_bans.aid = %s_admins.aid WHERE ((type = 0 AND %s_bans.authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, auth[8], ip);

	decl String:targetName[MAX_NAME_LENGTH];
	GetClientName(target, targetName, sizeof(targetName));

	new Handle:pack = CreateDataPack();
	WritePackCell(pack, (client == 0) ? 0 : GetClientUserId(client));
	WritePackString(pack, targetName);

	SQL_TQuery(g_DB, OnListBans, query, pack, DBPrio_Low);

	if (client == 0)
	{
		ReplyToCommand(client, "[SourceBans++] Note: if you are using this command through an rcon tool, you will not see results.");
	}
	else
	{
		ReplyToCommand(client, "\x04[SourceBans++]\x01 Look for %N's ban results in console.", target);
	}

	return Plugin_Handled;
}

public OnListBans(Handle:owner, Handle:hndl, const String:error[], any:pack)
{
	ResetPack(pack);
	new clientuid = ReadPackCell(pack);
	new client = GetClientOfUserId(clientuid);
	decl String:targetName[MAX_NAME_LENGTH];
	ReadPackString(pack, targetName, sizeof(targetName));
	CloseHandle(pack);

	if (clientuid > 0 && client == 0)
		return;

	if (hndl == INVALID_HANDLE)
	{
		PrintListResponse(clientuid, client, "[SourceBans++] DB error while retrieving bans for %s:\n%s", targetName, error);
		return;
	}

	if (SQL_GetRowCount(hndl) == 0)
	{
		PrintListResponse(clientuid, client, "[SourceBans++] No bans found for %s.", targetName);
		return;
	}

	PrintListResponse(clientuid, client, "[SourceBans++] Listing bans for %s", targetName);
	PrintListResponse(clientuid, client, "Ban Date    Banned By   Length      End Date    R  Reason");
	PrintListResponse(clientuid, client, "-------------------------------------------------------------------------------");
	while (SQL_FetchRow(hndl))
	{
		new String:createddate[11] = "<Unknown> ";
		new String:bannedby[11] = "<Unknown> ";
		new String:lenstring[11] = "N/A       ";
		new String:enddate[11] = "N/A       ";
		new String:reason[28];
		new String:RemoveType[2] = " ";

		if (!SQL_IsFieldNull(hndl, 0))
		{
			FormatTime(createddate, sizeof(createddate), "%Y-%m-%d", SQL_FetchInt(hndl, 0));
		}

		if (!SQL_IsFieldNull(hndl, 1))
		{
			new size_bannedby = sizeof(bannedby);
			SQL_FetchString(hndl, 1, bannedby, size_bannedby);
			new len = SQL_FetchSize(hndl, 1);
			if (len > size_bannedby - 1)
			{
				reason[size_bannedby - 4] = '.';
				reason[size_bannedby - 3] = '.';
				reason[size_bannedby - 2] = '.';
			}
			else
			{
				for (new i = len; i < size_bannedby - 1; i++)
				{
					bannedby[i] = ' ';
				}
			}
		}

		// NOT NULL
		new size_lenstring = sizeof(lenstring);
		new length = SQL_FetchInt(hndl, 3);
		if (length == 0)
		{
			strcopy(lenstring, size_lenstring, "Permanent ");
		}
		else
		{
			new len = IntToString(length, lenstring, size_lenstring);
			if (len < size_lenstring - 1)
			{
				// change the '\0' to a ' '. the original \0 at the end will still be there
				lenstring[len] = ' ';
			}
		}

		if (!SQL_IsFieldNull(hndl, 2))
		{
			FormatTime(enddate, sizeof(enddate), "%Y-%m-%d", SQL_FetchInt(hndl, 2));
		}

		// NOT NULL
		new reason_size = sizeof(reason);
		SQL_FetchString(hndl, 4, reason, reason_size);
		new len = SQL_FetchSize(hndl, 4);
		if (len > reason_size - 1)
		{
			reason[reason_size - 4] = '.';
			reason[reason_size - 3] = '.';
			reason[reason_size - 2] = '.';
		}
		else
		{
			for (new i = len; i < reason_size - 1; i++)
			{
				reason[i] = ' ';
			}
		}

		if (!SQL_IsFieldNull(hndl, 5))
		{
			SQL_FetchString(hndl, 5, RemoveType, sizeof(RemoveType));
		}

		PrintListResponse(clientuid, client, "%s  %s  %s  %s  %s  %s", createddate, bannedby, lenstring, enddate, RemoveType, reason);
	}
}

public Action:OnListSourceCommsCmd(client, args)
{
	if (args < 1)
	{
		ReplyToCommand(client, LISTCOMMS_USAGE);
	}

	if (g_DB == INVALID_HANDLE)
	{
		ReplyToCommand(client, "Error: Database not ready.");
		return Plugin_Handled;
	}

	decl String:targetarg[64];
	GetCmdArg(1, targetarg, sizeof(targetarg));

	new target = FindTarget(client, targetarg, true, true);
	if (target == INVALID_TARGET)
	{
		ReplyToCommand(client, "Error: Could not find a target matching '%s'.", targetarg);
		return Plugin_Handled;
	}

	decl String:auth[32];
	if (!GetClientAuthId(target, AuthId_Steam2, auth, sizeof(auth))
		 || auth[0] == 'B' || auth[9] == 'L')
	{
		ReplyToCommand(client, "Error: Could not retrieve %N's steam id.", target);
		return Plugin_Handled;
	}

	decl String:query[1024];
	FormatEx(query, sizeof(query), "SELECT created, %s_admins.user, ends, length, reason, RemoveType, type FROM %s_comms LEFT JOIN %s_admins ON %s_comms.aid = %s_admins.aid WHERE %s_comms.authid REGEXP '^STEAM_[0-9]:%s$' AND ((length > '0' AND ends > UNIX_TIMESTAMP()) OR RemoveType IS NOT NULL)", g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, g_DatabasePrefix, auth[8]);

	decl String:targetName[MAX_NAME_LENGTH];
	GetClientName(target, targetName, sizeof(targetName));

	new Handle:pack = CreateDataPack();
	WritePackCell(pack, (client == 0) ? 0 : GetClientUserId(client));
	WritePackString(pack, targetName);

	SQL_TQuery(g_DB, OnListComms, query, pack, DBPrio_Low);

	if (client == 0)
	{
		ReplyToCommand(client, "[SourceBans++] Note: if you are using this command through an rcon tool, you will not see results.");
	}
	else
	{
		ReplyToCommand(client, "\x04[SourceBans++]\x01 Look for %N's comm results in console.", target);
	}

	return Plugin_Handled;
}

public OnListComms(Handle:owner, Handle:hndl, const String:error[], any:pack)
{
	ResetPack(pack);
	new clientuid = ReadPackCell(pack);
	new client = GetClientOfUserId(clientuid);
	decl String:targetName[MAX_NAME_LENGTH];
	ReadPackString(pack, targetName, sizeof(targetName));
	CloseHandle(pack);

	if (clientuid > 0 && client == 0)
		return;

	if (hndl == INVALID_HANDLE)
	{
		PrintListResponse(clientuid, client, "[SourceBans++] DB error while retrieving comms for %s:\n%s", targetName, error);
		return;
	}

	if (SQL_GetRowCount(hndl) == 0)
	{
		PrintListResponse(clientuid, client, "[SourceBans++] No comms found for %s.", targetName);
		return;
	}

	PrintListResponse(clientuid, client, "[SourceBans++] Listing comms for %s", targetName);
	PrintListResponse(clientuid, client, "Ban Date    Banned By   Length      End Date    T  R  Reason");
	PrintListResponse(clientuid, client, "-------------------------------------------------------------------------------");
	while (SQL_FetchRow(hndl))
	{
		new String:createddate[11] = "<Unknown> ";
		new String:bannedby[11] = "<Unknown> ";
		new String:lenstring[11] = "N/A       ";
		new String:enddate[11] = "N/A       ";
		new String:reason[23];
		new String:CommType[2] = " ";
		new String:RemoveType[2] = " ";

		if (!SQL_IsFieldNull(hndl, 0))
		{
			FormatTime(createddate, sizeof(createddate), "%Y-%m-%d", SQL_FetchInt(hndl, 0));
		}

		if (!SQL_IsFieldNull(hndl, 1))
		{
			new size_bannedby = sizeof(bannedby);
			SQL_FetchString(hndl, 1, bannedby, size_bannedby);
			new len = SQL_FetchSize(hndl, 1);
			if (len > size_bannedby - 1)
			{
				reason[size_bannedby - 4] = '.';
				reason[size_bannedby - 3] = '.';
				reason[size_bannedby - 2] = '.';
			}
			else
			{
				for (new i = len; i < size_bannedby - 1; i++)
				{
					bannedby[i] = ' ';
				}
			}
		}

		// NOT NULL
		new size_lenstring = sizeof(lenstring);
		new length = SQL_FetchInt(hndl, 3);
		if (length == 0)
		{
			strcopy(lenstring, size_lenstring, "Permanent ");
		}
		else
		{
			new len = IntToString(length, lenstring, size_lenstring);
			if (len < size_lenstring - 1)
			{
				// change the '\0' to a ' '. the original \0 at the end will still be there
				lenstring[len] = ' ';
			}
		}

		if (!SQL_IsFieldNull(hndl, 2))
		{
			FormatTime(enddate, sizeof(enddate), "%Y-%m-%d", SQL_FetchInt(hndl, 2));
		}

		// NOT NULL
		new reason_size = sizeof(reason);
		SQL_FetchString(hndl, 4, reason, reason_size);
		new len = SQL_FetchSize(hndl, 4);
		if (len > reason_size - 1)
		{
			reason[reason_size - 4] = '.';
			reason[reason_size - 3] = '.';
			reason[reason_size - 2] = '.';
		}
		else
		{
			for (new i = len; i < reason_size - 1; i++)
			{
				reason[i] = ' ';
			}
		}

		if (!SQL_IsFieldNull(hndl, 5))
		{
			SQL_FetchString(hndl, 5, RemoveType, sizeof(RemoveType));
		}
		// NOT NULL
		SQL_FetchString(hndl, 6, CommType, sizeof(RemoveType));
		if(StrEqual(CommType,"1"))
			strcopy(CommType, sizeof(CommType), "M");
		if(StrEqual(CommType,"2"))
			strcopy(CommType, sizeof(CommType), "G");

		PrintListResponse(clientuid, client, "%s  %s  %s  %s  %s  %s  %s", createddate, bannedby, lenstring, enddate, CommType, RemoveType, reason);

	}
}

PrintListResponse(userid, client, const String:format[], any:...)
{
	decl String:msg[192];
	VFormat(msg, sizeof(msg), format, 4);

	if (userid == 0)
	{
		PrintToServer("%s", msg);
	}
	else
	{
		PrintToConsole(client, "%s", msg);
	}
}

PrintToBanAdmins(const String:format[], any:...)
{
	decl String:msg[128];
	VFormat(msg, sizeof(msg), format, 2);

	for (new i = 1; i <= MaxClients; i++)
	{
		if (IsClientInGame(i) && !IsFakeClient(i)
			 && CheckCommandAccess(i, "sm_listsourcebans", ADMFLAG_BAN)
			)
		{
			PrintToChat(i, "%s", msg);
		}
	}
}

stock ReadConfig()
{
	InitializeConfigParser();

	if (g_ConfigParser == INVALID_HANDLE)
	{
		return;
	}

	decl String:ConfigFile[PLATFORM_MAX_PATH];
	BuildPath(Path_SM, ConfigFile, sizeof(ConfigFile), "configs/sourcebans/sourcebans.cfg");

	if (FileExists(ConfigFile))
	{
		InternalReadConfig(ConfigFile);
	}
	else
	{
		decl String:Error[PLATFORM_MAX_PATH + 64];
		FormatEx(Error, sizeof(Error), "FATAL *** ERROR *** can not find %s", ConfigFile);
		SetFailState(Error);
	}
}

static InitializeConfigParser()
{
	if (g_ConfigParser == INVALID_HANDLE)
	{
		g_ConfigParser = SMC_CreateParser();
		SMC_SetReaders(g_ConfigParser, ReadConfig_NewSection, ReadConfig_KeyValue, ReadConfig_EndSection);
	}
}

static InternalReadConfig(const String:path[])
{
	new SMCError:err = SMC_ParseFile(g_ConfigParser, path);

	if (err != SMCError_Okay)
	{
		decl String:buffer[64];
		PrintToServer("%s", SMC_GetErrorString(err, buffer, sizeof(buffer)) ? buffer : "Fatal parse error");
	}
}

public SMCResult:ReadConfig_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
{
	return SMCParse_Continue;
}

public SMCResult:ReadConfig_KeyValue(Handle:smc, const String:key[], const String:value[], bool:key_quotes, bool:value_quotes)
{
	if (strcmp("DatabasePrefix", key, false) == 0)
	{
		strcopy(g_DatabasePrefix, sizeof(g_DatabasePrefix), value);

		if (g_DatabasePrefix[0] == '\0')
		{
			g_DatabasePrefix = "sb";
		}
	}

	return SMCParse_Continue;
}

public SMCResult:ReadConfig_EndSection(Handle:smc)
{
	return SMCParse_Continue;
}