DatabaseController.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. using UnityEngine.UI;
  8. public class DatabaseController : MonoBehaviour
  9. {
  10. private string secretKey = "TheNarKampenSecretKey";
  11. private string questionURL = "http://nordh.xyz:8088/Questions.php?";
  12. private string categoriesURL = "http://nordh.xyz:8088/Categories.php";
  13. public Text statusText;
  14. CategoryList categories;
  15. private void Awake()
  16. {
  17. StartCoroutine(getCategories());
  18. }
  19. private IEnumerator getCategories()
  20. {
  21. UnityWebRequest request = UnityWebRequest.Get(categoriesURL);
  22. yield return request.SendWebRequest();
  23. if (request.result == UnityWebRequest.Result.Success)
  24. {
  25. Debug.Log(request.downloadHandler.text);
  26. categories = JsonUtility.FromJson<CategoryList>("{\"list\":" + request.downloadHandler.text + "}");
  27. Debug.Log("Category count: " + categories.list.Count);
  28. foreach (Category c in categories.list)
  29. {
  30. Debug.Log(c.ToString());
  31. }
  32. }
  33. else if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.DataProcessingError)
  34. {
  35. Debug.Log("ERROR " + request.downloadHandler.error);
  36. }
  37. else
  38. {
  39. Debug.Log("OTHER ERROR " + request.responseCode + " " + request.downloadHandler.error);
  40. }
  41. }
  42. public IEnumerator getQuestion()
  43. {
  44. Question question = new Question();
  45. WWW getQuestion = new WWW(questionURL);
  46. yield return getQuestion;
  47. if (getQuestion.error != null)
  48. {
  49. print("There was an error getting question: " + getQuestion.error);
  50. }
  51. else
  52. {
  53. statusText.text = getQuestion.text;
  54. }
  55. }
  56. public string Md5Sum(string strToEncrypt)
  57. {
  58. System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
  59. byte[] bytes = ue.GetBytes(strToEncrypt);
  60. // encrypt bytes
  61. System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  62. byte[] hashBytes = md5.ComputeHash(bytes);
  63. // Convert the encrypted bytes back to a string (base 16)
  64. string hashString = "";
  65. for (int i = 0; i < hashBytes.Length; i++)
  66. {
  67. hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
  68. }
  69. return hashString.PadLeft(32, '0');
  70. }
  71. }