private void NewUser_Click(object sender, RoutedEventArgs e) { if (newun.Text == "" || newpw.Text == "" || newdata.Text == "") { outp.Text = "All new user fields must be filled"; goto RetryNew; } if (username.Text.Length > 16 || password.Text.Length > 16 || newdata.Text.Length > 50) { outp.Text = "Username or password cannot be longer than 16 characters. Tag must be less than 50 characters"; goto RetryNew; } string hash = null; using (MD5 md5hash = MD5.Create()) { hash = GetMd5Hash(md5hash, newpw.Text); } string cmd = $"INSERT INTO `usertable` (`id`, `username`, `pwhash`, `score`, `data`, `creation_date`, `datatag`) " + $"VALUES (NULL, '{newun.Text}', '{hash}', 0, '{newdata.Text}', NOW(), 'string')"; var dbCon = DBConnection.Instance(); MySqlDataReader reader = null; if (dbCon.IsConnect()) { reader = SqlExecute(cmd, dbCon); if (reader != null) { outp.Text = "Registration complete. You may now login"; } } else { outp.Text = dbCon.Failstr; } dbCon.Close(); RetryNew: outp.Text += ""; /* * string filepath = @"D:\Media\Images\20160731_172518.jpg"; * try * { * using (System.Drawing.Image i = System.Drawing.Image.FromFile(filepath, true)) * { * using (MemoryStream m = new MemoryStream()) * { * i.Save(m, i.RawFormat); * byte[] b = m.ToArray(); * string b64 = Convert.ToBase64String(b); * string txtpath = @"C:\Users\James\Desktop\txt.txt"; * if (!File.Exists(txtpath)) * { * File.WriteAllText(txtpath, b64); * } * } * } * }catch(Exception ex) * { * outp.Text = ex.Message; * } */ }
private void Timer_Tick(object sender, EventArgs e) { if (time <= 14) { countdowntimer.Interval = new TimeSpan(0, 0, 1); } timelabel.Content = "Time: " + time; time--; if (time < 0) { countdowntimer.Stop(); gametimer.Stop(); outp.Text = "Game over! " + "Score: " + score + "\n"; if (true) { MySqlDataReader reader = null; DBConnection dbCon = DBConnection.Instance(); if (dbCon.IsConnect()) { if (login == true && currentuser != null && score > topscore) { if (topscore < 0) { MessageBox.Show("AAA"); Thread.Sleep(100000000); } outp.Text += "New High score!\n"; try { reader = SqlExecute($"UPDATE `usertable` SET `score` = '{score}' WHERE `usertable`.`username` = '{currentuser}'", dbCon); } catch (Exception ex) { outp.Text = "Falure at score update"; MessageBox.Show("Failure at score update" + ex.ToString()); } } if (reader != null) { reader.Close(); } reader = SqlExecute($"SELECT * FROM `usertable` ORDER BY `score` DESC LIMIT 5", dbCon); if (reader != null) { outp.Text += "Top Scores\n"; int i = 1; outp.TextAlignment = TextAlignment.Left; int l = 0; while (reader.Read()) { l = reader.GetString("username").Length; outp.Text += i + ". " + reader.GetString("username"); if (l < 6) { outp.Text += "\t\t\t"; } if (l >= 6 && l < 14) { outp.Text += "\t\t"; } if (l >= 14) { outp.Text += "\t"; } outp.Text += reader.GetString("score") + "\n"; if (i < 4) { outp.Text += " Says: " + reader.GetString("data") + "\n"; } i++; } reader.Close(); } } } time = 15; for (int i = 0; i < rectlist.Count; i++) { grid.Children.Remove(rectlist[i]); } rectlist.Clear(); gamebutton.IsEnabled = true; if (login == false) { loginbtn.IsEnabled = true; } else { logoutbtn.IsEnabled = true; } newuserbtn.IsEnabled = true; } }
private void loginbtn_click(object sender, RoutedEventArgs e) { if (username.Text == "" || password.Text == "") { outp.Text = "Please enter Username and Password"; goto Retry; } string hash = null; using (MD5 md5hash = MD5.Create()) { hash = GetMd5Hash(md5hash, password.Text); } var dbCon = DBConnection.Instance(); if (dbCon.IsConnect()) { int col = 0; MySqlDataReader reader = null; reader = SqlExecute("SELECT count(*) FROM information_schema.columns WHERE table_name = 'usertable'", dbCon); if (reader != null && reader.Read()) { col = Int32.Parse(reader.GetString(0)); reader.Close(); } reader = SqlExecute($"SELECT * from usertable where username='******' and pwhash='" + hash + "'", dbCon); if (reader != null && !reader.HasRows) { outp.Text = "Bad un or pw"; reader.Close(); goto Retry; } else if (reader != null && reader.Read()) { topscore = reader.GetInt32("score"); currentuser = reader.GetString("username"); login = true; loginbtn.IsEnabled = false; logoutbtn.IsEnabled = true; outp.Text = ""; outp.Text += "Hello " + currentuser + "! Your top score: " + topscore + "\n"; reader.Close(); } reader = SqlExecute($"SELECT * FROM `usertable` ORDER BY `score` DESC LIMIT 5", dbCon); if (reader != null && !reader.HasRows) { outp.Text = "Bad un or pw"; reader.Close(); } else if (reader != null) { outp.Text += "Top Scores\n"; int i = 1; outp.TextAlignment = TextAlignment.Left; int l = 0; while (reader.Read()) { l = reader.GetString("username").Length; outp.Text += i + ". " + reader.GetString("username"); if (l < 6) { outp.Text += "\t\t\t"; } if (l >= 6 && l < 14) { outp.Text += "\t\t"; } if (l >= 14) { outp.Text += "\t"; } outp.Text += reader.GetString("score") + "\n"; if (i < 4) { outp.Text += "Says: " + reader.GetString("data") + "\n"; } i++; } } } else { outp.Text = dbCon.Failstr; } dbCon.Close(); Retry: outp.Text += ""; }