From 527c2a8d85715ce48d1dbd5bd5e56e546e12ca08 Mon Sep 17 00:00:00 2001 From: Philip White Date: Sat, 6 Nov 2021 05:31:57 +0000 Subject: [PATCH] Added Leaderboard Handle Script --- .../Assets/Scripts/Menu/LeaderboardHandle.cs | 92 +++++++++++++++++++ .../Scripts/Menu/LeaderboardHandle.cs.meta | 11 +++ 2 files changed, 103 insertions(+) create mode 100644 Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs create mode 100644 Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs.meta diff --git a/Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs b/Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs new file mode 100644 index 0000000..be6c6e3 --- /dev/null +++ b/Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs @@ -0,0 +1,92 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using MySql.Data.MySqlClient; +using System.Net; +public class LeaderboardHandle : MonoBehaviour +{ + private MySqlConnectionStringBuilder mySQLConectionBuilder = 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"; + + } + + // Update is called once per frame + void Update() + { + + } + + private void SetScoreRecords(string connectionString, string username, int score, int timeLasted) + { + try + { + using (MySqlConnection connection = new MySqlConnection(connectionString)) + { + connection.Open(); + string sql = $"INSERT INTO Scores VALUES ('{username}', '{score}', '{timeLasted}', '{GetIPAddress()}')"; + using (MySqlCommand command = new MySqlCommand(sql, connection)) + { + command.ExecuteNonQuery(); + } + } + } + catch (MySqlException e) + { + Debug.Log(e.ToString()); + } + } + + private void GetScoreRecords(string connectionString) + { + try + { + using (MySqlConnection connection = new MySqlConnection(connectionString)) + { + connection.Open(); + Debug.Log("connection established"); + string sql = "SELECT * FROM Scores ORDER BY Score DESC LIMIT 5"; + using (MySqlCommand command = new MySqlCommand(sql, connection)) + { + using (MySqlDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + + } + } + } + } + } + catch (MySqlException e) + { + Debug.Log(e.ToString()); + } + } + + public class Score + { + public string username { get; } + public int score { get; } + public int timeLasted { get; } + + public Score(string username, int score, int timeLasted) + { + this.username = username; + this.score = score; + this.timeLasted = timeLasted; + } + } + + static string GetIPAddress() + { + return new WebClient().DownloadString("https://api.ipify.org").TrimEnd(); + } +} diff --git a/Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs.meta b/Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs.meta new file mode 100644 index 0000000..9a26a22 --- /dev/null +++ b/Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 57c66944257aaea43a0675130f87a21b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: