DatabaseController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. private string questionCountURL = "http://nordh.xyz:8088/QuestionCount.php";
  14. private string checkUsernameTakenURL = "http://nordh.xyz:8088/CheckUsername.php";
  15. private string insertUserURL = "http://nordh.xyz:8088/InsertUser.php";
  16. public Text statusText;
  17. private CategoryList categories;
  18. public CategoryList Categories { get { return categories; } set { categories = value; } }
  19. public static DatabaseController Instance { get; private set; }
  20. private void Awake()
  21. {
  22. StartCoroutine(getCategories());
  23. if (Instance != null && Instance != this)
  24. {
  25. Destroy(this);
  26. }
  27. else
  28. {
  29. Instance = this;
  30. }
  31. }
  32. public IEnumerator getCategories()
  33. {
  34. UnityWebRequest request = UnityWebRequest.Get(categoriesURL);
  35. yield return request.SendWebRequest();
  36. if (request.result == UnityWebRequest.Result.Success)
  37. {
  38. Debug.Log(request.downloadHandler.text);
  39. categories = JsonUtility.FromJson<CategoryList>("{\"list\":" + request.downloadHandler.text + "}");
  40. Debug.Log("Category count: " + categories.list.Count);
  41. foreach (Category c in categories.list)
  42. {
  43. Debug.Log(c.ToString());
  44. }
  45. }
  46. else if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.DataProcessingError)
  47. {
  48. Debug.Log("ERROR " + request.downloadHandler.error);
  49. }
  50. else
  51. {
  52. Debug.Log("OTHER ERROR " + request.responseCode + " " + request.downloadHandler.error);
  53. }
  54. }
  55. public IEnumerator GetRandomQuestion()
  56. {
  57. Question question = new Question();
  58. UnityWebRequest request = new UnityWebRequest(questionURL);
  59. yield return request.SendWebRequest();
  60. if (request.error != null)
  61. {
  62. print("There was an error getting question: " + request.downloadHandler.error);
  63. }
  64. else
  65. {
  66. statusText.text = request.downloadHandler.text;
  67. }
  68. }
  69. public string Md5Sum(string strToEncrypt)
  70. {
  71. System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
  72. byte[] bytes = ue.GetBytes(strToEncrypt);
  73. // encrypt bytes
  74. System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  75. byte[] hashBytes = md5.ComputeHash(bytes);
  76. // Convert the encrypted bytes back to a string (base 16)
  77. string hashString = "";
  78. for (int i = 0; i < hashBytes.Length; i++)
  79. {
  80. hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
  81. }
  82. return hashString.PadLeft(32, '0');
  83. }
  84. public bool CheckUsername(string username)
  85. {
  86. WWWForm form = new WWWForm();
  87. form.AddField("username", username);
  88. UnityWebRequest request = UnityWebRequest.Post(checkUsernameTakenURL, form);
  89. request.SendWebRequest();
  90. if (request.downloadHandler.text == "true")
  91. {
  92. return true;
  93. }
  94. else
  95. {
  96. return false;
  97. }
  98. }
  99. internal void InsertUser(string username, string userId)
  100. {
  101. WWWForm form = new WWWForm();
  102. form.AddField("username", username);
  103. form.AddField("token", userId);
  104. UnityWebRequest request = UnityWebRequest.Post(insertUserURL, form);
  105. request.SendWebRequest();
  106. if (request.error != null)
  107. {
  108. Debug.Log("Something went wrong with local user register... " + request.downloadHandler.error);
  109. }
  110. }
  111. }