/* Plugin Template generated by Pawn Studio */

#include <sourcemod>
#include <sdktools>


/**
 * Unlocks an entity.
 *
 * @param entity		Entity index.
 * @noreturn
 */
stock Entity_UnLock(entity) {
	SetEntProp(entity, Prop_Data, "m_bLocked", 0, 1);
}


public Plugin:myinfo = 
{
	name = "Divine Plugin",
	author = "Bob Loblaw",
	description = "Handles mg_divine_multigame functionality",
	version = "1.0",
	url = "http://www.steamgamers.com"
}

//I DON'T COMMENT MY SHIT

new Handle:timer;
new bool:close_frags=true;
new ans;
new Handle:mathTimer;
new bool:active = false;
new scores[128];

public OnPluginStart()
{
}

public OnMapStart()
{
    decl String:map[64];
    GetCurrentMap(map, 64);
    if (StrContains(map, "mg_divine", true) != -1)
	{
		//RegServerCmd("gw_over", gwmr_Over, "Trigger whenever mini-round ends.");
		RegServerCmd("gw_start", gw_Start, "Trigger when the multigame starts.");
		//RegServerCmd("domath", doMath, "Trigger for math mini-game");
		RegConsoleCmd("songlist", printSongs, "Print a list of all songs.");
		HookEvent("round_start", FindButton);
		//HookEvent("round_start", Restore);
		
		active = true;
    }
	else
	{
        active = false;
    }
}

public FindButton(Handle:event, const String:name[], bool:dontBroadcast)
{
	if(active)
	{
		new String:ent_name[32];
		new String:real_name[32];
		real_name = "paulwarebutton";
		
		new ent=-1;
		while((ent = FindEntityByClassname(ent, "func_button")) != -1)
		{
			GetEntPropString(ent, Prop_Data, "m_iName", ent_name, sizeof(ent_name));
			if((strcmp(ent_name, real_name, true)) == 0)
			{
				break;
			}
		}
		
		Entity_UnLock(ent);
	}
}

public Action:printSongs(client, args)
{
	if(active)
	{
		new String:song1[256];
		new String:song2[256];
		new String:song3[256];
		new String:song4[256];
		new String:song5[256];
		new String:song6[256];
		new String:song7[256];
		new String:song8[256];
		new String:song9[256];
		new String:song10[256];
		new String:song11[256];
		new String:song12[256];
		song1="Song Artist - Song Name - Song URL";
		song2="----------------------------------";
		song3="The Glitch Mob - Drive It Like You Stole It - http://www.youtube.com/watch?v=N9vn6I17yWw";
		song4="Owsey & Stumbleine - You Came Out Of Nowhere (feat. Shura) - http://www.youtube.com/watch?v=pMyB6h3mfdQ";
		song5="The Prodigy - Stand Up - http://www.youtube.com/watch?v=0yvkoGZVajg";
		song6="Sub Focus - Rock It - http://www.youtube.com/watch?v=BdJgwf-_HGY";
		song7="Wiz Khalifa - Young Wild and Free (Instrumental) - http://www.youtube.com/watch?v=0WBF2hEm5t8";
		song8="Metal Gear Solid 2 - 15. Arsenal is going to take off - http://www.youtube.com/watch?v=dyOxJwBdUrs";
		song9="Metal Gear Solid 2 - Metal Gear Soundtrack - http://www.youtube.com/watch?v=gD3Dep3dLJs";
		song10="Metal Gear Solid 2 - Olga Gurlukovich - http://www.youtube.com/watch?v=MaSKpvah_tU";
		song11="Metal Gear Solid 2 - Russian soldiers from kasatka - http://www.youtube.com/watch?v=fAP6suecKxE";
		song12="Chicane - From Blue To Green - http://www.youtube.com/watch?v=fNgpMzFqVJs&ob=av2n";
		
		PrintToConsole(client, song1);
		PrintToConsole(client, song2);
		PrintToConsole(client, song3);
		PrintToConsole(client, song4);
		PrintToConsole(client, song5);
		PrintToConsole(client, song6);
		PrintToConsole(client, song7);
		PrintToConsole(client, song8);
		PrintToConsole(client, song9);
		PrintToConsole(client, song10);
		PrintToConsole(client, song11);
		PrintToConsole(client, song12);
	}
	
	return Plugin_Handled;
}

public OnClientDisconnect(client)
{
	scores[client]=0;
}

public Restore(Handle:event, const String:name[], bool:dontBroadcast)
{
	if(active)
	{
		new m=1;
		new maxClients = GetMaxClients();
		while(m<=maxClients)
		{
			if(IsClientInGame(m))
			{
				SetEntProp(m, Prop_Data, "m_iFrags", scores[m]);
				scores[m] = 0;
			}
			m++;
		}
	}
}

public Action:gwmr_Over(args)
{
	if(active)
	{
		if(close_frags)
		{
			return Plugin_Handled;
		}
		
		new maxClients = GetMaxClients();
		
		new m=1;
		while(m<=maxClients)
		{
			if(IsClientInGame(m))
			{
				if(GetClientFrags(m)==20)
				{
					GivePlayerItem(m, "weapon_m249", 0);
					scores[m] = scores[m]+1;
					KillTimer(timer, false);
					close_frags=true;
				} 
			}
			m++;
		}
	}
	
	return Plugin_Handled;
}

public Action:gw_Start(args)
{
	if(active)
	{
		/*new m=1;
		new maxClients = GetMaxClients();
		while(m<=maxClients)
		{
			if(IsClientInGame(m))
			{
				scores[m] = GetClientFrags(m);
				SetEntProp(m, Prop_Data, "m_iFrags", 0);
			}
			m++;
		}*/
		
		close_frags = false;
		timer = CreateTimer(120.0, TimeOut);
	}
	
	return Plugin_Handled;
}


public Action:TimeOut(Handle:theTimer)
{
	if(active)
	{
		new maxClients = GetMaxClients();
		new highscore = 0;
		new hsPlayer = 0;
		new Tie1 = 0;
		new Tie2 = 0;
		new Tie3 = 0;
		new bool:isTie = false;
		new m=1;
		
		while(m<=maxClients)
		{
			if(IsClientInGame(m))
			{
				if(GetClientFrags(m)>highscore)
				{
					highscore = GetClientFrags(m);
					hsPlayer = m;
					isTie = false;
					Tie1 = 0;
					Tie2 = 0;
					Tie3 = 0;
				}
				else if(GetClientFrags(m)==highscore)
				{
					isTie = true;
					if(Tie1==0)
					{
						Tie1 = m;
					}
					else if(Tie2==0)
					{
						Tie2 = m;
					}
					else if(Tie3==0)
					{
						Tie3 = m;
					}
					else
					{
						PrintToChatAll("Fuck guys really?");
					}
				}
			}
			
			m++;
		}
		
		GivePlayerItem(hsPlayer, "weapon_m249");
		scores[hsPlayer] = scores[hsPlayer]+1;
		
		if (isTie == true)
		{
			if(Tie1!=0)
			{
				GivePlayerItem(Tie1, "weapon_m249");
				scores[Tie1] = scores[Tie1]+1;
			}
			if(Tie2!=0)
			{
				GivePlayerItem(Tie2, "weapon_m249");
				scores[Tie2] = scores[Tie2]+1;
			}
			if(Tie3!=0)
			{
				GivePlayerItem(Tie3, "weapon_m249");
				scores[Tie3] = scores[Tie3]+1;
			}
		}
		
		close_frags = true;
	}
	
	return Plugin_Handled;
}

public Action:NobodyRight(Handle:theTimer)
{
	if(active)
	{
		PrintToChatAll("Nobody got it right!");
		RemoveCommandListener(compare, "say");
		
		KillTimer(mathTimer, false);
	}
	
	return Plugin_Handled;
}

public Action:doMath(args)
{
	if(active)
	{
		new String:statement[128];
		new iOne;
		new op = GetRandomInt(1, 4);
		
		new iTwo;
		if(op==3)
		{
			iOne = GetRandomInt(1, 30);
			iTwo = GetRandomInt(1, 10);
		}
		else if(op==4)
		{
			iOne = GetRandomInt(30, 140);
			iTwo = GetRandomInt(1, 13);
		}
		else
		{
			iOne = GetRandomInt(1, 150);
			iTwo = GetRandomInt(1, 150);
		}
		
		while(iOne%iTwo!=0)
		{
			iOne--;
		}
		
		if(op==1)
		{
			ans = iOne + iTwo;
			Format(statement, sizeof(statement), "%d + %d = ?", iOne, iTwo);
		}
		else if(op==2)
		{
			ans = iOne - iTwo;
			Format(statement, sizeof(statement), "%d - %d = ?", iOne, iTwo);
		}
		else if(op==3)
		{
			ans = iOne * iTwo;
			Format(statement, sizeof(statement), "%d x %d = ?", iOne, iTwo);
		}
		else
		{
			ans = iOne / iTwo;
			Format(statement, sizeof(statement), "%d / %d = ?", iOne, iTwo);
		}
		
		AddCommandListener(compare, "say");
		
		PrintToChatAll("%s", statement);
		
		mathTimer = CreateTimer(10.0, NobodyRight);
	}
	
	return Plugin_Handled;
}

public Action:compare(client, const String:command[], argc)
{
	if(active)
	{
		new String:buffer[256];
		
		GetCmdArgString(buffer, sizeof(buffer));
		
		new i = 0;
		
		while(i<6)
		{
			buffer[i]=buffer[i+1];
			if(buffer[i]=='"')
			{
				buffer[i]='\0';
			}
			i++;
		}
		
		new String:realAns[256];
		
		IntToString(ans, realAns, sizeof(realAns));
		
		if((strcmp(realAns, buffer, true))==0)
		{
			KillTimer(mathTimer, false);
			PrintToChat(client, "Correct!");
			RemoveCommandListener(compare, "say");
			new score = GetEntProp(client, Prop_Data, "m_iFrags");
			SetEntProp(client, Prop_Data, "m_iFrags", score+1);
		}
		else
		{
			PrintToChat(client, "Incorrect!");
		}
	}
	
	return Plugin_Continue;
}