Update LeaderboardHandle Script for Async
This commit is contained in:
parent
79eaa64ccf
commit
624a56420a
@ -4,33 +4,38 @@ using UnityEngine;
|
|||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Threading.Tasks;
|
||||||
public class LeaderboardHandle : MonoBehaviour
|
public class LeaderboardHandle : MonoBehaviour
|
||||||
{
|
{
|
||||||
public InputField submitUsername;
|
public InputField submitUsername;
|
||||||
public GameObject GameOverPanel;
|
public GameObject GameOverPanel;
|
||||||
|
public GameObject FetchingScores;
|
||||||
public float time { get; set; }
|
public float time { get; set; }
|
||||||
public int finalScore { 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
|
// Start is called before the first frame update
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
mySQLConectionBuilder.Server = "sql4.freesqldatabase.com";
|
mySQLConnectionBuilder.Server = "sql4.freesqldatabase.com";
|
||||||
mySQLConectionBuilder.Port = 3306;
|
mySQLConnectionBuilder.Port = 3306;
|
||||||
mySQLConectionBuilder.UserID = "sql4449219";
|
mySQLConnectionBuilder.UserID = "sql4449219";
|
||||||
mySQLConectionBuilder.Password = "hsFqWLxIIF";
|
mySQLConnectionBuilder.Password = "hsFqWLxIIF";
|
||||||
mySQLConectionBuilder.Database = "sql4449219";
|
mySQLConnectionBuilder.Database = "sql4449219";
|
||||||
|
|
||||||
GameObject.Find("FinalScoreLabel").GetComponent<Text>().text = GameObject.Find("FinalScoreLabel").GetComponent<Text>().text.Replace("000", finalScore.ToString());
|
GameObject.Find("FinalScoreLabel").GetComponent<Text>().text = GameObject.Find("FinalScoreLabel").GetComponent<Text>().text.Replace("000", finalScore.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SubmitScores()
|
public async void SubmitScores()
|
||||||
{
|
{
|
||||||
SetScoreRecords(mySQLConectionBuilder.ConnectionString, submitUsername.text);
|
GameObject.Find("GameOverLabel").SetActive(false);
|
||||||
|
FetchingScores.GetComponent<FetchingScoresAnimation>().enabled = true;
|
||||||
|
await SetScoreRecords(mySQLConnectionBuilder.ConnectionString, submitUsername.text);
|
||||||
|
List<Score> Scores = new List<Score>(await GetScoreRecords(mySQLConnectionBuilder.ConnectionString));
|
||||||
|
FetchingScores.GetComponent<FetchingScoresAnimation>().enabled = false;
|
||||||
|
FetchingScores.SetActive(false);
|
||||||
GameOverPanel.GetComponent<Animator>().enabled = true;
|
GameOverPanel.GetComponent<Animator>().enabled = true;
|
||||||
|
|
||||||
List<Score> Scores = new List<Score>(GetScoreRecords(mySQLConectionBuilder.ConnectionString));
|
|
||||||
for (int i = 0; i < Scores.Count; i++)
|
for (int i = 0; i < Scores.Count; i++)
|
||||||
{
|
{
|
||||||
Text scoreLabel = GameObject.Find($"ScoreText ({i + 1})").GetComponent<Text>();
|
Text scoreLabel = GameObject.Find($"ScoreText ({i + 1})").GetComponent<Text>();
|
||||||
@ -39,7 +44,9 @@ public class LeaderboardHandle : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetScoreRecords(string connectionString, string username)
|
private async Task SetScoreRecords(string connectionString, string username)
|
||||||
|
{
|
||||||
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -57,11 +64,14 @@ public class LeaderboardHandle : MonoBehaviour
|
|||||||
{
|
{
|
||||||
Debug.Log(e.ToString());
|
Debug.Log(e.ToString());
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Score> GetScoreRecords(string connectionString)
|
private async Task<List<Score>> GetScoreRecords(string connectionString)
|
||||||
{
|
{
|
||||||
List<Score> records = new List<Score>();
|
List<Score> records = new List<Score>();
|
||||||
|
await Task.Run(() =>
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
using (MySqlConnection connection = new MySqlConnection(connectionString))
|
||||||
@ -84,6 +94,7 @@ public class LeaderboardHandle : MonoBehaviour
|
|||||||
{
|
{
|
||||||
Debug.Log(e.ToString());
|
Debug.Log(e.ToString());
|
||||||
}
|
}
|
||||||
|
});
|
||||||
return records;
|
return records;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user