Database.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mono.Data.Sqlite;
  5. using System.Data;
  6. using System;
  7. using UnityEngine.Networking;
  8. using System.IO;
  9. public class Database : MonoBehaviour {
  10. private const string onlineQuestionsUrl = "nordh.xyz/narKampen/dbFiles/Question.php";
  11. string connectionType;
  12. string databaseUrl;
  13. string gameMode;
  14. int winAmount = -1;
  15. int questionTimer = -1;
  16. public GameObject questionCardPrefab;
  17. IDbConnection conn;
  18. [Serializable]
  19. public class Question {
  20. public string question;
  21. public string answer;
  22. public string id;
  23. public string category;
  24. }
  25. internal List<CategoryPanel.Category> GetCategories() {
  26. List<CategoryPanel.Category> list = new List<CategoryPanel.Category>();
  27. string sql = "SELECT name, r, g, b, a, id FROM category";
  28. IDbCommand cmd = GetConnection();
  29. cmd.CommandText = sql;
  30. IDataReader reader = cmd.ExecuteReader();
  31. while (reader.Read()) {
  32. CategoryPanel.Category cat = new CategoryPanel.Category();
  33. byte r = (byte)reader.GetInt32(1);
  34. byte g = (byte)reader.GetInt32(2);
  35. byte b = (byte)reader.GetInt32(3);
  36. byte a = (byte)reader.GetInt32(4);
  37. cat.color = new Color32(r, g, b, a);
  38. cat.name = reader.GetString(0);
  39. cat.id = reader.GetInt32(5);
  40. list.Add(cat);
  41. }
  42. CloseConnection();
  43. return list;
  44. }
  45. [Serializable]
  46. public class Questions {
  47. public List<Question> questionsList = new List<Question>();
  48. }
  49. internal void SetLastPlayedDate(int gameId) {
  50. string sql = "UPDATE game SET lastPlayedDate = DATE() WHERE id = " + gameId;
  51. IDbCommand cmd = GetConnection();
  52. cmd.CommandText = sql;
  53. cmd.ExecuteNonQuery();
  54. cmd.Dispose();
  55. CloseConnection();
  56. }
  57. private IDbCommand GetConnection() {
  58. if (databaseUrl == null) { // ej bra lösning för online
  59. SetLocalOrOnline("Local");
  60. }
  61. conn = new SqliteConnection(databaseUrl);
  62. conn.Open();
  63. IDbCommand cmd = conn.CreateCommand();
  64. return cmd;
  65. }
  66. private void CloseConnection() {
  67. conn.Close();
  68. }
  69. string questionString = "";
  70. string answerString = "";
  71. string idString = "";
  72. string categoryString = "";
  73. private int round = -1;
  74. public string QuestionString { get => questionString; set => questionString = value; }
  75. private void Start() {
  76. if (databaseUrl == null || databaseUrl.Equals("")) {
  77. SetLocalOrOnline("Local"); // Temporary, shoild not be needed after local testing
  78. }
  79. }
  80. internal int GetQuestionTimer(int gameId) {
  81. if (questionTimer == -1) {
  82. string sql = "SELECT answerTimer FROM game WHERE id = " + gameId;
  83. if (databaseUrl == null) {
  84. SetLocalOrOnline("Local");
  85. }
  86. IDbCommand cmd = GetConnection();
  87. cmd.CommandText = sql;
  88. IDataReader reader = cmd.ExecuteReader();
  89. while (reader.Read()) {
  90. questionTimer = reader.GetInt32(0);
  91. }
  92. reader.Close();
  93. cmd.Dispose();
  94. }
  95. CloseConnection();
  96. return this.questionTimer;
  97. }
  98. internal void GetOnlineGames(int userId) {
  99. string sql = "SELECT * FROM games WHERE userId = " + userId;
  100. }
  101. internal List<LocalGameScript> GetLocalGames(GameObject prefab) {
  102. List<LocalGameScript> games = new List<LocalGameScript>();
  103. string sql = "SELECT game.*, name, localUsers.id as userId FROM game INNER JOIN localGamePlayers on game.id = localGamePlayers.gameId INNER JOIN localUsers on playerId = localUsers.id order by game.id ASC, userId ASC";
  104. IDbConnection conn = new SqliteConnection(databaseUrl);
  105. conn.Open();
  106. IDbCommand cmd = conn.CreateCommand();
  107. cmd.CommandText = sql;
  108. IDataReader reader = cmd.ExecuteReader();
  109. int currentGame = -1;
  110. GameObject localGame;
  111. LocalGameScript lgs = null;
  112. string currentPlayerName = "";
  113. int currentPoints = 0;
  114. while (reader.Read()) {
  115. if (currentGame != reader.GetInt32(0)) {
  116. localGame = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  117. lgs = localGame.GetComponent<LocalGameScript>();
  118. lgs.GameId = reader.GetInt32(0);
  119. lgs.GameMode = reader.GetString(1);
  120. lgs.QuestionsNeededToWin = reader.GetInt32(2).ToString();
  121. lgs.AnswerTimer = reader.GetInt32(3);
  122. lgs.NumberOfPlayers = reader.GetInt32(4).ToString();
  123. lgs.CurrentPlayer = reader.GetString(5);
  124. lgs.Round = reader.GetInt32(6).ToString();
  125. lgs.StartDate = reader.GetString(7);
  126. lgs.LastPlayedDate = reader.GetString(8);
  127. lgs.FinishedDate = reader.GetString(9);
  128. currentPlayerName = reader.GetString(10);
  129. currentPoints = GetPlayerPoints(reader.GetInt32(0), currentPlayerName);
  130. lgs.AddPlayer(currentPlayerName, currentPoints);
  131. games.Add(lgs);
  132. } else if (currentGame == reader.GetInt32(0)) {
  133. currentPlayerName = reader.GetString(10);
  134. currentPoints = GetPlayerPoints(currentGame, currentPlayerName);
  135. lgs.AddPlayer(currentPlayerName, currentPoints);
  136. }
  137. currentGame = reader.GetInt32(0);
  138. }
  139. reader.Close();
  140. cmd.Dispose();
  141. conn.Close();
  142. return games;
  143. }
  144. internal void SetQuestionsLost(int gameId, string playerName, int questionsLost) {
  145. string sql = "UPDATE localGamePlayers SET questionsLost = questionsLost + " + questionsLost + " WHERE gameId = " + gameId + " AND playerId = (SELECT id FROM localUsers WHERE name = '" + playerName + "')";
  146. IDbConnection conn = new SqliteConnection(databaseUrl);
  147. conn.Open();
  148. IDbCommand cmd = conn.CreateCommand();
  149. cmd.CommandText = sql;
  150. cmd.ExecuteNonQuery();
  151. cmd.Dispose();
  152. conn.Close();
  153. }
  154. internal void RemoveGame(int gameId) {
  155. string sql = "DELETE FROM game WHERE id = " + gameId;
  156. List<LocalGameScript> games = new List<LocalGameScript>();
  157. IDbConnection conn = new SqliteConnection(databaseUrl);
  158. conn.Open();
  159. IDbCommand cmd = conn.CreateCommand();
  160. cmd.CommandText = sql;
  161. cmd.ExecuteNonQuery();
  162. string deleteLockedQuestionsSql = "DELETE FROM usersLockedQuestions WHERE gameId = " + gameId;
  163. cmd.CommandText = deleteLockedQuestionsSql;
  164. cmd.ExecuteNonQuery();
  165. cmd.Dispose();
  166. conn.Close();
  167. }
  168. public string GetCurrentPlayer(int gameId) {
  169. string sql = "SELECT currentPlayer FROM game WHERE id = " + gameId;
  170. IDbConnection conn = new SqliteConnection(databaseUrl);
  171. conn.Open();
  172. IDbCommand cmd = conn.CreateCommand();
  173. cmd.CommandText = sql;
  174. IDataReader reader = cmd.ExecuteReader();
  175. string currentPlayer = "";
  176. while (reader.Read()) {
  177. currentPlayer = reader.GetString(0);
  178. }
  179. reader.Close();
  180. cmd.Dispose();
  181. conn.Close();
  182. return currentPlayer;
  183. }
  184. public void SetCurrentPlayer(int gameId, string currentPlayer) {
  185. string sql = "UPDATE game SET currentPlayer = '" + currentPlayer + "' WHERE id = " + gameId;
  186. IDbConnection conn = new SqliteConnection(databaseUrl);
  187. conn.Open();
  188. IDbCommand cmd = conn.CreateCommand();
  189. cmd.CommandText = sql;
  190. cmd.ExecuteNonQuery();
  191. cmd.Dispose();
  192. conn.Close();
  193. }
  194. internal int GetPlayerPoints(int gameId, string playerName) {
  195. string sql = "SELECT count(*) FROM usersLockedQuestions WHERE gameId = " + gameId + " AND playerName = '" +playerName + "'";
  196. IDbConnection conn = new SqliteConnection(databaseUrl);
  197. conn.Open();
  198. IDbCommand cmd = conn.CreateCommand();
  199. cmd.CommandText = sql;
  200. IDataReader reader = cmd.ExecuteReader();
  201. int points = 0;
  202. while (reader.Read()) {
  203. points = reader.GetInt32(0);
  204. }
  205. reader.Close();
  206. cmd.Dispose();
  207. conn.Close();
  208. return points;
  209. }
  210. internal void SetFinishedDate(int gameId, string finishedDate) {
  211. string sql = "UPDATE game SET finishedDate = '" + finishedDate + "' WHERE id = " + gameId;
  212. IDbConnection conn = new SqliteConnection(databaseUrl);
  213. conn.Open();
  214. IDbCommand cmd = conn.CreateCommand();
  215. cmd.CommandText = sql;
  216. cmd.ExecuteNonQuery();
  217. cmd.Dispose();
  218. conn.Close();
  219. }
  220. internal void SetRoundValue(int gameId, int round) {
  221. string sql = "UPDATE game SET round = " + round + " WHERE id = " + gameId;
  222. IDbConnection conn = new SqliteConnection(databaseUrl);
  223. conn.Open();
  224. IDbCommand cmd = conn.CreateCommand();
  225. cmd.CommandText = sql;
  226. cmd.ExecuteNonQuery();
  227. cmd.Dispose();
  228. conn.Close();
  229. }
  230. internal int GetRoundValue(int gameId) {
  231. if (this.round == -1) {
  232. string sql = "SELECT round FROM game WHERE id = " + gameId;
  233. IDbConnection conn = new SqliteConnection(databaseUrl);
  234. conn.Open();
  235. IDbCommand cmd = conn.CreateCommand();
  236. cmd.CommandText = sql;
  237. IDataReader reader = cmd.ExecuteReader();
  238. while (reader.Read()) {
  239. this.round = reader.GetInt32(0);
  240. }
  241. reader.Close();
  242. cmd.Dispose();
  243. conn.Close();
  244. }
  245. return this.round;
  246. }
  247. internal List<KeyValuePair<string, int>> GetPlayersForGame(int gameId) {
  248. string sql = "SELECT name, (SELECT count(*) FROM usersLockedQuestions WHERE playerName = localUsers.name AND gameId = " + gameId + ") as numAnswers FROM localGamePlayers " +
  249. "LEFT JOIN localUsers ON localGamePlayers.playerId = localUsers.id " +
  250. "WHERE gameId = " + gameId;
  251. IDbConnection conn = new SqliteConnection(databaseUrl);
  252. conn.Open();
  253. IDbCommand cmd = conn.CreateCommand();
  254. cmd.CommandText = sql;
  255. IDataReader reader = cmd.ExecuteReader();
  256. List<KeyValuePair<string, int>> returnList = new List<KeyValuePair<string, int>>();
  257. while (reader.Read()) {
  258. KeyValuePair<string, int> player = new KeyValuePair<string, int>(reader.GetString(0), reader.GetInt32(1));
  259. returnList.Add(player);
  260. }
  261. return returnList;
  262. }
  263. public string AnswerString { get => answerString; set => answerString = value; }
  264. public string IdString { get => idString; set => idString = value; }
  265. public string CategoryString { get => categoryString; set => categoryString = value; }
  266. public void SetLocalOrOnline(string type) {
  267. gameMode = type;
  268. if (type.Equals("Local")) {
  269. string databaseName = "narKampenLocal.db";
  270. if (Application.platform == RuntimePlatform.Android) {
  271. databaseUrl = Application.persistentDataPath + "/" + databaseName;
  272. if (!File.Exists(databaseUrl)) {
  273. UnityWebRequest load = UnityWebRequest.Get("jar:file://" + Application.dataPath + "!/assets/" + databaseName);
  274. load.SendWebRequest();
  275. while (!load.isDone) { }
  276. File.WriteAllBytes(databaseUrl, load.downloadHandler.data);
  277. }
  278. databaseUrl = "URI=file:" + databaseUrl;
  279. } else {
  280. databaseUrl = "URI=file:" + Application.dataPath + "/narKampenLocal.db";
  281. }
  282. } else {
  283. databaseUrl = onlineQuestionsUrl;
  284. }
  285. connectionType = type;
  286. }
  287. internal int GetQuestionsLost(int gameId, string playerName) {
  288. string sql = "SELECT questionsLost FROM localGamePlayers WHERE gameId = " + gameId + " AND playerId = (SELECT id from localUsers WHERE name = '" + playerName + "')";
  289. IDbConnection conn = new SqliteConnection(databaseUrl);
  290. conn.Open();
  291. IDbCommand cmd = conn.CreateCommand();
  292. cmd.CommandText = sql;
  293. IDataReader reader = cmd.ExecuteReader();
  294. int returnValue = 0;
  295. while (reader.Read()) {
  296. returnValue = reader.GetInt32(0);
  297. }
  298. reader.Close();
  299. cmd.Dispose();
  300. conn.Close();
  301. return returnValue;
  302. }
  303. public string GetGameMode(int gameId) {
  304. if (this.gameMode == null) {
  305. string sql = "SELECT gameMode FROM game WHERE id = " + gameId;
  306. IDbConnection conn = new SqliteConnection(databaseUrl);
  307. conn.Open();
  308. IDbCommand cmd = conn.CreateCommand();
  309. cmd.CommandText = sql;
  310. IDataReader reader = cmd.ExecuteReader();
  311. while (reader.Read()) {
  312. this.gameMode = reader.GetString(0);
  313. }
  314. reader.Close();
  315. cmd.Dispose();
  316. conn.Close();
  317. }
  318. return this.gameMode;
  319. }
  320. public void LinkPlayersToLocalGame(List<string> playerNames, int gameId) {
  321. IDbConnection conn = new SqliteConnection(databaseUrl);
  322. conn.Open();
  323. string questionSql = "SELECT id FROM questions order by random() limit 1";
  324. IDbCommand cmd = conn.CreateCommand();
  325. cmd.CommandText = questionSql;
  326. IDataReader reader = cmd.ExecuteReader();
  327. int questionId = -1;
  328. while (reader.Read()) {
  329. questionId = reader.GetInt32(0);
  330. }
  331. foreach (string player in playerNames) {
  332. string sql = "SELECT id FROM localUsers WHERE name = '" + player + "'";
  333. int playerId;
  334. cmd = conn.CreateCommand();
  335. cmd.CommandText = sql;
  336. reader = cmd.ExecuteReader();
  337. if (reader.Read()) {
  338. playerId = reader.GetInt32(0);
  339. } else {
  340. reader.Close();
  341. sql = "INSERT INTO localUsers (name) VALUES ('" + player + "')";
  342. cmd.CommandText = sql;
  343. cmd.ExecuteNonQuery();
  344. cmd.CommandText = "SELECT last_insert_rowid()";
  345. Int64 lastInsert64 = (Int64)cmd.ExecuteScalar();
  346. playerId = (int)lastInsert64;
  347. }
  348. cmd.Dispose();
  349. reader.Close();
  350. LinkPlayerToGame(playerId, gameId);
  351. SavePlayersQuestion(questionId.ToString(), player, gameId);
  352. }
  353. IDbCommand cmd2 = conn.CreateCommand();
  354. cmd2.CommandText = "UPDATE game SET currentPlayer = '" + playerNames[0] + "' WHERE id = " + gameId;
  355. cmd2.ExecuteNonQuery();
  356. cmd2.Dispose();
  357. conn.Close();
  358. }
  359. private void LinkPlayerToGame(int playerId, int gameId) {
  360. string sql = "INSERT INTO localGamePlayers (gameId, playerId) VALUES (" + gameId + ", " + playerId + ")";
  361. SqliteConnection conn2 = new SqliteConnection(databaseUrl);
  362. conn2.Open();
  363. IDbCommand cmd = conn2.CreateCommand();
  364. cmd.CommandText = sql;
  365. cmd.ExecuteNonQuery();
  366. cmd.Dispose();
  367. conn2.Close();
  368. }
  369. internal int GetWinCondition(int gameId) {
  370. if (winAmount == -1) {
  371. string sql = "SELECT winNumber FROM game WHERE id = " + gameId;
  372. IDbConnection conn = new SqliteConnection(databaseUrl);
  373. conn.Open();
  374. IDbCommand cmd = conn.CreateCommand();
  375. cmd.CommandText = sql;
  376. IDataReader reader = cmd.ExecuteReader();
  377. while (reader.Read()) {
  378. this.winAmount = reader.GetInt32(0);
  379. }
  380. reader.Close();
  381. cmd.Dispose();
  382. conn.Close();
  383. }
  384. return this.winAmount;
  385. }
  386. public int SetupNewLocalGame(int winNumber, int numberOfPlayers, int questionTimer) {
  387. string sql = "INSERT INTO game (winNumber, numberOfPlayers, answerTimer, gameMode, startedDate) VALUES (" + winNumber + "," + numberOfPlayers + "," + questionTimer + ", 'Local', DATE())";
  388. IDbConnection conn = new SqliteConnection(databaseUrl);
  389. conn.Open();
  390. IDbCommand cmd = conn.CreateCommand();
  391. cmd.CommandText = sql;
  392. int status = cmd.ExecuteNonQuery();
  393. cmd.CommandText = "SELECT last_insert_rowid()";
  394. Int64 lastInsert64 = (Int64)cmd.ExecuteScalar();
  395. return (int)lastInsert64;
  396. }
  397. public NewQuestion GetNewQuestion(List<int> userAnsweredQuestions, string userName) {
  398. int gameId = GameObject.Find("GameManager").GetComponent<GameManagerScript>().GameId;
  399. Color32 questionCategoryColor = new Color32(0,0,20,20);
  400. if (connectionType == null) {
  401. SetLocalOrOnline(GetGameMode(gameId));
  402. }
  403. if (connectionType == "Local") {
  404. IDbConnection conn = new SqliteConnection(databaseUrl);
  405. conn.Open();
  406. IDbCommand cmd = conn.CreateCommand();
  407. string answeredIds = String.Join(",", userAnsweredQuestions);
  408. string sql = "SELECT questions.id, question, answer, categoryId as category, name, category.R, category.G, category.B, category.A FROM questions INNER JOIN questionToCategory ON questions.id = questionToCategory.questionId INNER JOIN category on category.id = questionToCategory.categoryId WHERE questions.id NOT IN (" + answeredIds + ") AND questions.id NOT IN (SELECT questionId FROM questionsInGame WHERE gameId = " + gameId + " AND userId = (SELECT id from localUsers WHERE name = '" + userName + "')) ORDER BY RANDOM() limit 1";
  409. cmd.CommandText = sql;
  410. IDataReader reader = cmd.ExecuteReader();
  411. int id = -1;
  412. while (reader.Read()) {
  413. id = reader.GetInt32(0);
  414. string question = reader.GetString(1);
  415. int answer = reader.GetInt32(2);
  416. int catergoryId = reader.GetInt32(3);
  417. string categoryName = reader.GetString(4);
  418. idString = id.ToString();
  419. questionString = question;
  420. categoryString = catergoryId.ToString();
  421. answerString = answer.ToString();
  422. byte r = (byte)reader.GetInt32(5);
  423. byte g = (byte)reader.GetInt32(6);
  424. byte b = (byte)reader.GetInt32(7);
  425. byte a = (byte)reader.GetInt32(8);
  426. questionCategoryColor = new Color32(r, g, b, a);
  427. }
  428. reader.Close();
  429. string saveSentQuestionSql = "INSERT INTO questionsInGame (gameId, questionId, userId) VALUES (" + gameId + ", " + id + ", (SELECT id FROM localUsers WHERE name = '" + userName + "'))";
  430. cmd.CommandText = saveSentQuestionSql;
  431. cmd.ExecuteNonQuery();
  432. cmd.Dispose();
  433. conn.Close();
  434. } else { // Connect Through db
  435. Debug.Log("Online Call");
  436. StartCoroutine(GetQuestionData(false));
  437. }
  438. NewQuestion q = NewQuestion.Instance();
  439. q.questionString = questionString;
  440. q.answerString = answerString;
  441. q.categoryString = categoryString;
  442. q.idString = idString;
  443. q.SetQuestionCategoryColor(questionCategoryColor);
  444. return q;
  445. }
  446. public void SavePlayersQuestion(string questionId, string playerNameValue, int gameId) {
  447. if (databaseUrl == null) {
  448. SetLocalOrOnline(GetGameMode(gameId));
  449. }
  450. Int32.TryParse(questionId, out int qId);
  451. string sql = "INSERT OR IGNORE INTO usersLockedQuestions (playerName, questionId, gameId) VALUES ('" + playerNameValue + "'," + qId + "," + gameId + ")";
  452. IDbConnection conn = new SqliteConnection(databaseUrl);
  453. conn.Open();
  454. IDbCommand cmd = conn.CreateCommand();
  455. cmd.CommandText = sql;
  456. cmd.ExecuteReader();
  457. cmd.Dispose();
  458. conn.Close();
  459. }
  460. public List<QuestionCard> GetPlayerQuestions(int gameId, string playerNameValue) {
  461. if (databaseUrl == null) {
  462. SetLocalOrOnline(GetGameMode(gameId));
  463. }
  464. List<QuestionCard> questions = new List<QuestionCard>();
  465. if (connectionType.Equals("Local")) {
  466. string sql = "SELECT * FROM questions inner join questionToCategory on questions.id = questionToCategory.questionId INNER JOIN category ON category.id = questionToCategory.categoryId WHERE questions.id IN ( SELECT questionId FROM usersLockedQuestions WHERE gameId = " + gameId + " AND playerName = '" + playerNameValue + "') ORDER BY answer ASC";
  467. IDbConnection conn = new SqliteConnection(databaseUrl);
  468. conn.Open();
  469. IDbCommand cmd = conn.CreateCommand();
  470. cmd.CommandText = sql;
  471. IDataReader reader = cmd.ExecuteReader();
  472. while (reader.Read()) {
  473. GameObject question = Instantiate(questionCardPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  474. QuestionCard q = question.GetComponent<QuestionCard>();
  475. q.SetAnswerText(reader.GetInt32(2).ToString());
  476. q.SetQuestionText(reader.GetString(1));
  477. q.idString = reader.GetInt32(0).ToString();
  478. Color32 questionCategoryColor = new Color32((byte)reader.GetInt32(7), (byte)reader.GetInt32(8), (byte)reader.GetInt32(9), (byte)reader.GetInt32(10));
  479. q.SetQuestionCategoryColor(questionCategoryColor);
  480. questions.Add(q);
  481. }
  482. cmd.Dispose();
  483. conn.Close();
  484. }
  485. return questions;
  486. }
  487. private IEnumerator GetQuestionData(bool showAnswer) {
  488. UnityWebRequest www = UnityWebRequest.Get("nordh.xyz/narKampen/dbFiles/Question.php");
  489. yield return www.SendWebRequest();
  490. if (www.isNetworkError || www.isHttpError) {
  491. Debug.Log(www.error);
  492. } else {
  493. while (!www.isDone) {
  494. yield return null;
  495. }
  496. // Show result
  497. string jsonData = www.downloadHandler.text;
  498. jsonData = "{\"questionsList\" : [ " + jsonData + " ]}";
  499. Questions qe = new Questions();
  500. JsonUtility.FromJsonOverwrite(jsonData, qe);
  501. questionString = qe.questionsList[0].question;
  502. answerString = qe.questionsList[0].answer;
  503. idString = qe.questionsList[0].id;
  504. categoryString = qe.questionsList[0].category;
  505. }
  506. }
  507. }