DatabaseController.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using UnityEngine.UI;
  7. public class DatabaseController : MonoBehaviour
  8. {
  9. private string secretKey = "TheNarKampenSecretKey";
  10. public string questionURL = "http://nordh.xyz:8088/Questions.php?";
  11. public string categoriesURL = "http://nordh.xyz:8088/Categories.php";
  12. public Text statusText;
  13. List<Category> categories = new List<Category>();
  14. private void Awake()
  15. {
  16. StartCoroutine(getCategories());
  17. }
  18. private IEnumerator getCategories()
  19. {
  20. UnityWebRequest request = UnityWebRequest.Get(categoriesURL);
  21. yield return request.SendWebRequest();
  22. if (request.result == UnityWebRequest.Result.Success)
  23. {
  24. Debug.Log(request.downloadHandler.text);
  25. }
  26. else if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.DataProcessingError)
  27. {
  28. Debug.Log("ERROR " + request.downloadHandler.error);
  29. }
  30. }
  31. public IEnumerator getQuestion()
  32. {
  33. Question question = new Question();
  34. WWW getQuestion = new WWW(questionURL);
  35. yield return getQuestion;
  36. if (getQuestion.error != null)
  37. {
  38. print("There was an error getting question: " + getQuestion.error);
  39. }
  40. else
  41. {
  42. statusText.text = getQuestion.text;
  43. }
  44. }
  45. public 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. }