Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

MD5 - Online HighScore Table on own server

Discussion in 'BlackBerry' started by SirMarley, Nov 26, 2014.

  1. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    Hey guys... I am posting this here and in the scriptting forum...

    I have a game made for WP and for Blackberry and I have tried to implement the same online highscore table but, for some reason which I dont know, it does not work for BB

    Does anyone know where the problem could be and how to fix it, please?

    Here are the two scripts (md5 and the one that sends the data to the table)

    MD5:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Text;
    4. using System;
    5. #if UNITY_WP8
    6. using System.Security.Cryptography;
    7. using UnityEngine.Windows;
    8. using UnityEngine.WindowsPhone;
    9. #else
    10. using System.Security.Cryptography;
    11. #endif
    12.  
    13. public static class Md5Functions {
    14.  
    15. #if UNITY_WP8
    16.  
    17.     static string md5val;
    18.  
    19.     // Use this for initialization
    20.     static void Start () {
    21.         md5val = Md5Sum("Hello World!");
    22.     }
    23.  
    24.     static void OnGUI()
    25.     {
    26.         GUILayout.Label(md5val);
    27.     }
    28.  
    29.     public static string Md5Sum(string strToEncrypt)
    30.     {
    31.         System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
    32.         byte[] bytes = ue.GetBytes(strToEncrypt);
    33.  
    34.         byte[] hashBytes = Crypto.ComputeMD5Hash(bytes);
    35.         string hashString = "";
    36.  
    37.         for (int i = 0; i < hashBytes.Length; i++)
    38.         {
    39.             hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
    40.         }
    41.  
    42.         return hashString.PadLeft(32, '0');
    43.     }
    44. #else
    45.     public  static string Md5Sum(string strToEncrypt)
    46. {
    47.     System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
    48.     byte[] bytes = ue.GetBytes(strToEncrypt);
    49.     // encrypt bytes
    50.     System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
    51.     byte[] hashBytes = md5.ComputeHash(bytes);
    52.     // Convert the encrypted bytes back to a string (base 16)
    53.     string hashString = "";
    54.     for (int i = 0; i < hashBytes.Length; i++)
    55.     {
    56.         hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
    57.     }
    58.     return hashString.PadLeft(32, '0');
    59. }
    60. #endif
    61. }
    62.  
    Sending data

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameOverController : MonoBehaviour {
    5.  
    6.     float gameOverTimer;
    7.  
    8.     public string stringToEdit;
    9.     public string _default;
    10.     public string name1;
    11.  
    12.     public GUIText scoreText;
    13.     public GUIText highScoreText;
    14.     public int _score;
    15.     public GUIStyle myStyle;
    16.  
    17.     public GUIStyle menuBtn;
    18.     public GUIStyle againBtn;
    19.  
    20.     private bool focused;
    21.  
    22.     private string secretKey = "SECRETKEY"; // Edit this value and make sure it's the same as the one stored on the server
    23.     public string addScoreURL = "http://sirmarleyproductions.esy.es/rabbitaddscore.php?"; //be sure to add a ? to your url
    24.  
    25.     void CheckHighscore()
    26.     {
    27.         _score = PlayerPrefs.GetInt("CurrentScore", 0);
    28.         int _highscore = PlayerPrefs.GetInt("highScore", 0);
    29.         if (_score > _highscore)
    30.         {
    31.             PlayerPrefs.SetInt("highScore", _score);
    32.         }
    33.     }
    34.  
    35.     void Start () {
    36.  
    37.         stringToEdit = PlayerPrefs.GetString("Nombre", "Hungry Rabbit");
    38.         _default = stringToEdit;
    39.         gameOverTimer = 5.0f;
    40.         CheckHighscore();
    41.  
    42.         scoreText.text = "Score / Puntos / 得分 : " + PlayerPrefs.GetInt("CurrentScore", 0);
    43.         highScoreText.text = "HighScore / Mejor Puntaje / 的最高得分 : " + PlayerPrefs.GetInt("highScore", 0);
    44.         focused = true;
    45.     }
    46.    
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.         gameOverTimer -= Time.deltaTime;
    51.         if (gameOverTimer <= 0.0f)
    52.         {
    53.  
    54.         }
    55.     }
    56.  
    57.         void OnGUI()
    58.     {
    59.         GUI.SetNextControlName("player_name");
    60.         stringToEdit = GUI.TextField(new Rect(Screen.width * .3f, Screen.height * .6f, Screen.width * .2f, Screen.height * .1f), stringToEdit, myStyle);
    61.         if (UnityEngine.Event.current.type == EventType.Repaint)
    62.         {
    63.             if (GUI.GetNameOfFocusedControl() == "player_name")
    64.             {
    65.                 if (focused)
    66.                 {
    67.                     stringToEdit = "";
    68.                     focused = false;
    69.                 }
    70.             }
    71.             else
    72.             {
    73.                 if (stringToEdit == "") stringToEdit = _default;
    74.                 focused = true;
    75.             }
    76.             if (GUI.GetNameOfFocusedControl() != "player_name" && focused == false)
    77.             {
    78.                 focused = true;
    79.             }
    80.         }
    81.  
    82.         if (GUI.Button(new Rect(Screen.width * .001f, Screen.height * .03f, Screen.width * .24f, Screen.height * .25f),"", againBtn))
    83.         {
    84.             PlayerPrefs.SetString("Nombre", stringToEdit);
    85.             SendDataToDB();
    86.             Application.LoadLevel("Game");
    87.         }
    88.         if (GUI.Button(new Rect(Screen.width * .76f, Screen.height * .03f, Screen.width * .24f, Screen.height * .25f),"", menuBtn))
    89.         {
    90.             PlayerPrefs.SetString("Nombre", stringToEdit);
    91.             SendDataToDB();
    92.             Application.LoadLevel("Menu");
    93.         }
    94.     }
    95.  
    96.     void SendDataToDB()
    97.     {
    98.         name1 = stringToEdit;
    99.         Debug.Log(name1);
    100.         StartCoroutine(PostScores(name1, _score));
    101.     }
    102.  
    103.     IEnumerator PostScores(string name, int score)
    104.     {
    105.         //This connects to a server side php script that will add the name and score to a MySQL DB.
    106.         // Supply it with a string representing the players name and the players score.
    107.         string hash = Md5Functions.Md5Sum(name + score + secretKey);
    108.  
    109.         string post_url = addScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
    110.  
    111.         // Post the URL to the site and create a download object to get the result.
    112.         WWW hs_post = new WWW(post_url);
    113.         yield return hs_post; // Wait until the download is done
    114.  
    115.         if (hs_post.error != null)
    116.         {
    117.             print("There was an error posting the high score: " + hs_post.error);
    118.         }
    119.     }
    120. }
    121.  
    Thanks!!!