Advertisement
RulerOf

KIA with time formatting

Jun 8th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.34 KB | None | 0 0
  1. /*
  2.     Designed for general use, by fright01.
  3. */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Xml.Serialization;
  12.  
  13. using D3;
  14.  
  15. namespace info
  16. {
  17.     public class Program
  18.     {
  19.         static bool
  20.             /* Set true to enable -- false to disable */
  21.             /* User Display Options: */
  22.              GoldEarned = true
  23.             , GoldPerHour = true
  24.             , GoldPerRun = true
  25.             , CurrRunTime = true
  26.             , AvgRunTime = true
  27.             , TotalPlayTime = true
  28.             , NumRuns = true
  29.             , NumDeaths = true
  30.             /* End of User Display Options */;
  31.  
  32.         static Stopwatch timer = new Stopwatch();
  33.  
  34.         static long startTime = -1;
  35.         static long currTime = -1;
  36.         static long playTime = -1;
  37.         static long runStart = -1;
  38.         static long runEnd = -1;
  39.         static long runTime = -1;
  40.         static double avgRunTime = 0.0;
  41.         static int startGold = -1;
  42.         static int currGold = -1;
  43.         static int earnGold = -1;
  44.         static double goldPerHour = -1.0;
  45.         static double goldPerRun = -1.0;
  46.         static int deaths = 0;
  47.         static int runs = 0;
  48.  
  49.         static bool alive = false;
  50.         static bool inGame = false;
  51.  
  52.         public static void Main(string[] args)
  53.         {
  54.             timer.Start();
  55.             Game.Print("KnowItAll has been successfully loaded.");
  56.             Game.OnTickEvent += new TickEventHandler(Game_OnTickEvent);
  57.             Game.OnDrawEvent += new DrawEventHandler(Game_OnDrawEvent);
  58.         }
  59.  
  60.         private static void Game_OnTickEvent(EventArgs e)
  61.         {
  62.  
  63.             if (startTime == -1)
  64.                 startTime = timer.ElapsedMilliseconds;
  65.             currTime = timer.ElapsedMilliseconds;
  66.             playTime = currTime - startTime;
  67.  
  68.             if (Ingame() && Me.Gold > 0)
  69.             {
  70.                 if (startGold == -1)
  71.                     startGold = Me.Gold;
  72.                 currGold = Me.Gold;
  73.                 earnGold = currGold - startGold;
  74.                 goldPerHour = (double)earnGold / ((double)playTime / (double)3600000);
  75.             }
  76.  
  77.             if (Ingame() && Me.WorldId > 0 && Me.Life == 0 && alive)
  78.             {
  79.                 deaths++;
  80.                 alive = false;
  81.             }
  82.             else if (Ingame() && Me.WorldId > 0 && Me.Life != 0)
  83.             {
  84.                 alive = true;
  85.             }
  86.  
  87.             if (inGame && !Game.Ingame)
  88.             {
  89.                 avgRunTime = (avgRunTime * runs + runTime) / (runs + 1);
  90.                 runs++;
  91.                 goldPerRun = ((double)earnGold) / runs;
  92.                 inGame = false;
  93.             }
  94.             else if (!inGame && Game.Ingame)
  95.             {
  96.                 runStart = timer.ElapsedMilliseconds;
  97.                 runEnd = timer.ElapsedMilliseconds;
  98.                 inGame = true;
  99.             }
  100.             if (inGame)
  101.                 runEnd = timer.ElapsedMilliseconds;
  102.             runTime = runEnd - runStart;
  103.         }
  104.  
  105.         static void Game_OnDrawEvent(EventArgs e)
  106.         {
  107.             List<String> messages = new List<String>();
  108.  
  109.             if (earnGold > 0)
  110.             {
  111.                 if (GoldEarned)
  112.                     messages.Add("Gold Earned: " + earnGold.ToString("#,##0.##") + "{icon:gold}");
  113.                 if (GoldPerHour)
  114.                     messages.Add("Gold per Hour: " + goldPerHour.ToString("#,##0.##") + "{icon:gold}");
  115.             }
  116.  
  117.             if (earnGold > 0 && runs > 0)
  118.             {
  119.                 if (GoldPerRun)
  120.                     messages.Add("Gold per Run: " + goldPerRun.ToString("#,##0.##") + "{icon:gold}");
  121.             }
  122.  
  123.             if (runTime > 0)
  124.             {
  125.                 if (CurrRunTime)
  126.                     messages.Add("Current Run Time: " + FormatTimeMsec(runTime));
  127.             }
  128.  
  129.             if (runs > 0)
  130.             {
  131.                 if (AvgRunTime)
  132.                     messages.Add("Average Run Time: " + FormatTimeMsec(avgRunTime));
  133.                 if (NumRuns)
  134.                     messages.Add("Runs Completed: " + runs);
  135.             }
  136.  
  137.             if (TotalPlayTime)
  138.                 messages.Add("Total Play Time: " + FormatTimeMsec(playTime));
  139.  
  140.             if (NumDeaths)
  141.                 messages.Add("Died: " + deaths);
  142.  
  143.             DrawMessages(messages);
  144.         }
  145.  
  146.         static string FormatTimeMsec(double _timeSpan)
  147.         {
  148.             return String.Format("{0}.{1}:{2}:{3}", TimeSpan.FromMilliseconds(_timeSpan).Days, TimeSpan.FromMilliseconds(_timeSpan).Hours.ToString("D2"), TimeSpan.FromMilliseconds(_timeSpan).Minutes.ToString("D2"), TimeSpan.FromMilliseconds(_timeSpan).Seconds.ToString("D2"));
  149.         }
  150.  
  151.         static void DrawMessages(List<String> messages)
  152.         {
  153.             float x = 30.0f;
  154.             float y = 5.0f;
  155.             uint fontId = 0x16A;
  156.             uint fontSize = 0x12;
  157.             uint fontColor = 0xFFFFFFFF;
  158.  
  159.             foreach (String str in messages)
  160.             {
  161.                 Draw.DrawText(x, y, fontId, fontSize, fontColor, str);
  162.                 y += fontSize;
  163.             }
  164.         }
  165.  
  166.         static bool Ingame()
  167.         {
  168.             return Game.Ingame && Me.LevelArea.ToString() != "Axe_Bad_Data";
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement