context: using System;
  using WCell.Addons.Default.Battlegrounds.EyeOfTheStorm.Bases;
  using WCell.Addons.Default.Lang;
  using WCell.Constants;
  using WCell.Constants.AreaTriggers;
  using WCell.Constants.Battlegrounds;
  using WCell.Constants.Factions;
  using WCell.Constants.GameObjects;
  using WCell.Constants.Spells;
  using WCell.Constants.World;
  using WCell.Core.Initialization;
  using WCell.Core.Timers;
  using WCell.RealmServer.AreaTriggers;
  using WCell.RealmServer.Battlegrounds;
  using WCell.RealmServer.Chat;
  using WCell.RealmServer.Entities;
  using WCell.RealmServer.GameObjects;
  using WCell.RealmServer.Global;
  using WCell.RealmServer.Handlers;
  using WCell.RealmServer.Lang;
  using WCell.RealmServer.NPCs;
  using WCell.RealmServer.NPCs.Spawns;
  using WCell.Util.Graphics;
  using WCell.Util.Variables;
  
  namespace WCell.Addons.Default.Battlegrounds.EyeOfTheStorm
  {
      public class EyeOfTheStorm : Battleground
      {
          #region Static Fields
          [Variable("EOTSMaxScore")]
          public static int MaxScoreDefault
          {
              get { return Constants.World.WorldStates.GetState(WorldStateId.EOTSMaxResources).DefaultValue; }
              set { Constants.World.WorldStates.GetState(WorldStateId.EOTSMaxResources).DefaultValue = value; }
          }
  
          [Variable("EOTSNearVictoryScore")]
          public static int NearVictoryScoreDefault
          {
              get { return Constants.World.WorldStates.GetState(WorldStateId.EOTSNearVictoryWarning).DefaultValue; }
              set { Constants.World.WorldStates.GetState(WorldStateId.EOTSNearVictoryWarning).DefaultValue = value; }
          }
  
          static EyeOfTheStorm()
          {
              MaxScoreDefault = 1600;
              NearVictoryScoreDefault = 1400;
          }
  
          [Variable("EOTSPrepTimeMillis")]
          public static int EOTSPreparationTimeMillis = 60 * 1000;
  
          public static int EOTSBuffRespawnTimeMillis = 2 * 60 * 100;
          #endregion
  
          #region Fields
  
          private GameObject _hForcefield;
          private GameObject _aForcefield;
          private GameObject _netherstormFlag;
          private GameObject _bebuffgo;
          private GameObject _frbuffgo;
          private GameObject _mtbuffgo;
          private GameObject _drbuffgo;
  
  
          private SpellId _bebuffspell;
          private SpellId _frbuffspell;
          private SpellId _mtbuffspell;
          private SpellId _drbuffspell;
  
  
          public EOTSBase[] Bases;
          public int MaxScore;
          public int NearVictoryScore;
          public bool isInformatedNearVictory;
          private AreaTrigger _felReaverAT;
          private AreaTrigger FRbuffTrigger;
          private AreaTrigger MTbuffTrigger;
          private AreaTrigger DRbuffTrigger;
          private AreaTrigger _bloodelf2;
          private AreaTrigger _bloodelf3;
          Random EOTSrnd = new Random();
  
          #endregion
  
          #region Props
          public int HordeScore
          {
              get { return WorldStates.GetInt32(WorldStateId.EOTSResourcesHorde); }
              set
              {
                  WorldStates.SetInt32(WorldStateId.EOTSResourcesHorde, value);
                  if (value >= MaxScore)
                  {
                      Winner = HordeTeam;
                      FinishFight();
                  }
                  if (value >= NearVictoryScore && !isInformatedNearVictory)
                  {
                      InformNearVictory(HordeTeam, value);
                  }
              }
          }
  
          public int AllianceScore
          {
              get { return WorldStates.GetInt32(WorldStateId.EOTSResourcesAlliance); }
              set
              {
                  WorldStates.SetInt32(WorldStateId.EOTSResourcesAlliance, value);
                  if (value >= MaxScore)
                  {
                      Winner = AllianceTeam;
                      FinishFight();
                  }
                  if (value >= NearVictoryScore && !isInformatedNearVictory)
                  {
                      InformNearVictory(AllianceTeam, value);
                  }
              }
          }
  
          public int HordeBaseCount
          {
              get { return WorldStates.GetInt32(WorldStateId.EOTSHordeBases); }
              set
              {
                  WorldStates.SetInt32(WorldStateId.EOTSHordeBases, value);
              }
          }
  
          public int AllianceBaseCount
          {
              get { return WorldStates.GetInt32(WorldStateId.EOTSAllianceBases); }
              set
              {
                  WorldStates.SetInt32(WorldStateId.EOTSAllianceBases, value);
              }
          }
  
          #endregion
  
          public EyeOfTheStorm()
          {
              _template = BattlegroundMgr.GetTemplate(BattlegroundId.EyeOfTheStorm);
              Bases = new EOTSBase[(int)EOTSBases.End];
          }
          #region Overrides
          protected override void InitMap()
          {
              base.InitMap();
              Bases[(int)EOTSBases.BloodElfTower] = new BloodElfTower(this);
              Bases[(int)EOTSBases.DraeneiRuins] = new DraeneiRuins(this);
              Bases[(int)EOTSBases.FelReaverRunis] = new FelReaverRuins(this);
              Bases[(int)EOTSBases.MageTower] = new MageTower(this);
              RegisterEvents();
              MaxScore = MaxScoreDefault;
              NearVictoryScore = NearVictoryScoreDefault;
          }
  
          protected override void OnStart()
          {
              base.OnStart();
              _hForcefield.State = GameObjectState.Disabled;
              _aForcefield.State = GameObjectState.Disabled;
              Characters.SendSystemMessage(DefaultAddonLocalizer.Instance.GetTranslations(AddonMsgKey.EOTSOnStart));
  
              SpawnBEBuff();
              SpawnFRBuff();
              SpawnMTBuff();
              SpawnDRBuff();
          }
  
          protected override void OnFinish(bool disposing)
          {
              base.OnFinish(disposing);
              Characters.SendSystemMessage(DefaultAddonLocalizer.Instance.GetTranslations(AddonMsgKey.EOTSOnFinish), Winner.Side.ToString());
          }
  
          protected override void OnPrepareHalftime()
          {
              base.OnPrepareHalftime();
              var time = RealmLocalizer.FormatTimeSecondsMinutes(PreparationTimeMillis / 2000);
              Characters.SendSystemMessage(DefaultAddonLocalizer.Instance.GetTranslations(AddonMsgKey.EOTSOnPrepareHalfTime), time);
          }
  
          protected override void OnPrepareBegin()
          {
              base.OnPrepareBegin();
              var time = RealmLocalizer.FormatTimeSecondsMinutes(PreparationTimeMillis / 1000);
              Characters.SendSystemMessage(DefaultAddonLocalizer.Instance.GetTranslations(AddonMsgKey.EOTSOnPrepareBegin), time);
          }
  
          protected override void OnLeave(Character chr)
          {
              base.OnLeave(chr);
              Characters.SendSystemMessage(DefaultAddonLocalizer.Instance.GetTranslations(AddonMsgKey.EOTSOnLeave), chr.Name);
          }
  
          protected override void OnEnter(Character chr)
          {
              base.OnEnter(chr);
              Characters.SendSystemMessage(DefaultAddonLocalizer.Instance.GetTranslations(AddonMsgKey.EOTSOnEnter), chr.Name);
          }
  
          protected override void SpawnGOs()
          {
              base.SpawnGOs();
              GOEntry allianceDoorEntry = GOMgr.GetEntry(GOEntryId.Forcefield000);
              GOEntry hordeDoorEntry = GOMgr.GetEntry(GOEntryId.Forcefield001);
              GOEntry netherstormFlagEntry = GOMgr.GetEntry(GOEntryId.NetherstormFlag);
  
              _hForcefield = allianceDoorEntry.FirstSpawnEntry.Spawn(this);
              _aForcefield = hordeDoorEntry.FirstSpawnEntry.Spawn(this);
              _netherstormFlag = netherstormFlagEntry.FirstSpawnEntry.Spawn(this);
  
              RegisterBuffEvents();
          }
  
          protected override void SpawnNPCs()
          {
              base.SpawnNPCs();
              NPCEntry hordeSGhome = NPCMgr.GetEntry(Constants.NPCs.NPCId.HordeSpiritGuide);
              NPCEntry allianceSGhome = NPCMgr.GetEntry(Constants.NPCs.NPCId.AllianceSpiritGuide); ;
  
              hordeSGhome.SpawnAt(this, new Vector3(1807.737f, 1539.417f, 1267.625f), false);
              allianceSGhome.SpawnAt(this, new Vector3(2523.711f, 1596.52f, 1269.361f), false);
          }
  
          public override void FinishFight()
          {
              base.FinishFight();
          }
  
          #endregion
  
          private void Graveyards(Character chr, BattlegroundTeam team)
          {
          }
  
          private void InformNearVictory(BattlegroundTeam team, int score)
          {
              Characters.SendSystemMessage(DefaultAddonLocalizer.Instance.GetTranslations(AddonMsgKey.EOTSNearVictory), team.Side.ToString(), score);
              isInformatedNearVictory = true;
          }
  
          private void HandlePowerUp(Unit unit, SpellId spell, GameObject go, Action respawnCallback)
          {
  
gt:             if (go != null && !go.IsDeleted)