Added Leaderboard Handle Script
This commit is contained in:
parent
602c5705ec
commit
527c2a8d85
92
Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs
Normal file
92
Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs
Normal 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();
|
||||
}
|
||||
}
|
11
Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs.meta
generated
Normal file
11
Unity-Files/Assets/Scripts/Menu/LeaderboardHandle.cs.meta
generated
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57c66944257aaea43a0675130f87a21b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
x
Reference in New Issue
Block a user