Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.IO;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Net;
- namespace scoreboardManager
- {
- public partial class Startup : Window
- {
- private CheckDataUpdates checkDataUpdates = new CheckDataUpdates();
- private string settingsPath = "";
- private string teamDatabasePath = "";
- private string studioURL = "http://studio.cbtg.us/scoreman/login.php";
- private string userDocumentPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/scoreman/";
- //Username: MrTest Pass: Chrisdw1945!
- public Startup()
- {
- InitializeComponent();//XAML built in initalization
- AppInitalize();
- }
- private void AppInitalize()
- {
- //---------------Check connection
- if (CheckInternetConnection(studioURL) == false)
- return;
- //---------------Check if the directory exists
- if (Directory.Exists(userDocumentPath))
- settingsPath = userDocumentPath + "scoreman.ini";
- else
- {
- DirectoryInfo basePath = System.IO.Directory.CreateDirectory(userDocumentPath);
- settingsPath = basePath + "scoreman.ini";
- }
- //---------------Check if the scoreman.ini exists
- if (!File.Exists(settingsPath))
- {
- System.Windows.Forms.MessageBox.Show("Please Select a path containing the Team Database");
- using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
- {
- DialogResult folderBrowserDialogResult = folderBrowserDialog.ShowDialog();
- if (folderBrowserDialogResult == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(folderBrowserDialog.SelectedPath))
- teamDatabasePath = folderBrowserDialog.SelectedPath;
- else
- System.Windows.Forms.MessageBox.Show("Team Data not found! Must be a root folder containing teamData and teamLogos, Configure this under the options menu.");
- }
- using (StreamWriter f = File.CreateText(settingsPath))
- {
- f.WriteLine(settingsPath);//path
- f.WriteLine(teamDatabasePath);//team path
- f.WriteLine(".");//username
- f.WriteLine(".");//password
- }
- }
- //---------------Read scoreman.ini
- string[] lines = File.ReadAllLines(settingsPath);// 0 = settingsPath, 1 = teamDatabasePath, 2 = username, 3 = password
- if (string.IsNullOrEmpty(lines[1]))
- System.Windows.MessageBox.Show("Team Data could not be found, please re-assign these paths");
- if (lines[1] != " ")
- TeamData_Path_Input.Text = lines[1];
- if(lines.Length > 2)
- {
- if (lines[2] != "." || lines[3] != ".")
- {
- Username.Text = lines[2];
- Password.Text = lines[3];
- }
- }
- ApplicationLogIn(true);
- }
- private void ApplyChangesToINI()
- {
- using (StreamWriter writer = new StreamWriter(File.Open(settingsPath, FileMode.Truncate)))
- {
- writer.Flush();
- writer.WriteLine(settingsPath);
- writer.WriteLine(TeamData_Path_Input.Text);
- if (RememberLogin.IsChecked == true)
- {
- writer.WriteLine(Username.Text);
- writer.WriteLine(Password.Text);
- }
- writer.Close();
- }
- }
- private bool VerifyAccountOnServer(string url, string user, string pass, bool startingUp)
- {
- WebRequest request = WebRequest.Create(url);
- string postData = "user_name=" + user;
- postData += "&user_password=" + pass;
- byte[] data = Encoding.ASCII.GetBytes(postData);
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = data.Length;
- using (Stream stream = request.GetRequestStream())
- stream.Write(data, 0, data.Length);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
- if (responseString == "0")
- {
- if (!startingUp)
- System.Windows.Forms.MessageBox.Show("Missing Password");
- return false;
- }
- else if (responseString == "1")
- {
- if (!startingUp)
- System.Windows.Forms.MessageBox.Show("Missing Username");
- return false;
- }
- else if (responseString == "2")
- {
- if (!startingUp)
- System.Windows.Forms.MessageBox.Show("Wrong Password");
- return false;
- }
- else if (responseString == "3")
- {
- if (!startingUp)
- System.Windows.Forms.MessageBox.Show("Wrong Username");
- return false;
- }
- else if (responseString == "4")
- {
- if (!startingUp)
- System.Windows.Forms.MessageBox.Show("Database error, contact your administrator");
- return false;
- }
- else
- {
- if (!startingUp)
- System.Windows.Forms.MessageBox.Show("Login Sucessful");//sucess = parse out comments, turn into array ( 5, {username}, {apikey} )
- return true;
- }
- }
- private void ApplicationLogIn (bool startup)
- {
- ApplyChangesToINI();
- if (VerifyAccountOnServer(studioURL, Username.Text, Password.Text, startup))
- {
- Login_Barrier.Visibility = Visibility.Hidden;
- RememberLogin.IsChecked = true;
- LoginIdentifer.Content = "Connected";
- }
- else
- {
- LoginIdentifer.Content = "Login Failed";
- Login_Barrier.Visibility = Visibility.Visible;
- }
- }
- private static bool CheckInternetConnection(string url)
- {
- try
- {
- using (var client = new WebClient())
- {
- using (client.OpenRead(url))
- return true;
- }
- }
- catch
- {
- System.Windows.Forms.MessageBox.Show("Unable to establish connection with server, please check your internet connection.", "Error");
- return false;
- }
- }
- //------------LINK OUR FUNCTIONS TO XAML FUNCTIONS--------------
- private void Browse_TeamData_Click(object sender, RoutedEventArgs e)
- {
- FolderBrowserDialog fb1 = new FolderBrowserDialog();
- DialogResult r1 = fb1.ShowDialog();
- if (r1 == System.Windows.Forms.DialogResult.Cancel)
- TeamData_Path_Input.Text = TeamData_Path_Input.Text;
- else if (r1 == System.Windows.Forms.DialogResult.OK)
- {
- TeamData_Path_Input.Text = fb1.SelectedPath;
- ApplyChangesToINI();
- }
- }
- private void CheckUpdates_Click(object sender, RoutedEventArgs e) { checkDataUpdates.Show(); }
- private void SportSelection_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- ApplyChangesToINI();
- if (SportSelection.SelectedIndex == 0)
- {
- Baseball bb = new Baseball();
- bb.Show();
- Close();
- }
- else if(SportSelection.SelectedIndex == 2)
- {
- Football fb = new Football();
- fb.Show();
- Close();
- }
- else if (SportSelection.SelectedIndex == 3)
- {
- Soccer s = new Soccer();
- s.Show();
- Close();
- }
- }
- private void LogOn_Click(object sender, RoutedEventArgs e) { ApplicationLogIn(false); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement