· 6 years ago · May 13, 2019, 08:06 PM
1using UnityEngine;
2using System.Collections;
3using UnityEngine.UI;
4
5public class SQL_HighScores: MonoBehaviour
6{
7 public string teamName;
8 public int score;
9
10 private string secretKey = "<<!!PUT SECRET KEY HERE!!>>"; // Edit this value and make sure it's the same as the one stored on the server
11 public string addScoreURL = "http://yourdomain.com/yourdirectory/addscore.php?"; //be sure to add a ? to your url
12 public string displayName = "http://yourdomain.com/yourdirectory/displayName.php";
13 public string displayScore = "http://yourdomain.com/yourdirectory/displayScore.php";
14
15 //Text to display the result on
16 public Text teamNameText;
17 public Text topNameText;
18 public Text scoreText;
19 public Text topScoreText;
20
21 public TeamNameHolder teamNameHolder;
22
23 void OnEnable()
24 {
25 StartCoroutine(GetScores());
26 }
27
28 public void PostScoresToServerHack(string TeamName, int score)
29 {
30 StartCoroutine(PostScores(teamName,score));
31 StartCoroutine(GetScores());
32 }
33
34 public void PostScoresToServer(int score)
35 {
36 teamName = teamNameHolder.teamName;
37 StartCoroutine(PostScores(teamName,score));
38 StartCoroutine(GetScores());
39 }
40
41 // remember to use StartCoroutine when calling this function!
42 IEnumerator PostScores(string name, int score)
43 {
44 //This connects to a server side php script that will add the name and score to a MySQL DB.
45 // Supply it with a string representing the players name and the players score.
46 string hash = Md5Sum(name + score + secretKey);
47
48 string post_url = addScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
49
50 // Post the URL to the site and create a download object to get the result.
51 WWW hs_post = new WWW(post_url);
52 yield return hs_post; // Wait until the download is done
53
54 if (hs_post.error != null)
55 {
56 print("There was an error posting the high score: " + hs_post.error);
57 }
58
59 StartCoroutine(GetScores());
60 }
61
62 // Get the scores from the MySQL DB to display in a GUIText.
63 // remember to use StartCoroutine when calling this function!
64 IEnumerator GetScores()
65 {
66
67//GET NAME
68 //statusText.text = "Loading Scores";
69 WWW name_get = new WWW(displayName);
70 yield return name_get;
71
72 if (name_get.error != null)
73 {
74 print("There was an error getting team name: " + name_get.error);
75 }
76 else
77 {
78 teamNameText.text = name_get.text; // this is a GUIText that will display the scores in game.
79 }
80
81//GET SCORE
82
83 //statusText.text = "Loading Scores";
84 WWW score_get = new WWW(displayScore);
85 yield return score_get;
86
87 if (score_get.error != null)
88 {
89 print("There was an error getting the high score: " + score_get.error);
90 }
91 else
92 {
93 scoreText.text = score_get.text; // this is a GUIText that will display the scores in game.
94 }
95
96 }
97
98 public string Md5Sum(string strToEncrypt)
99 {
100 System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
101 byte[] bytes = ue.GetBytes(strToEncrypt);
102
103 // encrypt bytes
104 System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
105 byte[] hashBytes = md5.ComputeHash(bytes);
106
107 // Convert the encrypted bytes back to a string (base 16)
108 string hashString = "";
109
110 for (int i = 0; i < hashBytes.Length; i++)
111 {
112 hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
113 }
114
115 return hashString.PadLeft(32, '0');
116 }
117
118}