#pragma semicolon 1

#include <sourcemod>
#include <sdktools>
#include <cstrike>
#include <sdkhooks>
#include <multicolors>

#define TAG "{green}[LvL+Perks]{default}"

ConVar cv_MinPlayers;

/*
public Plugin myinfo =
{
    name = 'LVL+PERKS',
    author = 'Tomgra',
    description = 'Plugin dodaje na serwer system leveli i perków. Wbijanie kolejnych leveli daje punkty umiejętności, za które można zakupić ulepszenia (perki).',
    version = '1.0',
    url = ''
};
*/

/*
bayonet
knife_survival_bowie
knife_butterfly
knife_css
knife
knife_falchion
knife_flip
knife_ghost
knife_gg
knife_gut
knife_tactical
knife_karambit
knife_m9_bayonet
knife_cord
knife_gypsy_jackknife
knife_outdoor
knife_push
knife_skeleton
knife_stiletto
knife_canis
knife_widowmaker
knife_t
knife_ursus
*/

Database DB;

int lvl_requirements[25]=
{
	0,	   // 0
	1,	   // 1
	750,	 // 2
	2500,	// 3
	7500,	// 4
	15000,   // 5
	30000,   // 6
	50000,   // 7
	75000,   // 8
	105000,  // 9
	140000,  // 10
	180000,  // 11
	225000,  // 12
	275000,  // 13
	330000,  // 14
	390000,  // 15
	455000,  // 16
	525000,  // 17
	600000,  // 18
	680000,  // 19
	765000,  // 20
	855000,  // 21
	950000,  // 22
	1050000, // 23
	1150000, // 24
};

int lvl_pd[MAXPLAYERS+1];
int lvl_poziom[MAXPLAYERS+1];
int lvl_points[MAXPLAYERS+1];
int lvl_gravity[MAXPLAYERS+1];
int lvl_speed[MAXPLAYERS+1];
int lvl_fallDamage[MAXPLAYERS+1];
int lvl_health[MAXPLAYERS+1];
int lvl_healthRegen[MAXPLAYERS+1];
int lvl_additationalDamage[MAXPLAYERS+1];

bool RoundMode;

public void OnPluginStart()
{
	RegConsoleCmd("say", Prefix);
	RegConsoleCmd("say_team", Prefix_Team);
	RegConsoleCmd("sm_lvl", sm_lvl);
	RegConsoleCmd("sm_poziom", sm_lvl);
	RegConsoleCmd("sm_level", sm_lvl);
	RegConsoleCmd("sm_lvls", sm_lvls);
	RegConsoleCmd("sm_poziomy", sm_lvls);
	RegConsoleCmd("sm_levels", sm_lvls);
	RegConsoleCmd("sm_toplvl", sm_toplvl);
	RegAdminCmd("sm_resetplayer", sm_ResetPlayer, ADMFLAG_ROOT);
	RegAdminCmd("sm_addpd", sm_AddPD, ADMFLAG_ROOT);
	RegAdminCmd("sm_addpoints", sm_AddPoints, ADMFLAG_ROOT);
	HookEvent("player_spawn", Event_PlayerSpawn);
	HookEvent("round_start", Event_RoundStart);
	HookEvent("player_death", Event_PlayerDeath);
	HookEvent("player_death", Health_Regen);
	HookEvent("round_end", Event_RoundEnd);
	HookEvent("round_mvp", Event_RoundMVP);
	HookEvent("bomb_planted", Event_BombPlanted);
	HookEvent("bomb_defused", Event_BombDefused);
	HookEvent("hostage_follows", Event_HostageFollows);
	HookEvent("hostage_stops_following", Event_HostageStopsFollowing);
	HookEvent("hostage_rescued", Event_HostageRescued);
	cv_MinPlayers=CreateConVar("lvl_min_players", "4", "Ilu graczy jest wymaganych na serwerze, by działało zdibywanie PD");
}

public void OnMapStart()
{
	DataBaseConnect();
}
public void OnMapEnd()
{
	for(int i=1; i<=MaxClients; i++)
	{
		if(!IsValidClient(i)) continue;

		DataBaseWrite(i);
	}
}

public void OnClientPutInServer(int client)
{
	SDKHook(client, SDKHook_OnTakeDamage, OnTakeDamage);
	if(!IsValidClient(client)||IsFakeClient(client)) return;

	lvl_pd[client]=0;
	lvl_poziom[client]=0;
	lvl_points[client]=0;
	lvl_gravity[client]=0;
	lvl_speed[client]=0;
	lvl_fallDamage[client]=0;
	lvl_health[client]=0;
	lvl_healthRegen[client]=0;
	lvl_additationalDamage[client]=0;
	DataBaseRead(client);
}


public Action Event_PlayerSpawn(Event event, char[] name, bool dontBroadcast)
{
	int client = GetClientOfUserId(event.GetInt("userid"));
	if (IsValidClient(client))
	{
		SetEntityGravity(client, ClientGravity(client));
		CreateTimer(1.0, Timer_CheckGravity, GetClientUserId(client), TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
		SetEntitySpeed(client, ClientSpeed(client));
		SetEntityHealth(client, 100+lvl_health[client]);
	}
}

public void OnClientConnected(int client)
{
	CreateTimer(1.0, Timer_HintText, client, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}
public void OnClientDisconnect(int client)
{
	if(!IsValidClient(client)) return;

	DataBaseWrite(client);
}

public Action Timer_HintText(Handle timer, int client)
{
	if(!IsValidClient(client)) return;

	int team=GetClientTeam(client);
	if(team==0) return;
	else if(!IsPlayerAlive(client)||team==1)
	{
		int target=GetEntPropEnt(client, Prop_Send, "m_hObserverTarget");
		if(target<=0||target>MaxClients) return;
		else HintText(client, target);
	}
	else if(IsPlayerAlive(client)&&team>=2)
		HintText(client, client);
}
public Action HintText(int client, int client2)
{
	PrintHintText(client, "<font color='#ff9c00'>» Gracz: <font color='#0000bd'>%N\n<font color='#ff9c00'>» Poziom: <font color='#bd0000'>%d\n<font color='#ff9c00'>» Doświadczenie: <font color='#bd0000'>%d</font>", client2, lvl_poziom[client2], lvl_pd[client2]);
}

public void LvlUp(int client)
{
	if(!IsValidClient(client)) return;
	char buffer[256];
	while(lvl_pd[client]>=lvl_requirements[lvl_poziom[client]+1]&&lvl_poziom[client]<25)
	{
		lvl_poziom[client]++;
		lvl_points[client]+=3;
		CPrintToChat(client, "%s {orange}Gratulacje! Awansowałeś do {darkred}%i{orange} poziomu!", TAG, lvl_poziom[client]);
		Format(buffer, sizeof(buffer), "%s Gracz {darkred}%N{default} osiągnął {darkred}%i{default} poziom!", TAG, client, lvl_poziom[client]);
		CPrintToChatAll(buffer);
	}
}

public Action sm_AddPD(int client, int args)
{
	char buffer[10];
	GetCmdArgString(buffer, sizeof(buffer));
	StripQuotes(buffer);
	int type=StringToInt(buffer);
	lvl_pd[client]+=type;
	CPrintToChat(client, "%s {orange}Pomyślnie dodano {darkred}%d {darkblue}PD", TAG, type);
	LvlUp(client);
}

public Action sm_AddPoints(int client, int args)
{
	char buffer[5];
	GetCmdArgString(buffer, sizeof(buffer));
	StripQuotes(buffer);
	int type=StringToInt(buffer);
	lvl_points[client]+=type;
	CPrintToChat(client, "%s {orange}Pomyślnie dodano {darkred}%d {darkblue}punktów doświadczenia", TAG, type);
	CPrintToChat(client, "%s {orange}Aktualnie posiadasz {darkred}%d {darkblue}punktów doświadczenia", TAG, type);
}


public void Event_RoundStart(Event event, const char[] sName, bool bDontBroadcast)
{
	if (PlayerCount()<cv_MinPlayers.IntValue)
	{
		CPrintToChatAll("%s {orange}Zdobywanie PD jest zablokowane. Na serwerze wymaganych jest min. {darkred}%d{orange} graczy", TAG, cv_MinPlayers.IntValue);
		RoundMode=false;
	}
	else
		RoundMode=true;
}

public Action Event_PlayerDeath(Event event, char[] name, bool dontBroadcast)
{
	if(IsWarmup()||!RoundMode) return Plugin_Continue;

	char weapon[32];
	GetEventString(event, "weapon", weapon, sizeof(weapon));

	int victim=GetClientOfUserId(event.GetInt("userid"));
	int attacker=GetClientOfUserId(event.GetInt("attacker"));
	int assister=GetClientOfUserId(event.GetInt("assister"));

	bool hs=event.GetBool("headshot");
	bool ns=event.GetBool("noscope");
	bool smoke=event.GetBool("thrusmoke");
	bool blind=event.GetBool("attackerblind");

	int wallbang=event.GetInt("penetrated");
	//int distance=event.GetInt("distance");

	if(attacker==victim||!IsValidClient(attacker)||!IsValidClient(victim)) return Plugin_Continue;
	if(StrEqual(weapon, "knife_ct")||StrEqual(weapon, "knife_t")||StrEqual(weapon, "knife_m9_bayonet")||StrEqual(weapon, "knife_karambit")||StrEqual(weapon, "knife_tactical")||StrEqual(weapon, "knife_gut")||StrEqual(weapon, "knife_gg")||StrEqual(weapon, "knife_ghost")||StrEqual(weapon, "knife_flip")||StrEqual(weapon, "knife_falchion")||StrEqual(weapon, "knife_css")||StrEqual(weapon, "knife_butterfly")||StrEqual(weapon, "knife_survival_bowie")||StrEqual(weapon, "bayonet")||StrEqual(weapon, "knife_cord")||StrEqual(weapon, "knife_gypsy_jackknife")||StrEqual(weapon, "knife_outdoor")||StrEqual(weapon, "knife_push")||StrEqual(weapon, "knife_skeleton")||StrEqual(weapon, "knife_stiletto")||StrEqual(weapon, "knife_canis")||StrEqual(weapon, "knife_widowmaker")||StrEqual(weapon, "knife_ursus"))
	{
		if(!IsVip(attacker))
			lvl_pd[attacker]+=50;
		else
			lvl_pd[attacker]+=80;

		CPrintToChat(attacker, "%s {green}+%d PD{default} za {darkblue}zabicie nożem {default}gracza {darkred}%N", TAG, !IsVip(attacker)?50:80, victim);
	}
	else
	{
		if(!IsVip(attacker))
			lvl_pd[attacker]+=20;
		else
			lvl_pd[attacker]+=32;
			
		CPrintToChat(attacker, "%s {green}+%d PD{default} za {darkblue}zabicie {default}gracza {darkred}%N", TAG, !IsVip(attacker)?20:32, victim);

		if(ns)
		{
			if(!IsVip(attacker))
				lvl_pd[attacker]+=10;
			else
				lvl_pd[attacker]+=16;
			
			CPrintToChat(attacker, "%s {green}+%d PD{default} za {darkblue}NoScope", TAG, !IsVip(attacker)?10:16);
		}
		if(hs)
		{
			if(!IsVip(attacker))
				lvl_pd[attacker]+=5;
			else
				lvl_pd[attacker]+=8;
			
			CPrintToChat(attacker, "%s {green}+%d PD{default} za {darkblue}HeadShot", TAG, !IsVip(attacker)?5:8);
		}
		if(wallbang)
		{
			if(!IsVip(attacker))
				lvl_pd[attacker]+=5;
			else
				lvl_pd[attacker]+=8;
			
			CPrintToChat(attacker, "%s {green}+%d PD{default} za {darkblue}WallBang", TAG, !IsVip(attacker)?5:8);
		}
		if(smoke)
		{
			if(!IsVip(attacker))
				lvl_pd[attacker]+=5;
			else
				lvl_pd[attacker]+=8;
			CPrintToChat(attacker, "%s {green}+%d PD{default} za {darkblue}zabicie przez Smoke'a", TAG, !IsVip(attacker)?5:8);
		}
		if(blind)
		{
			if(!IsVip(attacker))
				lvl_pd[attacker]+=5;
			else
				lvl_pd[attacker]+=8;
			CPrintToChat(attacker, "%s {green}+%d PD{default} za {darkblue}zabicie na Flash'u", TAG, !IsVip(attacker)?5:8);
		}
	}
	if(IsValidClient(assister))
	{
		if(!IsVip(assister))
			lvl_pd[assister]+=10;
		else
			lvl_pd[assister]+=16;
		CPrintToChat(assister, "%s {green}+%d PD{default} za {darkblue}asystowanie przy zabiciu gracza {darkred}%N", TAG, !IsVip(assister)?10:16, victim);
		LvlUp(assister);
		HintText(assister, assister);
	}
	LvlUp(attacker);
	HintText(attacker, attacker);

	return Plugin_Continue;
}
public Action Event_BombPlanted(Event event, char[] name, bool dontBroadcast)
{
	if(IsWarmup()||!RoundMode) return Plugin_Continue;
	
	int client=GetClientOfUserId(event.GetInt("userid"));

	if(!IsValidClient(client)) return Plugin_Continue;

	if(!IsVip(client))
		lvl_pd[client]+=5;
	else
		lvl_pd[client]+=8;
	CPrintToChat(client, "%s {green}+%d PD{default} za {darkblue}podłożenie bomby", TAG, !IsVip(client)?5:8);
	LvlUp(client);
	HintText(client, client);
	
	return Plugin_Continue;
}
public Action Event_HostageFollows(Event event, char[] name, bool dontBroadcast)
{
	if(IsWarmup()||!RoundMode) return Plugin_Continue;
	
	int client=GetClientOfUserId(event.GetInt("userid"));

	if(!IsValidClient(client)) return Plugin_Continue;

	if(!IsVip(client))
		lvl_pd[client]+=5;
	else
		lvl_pd[client]+=8;
	CPrintToChat(client, "%s {green}+%d PD{default} za {darkblue}podniesienie zakładnika", TAG, !IsVip(client)?5:8);
	LvlUp(client);
	HintText(client, client);
	
	return Plugin_Continue;
}
public Action Event_HostageStopsFollowing(Event event, char[] name, bool dontBroadcast)
{
	if(IsWarmup()||!RoundMode) return Plugin_Continue;
	
	int client=GetClientOfUserId(event.GetInt("userid"));

	if(!IsValidClient(client)) return Plugin_Continue;

	if(!IsVip(client))
		lvl_pd[client]+=5;
	else
		lvl_pd[client]+=8;
	CPrintToChat(client, "%s {green}+%d PD{default} za {darkblue}zabicie gracza niosącego zakładnika", TAG, !IsVip(client)?5:8);
	LvlUp(client);
	HintText(client, client);
	
	return Plugin_Continue;
}
public Action Event_BombDefused(Event event, char[] name, bool dontBroadcast)
{
	if(IsWarmup()||!RoundMode) return Plugin_Continue;
	
	int client=GetClientOfUserId(event.GetInt("userid"));

	if(!IsValidClient(client)) return Plugin_Continue;

	if(!IsVip(client))
		lvl_pd[client]+=5;
	else
		lvl_pd[client]+=8;
	CPrintToChat(client, "%s {green}+%d PD{default} za {darkblue}rozbrojenie bomby", TAG, !IsVip(client)?5:8);
	LvlUp(client);
	HintText(client, client);
	
	return Plugin_Continue;
}
public Action Event_HostageRescued(Event event, char[] name, bool dontBroadcast)
{
	if(IsWarmup()||!RoundMode) return Plugin_Continue;
	
	int client=GetClientOfUserId(event.GetInt("userid"));

	if(!IsValidClient(client)) return Plugin_Continue;

	if(!IsVip(client))
		lvl_pd[client]+=5;
	else
		lvl_pd[client]+=8;
	CPrintToChat(client, "%s {green}+%d PD{default} za {darkblue}uratowanie zakładnika", TAG, !IsVip(client)?5:8);
	LvlUp(client);
	HintText(client, client);

	return Plugin_Continue;
}
public Action Event_RoundMVP(Event event, char[] name, bool dontBroadcast)
{
	if(IsWarmup()||!RoundMode) return Plugin_Continue;
	
	int client=GetClientOfUserId(event.GetInt("userid"));

	if(!IsValidClient(client)) return Plugin_Continue;

	if(!IsVip(client))
		lvl_pd[client]+=15;
	else
		lvl_pd[client]+=24;
	CPrintToChat(client, "%s {green}+%d PD{default} za {darkblue}MVP", TAG, !IsVip(client)?15:24);
	LvlUp(client);
	HintText(client, client);

	return Plugin_Continue;
}
public Action Event_RoundEnd(Event event, char[] name, bool dontBroadcast)
{
	if(IsWarmup()||!RoundMode) return Plugin_Continue;
	
	int winner=event.GetInt("winner");
	for(int i=1; i<=MaxClients; i++)
	{
		if(!IsValidClient(i)) continue;

		if(GetClientTeam(i)==winner)
		{
			if(!IsVip(i))
				lvl_pd[i]+=5;
			else
				lvl_pd[i]+=8;
			CPrintToChat(i, "%s {green}+%d PD{default} za {darkblue}wygranie rundy", TAG, !IsVip(i)?5:8);
			LvlUp(i);
			HintText(i, i);
		}

		DataBaseWrite(i);
	}
	return Plugin_Continue;
}


public Action Prefix(int client, int args)
{
	char buffer[350];
	GetCmdArgString(buffer, sizeof(buffer));
	StripQuotes(buffer);
	if(strlen(buffer)==0) return Plugin_Handled;
	if(buffer[0]=='/'||buffer[0]=='@') return Plugin_Handled;
	if(GetClientTeam(client)==2)
		CPrintToChatAll("\x04[Poziom \x02%d\x04] {orange}%N :{default} %s", lvl_poziom[client], client, buffer);
	else if(GetClientTeam(client)==3)
		CPrintToChatAll("\x04[Poziom \x02%d\x04] {lightblue}%N :{default} %s", lvl_poziom[client], client, buffer);
	else if(GetClientTeam(client)==1)
		CPrintToChatAll("\x04[Poziom \x02%d\x04] {grey}%N :{default} %s", lvl_poziom[client], client, buffer);

	return Plugin_Handled;
}
public Action Prefix_Team(int client, int args)
{
	char buffer[350];
	GetCmdArgString(buffer, sizeof(buffer));
	StripQuotes(buffer);
	if(strlen(buffer)==0) return Plugin_Handled;
	if(buffer[0]=='/'||buffer[0]=='@') return Plugin_Handled;
		
	for(int i=1; i<=MaxClients; i++)
	{
		if(!IsValidClient(i)) continue;

		if(GetClientTeam(i)==2&&GetClientTeam(client)==2)
		{
			CPrintToChat(i, "{orange}(Terroryści) \x04[Poziom \x02%d\x04] {orange}%N :{default} %s", lvl_poziom[client], client, buffer);
			return Plugin_Handled;
		}
		else if(GetClientTeam(i)==3&&GetClientTeam(client)==3)
		{
			CPrintToChat(i, "{lightblue}(Antyterroryści) \x04[Poziom \x02%d\x04] {lightblue}%N :{default} %s", lvl_poziom[client], client, buffer);
			return Plugin_Handled;
		}
		else if(GetClientTeam(i)==1&&GetClientTeam(client)==1)
		{
			CPrintToChat(i, "{grey}(Spectator) \x04[Poziom \x02%d\x04] {grey}%N : %s", lvl_poziom[client], client, buffer);
			return Plugin_Handled;
		}
	}

	return Plugin_Handled;
}



public Action sm_lvl(int client, int args)
{
	Menu poziom=new Menu(Handler_Poziom);

	char buffer[256];
	Format(buffer, sizeof(buffer), "Menu LVL+PERKS\nPoziom: %d\nPD: %d/%d\nPunkty Umiejętności: %d", lvl_poziom[client], lvl_pd[client], lvl_requirements[lvl_poziom[client]+1], lvl_points[client]);

	poziom.SetTitle(buffer);
	poziom.AddItem("1", "Ulepszenia");
	poziom.AddItem("2", "Zdobywanie PD");
	poziom.AddItem("3", "Lista poziomów");
	poziom.AddItem("4", "TOP 10 graczy");
	if(GetUserFlagBits(client) & ADMFLAG_ROOT)
		poziom.AddItem("5", "Resetowanie umiejętności gracza");
	
	poziom.Display(client, 60);
}
public int Handler_Poziom(Menu poziom, MenuAction action, int client, int position)
{
	if(action == MenuAction_Select)
	{
		char Item[5];
		GetMenuItem(poziom, position, Item, sizeof(Item));
	   
		if(StrEqual(Item, "1"))
			lvl_upgrades(client, 0);
		else if(StrEqual(Item, "2"))
			lvl_description(client, 0);
		else if(StrEqual(Item, "3"))
			sm_lvls(client, 0);
		else if(StrEqual(Item, "4"))
			sm_toplvl(client, 0);
		else if(StrEqual(Item, "5"))
			sm_ResetPlayer(client, 0);
	}
	else if(action == MenuAction_End) delete poziom;
}

/*
public Action sm_toplvl(int client, int args)
{
	Menu menu=new Menu(Handler_Menu);
	char title[1024];
	char buffer[255];
	Format(title, sizeof(title), "TOP 10 graczy [poziom]\n");

	Format(buffer, sizeof(buffer), "SELECT `nick`, `lvl_poziom` FROM `tg_lvl` ORDER BY `lvl_pd` DESC LIMIT 10;");

	DBResultSet query=SQL_Query(DB, buffer);

	char nameBuffer[MAX_NAME_LENGTH];
	int lvlBuffer;

	int i=1;
	while(SQL_FetchRow(query))
	{
		lvlBuffer=SQL_FetchInt(query, 1);
		SQL_FetchString(query, 0, nameBuffer, sizeof(nameBuffer));
		Format(buffer, sizeof(buffer), "%d. [Poziom %d] - %s\n", i, lvlBuffer, nameBuffer);
		StrCat(title, 1024, buffer);
		i++;
	}
	menu.SetTitle(title);
	menu.Display(client, 120);
}
*/
public Action sm_toplvl(int client, int args)
{
	Menu menu=new Menu(Handler_Menu);
	menu.SetTitle("Top 10 graczy [Poziom]");

	char buffer[1024];
	Format(buffer, sizeof(buffer), "SELECT `nick`, `lvl_poziom` FROM `tg_lvl` ORDER BY `lvl_pd` DESC LIMIT 10;");

	DBResultSet query=SQL_Query(DB, buffer);

	char nameBuffer[MAX_NAME_LENGTH];
	int lvlBuffer;

	while(SQL_FetchRow(query))
	{
		lvlBuffer=SQL_FetchInt(query, 1);
		SQL_FetchString(query, 0, nameBuffer, sizeof(nameBuffer));
		Format(buffer, sizeof(buffer), "[Poziom %d] - %s", lvlBuffer, nameBuffer);
		menu.AddItem("", buffer, ITEMDRAW_DISABLED);
	}
	menu.Display(client, 120);
}
public int Handler_Menu(Menu menu, MenuAction action, int client, int position)
{
	if(action==MenuAction_End) delete menu;
}


public Action lvl_upgrades(int client, int args)
{
	Menu upgrades=new Menu(Handler_Upgrades);

	char buffer[64];

	upgrades.SetTitle("Ulepszenia");
	Format(buffer, sizeof(buffer), "Grawitacja: %d/10", lvl_gravity[client]);
	upgrades.AddItem("1", buffer);
	Format(buffer, sizeof(buffer), "Szybkość: %d/10", lvl_speed[client]);
	upgrades.AddItem("2", buffer);
	Format(buffer, sizeof(buffer), "Obrażenia od upadku: %d/10", lvl_fallDamage[client]);
	upgrades.AddItem("3", buffer);
	Format(buffer, sizeof(buffer), "Życie: %d/5", lvl_health[client]);
	upgrades.AddItem("4", buffer);
	Format(buffer, sizeof(buffer), "Regeneracja życia: %d/5", lvl_healthRegen[client]);
	upgrades.AddItem("5", buffer);
	Format(buffer, sizeof(buffer), "Obrażenia: %d/3", lvl_additationalDamage[client]);
	upgrades.AddItem("6", buffer);
	upgrades.Display(client, 60);
}
public int Handler_Upgrades(Menu upgrades, MenuAction action, int client, int position)
{
	if(action==MenuAction_Select)
	{
		char Item[5];
		GetMenuItem(upgrades, position, Item, sizeof(Item));
		StripQuotes(Item);
		int type=StringToInt(Item);

		switch(type)
		{
			case 1: gravity(client, 0);
			case 2: speed(client, 0);
			case 3: fall(client, 0);
			case 4: health(client, 0);
			case 5: regen(client, 0);
			case 6: damage(client, 0);
		}
	}
	else if(action == MenuAction_End) delete upgrades;
}

public Action lvl_description(int client, int args)
{
	Menu description=new Menu(Handler_Description);

	description.SetTitle("Zdobywanie PD");
	description.AddItem("0", "VIP otrzymuje 60% więcej PD");
	description.AddItem("1", "Za zabicie gracza +20 PD", ITEMDRAW_DISABLED);
	description.AddItem("2", "Za zabicie gracza nożem +50 PD", ITEMDRAW_DISABLED);
	description.AddItem("3", "Za zabicie NoScope dodatkowe +10 PD", ITEMDRAW_DISABLED);
	description.AddItem("4", "Za zabicie HeadShotem dodatkowe +5 PD", ITEMDRAW_DISABLED);
	description.AddItem("5", "Za zabicie WallBangiem dodatkowe +5 PD", ITEMDRAW_DISABLED);
	description.AddItem("6", "Za zabicie przez Smoke'a dodatkowe +5 PD", ITEMDRAW_DISABLED);
	description.AddItem("7", "Za zabicie na Flash'u dodatkowe +5 PD", ITEMDRAW_DISABLED);
	description.AddItem("8", "Za asystę przy zabiciu + 10 PD", ITEMDRAW_DISABLED);
	description.AddItem("9", "Za bycie MVP rundy +15 PD", ITEMDRAW_DISABLED);
	description.AddItem("10", "Za wygranie rundy +5 PD", ITEMDRAW_DISABLED);
	description.AddItem("11", "Za podłożenie bomby +5 PD", ITEMDRAW_DISABLED);
	description.AddItem("12", "Za rozbrojenie bomby +5 PD", ITEMDRAW_DISABLED);
	description.AddItem("13", "Za podniesienie zakładanika +5 PD", ITEMDRAW_DISABLED);
	description.AddItem("14", "Za uratowanie zakładanika +5 PD", ITEMDRAW_DISABLED);
	description.AddItem("15", "Za zabicie gracza niosącego zakładnika +5 PD", ITEMDRAW_DISABLED);
	description.Display(client, 90);
}
public int Handler_Description(Menu description, MenuAction action, int client, int position)
{
	
	if(action==MenuAction_Select)
		FakeClientCommandEx(client, "sm_vip");
	else if(action==MenuAction_End) delete description;
}

public Action sm_lvls(int client, int args)
{
	Menu lvls=new Menu(Handler_lvls);

	lvls.SetTitle("Lista poziomów");
	lvls.AddItem("1", "Poziom 1: 1PD", ITEMDRAW_DISABLED);
	lvls.AddItem("2", "Poziom 2: 750PD", ITEMDRAW_DISABLED);
	lvls.AddItem("3", "Poziom 3: 2500PD", ITEMDRAW_DISABLED);
	lvls.AddItem("4", "Poziom 4: 7500PD", ITEMDRAW_DISABLED);
	lvls.AddItem("5", "Poziom 5: 15000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("6", "Poziom 6: 30000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("7", "Poziom 7: 50000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("8", "Poziom 8: 75000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("9", "Poziom 9: 105000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("10", "Poziom 10: 140000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("11", "Poziom 11: 180000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("12", "Poziom 12: 225000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("13", "Poziom 13: 275000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("14", "Poziom 14: 330000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("15", "Poziom 15: 390000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("16", "Poziom 16: 455000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("17", "Poziom 17: 525000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("18", "Poziom 18: 600000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("19", "Poziom 19: 680000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("20", "Poziom 20: 765000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("21", "Poziom 21: 855000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("22", "Poziom 22: 950000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("23", "Poziom 23: 1050000PD", ITEMDRAW_DISABLED);
	lvls.AddItem("24", "Poziom 24: 1150000PD", ITEMDRAW_DISABLED);
	lvls.Display(client, 120);
}
public int Handler_lvls(Menu lvls, MenuAction action, int client, int position)
{
	if(action==MenuAction_End) delete lvls;
}

public Action sm_ResetPlayer(int client, int args)
{
	Menu reset=new Menu(Handler_Reset);
	char nick[MAX_NAME_LENGTH];
	char id[8];

	reset.SetTitle("Resetowanie danych gracza");
	for(int i=1; i<=MaxClients; i++)
	{
		if(!IsValidClient(i)) continue;
		
		Format(nick, sizeof(nick), "%N", i);
		Format(id, sizeof(id), "%d", GetClientUserId(i));
		reset.AddItem(id, nick);
	}
	reset.Display(client, 60);
}
public int Handler_Reset(Menu reset, MenuAction action, int client, int position)
{
	if(action==MenuAction_Select)
	{
		char Item[8];

		GetMenuItem(reset, position, Item, sizeof(Item));
		StripQuotes(Item);
		int type=GetClientOfUserId(StringToInt(Item));

		if(!IsValidClient(type))
		{
			CPrintToChat(client, "%s {orange}Gracz {darkred}%N wyszedł z serwera!", TAG, type);
			return;
		}

		lvl_pd[type]=0;
		lvl_poziom[type]=0;
		lvl_points[type]=0;
		lvl_gravity[type]=0;
		lvl_speed[type]=0;
		lvl_fallDamage[type]=0;
		lvl_health[type]=0;
		lvl_healthRegen[type]=0;
		lvl_additationalDamage[type]=0;

		DataBaseWrite(type);
		CPrintToChat(client, "%s Pomyślnie zresetowano dane gracza %N", TAG, type);
	}
	else if(action==MenuAction_End) delete reset;
}

public Action gravity(int client, int args)
{
	Menu gravity1=new Menu(Handler_gravity1);

	gravity1.SetTitle("Ulepszanie grawitacji\nKażde ulepszenie gravitacji kosztuje 1 punkt i zmniejsza ją o 1%");
	gravity1.AddItem("1", "Ulepsz grawitację", lvl_gravity[client]<10?ITEMDRAW_DEFAULT:ITEMDRAW_DISABLED);
	gravity1.AddItem("2", "Cofnij");
	gravity1.Display(client, 60);
}
public int Handler_gravity1(Menu gravity1, MenuAction action, int client, int position)
{
	if(action==MenuAction_Select)
	{
		char Item[5];
		GetMenuItem(gravity1, position, Item, sizeof(Item));

		if(StrEqual(Item, "1"))
			upgrade(client, 1);
		else if(StrEqual(Item, "2"))
			lvl_upgrades(client, 0);
	}
	else if(action==MenuAction_End) delete gravity1;
}

public Action speed(int client, int args)
{
	Menu speed1=new Menu(Handler_speed1);

	speed1.SetTitle("Ulepszanie prędkości\nKażde ulepszenie prędkości kosztuje 1 punkt i zwiększa ją o 1%");
	speed1.AddItem("1", "Ulepsz prędkość", lvl_speed[client]<10?ITEMDRAW_DEFAULT:ITEMDRAW_DISABLED);
	speed1.AddItem("2", "Cofnij");
	speed1.Display(client, 60);
}
public int Handler_speed1(Menu speed1, MenuAction action, int client, int position)
{
	if(action==MenuAction_Select)
	{
		char Item[5];
		GetMenuItem(speed1, position, Item, sizeof(Item));

		if(StrEqual(Item, "1"))
			upgrade(client, 2);
		else if(StrEqual(Item, "2"))
			lvl_upgrades(client, 0);
	}
	else if(action==MenuAction_End) delete speed1;
}

public Action fall(int client, int args)
{
	Menu fall1=new Menu(Handler_fall1);

	fall1.SetTitle("Ulepszanie obrażeń od upadku\nKażde ulepszenie obrażeń od upadku kosztuje 1 punkt i zmniejsza je o 5%");
	fall1.AddItem("1", "Ulepsz obrażenia od upadku", lvl_fallDamage[client]<10?ITEMDRAW_DEFAULT:ITEMDRAW_DISABLED);
	fall1.AddItem("2", "Cofnij");
	fall1.Display(client, 60);
}
public int Handler_fall1(Menu fall1, MenuAction action, int client, int position)
{
	if(action==MenuAction_Select)
	{
		char Item[5];
		GetMenuItem(fall1, position, Item, sizeof(Item));

		if(StrEqual(Item, "1"))
			upgrade(client, 3);
		else if(StrEqual(Item, "2"))
			lvl_upgrades(client, 0);
	}
	else if(action==MenuAction_End) delete fall1;
}

public Action health(int client, int args)
{
	Menu health1=new Menu(Handler_health1);

	health1.SetTitle("Ulepszanie życia\nKażde ulepszenie życia kosztuje 1 punkt i zwiększa je o 1HP");
	health1.AddItem("1", "Ulepsz życie", lvl_health[client]<5?ITEMDRAW_DEFAULT:ITEMDRAW_DISABLED);
	health1.AddItem("2", "Cofnij");
	health1.Display(client, 60);
}
public int Handler_health1(Menu health1, MenuAction action, int client, int position)
{
	if(action==MenuAction_Select)
	{
		char Item[5];
		GetMenuItem(health1, position, Item, sizeof(Item));

		if(StrEqual(Item, "1"))
			upgrade(client, 4);
		else if(StrEqual(Item, "2"))
			lvl_upgrades(client, 0);
	}
	else if(action==MenuAction_End) delete health1;
}

public Action regen(int client, int args)
{
	Menu regen1=new Menu(Handler_regen1);

	regen1.SetTitle("Ulepszanie regeneracji życia\nKażde ulepszenie regeneracji życia kosztuje 1 punkt i zwiększa ją o 1HP");
	regen1.AddItem("1", "Ulepsz regenerację życie", lvl_healthRegen[client]<5?ITEMDRAW_DEFAULT:ITEMDRAW_DISABLED);
	regen1.AddItem("2", "Cofnij");
	regen1.Display(client, 60);
}
public int Handler_regen1(Menu regen1, MenuAction action, int client, int position)
{
	if(action==MenuAction_Select)
	{
		char Item[5];
		GetMenuItem(regen1, position, Item, sizeof(Item));

		if(StrEqual(Item, "1"))
			upgrade(client, 5);
		else if(StrEqual(Item, "2"))
			lvl_upgrades(client, 0);
	}
	else if(action==MenuAction_End) delete regen1;
}

public Action damage(int client, int args)
{
	Menu damage1=new Menu(Handler_damage1);

	damage1.SetTitle("Ulepszanie zadawanych obrażeń\nKażde ulepszenie zadawanych obrażeń kosztuje 1 punkt i zwiększa je o 1HP");
	damage1.AddItem("1", "Ulepsz zadawane obrażenia", lvl_additationalDamage[client]<3?ITEMDRAW_DEFAULT:ITEMDRAW_DISABLED);
	damage1.AddItem("2", "Cofnij");
	damage1.Display(client, 60);
}
public int Handler_damage1(Menu damage1, MenuAction action, int client, int position)
{
	if(action==MenuAction_Select)
	{
		char Item[5];
		GetMenuItem(damage1, position, Item, sizeof(Item));

		if(StrEqual(Item, "1"))
			upgrade(client, 6);
		else if(StrEqual(Item, "2"))
			lvl_upgrades(client, 0);
	}
	else if(action==MenuAction_End) delete damage1;
}

public Action upgrade(int client, int args)
{
	switch(args)
	{
		case 1:
		{
			if(lvl_gravity[client]<10&&lvl_points[client]>=1)
			{
				lvl_gravity[client]++;
				lvl_points[client]--;
				CPrintToChat(client, "%s {olive}Pomyślnie ulepszyłeś{darkred} grawitację{olive}.{orange} Pozostałe punkty umiejętności: {darkred}%i", TAG, lvl_points[client]);
				gravity(client, 0);
			}
			else
			{
				CPrintToChat(client, "%s {lightred}Nie spełniasz wymagań!", TAG);
				gravity(client, 0);
			}
		}
		case 2:
		{
			if(lvl_speed[client]<10&&lvl_points[client]>=1)
			{
				lvl_speed[client]++;
				lvl_points[client]--;
				CPrintToChat(client, "%s {olive}Pomyślnie ulepszyłeś{darkred} prędkość{olive}.{orange} Pozostałe punkty umiejętności: {darkred}%i", TAG, lvl_points[client]);
				speed(client, 0);
			}
			else
			{
				CPrintToChat(client, "%s {lightred}Nie spełniasz wymagań!", TAG);
				speed(client, 0);
			}
		}
		case 3:
		{
			if(lvl_fallDamage[client]<10&&lvl_points[client]>=1)
			{
				lvl_fallDamage[client]++;
				lvl_points[client]--;
				CPrintToChat(client, "%s {olive}Pomyślnie ulepszyłeś{darkred} obrażenia od upadku{olive}.{orange} Pozostałe punkty umiejętności: {darkred}%i", TAG, lvl_points[client]);
				fall(client, 0);
			}
			else
			{
				CPrintToChat(client, "%s {lightred}Nie spełniasz wymagań!", TAG);
				fall(client, 0);
			}
		}
		case 4:
		{
			if(lvl_health[client]<5&&lvl_points[client]>=1)
			{
				lvl_health[client]++;
				lvl_points[client]-=3;
				CPrintToChat(client, "%s {olive}Pomyślnie ulepszyłeś{darkred} życie{olive}.{orange} Pozostałe punkty umiejętności: {darkred}%i", TAG, lvl_points[client]);
				health(client, 0);
			}
			else
			{
				CPrintToChat(client, "%s {lightred}Nie spełniasz wymagań!", TAG);
				health(client, 0);
			}
		}
		case 5:
		{
			if(lvl_healthRegen[client]<5&&lvl_points[client]>=1)
			{
				lvl_healthRegen[client]++;
				lvl_points[client]-=2;
				CPrintToChat(client, "%s {olive}Pomyślnie ulepszyłeś{darkred} regenerację życia{olive}.{orange} Pozostałe punkty umiejętności: {darkred}%i", TAG, lvl_points[client]);
				regen(client, 0);
			}
			else
			{
				CPrintToChat(client, "%s {lightred}Nie spełniasz wymagań!", TAG);
				regen(client, 0);
			}
		}
		case 6:
		{
			if(lvl_additationalDamage[client]<3&&lvl_points[client]>=1)
			{
				lvl_additationalDamage[client]++;
				lvl_points[client]-=5;
				CPrintToChat(client, "%s {olive}Pomyślnie ulepszyłeś{darkred} zadawane obrażenia{olive}.{orange} Pozostałe punkty umiejętności: {darkred}%i", TAG, lvl_points[client]);
				damage(client, 0);
			}
			else
			{
				CPrintToChat(client, "%s {lightred}Nie spełniasz wymagań!", TAG);
				damage(client, 0);
			}
		}
	}
}


public Action OnTakeDamage(int victim, int & attacker, int & inflictor, float & damage, int & damagetype)
{
	if(IsValidClient(victim)&&damagetype & DMG_FALL)
	{
		damage*=ClientFallDamage(victim);
		return Plugin_Changed;
	}
	else if(IsValidClient(attacker)&&attacker!=victim)
	{
		damage+=lvl_additationalDamage[attacker];
		return Plugin_Changed;
	}

	return Plugin_Continue;
}
public Action Health_Regen(Event event, char[] name, bool dontBroadcast)
{
	int attacker=GetClientOfUserId(event.GetInt("attacker"));

	if(!IsValidClient(attacker)) return Plugin_Continue;

	if(GetClientHealth(attacker)+lvl_healthRegen[attacker]<ClientHealth(attacker))
		SetEntityHealth(attacker, GetClientHealth(attacker)+lvl_healthRegen[attacker]);
	else
		SetEntityHealth(attacker, ClientHealth(attacker));

	return Plugin_Continue;
}


float ClientGravity(client)
{
	float gravitation=(1.0-0.01*lvl_gravity[client]);
	return gravitation;
}
float ClientSpeed(client)
{
	float agility=(1+0.01*lvl_speed[client]);
	return agility;
}
float ClientFallDamage(int client)
{
	float fallDamage=(1.0-0.05*lvl_fallDamage[client]);
	return fallDamage;
}
int ClientHealth(int client)
{
	int hp=100+lvl_health[client];
	return hp;
}

public Action Timer_CheckGravity(Handle timer, int UserId)
{
	int client = GetClientOfUserId(UserId);

	if(!IsValidClient(client)) return Plugin_Continue;
	if(!IsPlayerAlive(client))	return Plugin_Continue;
	if (IsPlayerAlive(client)&&GetEntityGravity(client)!=ClientGravity(client))
		SetEntityGravity(client, ClientGravity(client));

	return Plugin_Continue;
}
void SetEntitySpeed(int client, float speed)
{
	SetEntPropFloat(client, Prop_Data, "m_flLaggedMovementValue", speed);
}



// DATABASE

public void DataBaseConnect()
{
	char error[255];
	DB=SQL_Connect("DB_LVL", true, error, sizeof(error));
	if(DB==INVALID_HANDLE)
	{
		LogError("Nie udalo sie polaczyc! Error: %s", error);
		return;
	}

	char buffer[1024];
	Format(buffer, sizeof(buffer), "CREATE TABLE IF NOT EXISTS `tg_lvl`(`steamid` INT NOT NULL PRIMARY KEY, `nick` VARCHAR(64), `lvl_pd` INT NOT NULL, `lvl_poziom` INT NOT NULL, `lvl_points` INT NOT NULL, `lvl_gravity` INT NOT NULL, `lvl_speed` INT NOT NULL, `lvl_fallDamage` INT NOT NULL, `lvl_health` INT NOT NULL, `lvl_healthRegen` INT NOT NULL, `lvl_additationalDamage` INT NOT NULL);");
	if(!SQL_FastQuery(DB, buffer))
	{
		SQL_GetError(DB, error, sizeof(error));
		PrintToServer("Nie udalo sie stworzyc tabeli! Error: %s", error);
	}
}

public void DataBaseRead(int client)
{
	if(!IsValidClient(client)) return;
		
	int sid=GetSteamAccountID(client);
	char buffer[1024];
	Format(buffer, sizeof(buffer), "SELECT `lvl_pd`, `lvl_poziom`, `lvl_points`, `lvl_gravity`, `lvl_speed`, `lvl_fallDamage`, `lvl_health`, `lvl_healthRegen`, `lvl_additationalDamage` FROM `tg_lvl` WHERE `steamid`=%d;", sid);
	DBResultSet query=SQL_Query(DB, buffer);
	if(query==null)
	{
		char error[255];
		SQL_GetError(DB, error, sizeof(error));
		PrintToServer("Nie udalo sie pobrac danych gracza %N! (error: %s)", client, error);
		return;
	}
	if(!SQL_GetRowCount(query))
	{
		Format(buffer, sizeof(buffer), "INSERT INTO `tg_lvl` VALUES(%d, '%N', 0, 0, 0, 0, 0, 0, 0, 0, 0);", sid, client);
		if(!SQL_FastQuery(DB, buffer))
		{
			char error[255];
			SQL_GetError(DB, error, sizeof(error));
			PrintToServer("Nie udalo sie dodac nowego gracza %N! (error: %s)", client, error);
		}
		return;
	}
	SQL_FetchRow(query);
	lvl_pd[client]=SQL_FetchInt(query, 0);
	lvl_poziom[client]=SQL_FetchInt(query, 1);
	lvl_points[client]=SQL_FetchInt(query, 2);
	lvl_gravity[client]=SQL_FetchInt(query, 3);
	lvl_speed[client]=SQL_FetchInt(query, 4);
	lvl_fallDamage[client]=SQL_FetchInt(query, 5);
	lvl_health[client]=SQL_FetchInt(query, 6);
	lvl_healthRegen[client]=SQL_FetchInt(query, 7);
	lvl_additationalDamage[client]=SQL_FetchInt(query, 8);
}

public void DataBaseWrite(int client)
{
	if(!IsValidClient(client)) return;

	char buffer[1024];
	Format(buffer, sizeof(buffer), "UPDATE `tg_lvl` SET `lvl_pd`=%d, `lvl_poziom`=%d, `lvl_points`=%d, `lvl_gravity`=%d, `lvl_speed`=%d, `lvl_fallDamage`=%d, `lvl_health`=%d, `lvl_healthRegen`=%d, `lvl_additationalDamage`=%d  WHERE `steamid`=%d;", lvl_pd[client], lvl_poziom[client], lvl_points[client], lvl_gravity[client], lvl_speed[client], lvl_fallDamage[client], lvl_health[client], lvl_healthRegen[client], lvl_additationalDamage[client], GetSteamAccountID(client));
	if(!SQL_FastQuery(DB, buffer))
	{
		char error[255];
		SQL_GetError(DB, error, sizeof(error));
		PrintToServer("Nie udalo sie zaktualizowac danych gracza %N! (error: %s)", client, error);
	}
}



public bool IsValidClient(int client)
{
	if(client <= 0 ) return false;
	if(client > MaxClients) return false;
	if(!IsClientConnected(client)) return false;
	if(IsClientSourceTV(client)) return false;
	if(IsFakeClient(client)) return false;
	return IsClientInGame(client);
}

public bool IsVip(int client)
{

	if(GetUserFlagBits(client) & ADMFLAG_CUSTOM1) return true;
	if(GetUserFlagBits(client) & ADMFLAG_ROOT) return true;

	return false;
}

public bool IsWarmup()
{
	int warmup = GameRules_GetProp("m_bWarmupPeriod", 4, 0);
	if (warmup == 1) return true;
	else return false;
} 

public int PlayerCount()
{
	int count;
	for(int i=1; i<=MaxClients; i++)
	{
		if(!IsValidClient(i)) continue;
		else count++;
	}
	return count;
}