| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- public class DatabaseController : MonoBehaviour
- {
- private string secretKey = "TheNarKampenSecretKey";
- private string questionURL = "http://nordh.xyz:8088/Questions.php?";
- private string categoriesURL = "http://nordh.xyz:8088/Categories.php";
- private string questionCountURL = "http://nordh.xyz:8088/QuestionCount.php";
- private string checkUsernameTakenURL = "http://nordh.xyz:8088/CheckUsername.php";
- private string insertUserURL = "http://nordh.xyz:8088/InsertUser.php";
- public Text statusText;
- private CategoryList categories;
- public CategoryList Categories { get { return categories; } set { categories = value; } }
- public static DatabaseController Instance { get; private set; }
- private void Awake()
- {
- StartCoroutine(getCategories());
- if (Instance != null && Instance != this)
- {
- Destroy(this);
- }
- else
- {
- Instance = this;
- }
- }
- public IEnumerator getCategories()
- {
- UnityWebRequest request = UnityWebRequest.Get(categoriesURL);
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.Success)
- {
- Debug.Log(request.downloadHandler.text);
- categories = JsonUtility.FromJson<CategoryList>("{\"list\":" + request.downloadHandler.text + "}");
- Debug.Log("Category count: " + categories.list.Count);
- foreach (Category c in categories.list)
- {
- Debug.Log(c.ToString());
- }
- }
- else if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.DataProcessingError)
- {
- Debug.Log("ERROR " + request.downloadHandler.error);
- }
- else
- {
- Debug.Log("OTHER ERROR " + request.responseCode + " " + request.downloadHandler.error);
- }
- }
- public IEnumerator GetRandomQuestion()
- {
- Question question = new Question();
- UnityWebRequest request = new UnityWebRequest(questionURL);
- yield return request.SendWebRequest();
- if (request.error != null)
- {
- print("There was an error getting question: " + request.downloadHandler.error);
- }
- else
- {
- statusText.text = request.downloadHandler.text;
- }
- }
- public string Md5Sum(string strToEncrypt)
- {
- System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
- byte[] bytes = ue.GetBytes(strToEncrypt);
- // encrypt bytes
- System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
- byte[] hashBytes = md5.ComputeHash(bytes);
- // Convert the encrypted bytes back to a string (base 16)
- string hashString = "";
- for (int i = 0; i < hashBytes.Length; i++)
- {
- hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
- }
- return hashString.PadLeft(32, '0');
- }
- public bool CheckUsername(string username)
- {
- WWWForm form = new WWWForm();
- form.AddField("username", username);
- UnityWebRequest request = UnityWebRequest.Post(checkUsernameTakenURL, form);
- request.SendWebRequest();
- if (request.downloadHandler.text == "true")
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- internal void InsertUser(string username, string userId)
- {
- WWWForm form = new WWWForm();
- form.AddField("username", username);
- form.AddField("token", userId);
- UnityWebRequest request = UnityWebRequest.Post(insertUserURL, form);
- request.SendWebRequest();
- if (request.error != null)
- {
- Debug.Log("Something went wrong with local user register... " + request.downloadHandler.error);
- }
- }
- }
|