Advertisement
Guest User

RiftTimerPlugin.cs

a guest
Nov 18th, 2017
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. namespace Turbo.Plugins.Jack
  2. {
  3. using System;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using Turbo.Plugins.Default;
  8.  
  9. public class RiftTimerPlugin : BasePlugin, IInGameTopPainter, IAfterCollectHandler
  10. {
  11. public IFont ProgressBarTimerFont { get; set; }
  12. public IFont ObjectiveProgressFont { get; set; }
  13.  
  14. public string ObjectiveProgressSymbol { get; set; }
  15. public string GuardianAliveSymbol { get; set; }
  16. public string GuardianDeadSymbol { get; set; }
  17.  
  18. public string SecondsFormat { get; set; }
  19. public string MinutesSecondsFormat { get; set; }
  20. public string ProgressPercentFormat { get; set; }
  21. public string ClosingSecondsFormat { get; set; }
  22.  
  23. public bool ShowClosingTimer { get; set; }
  24.  
  25. public bool IsGuardianAlive { get { return riftQuest.QuestStepId == 3 || riftQuest.QuestStepId == 16; } }
  26.  
  27. public bool IsGuardianDead
  28. {
  29. get
  30. {
  31. if (Hud.Game.Monsters.Any(m => m.Rarity == ActorRarity.Boss && !m.IsAlive))
  32. return true;
  33.  
  34. return riftQuest.QuestStepId == 5 || riftQuest.QuestStepId == 10 || riftQuest.QuestStepId == 34 || riftQuest.QuestStepId == 46;
  35. }
  36. }
  37.  
  38. public bool IsNephalemRift { get { return riftQuest.QuestStepId == 1 || riftQuest.QuestStepId == 3 || riftQuest.QuestStepId == 10; } }
  39. public bool IsGreaterRift { get { return riftQuest.QuestStepId == 13 || riftQuest.QuestStepId == 16 || riftQuest.QuestStepId == 34 || riftQuest.QuestStepId == 46; } }
  40.  
  41. private bool show
  42. {
  43. get
  44. {
  45. if (riftQuest == null) return false;
  46. if (riftQuest.State == QuestState.none) return false;
  47. if (Hud.Inventory.InventoryMainUiElement.Visible) return false;
  48.  
  49. return true;
  50. }
  51. }
  52.  
  53. private const uint riftClosingMilliseconds = 30000;
  54. private SpecialArea? currentRun;
  55.  
  56. private IQuest riftQuest
  57. {
  58. get
  59. {
  60. return Hud.Game.Quests.FirstOrDefault(q => q.SnoQuest.Sno == 337492) ?? // rift
  61. Hud.Game.Quests.FirstOrDefault(q => q.SnoQuest.Sno == 382695); // gr
  62. }
  63. }
  64.  
  65. private IUiElement uiProgressBar
  66. {
  67. get
  68. {
  69. return Hud.Render.GetUiElement(IsNephalemRift
  70. ? "Root.NormalLayer.eventtext_bkgrnd.eventtext_region.stackpanel.rift_wrapper.rift_container.rift_progress_bar" // rift
  71. : "Root.NormalLayer.eventtext_bkgrnd.eventtext_region.stackpanel.rift_wrapper.greater_rift_container.rift_progress_bar"); // gr
  72. }
  73. }
  74.  
  75. private IWatch riftTimer;
  76. private IWatch guardianTimer;
  77. private IWatch pauseTimer;
  78.  
  79. private readonly StringBuilder textBuilder;
  80.  
  81. public RiftTimerPlugin()
  82. {
  83. Enabled = true;
  84.  
  85. textBuilder = new StringBuilder();
  86. }
  87.  
  88. public override void Load(IController hud)
  89. {
  90. base.Load(hud);
  91.  
  92. ObjectiveProgressSymbol = "\u2694"; //?
  93. GuardianAliveSymbol = "\uD83D\uDC7F"; //??
  94. GuardianDeadSymbol = "\uD83D\uDC80"; //??
  95.  
  96. MinutesSecondsFormat = "{0:%m}:{0:ss}";
  97. SecondsFormat = "{0:%s}";
  98.  
  99. ProgressPercentFormat = "({0:F1}%)";
  100. ClosingSecondsFormat = "({0:%s})";
  101.  
  102. ProgressBarTimerFont = Hud.Render.CreateFont("tahoma", 7, 224, 255, 210, 150, true, false, false);
  103. ProgressBarTimerFont.SetShadowBrush(222, 0, 0, 0, true);
  104.  
  105. ObjectiveProgressFont = Hud.Render.CreateFont("tahoma", 8, 224, 240, 240, 240, false, false, false);
  106. ObjectiveProgressFont.SetShadowBrush(222, 0, 0, 0, true);
  107.  
  108. pauseTimer = Hud.Time.CreateWatch();
  109. riftTimer = Hud.Time.CreateWatch();
  110. guardianTimer = Hud.Time.CreateWatch();
  111. }
  112.  
  113. public void PaintTopInGame(ClipState clipState)
  114. {
  115. if (clipState != ClipState.BeforeClip) return;
  116. if (!show) return;
  117. if (currentRun == null)
  118. {
  119. currentRun = IsNephalemRift ? SpecialArea.Rift : SpecialArea.GreaterRift;
  120. }
  121.  
  122. if (IsNephalemRift && uiProgressBar.Visible)
  123. {
  124. var layout = ProgressBarTimerFont.GetTextLayout(GetText(true));
  125. var x = uiProgressBar.Rectangle.Left - layout.Metrics.Width / 2 + uiProgressBar.Rectangle.Width * (float)Hud.Game.RiftPercentage / 100.0f;
  126.  
  127. ProgressBarTimerFont.DrawText(layout, x, uiProgressBar.Rectangle.Bottom + Hud.Window.Size.Height * 0.015f);
  128. }
  129. else
  130. {
  131. var layout = ObjectiveProgressFont.GetTextLayout(GetText(false));
  132. var x = Hud.Render.MinimapUiElement.Rectangle.Right - layout.Metrics.Width - Hud.Window.Size.Height * 0.033f;
  133. var y = Hud.Render.MinimapUiElement.Rectangle.Bottom + Hud.Window.Size.Height * 0.0033f;
  134.  
  135. ObjectiveProgressFont.DrawText(layout, x, y);
  136. }
  137. }
  138.  
  139. public void AfterCollect()
  140. {
  141. // reset states if needed
  142. if (riftQuest == null || (riftQuest != null && riftQuest.State == QuestState.none))
  143. {
  144. if (riftTimer.IsRunning || riftTimer.ElapsedMilliseconds > 0)
  145. {
  146. riftTimer.Reset();
  147. }
  148. if (guardianTimer.IsRunning || guardianTimer.ElapsedMilliseconds > 0)
  149. {
  150. guardianTimer.Reset();
  151. }
  152. if (pauseTimer.IsRunning || pauseTimer.ElapsedMilliseconds > 0)
  153. {
  154. pauseTimer.Reset();
  155. }
  156.  
  157. currentRun = null;
  158.  
  159. return;
  160. }
  161.  
  162. // guardian
  163. if (IsGuardianAlive)
  164. {
  165. if (!guardianTimer.IsRunning)
  166. guardianTimer.Start();
  167. }
  168. else if (IsGuardianDead && guardianTimer.IsRunning)
  169. {
  170. guardianTimer.Stop();
  171. }
  172.  
  173. // game pause
  174. if (Hud.Game.IsPaused || (IsGreaterRift && Hud.Game.NumberOfPlayersInGame == 1 && Hud.Game.IsLoading))
  175. {
  176. if (!pauseTimer.IsRunning)
  177. pauseTimer.Start();
  178.  
  179. if (riftTimer.IsRunning)
  180. riftTimer.Stop();
  181.  
  182. if (guardianTimer.IsRunning)
  183. guardianTimer.Stop();
  184.  
  185. return;
  186. }
  187.  
  188. // (re)start/stop timers if needed
  189. if (!riftTimer.IsRunning && !IsGuardianDead)
  190. riftTimer.Start();
  191.  
  192. if (!guardianTimer.IsRunning && IsGuardianAlive)
  193. guardianTimer.Start();
  194.  
  195. if (pauseTimer.IsRunning)
  196. pauseTimer.Stop();
  197.  
  198. if (IsGreaterRift && IsGuardianDead && riftTimer.IsRunning)
  199. riftTimer.Stop();
  200.  
  201. if (IsNephalemRift && riftQuest.State == QuestState.completed && riftTimer.IsRunning)
  202. riftTimer.Stop();
  203. }
  204.  
  205. private string GetText(bool onlyTimer)
  206. {
  207. textBuilder.Clear();
  208.  
  209. if (!onlyTimer && !string.IsNullOrWhiteSpace(ObjectiveProgressSymbol))
  210. {
  211. textBuilder.Append(ObjectiveProgressSymbol);
  212. textBuilder.Append(" ");
  213. }
  214.  
  215. if (currentRun == SpecialArea.GreaterRift)
  216. {
  217. var timeSpan = TimeSpan.FromMilliseconds(riftTimer.ElapsedMilliseconds);
  218. textBuilder.AppendFormat(CultureInfo.InvariantCulture, (timeSpan.Minutes < 1) ? SecondsFormat : MinutesSecondsFormat, timeSpan);
  219. }
  220. else
  221. {
  222. var _timeSpan = TimeSpan.FromMilliseconds(riftQuest.StartedOn.ElapsedMilliseconds - riftQuest.CompletedOn.ElapsedMilliseconds - pauseTimer.ElapsedMilliseconds);
  223. textBuilder.AppendFormat(CultureInfo.InvariantCulture, (_timeSpan.Minutes < 1) ? SecondsFormat : MinutesSecondsFormat, _timeSpan);
  224. }
  225.  
  226. if (onlyTimer)
  227. return textBuilder.ToString();
  228.  
  229. if (Hud.Game.RiftPercentage < 100)
  230. {
  231. if ((IsNephalemRift || !uiProgressBar.Visible) && Hud.Game.RiftPercentage > 0.1)
  232. {
  233. textBuilder.Append(" ");
  234. textBuilder.AppendFormat(CultureInfo.InvariantCulture, ProgressPercentFormat, Hud.Game.RiftPercentage);
  235. }
  236. }
  237. else if (IsGuardianAlive || IsGuardianDead || !guardianTimer.IsRunning)
  238. {
  239. textBuilder.Append(" ");
  240. textBuilder.Append(IsGuardianAlive ? GuardianAliveSymbol : GuardianDeadSymbol);
  241.  
  242. var guardianTimeSpan = TimeSpan.FromMilliseconds(guardianTimer.ElapsedMilliseconds);
  243. textBuilder.Append(" ");
  244. textBuilder.AppendFormat(CultureInfo.InvariantCulture, (guardianTimeSpan.Minutes < 1) ? SecondsFormat : MinutesSecondsFormat, guardianTimeSpan);
  245. }
  246.  
  247. if (ShowClosingTimer && !IsGreaterRift && riftQuest.State == QuestState.completed)
  248. {
  249. textBuilder.Append(" ");
  250. textBuilder.AppendFormat(ClosingSecondsFormat, TimeSpan.FromMilliseconds(riftClosingMilliseconds - riftQuest.CompletedOn.ElapsedMilliseconds));
  251. }
  252.  
  253. return textBuilder.ToString();
  254. }
  255. }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement