Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.IO;
- using System.Net;
- using System.Xml;
- namespace HackerplaceWidget
- {
- /// <summary>
- /// Interaktionslogik für MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- trayicon.BalloonTipText = "Das Widget wurde minimiert. Klicke hier um es wie anzuzeigen.";
- trayicon.BalloonTipTitle = "Hackerplace-Widget";
- trayicon.Text = "Hackerplace-Widget";
- trayicon.Icon = Properties.Resources.favicon;
- trayicon.Click += trayicon_Click;
- System.Threading.Thread t = new System.Threading.Thread(Start);
- t.Start();
- }
- void trayicon_Click(object sender, EventArgs e)
- {
- this.Show();
- window_state = WindowState;
- }
- System.Windows.Forms.NotifyIcon trayicon = new System.Windows.Forms.NotifyIcon();
- bool run = false;
- System.Timers.Timer tim;
- WindowState window_state = WindowState.Normal;
- private void Start()
- {
- tim = new System.Timers.Timer(HackerplaceWidget.Properties.Settings.Default.refreshinterval);
- tim.AutoReset = false;
- tim.Elapsed += t_Elapsed;
- tim.Start();
- }
- void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- tim.Stop();
- PerformRun();
- tim.Start();
- }
- private void PerformRun()
- {
- if (run)
- {
- if (FetchData() == false)
- {
- MessageBox.Show("Es ist ein Fehler aufgetreten. Bitte überprüfe deine Logindaten!");
- ShowSettings();
- }
- }
- }
- private void ShowSettings()
- {
- SettingsWindow sw = new SettingsWindow();
- sw.ShowDialog();
- tim.Interval = HackerplaceWidget.Properties.Settings.Default.refreshinterval;
- Topmost = Properties.Settings.Default.show_top;
- }
- private bool FetchData()
- {
- try
- {
- StringBuilder stb = new StringBuilder();
- stb.Append("http://hackerplace.de/gadget/gadget1_0.php");
- stb.Append("?username=");
- stb.Append(HackerplaceWidget.Properties.Settings.Default.username_md5);
- stb.Append("&password=");
- stb.Append(HackerplaceWidget.Properties.Settings.Default.password_and_pwstring_md5);
- stb.Append("&temp=");
- stb.Append(DateTime.Now.Millisecond);
- HttpWebRequest req = (HttpWebRequest)WebRequest.Create(stb.ToString());
- req.Method = WebRequestMethods.Http.Get;
- req.UserAgent = "HPWidget by Sarius Crawler";
- HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
- StreamReader str = new StreamReader(resp.GetResponseStream());
- string resp_string = str.ReadToEnd();
- str.Close();
- req = null;
- resp = null;
- stb = null;
- return ParseData(resp_string);
- }
- catch(TimeoutException te)
- {
- return false;
- }
- catch
- {
- return false;
- }
- }
- private bool ParseData(string resp_string)
- {
- try
- {
- XmlDocument xdoc = new XmlDocument();
- xdoc.LoadXml(resp_string);
- Dictionary<string, string> basic_informations = new Dictionary<string, string>();
- Dictionary<string, string> clan_informations = new Dictionary<string, string>();
- Dictionary<string, float> stock_informations = new Dictionary<string, float>();
- basic_informations.Add("Username", xdoc.GetElementsByTagName("username")[0].InnerText);
- basic_informations.Add("Place", xdoc.GetElementsByTagName("accountplace")[0].InnerText);
- basic_informations.Add("OldPlace", xdoc.GetElementsByTagName("accountoldplace")[0].InnerText);
- basic_informations.Add("Score", xdoc.GetElementsByTagName("accountscore")[0].InnerText);
- basic_informations.Add("MessageCount", xdoc.GetElementsByTagName("accountmessages")[0].InnerText);
- clan_informations.Add("ClanName", xdoc.GetElementsByTagName("clanname")[0].InnerText);
- clan_informations.Add("ClanPlace", xdoc.GetElementsByTagName("clanplace")[0].InnerText);
- clan_informations.Add("ClanScore", xdoc.GetElementsByTagName("clanscore")[0].InnerText);
- clan_informations.Add("ClanMembers", xdoc.GetElementsByTagName("clanusers")[0].InnerText);
- stock_informations.Add("StockPrice", Convert.ToInt32(xdoc.GetElementsByTagName("accountboersemoney")[0].InnerText.Replace("." , "")));
- stock_informations.Add("StockChange", Convert.ToSingle(xdoc.GetElementsByTagName("accountboerse")[0].InnerText.Replace("+" , "")));
- SetData(basic_informations, clan_informations, stock_informations);
- }
- catch
- {
- return false;
- }
- return true;
- }
- private void SetData(Dictionary<string, string> basic_informations, Dictionary<string, string> clan_informations, Dictionary<string, float> stock_informations)
- {
- this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(delegate() {
- username_lbl.Content = basic_informations["Username"];
- place_value_lbl.Content = basic_informations["Place"] + " / " + basic_informations["OldPlace"];
- score_value_lbl.Content = basic_informations["Score"];
- messages_value_lbl.Content = basic_informations["MessageCount"];
- clan_name_value_lbl.Content = clan_informations["ClanName"];
- clan_place_value_lbl.Content = clan_informations["ClanPlace"];
- clan_score_value_lbl.Content = clan_informations["ClanScore"];
- clan_members_value_lbl.Content = clan_informations["ClanMembers"];
- stock_price_value_lbl.Content = stock_informations["StockPrice"].ToString();
- stock_change_value_lbl.Content = stock_informations["StockChange"].ToString();
- }));
- CheckNotify(basic_informations, clan_informations, stock_informations);
- }
- private void CheckNotify(Dictionary<string, string> basic_informations, Dictionary<string, string> clan_informations, Dictionary<string, float> stock_informations)
- {
- if (HackerplaceWidget.Properties.Settings.Default.notify_below_percent_stock && HackerplaceWidget.Properties.Settings.Default.notify_below_percent_stock_value <= stock_informations["StockChange"])
- {
- MessageBox.Show("Die Börse hat sich um " + stock_informations["StockChange"].ToString() + "% verändert.");
- }
- if (HackerplaceWidget.Properties.Settings.Default.notify_over_percent_stock && HackerplaceWidget.Properties.Settings.Default.notify_over_percent_stock_value >= stock_informations["StockChange"])
- {
- MessageBox.Show("Die Börse hat sich um " + stock_informations["StockChange"].ToString() + "% verändert.");
- }
- if (HackerplaceWidget.Properties.Settings.Default.notify_below_stock && HackerplaceWidget.Properties.Settings.Default.notify_below_stock_value > stock_informations["StockPrice"])
- {
- MessageBox.Show("Die Börse ist bei " + stock_informations["StockPrice"].ToString() + " Credits/Aktie.");
- }
- if (HackerplaceWidget.Properties.Settings.Default.notify_over_stock && HackerplaceWidget.Properties.Settings.Default.notify_over_stock_value < stock_informations["StockPrice"])
- {
- MessageBox.Show("Die Börse ist bei " + stock_informations["StockPrice"].ToString() + " Credits/Aktie.");
- }
- if (HackerplaceWidget.Properties.Settings.Default.notify_new_message && 0 < Convert.ToInt32(basic_informations["MessageCount"]))
- {
- MessageBox.Show("Du hast " + basic_informations["MessageCount"] + " neue Nachricht(en).");
- }
- }
- private void open_settings_btn_Click(object sender, RoutedEventArgs e)
- {
- ShowSettings();
- }
- private void start_pause_btn_Click(object sender, RoutedEventArgs e)
- {
- run = !run;
- System.Threading.Thread t = new System.Threading.Thread(PerformRun);
- t.Start();
- }
- private void Window_Closed_1(object sender, EventArgs e)
- {
- trayicon.Dispose();
- trayicon = null;
- Environment.Exit(0);
- }
- private void Window_IsVisibleChanged_1(object sender, DependencyPropertyChangedEventArgs e)
- {
- ShowTrayIcon(!IsVisible);
- }
- private void Window_StateChanged_1(object sender, EventArgs e)
- {
- if (WindowState == WindowState.Minimized)
- {
- Hide();
- if (trayicon != null)
- trayicon.ShowBalloonTip(10000);
- }
- else
- window_state = WindowState;
- }
- void ShowTrayIcon(bool show)
- {
- if (trayicon != null)
- trayicon.Visible = show;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement