#pragma semicolon 1
#define DEBUG
#define PLUGIN_AUTHOR "jenz"
#define PLUGIN_VERSION "2.00"

#include <sourcemod>
#include <sdktools>

#pragma newdecls required
char g_cPollText[256][512];
char g_cPollVotes[256][512];
char g_cPollIRunOutOfNames[256][512];
char g_cPollTextClient[256];
char g_cPollVotesCvar[256];
char g_cPollCommands[512];
int g_iPollVoteWithHigestResult;
int g_iPollVotesCvar;
int g_iPollIndex;
int g_iAdminSay[MAXPLAYERS];
int g_iAdminVoteOptions[MAXPLAYERS];
Handle g_hTimer;

public Plugin myinfo = 
{
	name = "Unloze ingame polls",
	author = PLUGIN_AUTHOR,
	description = "ingame polls",
	version = PLUGIN_VERSION,
	url = "www.unloze.com"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	//Reg Cmds
	RegConsoleCmd("sm_poll", Cmd_Polls, "Vote on ongoing polls");
	RegConsoleCmd("sm_polls", Cmd_Polls, "Vote on ongoing polls");
	RegConsoleCmd("say", Cmd_Say);
	
	//mysql
	SQL_StartConnection();
	
	//admin cmds
	RegAdminCmd("sm_pollmenu", Cmd_pollAdminMenu, ADMFLAG_GENERIC);
}

public void OnMapStart()
{
	MySQLSelectPollTitles();
	CreateTimer(1.0, evaluatedelayAll, INVALID_HANDLE);
	g_hTimer = CreateTimer(260.0, pollNotifier, INVALID_HANDLE, TIMER_REPEAT);
	//change to 260.0 again for main server
}

public void OnMapEnd()
{
	if (g_hTimer != INVALID_HANDLE)
	{
		KillTimer(g_hTimer);
		g_hTimer = INVALID_HANDLE;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void InitializeQueries(char[] PollText)
{
	int l_iPollVoteOptions;
	int l_iMax;
	int i_lCounter;
	//for loop here to count votes for each option of the specified poll
	//needs state 0 to only count active votes
	l_iPollVoteOptions = MySQLSelectPollVotes(PollText, 0);
	for (int j = 0; j <= l_iPollVoteOptions; j++)
	{
		l_iMax = MySQLQueryVotes(PollText, j);
		if (l_iMax > i_lCounter)
		{
			//index 4 is wrong should be 3
			g_iPollVoteWithHigestResult = j;
			i_lCounter = l_iMax;
		}
	}
}

public Action ExecuteVoteCommand(char[] command, bool mapstart)
{
	if (StrContains(command, "mp_", true) == -1 && StrContains(command, "sv_", true) == -1 && StrContains(command, "plugins", true) == -1)
	{
		ServerCommand(command);	
	}
	else if (mapstart)
	{
		ServerCommand(command);	
	}
	//PrintToChatAll("command: %s", command);
	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action evaluatedelay(Handle timer)
{
	char command[512];
	InitializeQueries(g_cPollTextClient);
	MySQLSelectHighestVoteForPoll(g_cPollText[g_iPollVotesCvar][256], g_iPollVoteWithHigestResult);
	Format(command, sizeof(command), g_cPollCommands);
	ExecuteVoteCommand(command, false);
}

//experimental to launch all on mapstart
public Action evaluatedelayAll(Handle timer)
{
	int l_iIndex;
	char command[512];
	//should correctly itterate through all
	//[numberOfStrings][lengthOfLongestString]
	while (g_cPollText[l_iIndex][256] != '\0')
	{
		InitializeQueries(g_cPollText[l_iIndex][256]);
		MySQLSelectHighestVoteForPoll(g_cPollText[l_iIndex][256], g_iPollVoteWithHigestResult);
		Format(command, sizeof(command), g_cPollCommands);
		ExecuteVoteCommand(command, true);
		l_iIndex++;	
	}
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action pollNotifier(Handle timer)
{
	int l_iIndex;
	//edited
	while (g_cPollText[l_iIndex][256] != '\0')
	{
		PrintToChatAll("\x06[UNLOZE]\x07 Current Polls: %s", g_cPollText[l_iIndex][256]);
		l_iIndex++;	
	}
	PrintToChatAll("\x06[UNLOZE]\x07 Type !poll to vote");
}

public Action Cmd_pollAdminMenu(int client, int args)
{
	EditPollAdminMenu(client);
}

public Action EditPollAdminMenu(int client)
{
	Menu menu1 = new Menu(Menu_PollAdminMenu);
	menu1.SetTitle("[UNLOZE] Admin menu to edit polls");
	menu1.AddItem("nuffin here", "Create new Poll");
	menu1.AddItem("nuffin here", "Edit poll");
	menu1.Display(client, MENU_TIME_FOREVER);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Cmd_Say(int client, int args)
{
	int l_iIndex;
	
	if (g_iAdminSay[client] == 1)
	{
		//edited to dimensional array
		while (g_cPollText[l_iIndex][256] != '\0')
		{
			l_iIndex++;	
		}
		GetCmdArgString(g_cPollText[l_iIndex][256], sizeof(g_cPollText));
		ReplaceString(g_cPollText[l_iIndex][256], sizeof(g_cPollText), "\"", "");
		PrintToChat(client, "Creating new PollTitle: %s", g_cPollText[l_iIndex][256]);
		//sends a new poll title to DB as a COLUMN
		sendMYSQLPollTextCOLUMN(g_cPollText[l_iIndex][256]);
	}
	else if (g_iAdminSay[client] == 2)
	{
		//edited to dimensional array
		char l_cArgStringUpdate[256];
		GetCmdArgString(l_cArgStringUpdate, sizeof(l_cArgStringUpdate));
		ReplaceString(l_cArgStringUpdate, sizeof(l_cArgStringUpdate), "\"", "");
		PrintToChat(client, "Updating PollTitle to: %s", l_cArgStringUpdate);
		//update the coloumn title if somebody wants to rename poll
		//g_iPollIndex stores what POll an admin choose to edit
		MySQLUpdateTextCOLUMN(g_cPollText[g_iPollIndex][256], l_cArgStringUpdate);
	}
	if (g_iAdminVoteOptions[client] == 1)
	{
		char l_cDummy[1];
		GetCmdArgString(g_cPollVotes[client][256], sizeof(g_cPollVotes));
		ReplaceString(g_cPollVotes[client][256], sizeof(g_cPollVotes), "\"", "");
		PrintToChat(client, "Made %s an Vote Option", g_cPollVotes[client][256]);
		PrintToChat(client, "Give the Vote Option a COMMAND BEFORE MAKING NEXT VOTE OPTION");
		
		//After each vote option its cvar has to be set before next vote option can be made 
		mysqlStoreVoteOption(0, l_cDummy[0], client);
	}
	else if (g_iAdminVoteOptions[client] == 2)
	{
		char l_cPollcommands[256][512];
		GetCmdArgString(l_cPollcommands[0][256], sizeof(l_cPollcommands));
		ReplaceString(l_cPollcommands[0][256], sizeof(l_cPollcommands), "\"", "");
		PrintToChat(client, "\x06Added %s as a cvar/command to vote option: %s", l_cPollcommands[0][256], g_cPollVotesCvar);
		PrintToChat(client, "You can now add another vote option to the poll");
		//After each vote option its cvar has to be set before next vote option can be made
		mysqlStoreVoteOption(1, l_cPollcommands[0][256], client);
	}
	//Updating all polltitles
	MySQLSelectPollTitles();
	g_iAdminSay[client] = 0;
	g_iAdminVoteOptions[client] = 0;
	return Plugin_Continue;
}

public Action Cmd_Polls(int client, int args)
{
	//select what poll to vote on
	pollMenu(client, 0);
	return Plugin_Handled;
}

public void pollMenu(int client, int state)
{
	char c_activePoll1[512][512];
	Menu menu1;
	if (state == 0)
	{
		menu1 = new Menu(Menu_onGoingPolls);
	}
	else
	{
		menu1 = new Menu(Menu_EditonGoingPolls);
		menu1.AddItem("Go Back", "Go Back");
	}
	int i;	
	menu1.SetTitle("Active Polls:");
	while (g_cPollText[i][256] != '\0')
	{
		Format(c_activePoll1[i][256], sizeof(c_activePoll1), g_cPollText[i][256]);
		menu1.AddItem("nuffin here", c_activePoll1[i][256]);
		i++;	
	}
	if (i < 1)
	{
		PrintToChat(client, "There are currently no Active polls to vote on");
		return;
	}
	//display votes count for each vote here
	menu1.Display(client, MENU_TIME_FOREVER);
}

public void pollSubSection(int client, int selection)
{
	char l_cActivePoll[512];
	char l_cPollIRunOutOfNames[256][512];
	int l_iPollVoteOptions;
	int l_iMax;
	//selection here is the poll index that is voted on
	g_iPollVotesCvar = selection;
	Format(l_cActivePoll, sizeof(l_cActivePoll), g_cPollText[selection][256]);
	Format(g_cPollTextClient, sizeof(g_cPollTextClient), l_cActivePoll);
	Menu menu = new Menu(Menu_IndividualPoll);
	//needs state 1 to select all vote options, aka not voted for yet
	l_iPollVoteOptions = MySQLSelectPollVotes(l_cActivePoll, 1);
	menu.SetTitle("Poll to vote on: %s", l_cActivePoll);
	menu.AddItem("Go Back", "Go Back");
	//for loop here to take care of all vote options for each pollText
	for (int j = 0; j < l_iPollVoteOptions; j++)
	{
		menu.AddItem(g_cPollIRunOutOfNames[j][256], g_cPollIRunOutOfNames[j][256]);
	}
	for (int i = 0; i < l_iPollVoteOptions; i++)
	{
		//should be correct with searching for polltext
		l_iMax = MySQLQueryVotes(g_cPollText[selection][256], i);	
		Format(l_cPollIRunOutOfNames[i][256], sizeof(l_cPollIRunOutOfNames), "%s = VOTES %i", g_cPollIRunOutOfNames[i][256], l_iMax);
		menu.AddItem("", l_cPollIRunOutOfNames[i][256], ITEMDRAW_DISABLED);
	}
	menu.Display(client, MENU_TIME_FOREVER);
}

public int Menu_EditonGoingPolls(Menu menu, MenuAction action, int client, int selection)
{
	if (action == MenuAction_Select)
	{
		char info[32];
		menu.GetItem(selection, info, sizeof(info));
		if (StrContains(info, "Go Back", true) > -1)
		{
			EditPollAdminMenu(client);
		}
		else
		{
			//storing selection
			char l_cAtivePoll[512];
			Format(l_cAtivePoll, sizeof(l_cAtivePoll), g_cPollText[selection][256]);
			//get selected poll to edit down here so the cmd_say knows which index to update
			g_cPollVotes[client][256] = selection;
			g_iPollIndex = selection;
			Menu menu1 = new Menu(Menu_EditSelectedPoll);
			menu1.SetTitle("Selected Poll to Edit: %s", l_cAtivePoll);
			menu1.AddItem("", "Rename Poll Title");
			menu1.AddItem("", "add vote options");
			menu1.AddItem("", "Add cvar or command to execute for each vote option");
			menu1.AddItem("", "Delete poll");
			menu1.Display(client, MENU_TIME_FOREVER);	
		}
	}
	else if (action == MenuAction_End)
	{
		delete menu;
	}
}

public int Menu_EditSelectedPoll(Menu menu, MenuAction action, int client, int selection)
{
	if (action == MenuAction_Select)
	{
		if (selection == 0)
		{
			g_iAdminSay[client] = 2;
			PrintToChat(client, "Enter the poll Title into the chat to update it!");
		}
		else if (selection == 1)
		{
			//display menu here to list the polls current vote options
			//and ability to add new one
			int l_iPollVoteOptions;
			char c_activePoll[512];
			Format(c_activePoll, sizeof(c_activePoll), g_cPollText[g_iPollIndex][256]);
			//needs state 1 to select all possible vote options
			l_iPollVoteOptions = MySQLSelectPollVotes(c_activePoll, 1);
			
			//gets c_activePoll correctly now ingame
			Menu menu1 = new Menu(Menu_pollSpecificVoteOptions);
			menu1.SetTitle("Selected Poll %s has Vote Options:", c_activePoll);
			
			//gets these correctly now
			for (int j = 0; j < l_iPollVoteOptions; j++)
			{
				menu1.AddItem(g_cPollIRunOutOfNames[j][256], g_cPollIRunOutOfNames[j][256], ITEMDRAW_DISABLED);
			}
			
			menu1.AddItem("AddVote Option", "AddVote Option");
			menu1.Display(client, MENU_TIME_FOREVER);
		}
		else if (selection == 2)
		{
			//menu displaying vote options here
			int l_iPollVoteOptions;
			char c_activePoll[512];
			Format(c_activePoll, sizeof(c_activePoll), g_cPollText[g_iPollIndex][256]);
			//needs state 1 to select all possible vote options
			l_iPollVoteOptions = MySQLSelectPollVotes(c_activePoll, 1);
			Menu menu1 = new Menu(Menu_PollAdminCvartoVote);
			menu1.SetTitle("[UNLOZE] Select what vote Option to Give a cvar/command");
			for (int j = 0; j < l_iPollVoteOptions; j++)
			{
				menu1.AddItem(g_cPollIRunOutOfNames[j][256], g_cPollIRunOutOfNames[j][256]);
			}
			if (l_iPollVoteOptions > 0)
			{
				menu1.Display(client, MENU_TIME_FOREVER);
			}
			else
			{
				PrintToChat(client, "There currently arenĀ“t any Vote options, add one to give it a cvar");
			}
		}
		else if (selection == 3)
		{
			//add option to delete poll here
			char c_activePoll[512];
			Format(c_activePoll, sizeof(c_activePoll), g_cPollText[g_iPollIndex][256]);
			MySQLDeletePoll(c_activePoll);
			//Updating all polltitles
			MySQLSelectPollTitles();
		}
	}
	else if (action == MenuAction_End)
	{
		delete menu;
	}
}

public int Menu_pollSpecificVoteOptions(Menu menu, MenuAction action, int client, int selection)
{
	if (action == MenuAction_Select)
	{
		char info[32];
		menu.GetItem(selection, info, sizeof(info));
		if (StrContains(info, "AddVote Option", true) > -1)
		{
			//Does insert part correctly with mysql query on mywebsql
			g_iAdminVoteOptions[client] = 1;
			PrintToChat(client, "Enter the vote option into the chat!");
		}
	}
	else if (action == MenuAction_End)
	{
		delete menu;
	}
}

public int Menu_PollAdminCvartoVote(Menu menu, MenuAction action, int client, int selection)
{
	if (action == MenuAction_Select)
	{
		//storing which vote option to edit
		char info[256];
		menu.GetItem(selection, info, sizeof(info));
		g_cPollVotesCvar = info;
		//PrintToChatAll("g_cPollVotesCvar: %s", g_cPollVotesCvar);
		g_iAdminVoteOptions[client] = 2;
		PrintToChat(client, "Enter the cvar/command related to the vote into the chat");
	}
	else if (action == MenuAction_End)
	{
		delete menu;
	}
}

public int Menu_onGoingPolls(Menu menu, MenuAction action, int client, int selection)
{
	if (action == MenuAction_Select)
	{
		//VoteOption from the Poll to vote on
		pollSubSection(client, selection);
	}
	else if (action == MenuAction_End)
	{
		delete menu;
	}
}

public int Menu_IndividualPoll(Menu menu, MenuAction action, int client, int selection)
{
	if (action == MenuAction_Select)
	{	
		char info[32];
		menu.GetItem(selection, info, sizeof(info));
		if (StrContains(info, "Go Back", true) > -1)
		{
			pollMenu(client, 0);
		}
		else
		{
			char VOTE[256];
			menu.GetItem(selection, VOTE, sizeof(VOTE));
			PrintToChat(client, "You voted %s", VOTE);
			MYSQLInsertVotes(client, g_cPollTextClient, VOTE);
			CreateTimer(1.0, evaluatedelay, INVALID_HANDLE);
		}
	}
	else if (action == MenuAction_End)
	{
		delete menu;
	}
}

public int Menu_PollAdminMenu(Menu menu, MenuAction action, int client, int selection)
{
	if (action == MenuAction_Select)
	{
		if (selection == 0)
		{
			g_iAdminSay[client] = 1;
			PrintToChat(client, "Enter the poll text into the chat!");
		}
		else if (selection == 1)
		{
			//select what poll to edit
			pollMenu(client, 1);
		}
	}
	else if (action == MenuAction_End)
	{
		delete menu;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect (int client)
{
	g_iAdminSay[client] = 0;
	g_iAdminVoteOptions[client] = 0;
}

public void OnClientPostAdminCheck(int client)
{
	g_iAdminSay[client] = 0;
	g_iAdminVoteOptions[client] = 0;
}

//----------------------------------------------------------------------------------------------------
// Purpose: MYSQL queries
//----------------------------------------------------------------------------------------------------
 
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_pollvotes` (`steam_id` VARCHAR(64) NOT NULL, PRIMARY KEY (`steam_id`))");
    SQL_TQuery(db, DummyCallbackSimple, sQuery);
    Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `unloze_pollVoteCvars` (`command` VARCHAR(256) NOT NULL, PRIMARY KEY (`command`))");
    SQL_TQuery(db, DummyCallbackSimple, sQuery);
    delete db;
}

public void MySQLUpdateTextCOLUMN(char[] CurrentPollText, char[] NewPollText)
{
	char error[255];
	char sQuery[512];
	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;
	}
	Format(sQuery, sizeof(sQuery), "ALTER TABLE `unloze_pollvotes` CHANGE `%s` `%s` VARCHAR(256) NOT NULL;", CurrentPollText, NewPollText);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	Format(sQuery, sizeof(sQuery), "ALTER TABLE `unloze_pollVoteCvars` CHANGE `%s` `%s` VARCHAR(256) NOT NULL;", CurrentPollText, NewPollText);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	delete db;
}

public void sendMYSQLPollTextCOLUMN(char[] pollText)
{
	char error[255];
	char sQuery[512];
	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;
	}
	Format(sQuery, sizeof(sQuery), "ALTER TABLE `unloze_pollvotes` ADD COLUMN `%s` VARCHAR(256) NOT NULL;", pollText);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	Format(sQuery, sizeof(sQuery), "ALTER TABLE `unloze_pollVoteCvars` ADD COLUMN `%s` VARCHAR(256) NOT NULL;", pollText);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	delete db;
}

public void MYSQLInsertVotes(int client, char[] pollText, char[] VOTE)
{
	char error[255];
	char sQuery[512];
	char sSID[64];
	Database db;
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	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;
	}
	GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
	Format(sQuery, sizeof(sQuery), "INSERT INTO `unloze_pollvotes` (`steam_id`,`%s`) VALUES ('%s', '%s') ON DUPLICATE KEY UPDATE `%s` = '%s'", pollText, sSID, VOTE, pollText, VOTE);
	//PrintToChatAll("MYSQLInsertVotes sQuery: %s", sQuery);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	delete db;
}

public void mysqlStoreVoteOption(int state, char[] Command, int client)
{
	char error[255];
	Database db;
	
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		delete db;
		return;
	}
	
	char sQuery[512];
	if (state == 1)
	{
		Format(sQuery, sizeof(sQuery), "UPDATE `unloze_pollVoteCvars` SET `command`= '%s' WHERE `%s`= '%s'", Command, g_cPollText[g_iPollIndex][256], g_cPollVotesCvar);
		//PrintToChatAll("VoteOption Squery: %s", sQuery);
	}
	else
	{
		Format(sQuery, sizeof(sQuery), "INSERT INTO `unloze_pollVoteCvars` (`%s`) VALUES ('%s') ON DUPLICATE KEY UPDATE `%s` = '%s'", g_cPollText[g_iPollIndex][256], g_cPollVotes[client][256], g_cPollText[g_iPollIndex][256], g_cPollVotes[client][256]);
		//PrintToChatAll("VoteOption Squery: %s", sQuery);
	}
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	delete db;
}

public void MySQLDeletePoll(char[] PollText)
{
	char error[255];
	Database db;
	
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		delete db;
		return;
	}
	
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "ALTER TABLE unloze_pollvotes DROP COLUMN `%s`", PollText);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	Format(sQuery, sizeof(sQuery), "ALTER TABLE unloze_pollVoteCvars DROP COLUMN `%s`", PollText);
	SQL_TQuery(db, DummyCallbackSimple, sQuery);
	delete db;
}

public int MySQLQueryVotes(char[] PollText, int indexJ)
{
	char sQuery[512];
	char error[255];
	int l_iCounter;
	Database db;
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		delete db;
		return 0;
	}
	DBResultSet rs;
	Format(sQuery, sizeof(sQuery), "SELECT COUNT(*) FROM unloze_pollvotes WHERE `%s` = '%s'", PollText, g_cPollIRunOutOfNames[indexJ][256]);

	if ((rs = SQL_Query(db, sQuery)) == null)
	{  
		delete db;
		delete rs;
		return 0;
	}
	//PrintToChatAll("success: %s", sQuery);
	if (rs.RowCount > 0 && rs.FetchRow())
	{
		char l_cPollVoteCounter[512];
		SQL_FetchString(rs, 0, l_cPollVoteCounter, sizeof(l_cPollVoteCounter));
		//PrintToChatAll("l_cPollVoteCounter: %s", l_cPollVoteCounter);
		//counting votes
		l_iCounter = StringToInt(l_cPollVoteCounter);
	}
	delete rs;
	delete db;
	return l_iCounter;
}

public void MySQLSelectPollTitles()
{
	char error[255];
	Database db;
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		delete db;
		return;
	}
	char sQuery[512];
	//Collects the column_names requires
	Format(sQuery, sizeof(sQuery), "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = 'unloze_pollvotes'");
	
	DBResultSet rs;
	
	if ((rs = SQL_Query(db, sQuery)) == null)
	{  
		delete db;
		delete rs;
		Format(g_cPollText[0][256], sizeof(g_cPollText), "No Active polls");
		return;
	}
	int index;
	if (rs.RowCount > 0)
	{
		//should skip steam_id
		rs.FetchRow();
		while (rs.FetchRow())
		{
			//should be 0
			SQL_FetchString(rs, 0, g_cPollText[index][256], sizeof(g_cPollText));
			index++;
		}
	}
	delete rs;
	delete db;
}

public int MySQLSelectPollVotes(char[] PollText, int state)
{
	char error[255];
	Database db;
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		delete db;
		return 0;
	}
	char sQuery[512];
	if (state == 1)
	{
		Format(sQuery, sizeof(sQuery), "SELECT `%s` FROM unloze_pollVoteCvars", PollText);
	}
	else
	{
		Format(sQuery, sizeof(sQuery), "SELECT `%s` FROM unloze_pollvotes", PollText);
	}
	//PrintToChatAll("Query i want: %s", sQuery);
	DBResultSet rs;
	
	if ((rs = SQL_Query(db, sQuery)) == null)
	{  
		delete db;
		delete rs;
		Format(g_cPollIRunOutOfNames[0][256], sizeof(g_cPollIRunOutOfNames), "No Active Votes available for the poll");
		return 0;
	}
	int index;
	char l_cLocalFix[256][512];
	//regardless of state 1/0 it should need this to allocate it slightly more effectively
	if (rs.RowCount > 0)
	{
		while (rs.FetchRow())
		{
			//preventing empty fields
			SQL_FetchString(rs, 0, l_cLocalFix[index][256], sizeof(l_cLocalFix));
			if (l_cLocalFix[index][256] != '\0')
			{
				Format(g_cPollIRunOutOfNames[index][256], sizeof(g_cPollIRunOutOfNames), l_cLocalFix[index][256]);
				//PrintToChatAll("SUCCESS g_cPollIRunOutOfNames[index][256]: %s", g_cPollIRunOutOfNames[index][256]);
				index++;
			}
		}
	}
	delete rs;
	delete db;
	return index;
}

public void MySQLSelectHighestVoteForPoll(char[] PollText, int index)
{
	char error[255];
	Database db;
	if (SQL_CheckConfig("unloze_tracerpref"))
	{
		db = SQL_Connect("unloze_tracerpref", true, error, sizeof(error));
	}
	if (db == null)
	{
		delete db;
		return;
	}
	char sQuery[512];
	Format(sQuery, sizeof(sQuery), "SELECT command FROM unloze_pollVoteCvars WHERE `%s` = '%s'", PollText, g_cPollIRunOutOfNames[index][256]);
	//PrintToChatAll("MySQLSelectHighestVoteForPoll: %s",sQuery);
	DBResultSet rs;
	if ((rs = SQL_Query(db, sQuery)) == null)
	{  
		delete db;
		delete rs;
		
		return;
	}
	if (rs.RowCount > 0 && rs.FetchRow())
	{
		SQL_FetchString(rs, 0, g_cPollCommands, sizeof(g_cPollCommands));
	}
	delete rs;
	delete db;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock bool IsValidClient(int client)
{
    if (client > 0 && client <= MaxClients && IsClientInGame(client) && !IsFakeClient(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);
    }
}