Advertisement
rajlu

Untitled

Sep 8th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Net;
  12. using MySql.Data.MySqlClient;
  13. using System.Web;
  14. using System.Text.RegularExpressions;
  15. using System.Diagnostics;
  16. using System.Runtime.InteropServices;
  17.  
  18. namespace csgopicker_main
  19. {
  20.  
  21. public partial class Form1 : Form
  22. {
  23.  
  24. //This function returns true if the emailid matches the pattern. Else it will return false
  25. private bool IsValidEmailId(string InputEmail)
  26. {
  27. //Regex To validate Email Address
  28. Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
  29. Match match = regex.Match(InputEmail);
  30. if (match.Success)
  31. return true;
  32. else
  33. return false;
  34. }
  35.  
  36. //Internet conn
  37. public bool CheckForInternetConnection()
  38. {
  39. try
  40. {
  41. using (var client = new WebClient())
  42. using (var stream = client.OpenRead("http://www.google.com"))
  43. label_internet.Text = "Internet: online";
  44. label_internet.ForeColor = System.Drawing.Color.Green;
  45. label_internet.Visible = true;
  46. {
  47. return true;
  48. }
  49. }
  50. catch
  51. {
  52. label_internet.Text = "Internet: offline";
  53. label_internet.ForeColor = System.Drawing.Color.Green;
  54. label_internet.Visible = true;
  55. database_conn.Stop();
  56. MessageBox.Show("Please check your internet connection. Application will be closed.");
  57. Application.Exit();
  58. return false;
  59. }
  60. }
  61. //MySQL conn
  62. public bool MySqlConnection()
  63. {
  64. try
  65. {
  66.  
  67. string myConnection = "datasource=localhost;port=3306;username=root;password=root19721999";
  68. MySqlConnection myConn = new MySqlConnection(myConnection);
  69. MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
  70. myDataAdapter.SelectCommand = new MySqlCommand(" select * csgopicker_main.users ;", myConn);
  71. MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
  72. myConn.Open();
  73. DataSet ds = new DataSet();
  74. label_database.Text = "Database: online";
  75. label_database.ForeColor = System.Drawing.Color.Green;
  76. label_database.Visible = true;
  77. database_conn.Stop();
  78. myConn.Close();
  79. return true;
  80. }
  81. catch (Exception)
  82. {
  83. label_database.Text = "Database: offline";
  84. label_database.ForeColor = System.Drawing.Color.Red;
  85. database_conn.Stop();
  86. return false;
  87. }
  88. }
  89. //Website conn
  90. public bool WebsiteConnection()
  91. {
  92. try
  93. {
  94. label_website.Text = "Website: offline";
  95. label_website.ForeColor = System.Drawing.Color.Red;
  96. return true;
  97. }
  98. catch
  99. {
  100. return false;
  101. }
  102. }
  103. //your ip
  104. public string GetPublicIP()
  105. {
  106. WebClient client = new WebClient();
  107. return client.DownloadString("http://icanhazip.com/");
  108. }
  109. //Timer do sprawdzania łączności
  110. private void timer_conn(object sender, EventArgs e)
  111. {
  112. Properties.Settings.Default.ip = GetPublicIP();
  113. label5.Text = "Your IP address: " + Properties.Settings.Default.ip;
  114. //internet
  115. CheckForInternetConnection();
  116. //MySql
  117. MySqlConnection();
  118. //website
  119. WebsiteConnection();
  120.  
  121. //eneble form
  122. Enabled = true;
  123. }
  124.  
  125. public Form1()
  126. {
  127. InitializeComponent();
  128.  
  129. }
  130.  
  131. private void Form1_Load(object sender, EventArgs e)
  132. {
  133.  
  134. //Wykrywanie internetu
  135. database_conn.Start();
  136.  
  137. textbox_email.Text = Properties.Settings.Default.lastemail;
  138. textbox_password.Text = Properties.Settings.Default.lastpassword;
  139.  
  140. using (var conn = new MySqlConnection("datasource=localhost;port=3306;username=root;password=root19721999"))
  141. {
  142. try
  143. {
  144. conn.Open();
  145. using (var cmd = new MySqlCommand("SELECT COUNT(*) FROM csgopicker_main.users", conn))
  146. {
  147. int count = Convert.ToInt32(cmd.ExecuteScalar());
  148. string co = count.ToString();
  149. labelnumberusersregister.Text = "Users registered: " + co;
  150. }
  151. }
  152. catch (Exception ex)
  153. {
  154. MessageBox.Show(ex.Message);
  155. labelnumberusersregister.Text = "OFFLINE";
  156. }
  157.  
  158.  
  159. }
  160.  
  161. }
  162.  
  163. //register button i schematy
  164. private void button_register_Click_1(object sender, EventArgs e)
  165. {
  166. //sprawdzanie e-maila
  167. if (IsValidEmailId(textbox_email_register.Text))
  168. {
  169. //sprawdzanie poprawności hasła
  170. if (textbox_password_register.Text == textbox_password_re_register.Text)
  171. {
  172. //sprawdzanie czy zostały zaznaczone terms
  173. if (checkterms.Checked == true)
  174. {
  175. //ID gen
  176. string NewGUID = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper();
  177. //connection
  178. string myConnection = "datasource=localhost;port=3306;username=root;password=root19721999";
  179. string Query = "insert into csgopicker_main.users set id='" + NewGUID + "', email='" + this.textbox_email_register.Text + "', password='" + this.textbox_password_re_register.Text + "' ;";
  180. MySqlConnection conDataBase = new MySqlConnection(myConnection);
  181. MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
  182. MySqlDataReader myReader;
  183. try
  184. {
  185. conDataBase.Open();
  186. myReader = cmdDataBase.ExecuteReader();
  187. MessageBox.Show("Thanks for register. You can now login using your email and password!");
  188. conDataBase.Close();
  189. }
  190. catch (Exception ex)
  191. {
  192. MessageBox.Show(ex.Message);
  193. }
  194. }
  195. else { MessageBox.Show("Error: u didn't accept terms"); }
  196.  
  197. }
  198. else { MessageBox.Show("Error: wrong repassword"); }
  199. }
  200. else
  201. {
  202. MessageBox.Show(textbox_email_register.Text + " is not valid email");
  203. }
  204.  
  205.  
  206. }
  207.  
  208. //login (do zrobienia: potwierdzenie o zmianie IP, status online i offline w MySql)
  209. private void button_login_Click(object sender, EventArgs e)
  210. {
  211. //łączność
  212. database_conn.Start();
  213. //login
  214. try
  215. {
  216. Properties.Settings.Default.lastemail = textbox_email.Text;
  217. Properties.Settings.Default.lastpassword = textbox_password.Text;
  218. Properties.Settings.Default.Save();
  219. textbox_password.Text = Properties.Settings.Default.lastpassword;
  220. string myConnection = "datasource=localhost;port=3306;username=root;password=root19721999";
  221. MySqlDataReader myReader;
  222. MySqlConnection myConn = new MySqlConnection(myConnection);
  223.  
  224. MySqlCommand SelectCommand = new MySqlCommand("select * from csgopicker_main.users where email = '" + textbox_email.Text + "' and password = '" + textbox_password.Text + "';", myConn);
  225.  
  226.  
  227. myConn.Open();
  228. myReader = SelectCommand.ExecuteReader();
  229. int count = 0;
  230. while (myReader.Read())
  231. {
  232. count = count + 1;
  233. }
  234. if (count == 1)
  235. {
  236. MessageBox.Show("Login");
  237. myConn.Close();
  238.  
  239. MySqlConnection conn = null;
  240. MySqlCommand cmd = null;
  241. MySqlDataReader reader = null;
  242.  
  243. try
  244. {
  245. Form1 form1 = new Form1();
  246.  
  247. string sql = "SELECT cfgstart FROM csgopicker_main.users WHERE email = '" + Properties.Settings.Default.lastemail + "'";
  248.  
  249. conn = new MySqlConnection(myConnection);
  250. cmd = new MySqlCommand(sql, conn);
  251.  
  252. conn.Open();
  253.  
  254. reader = cmd.ExecuteReader();
  255.  
  256. while (reader.Read())
  257. {
  258. Properties.Settings.Default.cfgstart = reader.GetString("cfgstart");
  259. }
  260. }
  261. catch (Exception ex)
  262. {
  263. MessageBox.Show(string.Format("An error occurred {0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  264. }
  265. finally
  266. {
  267. if (reader != null) reader.Close();
  268. if (conn != null) conn.Close();
  269. if (Properties.Settings.Default.cfgstart == "0")
  270. {
  271. this.Hide();
  272. var form2 = new cfgstart_form();
  273. form2.Closed += (s, args) => this.Close();
  274. form2.Show();
  275. myConn.Close();
  276. }
  277. else
  278. {
  279. MessageBox.Show("CFGstart done");
  280. this.Hide();
  281. var form2 = new MainForm();
  282. form2.Closed += (s, args) => this.Close();
  283. form2.Show();
  284. myConn.Close();
  285. }
  286. }
  287. }
  288. else if (count > 1)
  289. {
  290. MessageBox.Show("Access denied");
  291. }
  292. else
  293. MessageBox.Show("Not correct");
  294. myConn.Close();
  295. }
  296. catch (Exception ex) { MessageBox.Show(ex.Message); }
  297. }
  298. //guest
  299. private void button_guest_Click(object sender, EventArgs e)
  300. {
  301. //czas i data
  302. DateTime time = DateTime.Now;
  303. string format = "yyyy-MM-dd HH:mm:ss";
  304. var mytime = time.ToString(format);
  305.  
  306. //connection
  307.  
  308. Stopwatch s = new Stopwatch();
  309. s.Start();
  310. while (s.Elapsed < TimeSpan.FromSeconds(5))
  311. {
  312. label4.Text = "Wait";
  313. Random rndID = new Random();
  314. int IDint = rndID.Next(1, 10);
  315. string ID = IDint.ToString();
  316. string myConnection = "datasource=localhost;port=3306;username=root;password=root19721999";
  317. string Query = "insert into csgopicker_main.guest set id='" + ID + "', time='" + mytime + "',ip='" + Properties.Settings.Default.ip + "';";
  318. MySqlConnection conDataBase = new MySqlConnection(myConnection);
  319. MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
  320. MySqlDataReader myReader;
  321. try
  322. {
  323. label4.Text = "Wait";
  324. conDataBase.Open();
  325. myReader = cmdDataBase.ExecuteReader();
  326. label4.Text = "Guest-" + ID;
  327. MessageBox.Show("Login");
  328. break;
  329.  
  330. }
  331. catch
  332. {
  333. //łączność
  334. label4.Text = "Wait";
  335. database_conn.Start();
  336. label4.Text = "No active slots.\r\nTry again later.";
  337. conDataBase.Close();
  338. }
  339. }
  340. s.Stop();
  341.  
  342.  
  343.  
  344. }
  345.  
  346.  
  347.  
  348.  
  349. }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement