Added Leaderboard Handle Script

This commit is contained in:
Philip W 2021-11-06 05:31:57 +00:00
parent 602c5705ec
commit 527c2a8d85
2 changed files with 103 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 57c66944257aaea43a0675130f87a21b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: