From 624a56420a70429696cd64b854ee5dbfea97fc40 Mon Sep 17 00:00:00 2001 From: Philip White Date: Sun, 14 Nov 2021 12:28:18 +0000 Subject: [PATCH] Update LeaderboardHandle Script for Async --- .../Assets/Scripts/Menu/LeaderboardHandle.cs | 83 +++++++++++-------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs b/Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs index e6de9bd..39506c2 100644 --- a/Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs +++ b/Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs @@ -4,33 +4,38 @@ using UnityEngine; using UnityEngine.UI; using MySql.Data.MySqlClient; using System.Net; +using System.Threading.Tasks; public class LeaderboardHandle : MonoBehaviour { public InputField submitUsername; public GameObject GameOverPanel; + public GameObject FetchingScores; public float time { get; set; } public int finalScore { get; set; } - private MySqlConnectionStringBuilder mySQLConectionBuilder = new MySqlConnectionStringBuilder(); + private MySqlConnectionStringBuilder mySQLConnectionBuilder = new MySqlConnectionStringBuilder(); // Start is called before the first frame update void Start() { - mySQLConectionBuilder.Server = "sql4.freesqldatabase.com"; - mySQLConectionBuilder.Port = 3306; - mySQLConectionBuilder.UserID = "sql4449219"; - mySQLConectionBuilder.Password = "hsFqWLxIIF"; - mySQLConectionBuilder.Database = "sql4449219"; + mySQLConnectionBuilder.Server = "sql4.freesqldatabase.com"; + mySQLConnectionBuilder.Port = 3306; + mySQLConnectionBuilder.UserID = "sql4449219"; + mySQLConnectionBuilder.Password = "hsFqWLxIIF"; + mySQLConnectionBuilder.Database = "sql4449219"; GameObject.Find("FinalScoreLabel").GetComponent().text = GameObject.Find("FinalScoreLabel").GetComponent().text.Replace("000", finalScore.ToString()); } - public void SubmitScores() + public async void SubmitScores() { - SetScoreRecords(mySQLConectionBuilder.ConnectionString, submitUsername.text); + GameObject.Find("GameOverLabel").SetActive(false); + FetchingScores.GetComponent().enabled = true; + await SetScoreRecords(mySQLConnectionBuilder.ConnectionString, submitUsername.text); + List Scores = new List(await GetScoreRecords(mySQLConnectionBuilder.ConnectionString)); + FetchingScores.GetComponent().enabled = false; + FetchingScores.SetActive(false); GameOverPanel.GetComponent().enabled = true; - - List Scores = new List(GetScoreRecords(mySQLConectionBuilder.ConnectionString)); for (int i = 0; i < Scores.Count; i++) { Text scoreLabel = GameObject.Find($"ScoreText ({i + 1})").GetComponent(); @@ -39,51 +44,57 @@ public class LeaderboardHandle : MonoBehaviour } } - private void SetScoreRecords(string connectionString, string username) + private async Task SetScoreRecords(string connectionString, string username) { - try + await Task.Run(() => { - using (MySqlConnection connection = new MySqlConnection(connectionString)) + try { - connection.Open(); - string sql = $"INSERT INTO Scores VALUES ('{username.ToUpper()}', '{finalScore}', '{Mathf.FloorToInt(time)}', '{GetIPAddress()}')"; - using (MySqlCommand command = new MySqlCommand(sql, connection)) + using (MySqlConnection connection = new MySqlConnection(connectionString)) { - command.ExecuteNonQuery(); + connection.Open(); + string sql = $"INSERT INTO Scores VALUES ('{username.ToUpper()}', '{finalScore}', '{Mathf.FloorToInt(time)}', '{GetIPAddress()}')"; + using (MySqlCommand command = new MySqlCommand(sql, connection)) + { + command.ExecuteNonQuery(); + } } } - } - catch (MySqlException e) - { - Debug.Log(e.ToString()); - } + catch (MySqlException e) + { + Debug.Log(e.ToString()); + } + }); } - private List GetScoreRecords(string connectionString) + private async Task> GetScoreRecords(string connectionString) { List records = new List(); - try + await Task.Run(() => { - using (MySqlConnection connection = new MySqlConnection(connectionString)) + try { - connection.Open(); - string sql = "SELECT Username, Score, TimeLasted FROM Scores ORDER BY Score DESC LIMIT 5"; - using (MySqlCommand command = new MySqlCommand(sql, connection)) + using (MySqlConnection connection = new MySqlConnection(connectionString)) { - using (MySqlDataReader reader = command.ExecuteReader()) + connection.Open(); + string sql = "SELECT Username, Score, TimeLasted FROM Scores ORDER BY Score DESC LIMIT 5"; + using (MySqlCommand command = new MySqlCommand(sql, connection)) { - while (reader.Read()) + using (MySqlDataReader reader = command.ExecuteReader()) { - records.Add(new Score(reader.GetString(0), reader.GetInt32(1), reader.GetInt32(2))); + while (reader.Read()) + { + records.Add(new Score(reader.GetString(0), reader.GetInt32(1), reader.GetInt32(2))); + } } } } } - } - catch (MySqlException e) - { - Debug.Log(e.ToString()); - } + catch (MySqlException e) + { + Debug.Log(e.ToString()); + } + }); return records; }