Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 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. string[] lines = File.ReadAllLines(settingsPath);// 0 = settingsPath, 1 = teamDatabasePath, 2 = username, 3 = password
  61.  
  62. if (string.IsNullOrEmpty(lines[1]))
  63. System.Windows.MessageBox.Show("Team Data could not be found, please re-assign these paths");
  64.  
  65. if (lines[1] != " ")
  66. TeamData_Path_Input.Text = lines[1];
  67.  
  68. if(lines.Length > 2)
  69. {
  70. if (lines[2] != "." || lines[3] != ".")
  71. {
  72. Username.Text = lines[2];
  73. Password.Text = lines[3];
  74. }
  75. }
  76.  
  77. ApplicationLogIn(true);
  78. }
  79. private void ApplyChangesToINI()
  80. {
  81. using (StreamWriter writer = new StreamWriter(File.Open(settingsPath, FileMode.Truncate)))
  82. {
  83. writer.Flush();
  84. writer.WriteLine(settingsPath);
  85. writer.WriteLine(TeamData_Path_Input.Text);
  86. if (RememberLogin.IsChecked == true)
  87. {
  88. writer.WriteLine(Username.Text);
  89. writer.WriteLine(Password.Text);
  90. }
  91. writer.Close();
  92. }
  93. }
  94. private bool VerifyAccountOnServer(string url, string user, string pass, bool startingUp)
  95. {
  96. WebRequest request = WebRequest.Create(url);
  97. string postData = "user_name=" + user;
  98. postData += "&user_password=" + pass;
  99. byte[] data = Encoding.ASCII.GetBytes(postData);
  100.  
  101. request.Method = "POST";
  102. request.ContentType = "application/x-www-form-urlencoded";
  103. request.ContentLength = data.Length;
  104.  
  105. using (Stream stream = request.GetRequestStream())
  106. stream.Write(data, 0, data.Length);
  107.  
  108. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  109. string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  110.  
  111. if (responseString == "0")
  112. {
  113. if (!startingUp)
  114. System.Windows.Forms.MessageBox.Show("Missing Password");
  115. return false;
  116. }
  117. else if (responseString == "1")
  118. {
  119. if (!startingUp)
  120. System.Windows.Forms.MessageBox.Show("Missing Username");
  121. return false;
  122. }
  123. else if (responseString == "2")
  124. {
  125. if (!startingUp)
  126. System.Windows.Forms.MessageBox.Show("Wrong Password");
  127. return false;
  128. }
  129. else if (responseString == "3")
  130. {
  131. if (!startingUp)
  132. System.Windows.Forms.MessageBox.Show("Wrong Username");
  133. return false;
  134. }
  135. else if (responseString == "4")
  136. {
  137. if (!startingUp)
  138. System.Windows.Forms.MessageBox.Show("Database error, contact your administrator");
  139. return false;
  140. }
  141. else
  142. {
  143. if (!startingUp)
  144. System.Windows.Forms.MessageBox.Show("Login Sucessful");//sucess = parse out comments, turn into array ( 5, {username}, {apikey} )
  145. return true;
  146. }
  147. }
  148. private void ApplicationLogIn (bool startup)
  149. {
  150. ApplyChangesToINI();
  151. if (VerifyAccountOnServer(studioURL, Username.Text, Password.Text, startup))
  152. {
  153. Login_Barrier.Visibility = Visibility.Hidden;
  154. RememberLogin.IsChecked = true;
  155. LoginIdentifer.Content = "Connected";
  156. }
  157. else
  158. {
  159. LoginIdentifer.Content = "Login Failed";
  160. Login_Barrier.Visibility = Visibility.Visible;
  161. }
  162. }
  163. private static bool CheckInternetConnection(string url)
  164. {
  165. try
  166. {
  167. using (var client = new WebClient())
  168. {
  169. using (client.OpenRead(url))
  170. return true;
  171. }
  172. }
  173. catch
  174. {
  175. System.Windows.Forms.MessageBox.Show("Unable to establish connection with server, please check your internet connection.", "Error");
  176. return false;
  177. }
  178. }
  179. //------------LINK OUR FUNCTIONS TO XAML FUNCTIONS--------------
  180. private void Browse_TeamData_Click(object sender, RoutedEventArgs e)
  181. {
  182. FolderBrowserDialog fb1 = new FolderBrowserDialog();
  183. DialogResult r1 = fb1.ShowDialog();
  184.  
  185. if (r1 == System.Windows.Forms.DialogResult.Cancel)
  186. TeamData_Path_Input.Text = TeamData_Path_Input.Text;
  187. else if (r1 == System.Windows.Forms.DialogResult.OK)
  188. {
  189. TeamData_Path_Input.Text = fb1.SelectedPath;
  190. ApplyChangesToINI();
  191. }
  192. }
  193. private void CheckUpdates_Click(object sender, RoutedEventArgs e) { checkDataUpdates.Show(); }
  194. private void SportSelection_SelectionChanged(object sender, SelectionChangedEventArgs e)
  195. {
  196. ApplyChangesToINI();
  197. if (SportSelection.SelectedIndex == 0)
  198. {
  199. Baseball bb = new Baseball();
  200. bb.Show();
  201. Close();
  202. }
  203. else if(SportSelection.SelectedIndex == 2)
  204. {
  205. Football fb = new Football();
  206. fb.Show();
  207. Close();
  208. }
  209. else if (SportSelection.SelectedIndex == 3)
  210. {
  211. Soccer s = new Soccer();
  212. s.Show();
  213. Close();
  214. }
  215. }
  216. private void LogOn_Click(object sender, RoutedEventArgs e) { ApplicationLogIn(false); }
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement