Guest User

xppentalty

a guest
Oct 23rd, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.32 KB | None | 0 0
  1. using PoeHUD.Controllers;
  2. using PoeHUD.Framework;
  3. using PoeHUD.Framework.Helpers;
  4. using PoeHUD.Hud.UI;
  5. using PoeHUD.Models;
  6. using PoeHUD.Poe.Components;
  7. using SharpDX;
  8. using SharpDX.Direct3D9;
  9. using System;
  10. using System.Windows.Forms;
  11.  
  12. namespace PoeHUD.Hud.XpRate
  13. {
  14.     public class XpRatePlugin :SizedPlugin<XpRateSettings>
  15.     {
  16.         private string xpRate, timeLeft;
  17.         private DateTime startTime, lastTime;
  18.         private long startXp;
  19.         private bool holdKey;
  20.  
  21.         public XpRatePlugin(GameController gameController, Graphics graphics, XpRateSettings settings)
  22.             : base(gameController, graphics, settings)
  23.         {
  24.             Reset();
  25.             GameController.Area.OnAreaChange += area => Reset();
  26.         }
  27.  
  28.         public override void Render()
  29.         {
  30.             base.Render();
  31.  
  32.             if (!holdKey && WinApi.IsKeyDown(Keys.F10))
  33.             {
  34.                 holdKey = true;
  35.                 Settings.Enable.Value = !Settings.Enable.Value;
  36.             }
  37.             else if (holdKey && !WinApi.IsKeyDown(Keys.F10))
  38.             {
  39.                 holdKey = false;
  40.             }
  41.  
  42.             if (!Settings.Enable || (GameController.Player != null && GameController.Player.GetComponent<Player>().Level >= 100))
  43.             {
  44.                 return;
  45.             }
  46.  
  47.             DateTime nowTime = DateTime.Now;
  48.             TimeSpan elapsedTime = nowTime - lastTime;
  49.             if (elapsedTime.TotalSeconds > 1)
  50.             {
  51.                 CalculateXp(nowTime);
  52.                 lastTime = nowTime;
  53.             }
  54.  
  55.             //this should be simplified or combined with xph
  56.             if (Settings.OnlyAreaName)
  57.             {
  58.                 var position = StartDrawPointFunc();
  59.                 string latency = $"( {GameController.Game.IngameState.CurLatency} )";
  60.                 string areaName = String.Concat(GameController.Area.CurrentArea.DisplayName, XPPenalty());
  61.                 var areaNameSize = Graphics.MeasureText(areaName, Settings.FontSize);
  62.                 float boxHeight = areaNameSize.Height;
  63.                 float boxWidth = MathHepler.Max(areaNameSize.Width);
  64.                 var bounds = new RectangleF(position.X - 72 - boxWidth, position.Y - 5, boxWidth + 80, boxHeight + 12);
  65.  
  66.                 Graphics.DrawText(areaName, Settings.FontSize, new Vector2(bounds.X + 74, position.Y), Settings.AreaFontColor);
  67.                 Graphics.DrawImage("preload-start.png", bounds, Settings.BackgroundColor);
  68.                 Graphics.DrawImage("preload-end.png", bounds, Settings.BackgroundColor);
  69.                 if (Settings.ShowLatency)
  70.                 {
  71.                     Graphics.DrawText(latency, Settings.FontSize, new Vector2(bounds.X + 25, position.Y), Settings.LatencyFontColor);
  72.                 }
  73.                 Size = bounds.Size;
  74.                 Margin = new Vector2(0, 5);
  75.             }
  76.  
  77.             if (!Settings.OnlyAreaName)
  78.             {
  79.                 Vector2 position = StartDrawPointFunc();
  80.                 string areaName = String.Concat(GameController.Area.CurrentArea.DisplayName, XPPenalty());
  81.                 Size2 areaNameSize = Graphics.DrawText(areaName, Settings.FontSize, position, Settings.AreaFontColor, FontDrawFlags.Right);
  82.                 Vector2 secondLine = position.Translate(0, areaNameSize.Height);
  83.                 Size2 xpRateSize = Graphics.DrawText(timeLeft, Settings.FontSize, secondLine, Settings.TimeLeftColor, FontDrawFlags.Right);
  84.                 Vector2 thirdLine = secondLine.Translate(0, xpRateSize.Height);
  85.                 Size2 xpLeftSize = Graphics.DrawText(xpRate, Settings.FontSize, thirdLine, Settings.TimeLeftColor, FontDrawFlags.Right);
  86.                 string timer = AreaInstance.GetTimeString(nowTime - GameController.Area.CurrentArea.TimeEntered);
  87.                 Size2 timerSize = Graphics.MeasureText(timer, Settings.FontSize);
  88.  
  89.                 float boxWidth = MathHepler.Max(xpRateSize.Width, xpLeftSize.Width, areaNameSize.Width + 85, timerSize.Width);
  90.                 float boxHeight = xpRateSize.Height + xpLeftSize.Height + areaNameSize.Height;
  91.                 var bounds = new RectangleF(position.X - boxWidth - 81, position.Y - 5, boxWidth + 90, boxHeight + 13);
  92.  
  93.                 string fps = $"ping ( {GameController.Game.IngameState.CurLatency} )";
  94.                 string ping = $"fps ( {GameController.Game.IngameState.CurFps} )";
  95.                 Size2 timeFpsSize = Graphics.MeasureText(fps, Settings.FontSize);
  96.                 var dif = bounds.Width - (12 + timeFpsSize.Width + xpRateSize.Width);
  97.                 if (dif < 0)
  98.                 {
  99.                     bounds.X += dif;
  100.                     bounds.Width -= dif;
  101.                 }
  102.                 Graphics.DrawText(fps, Settings.FontSize, new Vector2(bounds.X + 45, position.Y), Settings.FpsFontColor);
  103.                 Graphics.DrawText(timer, Settings.FontSize, new Vector2(bounds.X + 45, secondLine.Y), Settings.TimerFontColor);
  104.                 Graphics.DrawText(ping, Settings.FontSize, new Vector2(bounds.X + 45, thirdLine.Y), Settings.LatencyFontColor);
  105.                 Graphics.DrawImage("preload-start.png", bounds, Settings.BackgroundColor);
  106.                 Graphics.DrawImage("preload-end.png", bounds, Settings.BackgroundColor);
  107.                 Size = bounds.Size;
  108.                 Margin = new Vector2(0, 5);
  109.             }
  110.         }
  111.  
  112.         private string XPPenalty()
  113.         {
  114.             string percentage = "";
  115.             if (!GameController.Area.CurrentArea.IsTown && !GameController.Area.CurrentArea.Name.Contains("Hideout"))
  116.             {
  117.                 //based on xp penalty formula from http://pathofexile.gamepedia.com/Experience
  118.                 int arenaLevel = GameController.Area.CurrentArea.RealLevel;
  119.                 int characterLevel = GameController.Player.GetComponent<Player>().Level;
  120.                 double safeZone = Math.Floor(Convert.ToDouble(characterLevel)/16) + 3;
  121.                 double EffectiveDifference = Math.Max(Math.Abs(characterLevel - arenaLevel) - safeZone, 0);
  122.                 double XPMultiplier = Math.Max(Math.Pow((characterLevel + 5)/(characterLevel + 5 + Math.Pow(EffectiveDifference, 2.5)), 1.5), 0.01);
  123.                 percentage = String.Format(" XP: {0:P0}", XPMultiplier);
  124.             }
  125.             return percentage;
  126.         }
  127.  
  128.         private void CalculateXp(DateTime nowTime)
  129.         {
  130.             long currentXp = GameController.Player.GetComponent<Player>().XP;
  131.             double rate = (currentXp - startXp) / (nowTime - startTime).TotalHours;
  132.             xpRate = $"{ConvertHelper.ToShorten(rate, "0.00")} xp/h";
  133.             int level = GameController.Player.GetComponent<Player>().Level;
  134.             if (level >= 0 && level + 1 < Constants.PlayerXpLevels.Length && rate > 1)
  135.             {
  136.                 long xpLeft = Constants.PlayerXpLevels[level + 1] - currentXp;
  137.                 TimeSpan time = TimeSpan.FromHours(xpLeft / rate);
  138.                 timeLeft = $"{time.Hours}h {time.Minutes}m {time.Seconds}s to level up";
  139.             }
  140.         }
  141.  
  142.         private void Reset()
  143.         {
  144.             if (GameController.InGame)
  145.             {
  146.                 startXp = GameController.Player.GetComponent<Player>().XP;
  147.             }
  148.             startTime = lastTime = DateTime.Now;
  149.             xpRate = "0.00 xp/h";
  150.             timeLeft = "--h --m --s to level up";
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment