-
Notifications
You must be signed in to change notification settings - Fork 1
/
HSController.cs
134 lines (104 loc) · 4.16 KB
/
HSController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class HSController : MonoBehaviour {
private string secretKey ="devAndre@!"; // Edit this value and make sure it's the same as the one stored on the server
public Text uiText;
[HideInInspector] public string addScoreUrl ="http://www.andregamedeveloper.com.br/games/addscore.php?"; //be sure to add a ? to your url
[HideInInspector] public string highscoreUrl ="http://www.andregamedeveloper.com.br/games/display.php";
public Button postButton;
public Text postText;
public GameObject txtText;
public GameObject sucessText;
public Text bigScoreText;
private GameController gameController;
private bool sent = false;
private Dictionary<string, string> headers;
// Use this for initialization
void Awake () {
GameObject controllerObj = GameObject.FindWithTag("GameController");
if (controllerObj != null)
gameController = controllerObj.GetComponent<GameController> ();
if (gameController == null)
Debug.Log ("Can't find game controller");
// Reset
postButton.gameObject.SetActive (true);
txtText.SetActive (true);
postText.text = "";
sucessText.SetActive (false);
bigScoreText.text = "" + gameController.getScore ();
// Create Readers
WWWForm form = new WWWForm();
headers = form.headers;
headers ["Access-Control-Allow-Credentials"] = "true";
headers ["Access-Control-Allow-Headers"] = "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time";
headers ["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
headers ["Access-Control-Allow-Origin"] = "*";
StartCoroutine(GetScores());
sent = false;
}
public void postScore() {
if (sent == true)
return;
Debug.Log ("postScores!");
StartCoroutine(PostScores(postText.text, gameController.getScore()));
}
// remember to use StartCoroutine when calling this function!
IEnumerator PostScores(string name, int score)
{
//This connects to a server side php script that will add the name and score to a MySQL DB.
// Supply it with a string representing the players name and the players score.
string hash = secretKey; //Md5Sum(name + score + secretKey);
string post_url = addScoreUrl + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
Debug.Log ("URL: " + post_url);
// Post the URL to the site and create a download object to get the result.
WWW hs_post = new WWW(post_url, null, headers);
yield return hs_post; // Wait until the download is done
if (hs_post.error != null) {
print ("There was an error posting the high score: " + hs_post.error);
} else {
sent = true;
Debug.Log ("Score Sent!");
postButton.gameObject.SetActive (false);
txtText.SetActive (false);
sucessText.SetActive (true);
StartCoroutine(GetScores());
}
}
// Get the scores from the MySQL DB to display in a GUIText.
// remember to use StartCoroutine when calling this function!
IEnumerator GetScores()
{
uiText.text = "Loading Scores";
// Post the URL to the site and create a download object to get the result.
WWW hs_get = new WWW(highscoreUrl, null, headers);
foreach(KeyValuePair<string, string> obj in headers) {
Debug.Log (obj.Key + " : " + obj.Value);
}
yield return hs_get;
if (hs_get.error != null)
{
print("There was an error getting the high score: " + hs_get.error);
}
else
{
uiText.text = hs_get.text; // this is a GUIText that will display the scores in game.
}
}
// Generate MD5 Hash
public string Md5Sum(string strToEncrypt) {
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
}