#pragma semicolon 1

#define DEBUG

#define PLUGIN_AUTHOR "jenz"
#define PLUGIN_VERSION "1.00"

#include <sourcemod>
#include <sdktools>

#pragma newdecls required

//material
int g_iBeam = -1;

//zonepoints
char c_storestats[MAXPLAYERS+1][1024];

//drawing
int b_beamcolor[MAXPLAYERS];
float aimpos[MAXPLAYERS][3];
float beamangle[MAXPLAYERS];
float g_fClientDurationPref[MAXPLAYERS];
int colorrainbow[MAXPLAYERS][4];
int clientcolors[MAXPLAYERS][4];
int g_iClients[MAXPLAYERS];
int client_count;
bool b_beamhiding[MAXPLAYERS];

public Plugin myinfo = 
{
	name = "unloze shop beams",
	author = PLUGIN_AUTHOR,
	description = "beams purchaseable in the shop",
	version = PLUGIN_VERSION,
	url = "www.unloze.com"
};

public void OnPluginStart()
{
	AddFileToDownloadsTable("materials/unloze_tracers/xbeam.vmt");
	AddFileToDownloadsTable("materials/unloze_tracers/xbeam.vtf");
	
	RegConsoleCmd("sm_beamers", cmd_beamPoint, "Enables beams through shop");
	RegConsoleCmd("sm_beams", cmd_beamPoint, "Enables beams through shop");
	RegConsoleCmd("sm_beam", cmd_beamPoint, "Enables beams through shop");
	RegConsoleCmd("sm_hidebeam", cmd_beamPointHide, "Hides beams for the client");
	RegConsoleCmd("sm_beamsize", cmd_beamPointangle, "Changes the BeamSize");
	RegConsoleCmd("sm_beamtime", cmd_beamPointDuration, "Changes the BeamSize");
	
	RegAdminCmd("sm_beammysql", cmd_beammysqltest, ADMFLAG_ROOT);
	SQL_StartConnection();
	
	//0.1 is not good enough
	//CreateTimer(0.1, beamTesendDelay, INVALID_HANDLE, TIMER_REPEAT);
}


public Action cmd_beammysqltest(int client, int args)
{	
	CheckbeamprefHideMYSQL(client);
}

public Action cmd_beamPointDuration(int client, int args)
{	
	char number[5];
	float check;
	GetCmdArg(args, number, sizeof(number));
	check = StringToFloat(number);
	if (check > 5.0 || check < 0.1)
	{
		PrintToChat(client, "Input Range 0.1 - 5.0");
		return Plugin_Handled;
	}
	g_fClientDurationPref[client] = check;
	PrintToChat(client, "beamDuration value: %f", g_fClientDurationPref[client]);
	MySQLStoreBeamDurationPref(client, check);
	return Plugin_Handled;
}

public Action cmd_beamPointangle(int client, int args)
{	
	char number[5];
	float check;
	GetCmdArg(args, number, sizeof(number));
	check = StringToFloat(number);
	if (check > 20.0 || check < 0.1)
	{
		PrintToChat(client, "Input Range 0.1 - 20.0");
		return Plugin_Handled;
	}
	beamangle[client] = check;
	PrintToChat(client, "beamangle value: %f", beamangle[client]);
	MySQLStoreBeamSizePref(client, check);
	return Plugin_Handled;
}

public Action cmd_beamPointHide( int client, int args )
{
	if (!b_beamhiding[client])
	{
		mysqlentryhidebeam(client, 1);
		PrintToChat(client, "[UNLOZE] Disabled beams!");
		b_beamhiding[client] = true;
	}
	else
	{
		mysqlentryhidebeam(client, 0);
		PrintToChat(client, "[UNLOZE] Enabled beams!");
		b_beamhiding[client] = false;
	}
	ClientCount();
	return Plugin_Handled;
}

public Action cmd_beamPoint( int client, int args )
{
	Menu menu = new Menu(MenuHandler1);
	menu.SetTitle("UNLOZE Beams are unlocked through !shop");
	menu.AddItem("Disable", "Disable");
	if (StrContains(c_storestats[client], "w") >= 0)
	{
		menu.AddItem("Rainbow Beam", "Rainbow Beam");
	}
	if (StrContains(c_storestats[client], "v") >= 0)
	{
		menu.AddItem("Blue Beam", "Blue Beam");
	}
	if (StrContains(c_storestats[client], "x") >= 0)
	{
		menu.AddItem("Green Beam", "Green Beam");
	}
	if (StrContains(c_storestats[client], "y") >= 0)
	{
		menu.AddItem("Red Beam", "Red Beam");
	}
	if (StrContains(c_storestats[client], "z") >= 0)
	{
		menu.AddItem("Gold Beam", "Gold Beam");
	}
	if (StrContains(c_storestats[client], "3") >= 0)
	{
		menu.AddItem("Black Beam", "Black Beam");
	}
	if (StrContains(c_storestats[client], "4") >= 0)
	{
		menu.AddItem("Cyan Beam", "Cyan Beam");
	}
	if (StrContains(c_storestats[client], "5") >= 0)
	{
		menu.AddItem("Turquoise Beam", "Turquoise Beam");
	}
	if (StrContains(c_storestats[client], "6") >= 0)
	{
		menu.AddItem("Yellow Beam", "Yellow Beam");
	}
	if (StrContains(c_storestats[client], "7") >= 0)
	{
		menu.AddItem("Pink Beam", "Pink Beam");
	}
	if (StrContains(c_storestats[client], "8") >= 0)
	{
		menu.AddItem("Purple Beam", "Purple Beam");
	}
	if (StrContains(c_storestats[client], "9") >= 0)
	{
		menu.AddItem("gray Beam", "gray Beam");
	}
	menu.ExitButton = true;
	menu.Display(client, 0);
	return Plugin_Handled;
}


public int MenuHandler1(Menu menu, MenuAction action, int param1, int choice)
{
	/* If the menu was cancelled, print a message to the server about it. */
	if (action == MenuAction_Cancel)
	{
		PrintToServer("Client %d's menu was cancelled.  Reason: %d", param1, choice);
	}
	/* If the menu has ended, destroy it */
	else if (action == MenuAction_End)
	{
		delete menu;
	}
	else if (action == MenuAction_Select)
	{
		char info[32];
		menu.GetItem(choice, info, sizeof(info));
		if (StrEqual(info, "Blue Beam", true))
		{
			PrintToChat(param1, "Selected Blue Beam!");
			b_beamcolor[param1] = 1;
		}
		else if (StrEqual(info, "Red Beam", true))
		{
			PrintToChat(param1, "Selected Red Beam!");
			b_beamcolor[param1] = 2;
		}
		else if (StrEqual(info, "Green Beam", true))
		{
			PrintToChat(param1, "Selected Green Beam!");
			b_beamcolor[param1] = 3;
		}
		else if (StrEqual(info, "Gold Beam", true))
		{
			PrintToChat(param1, "Selected Gold Beam!");
			b_beamcolor[param1] = 4;
		}
		else if (StrEqual(info, "Black Beam", true))
		{
			PrintToChat(param1, "Selected Black Beam!");
			b_beamcolor[param1] = 5;
		}
		else if (StrEqual(info, "Cyan Beam", true))
		{
			PrintToChat(param1, "Selected Cyan Beam!");
			b_beamcolor[param1] = 6;
		}
		else if (StrEqual(info, "Turquoise Beam", true))
		{
			PrintToChat(param1, "Selected Turquoise Beam!");
			b_beamcolor[param1] = 7;
		}
		else if (StrEqual(info, "Yellow Beam", true))
		{
			PrintToChat(param1, "Selected Yellow Beam!");
			b_beamcolor[param1] = 8;
		}
		else if (StrEqual(info, "Pink Beam", true))
		{
			PrintToChat(param1, "Selected Pink Beam!");
			b_beamcolor[param1] = 9;
		}
		else if (StrEqual(info, "Purple Beam", true))
		{
			PrintToChat(param1, "Selected Purple Beam!");
			b_beamcolor[param1] = 10;
		}
		else if (StrEqual(info, "gray Beam", true))
		{
			PrintToChat(param1, "Selected gray Beam!");
			b_beamcolor[param1] = 11;
		}
		else if (StrEqual(info, "Rainbow Beam", true))
		{
			PrintToChat(param1, "Selected Rainbow Beam!");
			b_beamcolor[param1] = 12;
		}
		else if (StrEqual(info, "Disable", true))
		{
			PrintToChat(param1, "Disabled Beam!");
			b_beamcolor[param1] = 0;
		}
		InsertbeamprefMYSQL(param1, b_beamcolor[param1]);
	}
}

public Action OnPlayerRunCmd(int client, int& buttons, int& impulse, float vel[3], float angles[3], int& weapon, int& subtype, int& cmdnum, int& tickcount, int& seed, int mouse[2])
{
	if (!IsValidClient(client) || !IsPlayerAlive(client) || client_count < 1)
	{
		return Plugin_Continue;
	}
	if (b_beamcolor[client] == 0)
	{
		return Plugin_Continue;
	}
	//blue
	if (b_beamcolor[client] == 1)
	{
		clientcolors[client][0] = 0;
		clientcolors[client][1] = 0;
		clientcolors[client][2] = 255;
	}
	//red
	else if (b_beamcolor[client] == 2)
	{
		clientcolors[client][0] = 255;
		clientcolors[client][1] = 0;
		clientcolors[client][2] = 0;
	}
	//green
	else if (b_beamcolor[client] == 3)
	{
		clientcolors[client][0] = 0;
		clientcolors[client][1] = 255;
		clientcolors[client][2] = 0;
	}
	//gold
	else if (b_beamcolor[client] == 4)
	{
		clientcolors[client][0] = 255;
		clientcolors[client][1] = 215;
		clientcolors[client][2] = 0;
	}
	//black
	else if (b_beamcolor[client] == 5)
	{
		clientcolors[client][0] = 0;
		clientcolors[client][1] = 0;
		clientcolors[client][2] = 0;
	}
	//cyan
	else if (b_beamcolor[client] == 6)
	{
		clientcolors[client][0] = 0;
		clientcolors[client][1] = 255;
		clientcolors[client][2] = 255;
	}
	//turqouise
	else if (b_beamcolor[client] == 7)
	{
		clientcolors[client][0] = 64;
		clientcolors[client][1] = 224;
		clientcolors[client][2] = 208;
	}
	//yellow
	else if (b_beamcolor[client] == 8)
	{
		clientcolors[client][0] = 255;
		clientcolors[client][1] = 255;
		clientcolors[client][2] = 0;
	}
	//pink
	else if (b_beamcolor[client] == 9)
	{
		clientcolors[client][0] = 255;
		clientcolors[client][1] = 105;
		clientcolors[client][2] = 180;
	}
	//purple
	else if (b_beamcolor[client] == 10)
	{
		clientcolors[client][0] = 128;
		clientcolors[client][1] = 0;
		clientcolors[client][2] = 128;
	}
	//gray
	else if (b_beamcolor[client] == 11)
	{
		clientcolors[client][0] = 128;
		clientcolors[client][1] = 128;
		clientcolors[client][2] = 128;
	}
	//rainbow
	else if (b_beamcolor[client] == 12)
	{
		int ran;
		ran = GetRandomInt(0, 2);
		colorrainbow[client][ran] -= 1;
		if (colorrainbow[client][0] < 1)
		{
			colorrainbow[client][0] = 255;
		}
		if (colorrainbow[client][1] < 1)
		{
			colorrainbow[client][1] = 255;
		}
		if (colorrainbow[client][2] < 1)
		{
			colorrainbow[client][2] = 255;
		}
	}
	float otherhalf;
	otherhalf -= beamangle[client];
	GetClientAbsOrigin(client, aimpos[client]);
	if (b_beamcolor[client] == 12)
	{
		Circle(aimpos[client], beamangle[client], g_iBeam, client, colorrainbow[client]);
		Circle(aimpos[client], otherhalf, g_iBeam, client, colorrainbow[client]);
	}
	else
	{
		Circle(aimpos[client], beamangle[client], g_iBeam, client, clientcolors[client]);
		Circle(aimpos[client], otherhalf, g_iBeam, client, clientcolors[client]);	
	}
	return Plugin_Continue;
}

//----------------------------------------------------------------------------------------------------
// Purpose: Mapstart
//----------------------------------------------------------------------------------------------------
public void OnMapStart()
{
	g_iBeam = PrecacheModel("materials/unloze_tracers/xbeam.vmt");
}

public void OnClientDisconnect_Post(int client)
{
	ClientCount();
}

//actually used on each mapchange according to mitchell
public void OnClientDisconnect (int client)
{
	Format(c_storestats[client], sizeof(c_storestats), "");
	beamangle[client] = 0.0;
	b_beamcolor[client] = 0;
	aimpos[client][0] = 0.0;
	aimpos[client][1] = 0.0;
	aimpos[client][2] = 0.0;
	colorrainbow[client][0] = 255;
	colorrainbow[client][1] = 255;
	colorrainbow[client][2] = 255;
	colorrainbow[client][3] = 255;
	
	clientcolors[client][0] = 255;
	clientcolors[client][1] = 255;
	clientcolors[client][2] = 255;
	clientcolors[client][3] = 255;
	b_beamhiding[client] = false;
	g_fClientDurationPref[client] = 1.0;
	//IsClientConnected(client) && IsClientInGame(client) would be true here
}

 
public void OnClientPostAdminCheck(int client)
{
	beamangle[client] = 5.0;
	b_beamhiding[client] = false;
	aimpos[client][0] = 0.0;
	aimpos[client][1] = 0.0;
	aimpos[client][2] = 0.0;
	b_beamcolor[client] = 0;
	colorrainbow[client][0] = 255;
	colorrainbow[client][1] = 255;
	colorrainbow[client][2] = 255;
	colorrainbow[client][3] = 255;
	clientcolors[client][0] = 255;
	clientcolors[client][1] = 255;
	clientcolors[client][2] = 255;
	clientcolors[client][3] = 255;
	g_fClientDurationPref[client] = 1.0;
	CheckbeamSizeMYSQL(client);
	CheckBeamDurationMYSQL(client);
	CheckFlagsMYSQL(client);
	CheckbeamprefMYSQL(client);
	CheckbeamprefHideMYSQL(client);
}


stock void ClientCount()
{
	client_count = 0;
	for (int i = 1; i <= MaxClients; i++)
	{
		if (IsValidClient(i) && !b_beamhiding[i])
		{
			g_iClients[client_count++] = i;
		}
	}
}

public void MySQLStoreBeamDurationPref(int client, float check)
{
	char error[255];
	Database db; 
	//the points not related to hlstats are stored together with tracer prefferences but have
	//their own table
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		PrintToChat(client, "{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
		delete db;
		return;
	}
	char sSID[64];
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "INSERT INTO `unloze_beampref2` (`steam_id`, `Duration`) VALUES ('%s','%f') ON DUPLICATE KEY UPDATE `Duration` = '%f'", sSID, check, check);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	delete db;
}

public void MySQLStoreBeamSizePref(int client, float check)
{
	char error[255];
	Database db; 
	//the points not related to hlstats are stored together with tracer prefferences but have
	//their own table
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		PrintToChat(client, "{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
		delete db;
		return;
	}
	char sSID[64];
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "INSERT INTO `unloze_beampref2` (`steam_id`, `size`) VALUES ('%s','%f') ON DUPLICATE KEY UPDATE `size` = '%f'", sSID, check, check);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	delete db;
}

public void CheckBeamDurationMYSQL(int client)
{
	char error[255];
	Database db; 
	//the points not related to hlstats are stored together with tracer prefferences but have
	//their own table
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		PrintToChat(client, "{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
		delete db;
		return;
	}
	char sSID[64];
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "SELECT Duration FROM `unloze_beampref2` WHERE `steam_id` = '%s'", sSID);
	
	DBResultSet rs;
	
	if ((rs = SQL_Query(db, sQuery)) == null)
	{	
		delete db;
		delete rs;
		return;
	}
	
	if (rs.RowCount > 0 && rs.FetchRow())
	{
		char l_cSize[4];
		SQL_FetchString(rs, 0, l_cSize, sizeof(l_cSize));
		g_fClientDurationPref[client] = StringToFloat(l_cSize);
		if (g_fClientDurationPref[client] == 0.0)
		{
			g_fClientDurationPref[client] = 1.0;
		}
	}
	delete rs;
	delete db;
}

public void CheckbeamSizeMYSQL(int client)
{
	char error[255];
	Database db; 
	//the points not related to hlstats are stored together with tracer prefferences but have
	//their own table
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		PrintToChat(client, "{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
		delete db;
		return;
	}
	char sSID[64];
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "SELECT size FROM `unloze_beampref2` WHERE `steam_id` = '%s'", sSID);
	
	DBResultSet rs;
	
	if ((rs = SQL_Query(db, sQuery)) == null)
	{	
		delete db;
		delete rs;
		return;
	}
	
	if (rs.RowCount > 0 && rs.FetchRow())
	{
		char l_cSize[4];
		SQL_FetchString(rs, 0, l_cSize, sizeof(l_cSize));
		beamangle[client] = StringToFloat(l_cSize);
	}
	delete rs;
	delete db;
}

public void CheckFlagsMYSQL(int client)
{
	char error[255];
	Database db; 
	//the points not related to hlstats are stored together with tracer prefferences but have
	//their own table
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		PrintToChat(client, "{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
		delete db;
		return;
	}
	char sSID[64];
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "SELECT storestats FROM `unloze_zonepoints` WHERE `steam_id` = '%s'", sSID);
	
	DBResultSet rs;
	
	if ((rs = SQL_Query(db, sQuery)) == null)
	{	
		delete db;
		delete rs;
		return;
	}
	
	if (rs.RowCount > 0 && rs.FetchRow())
	{
		SQL_FetchString(rs, 0, c_storestats[client], sizeof(c_storestats));
	}
	delete rs;
	delete db;
} 

public void CheckbeamprefHideMYSQL(int client)
{
	char error[255];
	Database db; 
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		PrintToChat(client, "{green}[Unloze] {white}Error! Could not connect to MYSQL-DB CheckbeamprefHideMYSQL!");
		delete db;
		return;
	}
	char sSID[64];
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "SELECT hide FROM `unloze_beamprefhide` WHERE `steam_id` = '%s'", sSID);
	
	DBResultSet rs1;
	
	if ((rs1 = SQL_Query(db, sQuery)) == null)
	{	
		//PrintToChatAll("Failed, null result");
		delete db;
		delete rs1;
		return;
	}
	
	if (rs1.RowCount > 0 && rs1.FetchRow())
	{
		//PrintToChatAll("Has Entry");
		int translate;
		translate = SQL_FetchInt(rs1, 0);
		if (translate == 1)
		{
			b_beamhiding[client] = true;
			delete db;
			delete rs1;
			return;
		}
	}
	ClientCount();
	delete rs1;
	delete db;
}

public void CheckbeamprefMYSQL(int client)
{
	char error[255];
	Database db; 
	//the points not related to hlstats are stored together with tracer prefferences but have
	//their own table
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		PrintToChat(client, "{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
		delete db;
		return;
	}
	char sSID[64];
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "SELECT beam FROM `unloze_beampref` WHERE `steam_id` = '%s'", sSID);
	DBResultSet rs;
	
	if ((rs = SQL_Query(db, sQuery)) == null)
	{	
		delete db;
		delete rs;
		return;
	}
	if (rs.RowCount > 0 && rs.FetchRow())
	{
		char translate[124];
		SQL_FetchString(rs, 0, translate[client], MAX_NAME_LENGTH);
		b_beamcolor[client] = StringToInt(translate[client]);
	}
	delete rs;
	delete db;
} 


public void InsertbeamprefMYSQL(int client, int colornumber)
{
	char error[255];
	Database db; 
	//the points not related to hlstats are stored together with tracer prefferences but have
	//their own table
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		PrintToChat(client, "{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
		delete db;
		return;
	}
	char sSID[64];
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "INSERT INTO `unloze_beampref` (`steam_id`, `beam`) VALUES ('%s','%i') ON DUPLICATE KEY UPDATE `beam` = '%i'", sSID, colornumber, colornumber);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	delete db;
}


public void mysqlentryhidebeam(int client, int hide)
{
	char error[255];
	Database db; 
	//the points not related to hlstats are stored together with tracer prefferences but have
	//their own table
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		PrintToChat(client, "{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
		delete db;
		return;
	}
	char sSID[64];
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "INSERT INTO `unloze_beamprefhide` (`steam_id`, `hide`) VALUES ('%s','%i') ON DUPLICATE KEY UPDATE `hide` = '%i'", sSID, hide, hide);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	delete db;
}


public void SQL_StartConnection()
{
    char error[255];
    Database db;
    if (SQL_CheckConfig("unloze_tracerpref"))
    {
        db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
    }
    if (db == null)
    {
        PrintToChatAll("{green}[Unloze] {white}Error! Could not connect to MYSQL-DB!");
        delete db;
        return;
    }
    //create tables
    char sQuery[255];
    Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `unloze_beampref` (`steam_id` VARCHAR(254) NOT NULL, `beam` VARCHAR(254) NOT NULL, PRIMARY KEY (`steam_id`))");
    SQL_TQuery(db, DummyCallbackSimple, sQuery);
    Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `unloze_beamprefhide` (`steam_id` VARCHAR(254) NOT NULL, `hide` VARCHAR(254) NOT NULL, PRIMARY KEY (`steam_id`))");
    SQL_TQuery(db, DummyCallbackSimple, sQuery);
    Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `unloze_beampref2` (`steam_id` VARCHAR(254) NOT NULL, `size` VARCHAR(254) NOT NULL, `Duration` VARCHAR(254) NOT NULL, PRIMARY KEY (`steam_id`))");
    SQL_TQuery(db, DummyCallbackSimple, sQuery);
   
    delete db;
}

//old mitch coderino https://forums.alliedmods.net/showthread.php?t=141518&page=2
public void Circle(float vecLocation[3], float radius, int material, int client, const int colors[4])
{
	float pos2[4][3];
	
	//Create the start position for the first part of the beam
	pos2[0][0] = vecLocation[0];
	pos2[1][0] = vecLocation[0] + radius;
	pos2[2][0] = vecLocation[0] + (radius*DegToRad(90.0));
	pos2[3][0] = vecLocation[0] + (radius*DegToRad(45.0));
	pos2[0][1] = vecLocation[1] + radius;
	pos2[1][1] = vecLocation[1];
	pos2[2][1] = vecLocation[1] + (radius*DegToRad(90.0));
	pos2[3][1] = vecLocation[1] - (radius*DegToRad(45.0));
	pos2[0][2] = vecLocation[2];
	pos2[1][2] = vecLocation[2];
	pos2[2][2] = vecLocation[2];
	pos2[3][2] = vecLocation[2];
	
	TE_SetupBeamPoints(pos2[0], vecLocation, material, 0, 0, 0, g_fClientDurationPref[client], 5.0, 5.0, 5, 0.0, colors, 3);
	TE_Send(g_iClients, client_count);
}  

//----------------------------------------------------------------------------------------------------
// Purpose: stocks
//----------------------------------------------------------------------------------------------------
stock bool IsValidClient(int client, bool alive = true, bool bots = false)
{
	if (client > 0 && client <= MaxClients && IsClientConnected(client) && IsClientInGame(client))
	{
		return true;
	}
	return false;
}

public void DummyCallbackSimple(Handle hOwner, Handle hChild, const char[] err, DataPack pack1)
{
    if (hOwner == null || hChild == null)
    {
        LogError("Query error. (%s)", err);
    }
}