DatabaseController.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. private string questionURL = "http://nordh.xyz:8088/Questions.php?";
  11. private 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. else
  31. {
  32. Debug.Log("OTHER ERROR " + request.responseCode + " " + request.downloadHandler.error);
  33. }
  34. }
  35. public IEnumerator getQuestion()
  36. {
  37. Question question = new Question();
  38. WWW getQuestion = new WWW(questionURL);
  39. yield return getQuestion;
  40. if (getQuestion.error != null)
  41. {
  42. print("There was an error getting question: " + getQuestion.error);
  43. }
  44. else
  45. {
  46. statusText.text = getQuestion.text;
  47. }
  48. }
  49. public string Md5Sum(string strToEncrypt)
  50. {
  51. System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
  52. byte[] bytes = ue.GetBytes(strToEncrypt);
  53. // encrypt bytes
  54. System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  55. byte[] hashBytes = md5.ComputeHash(bytes);
  56. // Convert the encrypted bytes back to a string (base 16)
  57. string hashString = "";
  58. for (int i = 0; i < hashBytes.Length; i++)
  59. {
  60. hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
  61. }
  62. return hashString.PadLeft(32, '0');
  63. }
  64. }