// Better Gaming Experience
//	 By Phantom

// Feel free to disable or enable.
bool Weather = true;

//		SCRIPT

PlayerModifiers modify = new PlayerModifiers();

int hp = 100, Energy = 100;
bool SP;

Events.PlayerDeathCallback m_playerDeathEvent = null;
Events.UserMessageCallback m_userMessageEvent = null;

public void OnStartup()
{
//		Health Setup
	if (!Game.LocalStorage.TryGetItemInt("hp", out hp))
	{
		hp = 100;
	}
	else if (hp != 100)	Game.ShowChatMessage("Max Hp is set to: " + hp, Color.Green);

//		Energy Setup
	if (!Game.LocalStorage.TryGetItemInt("Energy", out Energy))
	{
		Energy = 100;
	}
	else if (Energy != 100)	Game.ShowChatMessage("Max Energy is set to: " + Energy, Color.Green);

	foreach (IPlayer ply in Game.GetPlayers())
	{
		modify.MaxHealth = hp;
		modify.MaxEnergy = Energy;
		ply.SetModifiers(modify);
	}

//		Weather Setup
	if (Weather)
	{
		if (Game.GetWeatherType() == WeatherType.Snow || Game.GetWeatherType() == WeatherType.Rain)
		{
			Game.ShowChatMessage("Changing Weather " + Game.GetWeatherType() + " to Clear", Color.Cyan);
			Game.SetWeatherType(WeatherType.None);
		}
		else Game.ShowChatMessage("Weather Checked!", Color.Cyan);
	}

//		Startup & Death Sequence Setup
	if (!Game.StartupSequenceEnabled)
	{
		Game.StartupSequenceEnabled = true;
		Game.ShowChatMessage("StartupSequence is set to True", Color.Green);
	}
	if (!Game.DeathSequenceEnabled)
	{
		Game.DeathSequenceEnabled = true;
		Game.ShowChatMessage("DeathSequence is set to True", Color.Green);
	}

	Game.ShowChatMessage("Type /cmd to see available Commands.", Color.Green);

	SP = (bool)Game.LocalStorage.GetItem("SP");
	if (SP) {
      Game.ShowChatMessage("SP Mode is Enabled", Color.Cyan);
      m_playerDeathEvent = Events.PlayerDeathCallback.Start(OnPlayerDeath);
   }

	m_userMessageEvent = Events.UserMessageCallback.Start(OnCommand);

}

public void OnShutdown()
{
	Game.LocalStorage.SetItem("hp", hp);
	Game.LocalStorage.SetItem("Energy", Energy);
	Game.LocalStorage.SetItem("SP", SP);

   if (SP) Events.PlayerDeathCallback.Stop(m_playerDeathEvent);
	Events.UserMessageCallback.Stop(m_userMessageEvent);
}

public void OnPlayerDeath(IPlayer player, PlayerDeathArgs args) {
   if (Game.IsGameOver || player.IsBot) return;
   foreach (IPlayer ply in Game.GetPlayers()) {
      if (!ply.IsBot && !ply.IsDead) {
         return;
      }
   }
   foreach (IPlayer ply in Game.GetPlayers()) {
      if (ply.GetTeam() != PlayerTeam.Independent) return;
   }
   Game.SetGameOver("NOBODY LEFT!");
}

public void OnCommand(UserMessageCallbackArgs args)
{
	if (!args.User.IsHost && !args.User.IsModerator) return;
   //	Display all available Commands
   if (args.Command == "CMD")
   {
      Game.ShowChatMessage("Available Commands by Better Gaming", Color.Red);
      Game.ShowChatMessage("/rs = Reset all Scores", Color.Green);
      Game.ShowChatMessage("/hp = Set Max Hp for all Players (E.g: /hp 1)", Color.Green);
      Game.ShowChatMessage("/e = Set Max Energy for all Players", Color.Green);
      Game.ShowChatMessage("/kill = Kills all, kills bot or kills a specfic player by name", Color.Green);
      Game.ShowChatMessage("/SP = Enables or Disables SP Mode", Color.Green);
   }

   //	Score Reset on User Command
   else if (args.Command == "RS")
   {
      Game.ResetScore();
      Game.ShowChatMessage("All Scores has been reset!", Color.Red);
   }

   //	Health Setup on User Command
   else if (args.Command == "HP")
   {
      try
      {
         hp = Convert.ToInt32(args.CommandArguments);
         Game.LocalStorage.SetItem("hp", hp);
         Game.ShowChatMessage("Max Health is set to " + hp, Color.Red);
         foreach (IPlayer ply in Game.GetPlayers())
         {
            modify.MaxHealth = hp;
            ply.SetModifiers(modify);
         }
      }
      catch	{Game.RunCommand("/MSG Invalid Hp!");}
   }

   //	Energy Setup on User Command
   else if (args.Command == "E")
   {
      try
      {
         Energy = Convert.ToInt32(args.CommandArguments);
         Game.LocalStorage.SetItem("Energy", Energy);
         Game.ShowChatMessage("Max Energy is set to " + Energy, Color.Red);
         foreach (IPlayer ply in Game.GetPlayers())
         {
            modify.MaxEnergy = Energy;
            ply.SetModifiers(modify);
         }
      }
      catch	{Game.RunCommand("/MSG Invalid Energy!");}
   }

   //	Kill players on User Command
   else if (args.Command == "KILL")
   {
      //		Kill all Bots
      if (args.CommandArguments == "bot")
      {
         foreach (IPlayer ply in Game.GetPlayers())
         {
            if (ply.IsBot && !ply.IsDead) ply.Gib();
         }
      }

      //	Kill all Players (including bots)
      else if (args.CommandArguments == "all")
      {
         foreach (IPlayer ply in Game.GetPlayers())
         {
            if (!ply.IsDead) ply.Gib();
         }
      }

      //	Kills specific player by Name
      else
      {
         foreach (IPlayer ply in Game.GetPlayers())
         {
            if (ply.Name.ToLower() == args.CommandArguments.ToLower()) ply.Gib();
         }
      }
   }
   else if (args.Command == "SP") {
      SP = !SP;
      if (SP) {
         Game.ShowChatMessage("SP Mode Enabled", Color.Cyan);
         Game.ShowChatMessage("NOTE: Set GameOver when no human players remain alive", Color.Yellow);
      }
      else Game.ShowChatMessage("SP Mode Disabled", Color.Red);
   }
}