Advertisement
Guest User

LoL Ping Source Code

a guest
Jun 9th, 2013
15,524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.NetworkInformation;
  7. using System.Reflection;
  8. using System.Timers;
  9. using System.Windows.Forms;
  10. using Microsoft.Win32;
  11.  
  12. namespace LoLPing
  13. {
  14.     internal class Program : Form
  15.     {
  16.         private static void Main(string[] args)
  17.         {
  18.             Application.Run(new Program());
  19.         }
  20.  
  21.         private NotifyIcon tray;
  22.         private ContextMenu menu;
  23.         private Dictionary<string, string> serverOptions = new Dictionary<string, string>() { { "NA", "riot.ca" }, { "EUW", "riot.de" }, { "EUNE", "riot.de" } };
  24.         private int[] alertOptions = new int[] { 100, 200, 300, 400, 500 };
  25.         private MenuItem[] servers;
  26.         private MenuItem[] alerts;
  27.         private System.Timers.Timer timer;
  28.         private bool runWithWindows;
  29.         private RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  30.  
  31.         public Program()
  32.         {
  33.             tray = new NotifyIcon();
  34.             tray.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
  35.             runWithWindows = (rk.GetValue("LoLPing") != null) ? true : false;
  36.  
  37.             menu = new System.Windows.Forms.ContextMenu();
  38.             menu.MenuItems.Add("Ping Now", new EventHandler(pingNowHandler));
  39.             menu.MenuItems.Add("Start with Windows", new EventHandler(startWithWindows));
  40.             menu.MenuItems[1].Checked = runWithWindows;
  41.  
  42.             servers = new MenuItem[serverOptions.Count];
  43.             string defaultServer = Properties.Settings.Default.Server;
  44.             for (int i = 0; i < serverOptions.Count; i++)
  45.             {
  46.                 servers[i] = new MenuItem(serverOptions.Keys.ToArray()[i], new EventHandler(setServer));
  47.                 servers[i].RadioCheck = true;
  48.                 if (defaultServer == serverOptions.Keys.ToArray()[i]) servers[i].Checked = true;
  49.             }
  50.             menu.MenuItems.Add("Server", servers);
  51.  
  52.             alerts = new MenuItem[alertOptions.Length];
  53.             for (int i = 0; i < alertOptions.Length; i++)
  54.             {
  55.                 alerts[i] = new MenuItem(alertOptions[i].ToString(), new EventHandler(setAlert));
  56.                 alerts[i].RadioCheck = true;
  57.                 if (Properties.Settings.Default.AlertWhen == alertOptions[i]) alerts[i].Checked = true;
  58.             }
  59.             menu.MenuItems.Add("Alert", alerts);
  60.             menu.MenuItems.Add("Close app", new EventHandler(Exit));
  61.  
  62.             tray.ContextMenu = menu;
  63.             tray.Visible = true;
  64.             timer = new System.Timers.Timer(60000);
  65.             timer.Elapsed += new ElapsedEventHandler(checkPing);
  66.             timer.Start();
  67.             tray.ShowBalloonTip(3000, "LoLPing", "LoLPing is now running", ToolTipIcon.None);
  68.         }
  69.  
  70.         protected override void OnLoad(EventArgs e)
  71.         {
  72.             Visible = false;
  73.             ShowInTaskbar = false;
  74.             base.OnLoad(e);
  75.         }
  76.  
  77.         private void pingNowHandler(object sender, EventArgs e)
  78.         {
  79.             pingNow();
  80.         }
  81.  
  82.         private void pingNow()
  83.         {
  84.             Ping ping = new Ping();
  85.             PingOptions opt = new PingOptions(128, true);
  86.             byte[] buffer = new byte[32];
  87.             string srv = Properties.Settings.Default.Server;
  88.             string loldns = serverOptions[srv];
  89.             IPAddress ipa = Dns.GetHostAddresses(loldns).First();
  90.             PingReply rpl = ping.Send(ipa, 1000, buffer, opt);
  91.             switch (rpl.Status)
  92.             {
  93.                 case IPStatus.Success: showBalloon("Your ping to the " + srv + "-server is  " + rpl.RoundtripTime + "ms"); break;
  94.                 case IPStatus.TimedOut: showBalloon("Connection to the " + srv + "-server has timed out - You should NOT start a game now"); break;
  95.                 default: showBalloon("Ping failed: " + rpl.Status.ToString()); break;
  96.             }
  97.         }
  98.  
  99.         private void checkPing(object sender, ElapsedEventArgs e)
  100.         {
  101.             Ping ping = new Ping();
  102.             PingOptions opt = new PingOptions(128, true);
  103.             byte[] buffer = new byte[32];
  104.             string srv = Properties.Settings.Default.Server;
  105.             string loldns = serverOptions[srv];
  106.             IPAddress ipa = Dns.GetHostAddresses(loldns).First();
  107.             PingReply rpl = ping.Send(ipa, 1000, buffer, opt);
  108.             switch (rpl.Status)
  109.             {
  110.                 case IPStatus.Success:
  111.                     if (rpl.RoundtripTime > Properties.Settings.Default.AlertWhen)
  112.                         showBalloon("Your ping to the " + srv + "-server is  " + rpl.RoundtripTime + "ms");
  113.                     break;
  114.  
  115.                 case IPStatus.TimedOut: showBalloon("Connection to the " + srv + "-server has timed out - You should NOT start a game now"); break;
  116.                 default: showBalloon("Ping failed: " + rpl.Status.ToString()); break;
  117.             }
  118.         }
  119.  
  120.         private void showBalloon(string txt)
  121.         {
  122.             tray.ShowBalloonTip(2000, "LoLPing", txt, ToolTipIcon.None);
  123.         }
  124.  
  125.         private void setAlert(object sender, EventArgs e)
  126.         {
  127.             MenuItem mi = (MenuItem)sender;
  128.             foreach (MenuItem tmpmi in alerts) tmpmi.Checked = false;
  129.             mi.Checked = true;
  130.             Properties.Settings.Default.AlertWhen = int.Parse(mi.Text);
  131.         }
  132.  
  133.         private void setServer(object sender, EventArgs e)
  134.         {
  135.             MenuItem mi = (MenuItem)sender;
  136.             foreach (MenuItem tmpmi in servers) tmpmi.Checked = false;
  137.             mi.Checked = true;
  138.             Properties.Settings.Default.Server = mi.Text;
  139.             pingNow();
  140.         }
  141.  
  142.         private void startWithWindows(object sender, EventArgs e)
  143.         {
  144.             runWithWindows = (runWithWindows) ? false : true;
  145.             if (runWithWindows) rk.SetValue("LoLPing", "\"" + Application.ExecutablePath.ToString() + "\""); else rk.DeleteValue("LoLPing");
  146.             menu.MenuItems[1].Checked = runWithWindows;
  147.         }
  148.  
  149.         private void Exit(object sender, EventArgs e)
  150.         {
  151.             Application.Exit();
  152.         }
  153.  
  154.         protected override void Dispose(bool isDisposing)
  155.         {
  156.             if (isDisposing) tray.Dispose();
  157.             base.Dispose(isDisposing);
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement