Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.86 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.IO;
  6. using System.ComponentModel;
  7. using System.Windows.Forms;
  8. using System.Net;
  9.  
  10. namespace scoreboardManager
  11. {
  12. public partial class Startup : Window
  13. {
  14. private CheckDataUpdates checkDataUpdates = new CheckDataUpdates();
  15. private string settingsPath = "";
  16. private string teamDatabasePath = "";
  17. private string studioURL = "http://studio.cbtg.us/scoreman/login.php";
  18. private string userDocumentPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/scoreman/";
  19. //Username: MrTest Pass: Chrisdw1945!
  20. public Startup()
  21. {
  22. InitializeComponent();//XAML built in initalization
  23. AppInitalize();
  24. }
  25. private void AppInitalize()
  26. {
  27. //---------------Check connection
  28. if (CheckInternetConnection(studioURL) == false)
  29. return;
  30. //---------------Check if the directory exists
  31. if (Directory.Exists(userDocumentPath))
  32. settingsPath = userDocumentPath + "scoreman.ini";
  33. else
  34. {
  35. DirectoryInfo basePath = System.IO.Directory.CreateDirectory(userDocumentPath);
  36. settingsPath = basePath + "scoreman.ini";
  37. }
  38. //---------------Check if the scoreman.ini exists
  39. if (!File.Exists(settingsPath))
  40. {
  41. System.Windows.Forms.MessageBox.Show("Please Select a path containing the Team Database");
  42. using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
  43. {
  44. DialogResult folderBrowserDialogResult = folderBrowserDialog.ShowDialog();
  45.  
  46. if (folderBrowserDialogResult == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(folderBrowserDialog.SelectedPath))
  47. teamDatabasePath = folderBrowserDialog.SelectedPath;
  48. else
  49. System.Windows.Forms.MessageBox.Show("Team Data not found! Must be a root folder containing teamData and teamLogos, Configure this under the options menu.");
  50. }
  51. using (StreamWriter f = File.CreateText(settingsPath))
  52. {
  53. f.WriteLine(settingsPath);//path
  54. f.WriteLine(teamDatabasePath);//team path
  55. f.WriteLine("");//username
  56. f.WriteLine("");//password
  57. }
  58. }
  59. //---------------Read scoreman.ini
  60. using (StreamReader sr = new StreamReader(settingsPath))
  61. {
  62. string[] lines = { sr.ReadLine(), sr.ReadLine(), sr.ReadLine(), sr.ReadLine() };// 0 = settingsPath, 1 = teamDatabasePath, 2 = username, 3 = password
  63.  
  64. if (string.IsNullOrEmpty(lines[1]))
  65. System.Windows.MessageBox.Show("Team Data could not be found, please re-assign these paths");
  66.  
  67. if (lines[1] != " ")
  68. TeamData_Path_Input.Text = lines[1];
  69.  
  70. if (!String.IsNullOrEmpty(lines[2]) || !String.IsNullOrEmpty(lines[3]))
  71. {
  72. Username.Text = lines[2];
  73. Password.Text = lines[3];
  74. }
  75. }
  76. ApplicationLogIn(true);
  77. }
  78. private void ApplyChangesToINI()
  79. {
  80. using (StreamWriter writer = new StreamWriter(File.Open(settingsPath, FileMode.Truncate)))
  81. {
  82. writer.Flush();
  83. writer.WriteLine(settingsPath);
  84. writer.WriteLine(TeamData_Path_Input.Text);
  85. if (RememberLogin.IsChecked == true)
  86. {
  87. writer.WriteLine(Username.Text);
  88. writer.WriteLine(Password.Text);
  89. }
  90. writer.Close();
  91. }
  92. }
  93. private bool VerifyAccountOnServer(string url, string user, string pass, bool startingUp)
  94. {
  95. WebRequest request = WebRequest.Create(url);
  96. string postData = "user_name=" + user;
  97. postData += "&user_password=" + pass;
  98. byte[] data = Encoding.ASCII.GetBytes(postData);
  99.  
  100. request.Method = "POST";
  101. request.ContentType = "application/x-www-form-urlencoded";
  102. request.ContentLength = data.Length;
  103.  
  104. using (Stream stream = request.GetRequestStream())
  105. stream.Write(data, 0, data.Length);
  106.  
  107. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  108. string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  109.  
  110. if (responseString == "0")
  111. {
  112. if (!startingUp)
  113. System.Windows.Forms.MessageBox.Show("Missing Password");
  114. return false;
  115. }
  116. else if (responseString == "1")
  117. {
  118. if (!startingUp)
  119. System.Windows.Forms.MessageBox.Show("Missing Username");
  120. return false;
  121. }
  122. else if (responseString == "2")
  123. {
  124. if (!startingUp)
  125. System.Windows.Forms.MessageBox.Show("Wrong Password");
  126. return false;
  127. }
  128. else if (responseString == "3")
  129. {
  130. if (!startingUp)
  131. System.Windows.Forms.MessageBox.Show("Wrong Username");
  132. return false;
  133. }
  134. else if (responseString == "4")
  135. {
  136. if (!startingUp)
  137. System.Windows.Forms.MessageBox.Show("Database error, contact your administrator");
  138. return false;
  139. }
  140. else
  141. {
  142. if (!startingUp)
  143. System.Windows.Forms.MessageBox.Show("Login Sucessful");//sucess = parse out comments, turn into array ( 5, {username}, {apikey} )
  144. return true;
  145. }
  146. }
  147. private void ApplicationLogIn (bool startup)
  148. {
  149. ApplyChangesToINI();
  150. if (VerifyAccountOnServer(studioURL, Username.Text, Password.Text, startup))
  151. {
  152. Login_Barrier.Visibility = Visibility.Hidden;
  153. RememberLogin.IsChecked = true;
  154. LoginIdentifer.Content = "Connected";
  155. }
  156. else
  157. {
  158. LoginIdentifer.Content = "Login Failed";
  159. Login_Barrier.Visibility = Visibility.Visible;
  160. }
  161. }
  162. private static bool CheckInternetConnection(string url)
  163. {
  164. try
  165. {
  166. using (var client = new WebClient())
  167. {
  168. using (client.OpenRead(url))
  169. return true;
  170. }
  171. }
  172. catch
  173. {
  174. System.Windows.Forms.MessageBox.Show("Unable to establish connection with server, please check your internet connection.", "Error");
  175. return false;
  176. }
  177. }
  178. //------------LINK OUR FUNCTIONS TO XAML FUNCTIONS--------------
  179. protected override void OnClosing(CancelEventArgs e)
  180. {
  181. if(SportSelection.SelectedIndex == 0) //if there is no sport selected go ahead and close the app fully
  182. {
  183. DialogResult confirmation = System.Windows.Forms.MessageBox.Show("Are you sure you want to quit?", " Qutting", MessageBoxButtons.YesNo);
  184. if (confirmation == System.Windows.Forms.DialogResult.Yes)
  185. {
  186. System.Windows.Application.Current.Shutdown();
  187. e.Cancel = false;
  188. }
  189. else if (confirmation == System.Windows.Forms.DialogResult.No)
  190. e.Cancel = true;
  191. }
  192. }
  193. private void Browse_TeamData_Click(object sender, RoutedEventArgs e)
  194. {
  195. FolderBrowserDialog fb1 = new FolderBrowserDialog();
  196. DialogResult r1 = fb1.ShowDialog();
  197.  
  198. if (r1 == System.Windows.Forms.DialogResult.Cancel)
  199. TeamData_Path_Input.Text = TeamData_Path_Input.Text;
  200. else if (r1 == System.Windows.Forms.DialogResult.Yes)
  201. {
  202. TeamData_Path_Input.Text = fb1.SelectedPath;
  203. ApplyChangesToINI();
  204. }
  205. }
  206. private void CheckUpdates_Click(object sender, RoutedEventArgs e) { checkDataUpdates.Show(); }
  207. private void SportSelection_SelectionChanged(object sender, SelectionChangedEventArgs e) { MainWindow mw = new MainWindow(); mw.Show(); Close(); }
  208. private void LogOn_Click(object sender, RoutedEventArgs e) { ApplicationLogIn(false); }
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement