OnlineDatabase.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 OnlineDatabase : MonoBehaviour {
  10. private const string onlineQuestionsUrl = "nordh.xyz/narKampen/dbFiles/Question.php";
  11. private const string serverUrl = "nordh.xyz/narKampen/dbFiles/";
  12. string databaseUrl;
  13. string gameMode;
  14. int winAmount = -1;
  15. int questionTimer = -1;
  16. public GameObject questionCardPrefab;
  17. private static OnlineDatabase instance;
  18. public static OnlineDatabase Instance { get { return instance; } }
  19. private void Awake() {
  20. if (instance == null) {
  21. instance = this;
  22. }
  23. }
  24. internal List<CategoryPanel.Category> GetCategories(List<CategoryPanel.Category> list) {
  25. string response = CallOnlineDatabaseWithResponse("Categories.php", null);
  26. response = "{\"categoryList\" : " + response + " }";
  27. Categories categories = new Categories();
  28. Debug.Log(response);
  29. JsonUtility.FromJsonOverwrite(response, categories);
  30. foreach (Category c in categories.categoryList) {
  31. CategoryPanel.Category cat = new CategoryPanel.Category();
  32. cat.color = new Color32((byte)c.r, (byte)c.g, (byte)c.b, (byte)c.a);
  33. cat.name = c.name;
  34. cat.id = c.id;
  35. list.Add(cat);
  36. }
  37. return list;
  38. }
  39. internal void SetupNewOnlineGame(int limitPerQuestion, int limitPerPlayer, int toWin, List<InviteSearchResult> inviteUsers) {
  40. List<int> playerIds = new List<int>();
  41. inviteUsers.ForEach(i => playerIds.Add(i.GetId()));
  42. int currentUser = Database.Instance.GetSignedInUser().Key;
  43. playerIds.Add(currentUser);
  44. var form = new WWWForm();
  45. form.AddField("currentUser", currentUser);
  46. form.AddField("winNumber", toWin);
  47. form.AddField("limitPerQuestion", limitPerQuestion);
  48. form.AddField("limitPerPlayer", limitPerPlayer);
  49. form.AddField("playerIds", String.Join(",", playerIds));
  50. Debug.Log(CallOnlineDatabaseWithResponse("NewOnlineGame.php", form));
  51. }
  52. [Serializable]
  53. public class Question {
  54. public string question;
  55. public string answer;
  56. public string id;
  57. public string category;
  58. }
  59. [Serializable]
  60. public class Questions {
  61. public List<Question> questionsList = new List<Question>();
  62. }
  63. [Serializable]
  64. public class PlayerInfo {
  65. public string username;
  66. public string status;
  67. }
  68. [Serializable]
  69. public class PlayerInfos {
  70. public List<PlayerInfo> playerInfoList = new List<PlayerInfo>();
  71. }
  72. [Serializable]
  73. public class GamePlayerInfo {
  74. public string username;
  75. public string userLockedQuestions;
  76. }
  77. [Serializable]
  78. public class GamePlayerInfos {
  79. public List<GamePlayerInfo> gamePlayerInfoList = new List<GamePlayerInfo>();
  80. }
  81. [Serializable]
  82. public class Category {
  83. public int r;
  84. public int g;
  85. public int b;
  86. public int a;
  87. public int id;
  88. public string name;
  89. }
  90. [Serializable]
  91. public class Categories {
  92. public List<Category> categoryList = new List<Category>();
  93. }
  94. [Serializable]
  95. public class OnlineGame {
  96. public string id;
  97. public string winNumber;
  98. public string answerTimer;
  99. public string roundTimeLimit;
  100. public string currentPlayer;
  101. public string round;
  102. public string startDate;
  103. public string LastPlayedDate;
  104. public string finishedDate;
  105. public string status;
  106. }
  107. [Serializable]
  108. public class OnlineGames {
  109. public List<OnlineGame> onlineGamesList = new List<OnlineGame>();
  110. }
  111. [Serializable]
  112. public class UserName {
  113. public string id;
  114. public string username;
  115. }
  116. [Serializable]
  117. public class UserNames {
  118. public List<UserName> usernamesList = new List<UserName>();
  119. }
  120. private void CallDatabase(string filename, WWWForm formData) {
  121. string postUrl = serverUrl + filename;
  122. UnityWebRequest www = UnityWebRequest.Post(postUrl, formData);
  123. www.SendWebRequest();
  124. if (www.isNetworkError || www.isHttpError) {
  125. Debug.Log(www.error);
  126. } else {
  127. while (!www.isDone) {
  128. }
  129. }
  130. }
  131. private string CallOnlineDatabaseWithResponse(string filename, WWWForm formData) {
  132. string postUrl = serverUrl + filename;
  133. UnityWebRequest www = UnityWebRequest.Post(postUrl, formData);
  134. www.SendWebRequest();
  135. if (www.isNetworkError || www.isHttpError) {
  136. Debug.Log(www.error);
  137. } else {
  138. while (!www.isDone) {
  139. }
  140. }
  141. return www.downloadHandler.text;
  142. }
  143. string questionString = "";
  144. string answerString = "";
  145. string idString = "";
  146. string categoryString = "";
  147. private int round = -1;
  148. private string SearchString;
  149. public string QuestionString { get => questionString; set => questionString = value; }
  150. private void Start() {
  151. if (instance == null) {
  152. instance = this;
  153. }
  154. }
  155. internal void SetLastPlayedDate(int gameId) {
  156. WWWForm form = new WWWForm();
  157. form.AddField("gameId", gameId);
  158. form.AddField("f", "SetLastPlayed");
  159. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  160. if (!response.Equals("")) {
  161. Debug.Log(response);
  162. }
  163. }
  164. internal void DeclineOnlineGame(string userName, int gameId) {
  165. WWWForm formData = new WWWForm();
  166. formData.AddField("userId", -1);
  167. formData.AddField("f", "decline");
  168. formData.AddField("gameId", gameId);
  169. formData.AddField("userName", userName);
  170. CallDatabase("OnlineGames.php", formData);
  171. }
  172. internal void AcceptOnlineGame(string userName, int gameId) {
  173. WWWForm formData = new WWWForm();
  174. formData.AddField("userId", -1);
  175. formData.AddField("f", "accept");
  176. formData.AddField("gameId", gameId);
  177. formData.AddField("userName", userName);
  178. CallDatabase("OnlineGames.php", formData);
  179. }
  180. internal List<OnlineGameScript> GetOnlineGames(int userId, string userName, GameObject prefab) {
  181. WWWForm formData = new WWWForm();
  182. formData.AddField("userId", userId);
  183. formData.AddField("f", "list");
  184. formData.AddField("gameId", -1);
  185. formData.AddField("userName", userName);
  186. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", formData);
  187. if (response.Equals("No games found for user")) {
  188. return null;
  189. }
  190. response = "{\"onlineGamesList\" : " + response + " }";
  191. OnlineGames og = new OnlineGames();
  192. JsonUtility.FromJsonOverwrite(response, og);
  193. GameObject onlineGameObject;
  194. OnlineGameScript ogs = null;
  195. List<OnlineGameScript> games = new List<OnlineGameScript>();
  196. foreach (OnlineGame game in og.onlineGamesList) {
  197. onlineGameObject = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  198. ogs = onlineGameObject.GetComponent<OnlineGameScript>();
  199. ogs.CurrentPlayer = game.currentPlayer;
  200. ogs.SetGameStatus(game.status);
  201. Int32.TryParse(game.id, out int gameId);
  202. List<KeyValuePair<string, string>> playerInfos = GetGameInfo(gameId);
  203. if (game.status.Equals("PENDING")) {
  204. string extraInfo = "";
  205. foreach (KeyValuePair<string, string> s in playerInfos) {
  206. if (s.Value.Equals("WAITING") && s.Key.Equals(userName)) {
  207. ogs.SetGameStatus("INVITED");
  208. extraInfo += s.Key + ",";
  209. } else if (s.Value.EndsWith("WAITING")) {
  210. extraInfo += s.Key + ",";
  211. }
  212. }
  213. extraInfo = extraInfo.TrimEnd(',');
  214. ogs.SetGameStatusText(extraInfo);
  215. } else if (game.status.Equals("OTHERS_TURN")) { // Wont work
  216. ogs.SetGameStatusText(game.currentPlayer);
  217. } else if (game.status.Equals("ACTIVE")) {
  218. ogs.SetGameStatusText(game.currentPlayer);
  219. } else {
  220. ogs.SetGameStatusText();
  221. }
  222. ogs.SetId(game.id);
  223. ogs.SetWinNumber(game.winNumber);
  224. ogs.SetAnswerTimer(game.answerTimer);
  225. ogs.SetRoundTimeLimit(game.roundTimeLimit);
  226. ogs.SetRound(game.round);
  227. ogs.StartDate = game.startDate;
  228. games.Add(ogs);
  229. ogs.PlayerInfos = playerInfos;
  230. }
  231. return games;
  232. }
  233. internal List<KeyValuePair<string, string>> GetGameInfo(int gameId) {
  234. List<KeyValuePair<string, string>> returnList = new List<KeyValuePair<string, string>>();
  235. WWWForm form = new WWWForm();
  236. form.AddField("gameId", gameId);
  237. string response = CallOnlineDatabaseWithResponse("OnlineGameInfo.php", form);
  238. response = "{\"playerInfoList\" : " + response + " }";
  239. PlayerInfos pi = new PlayerInfos();
  240. JsonUtility.FromJsonOverwrite(response, pi);
  241. foreach (PlayerInfo p in pi.playerInfoList) {
  242. KeyValuePair<string, string> player = new KeyValuePair<string, string>(p.username, p.status);
  243. returnList.Add(player);
  244. }
  245. return returnList;
  246. }
  247. internal void SetQuestionsLost(int gameId, string playerName, int questionsLost) {
  248. WWWForm form = new WWWForm();
  249. form.AddField("f", "SetQuestionsLost");
  250. form.AddField("questionsLost", questionsLost);
  251. form.AddField("userName", playerName);
  252. form.AddField("gameId", gameId);
  253. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  254. if (!response.Equals("")) {
  255. Debug.Log(response);
  256. }
  257. }
  258. internal void RemoveGame(int gameId) {
  259. WWWForm form = new WWWForm();
  260. form.AddField("f", "DeleteGame");
  261. form.AddField("gameId", gameId);
  262. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  263. if (!response.Equals("")) {
  264. Debug.Log(response);
  265. }
  266. }
  267. public string GetCurrentPlayer(int gameId) {
  268. WWWForm form = new WWWForm();
  269. form.AddField("f", "CurrentPlayer");
  270. form.AddField("gameId", gameId);
  271. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  272. if (response.Equals("")) {
  273. Debug.Log("Something wrong with current player for game with id: " + gameId);
  274. }
  275. return response;
  276. }
  277. public void SetCurrentPlayer(int gameId, string currentPlayer) {
  278. WWWForm form = new WWWForm();
  279. form.AddField("f", "SetCurrentPlayer");
  280. form.AddField("gameId", gameId);
  281. form.AddField("currentPlayer", currentPlayer);
  282. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  283. if (!response.Equals("")) {
  284. Debug.Log(response);
  285. }
  286. }
  287. internal int GetPlayerPoints(int gameId, string playerName) {
  288. WWWForm form = new WWWForm();
  289. form.AddField("f", "GetPlayerPoints");
  290. form.AddField("gameId", gameId);
  291. form.AddField("userName", playerName);
  292. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  293. if (response.Equals("")) {
  294. Debug.Log("Something wrong with current player for game with id: " + gameId);
  295. }
  296. Int32.TryParse(response, out int playerPoints);
  297. return playerPoints;
  298. }
  299. internal void SetFinishedDate(int gameId, string finishedDate) {
  300. WWWForm form = new WWWForm();
  301. form.AddField("f", "SetFinishedDate");
  302. form.AddField("gameId", gameId);
  303. form.AddField("finishedDate", finishedDate);
  304. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  305. if (!response.Equals("")) {
  306. Debug.Log(response);
  307. }
  308. }
  309. internal void SetRoundValue(int gameId, int round) {
  310. WWWForm form = new WWWForm();
  311. form.AddField("f", "SetRound");
  312. form.AddField("gameId", gameId);
  313. form.AddField("round", round);
  314. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  315. if (!response.Equals("")) {
  316. Debug.Log(response);
  317. }
  318. }
  319. internal int GetRoundValue(int gameId) {
  320. WWWForm form = new WWWForm();
  321. form.AddField("f", "GetRound");
  322. form.AddField("gameId", gameId);
  323. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  324. if (response.Equals("")) {
  325. Debug.Log("Something wrong with getting current player for game with id: " + gameId);
  326. }
  327. Int32.TryParse(response, out int round);
  328. return round;
  329. }
  330. internal List<KeyValuePair<string, int>> GetPlayersForGame(int gameId) {
  331. WWWForm form = new WWWForm();
  332. form.AddField("f", "GetPlayers");
  333. form.AddField("gameId", gameId);
  334. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  335. if (response.Equals("")) {
  336. Debug.Log("Something wrong with getting players from game with id: " + gameId);
  337. }
  338. GamePlayerInfos gpi = new GamePlayerInfos();
  339. JsonUtility.FromJsonOverwrite(response, gpi);
  340. foreach (GamePlayerInfo p in gpi.gamePlayerInfoList) {
  341. Int32.TryParse(p.userLockedQuestions, out int points);
  342. KeyValuePair<string, int> player = new KeyValuePair<string, int>(p.username, points);
  343. }
  344. string sql = "SELECT name, (SELECT count(*) FROM usersLockedQuestions WHERE playerName = localUsers.name AND gameId = " + gameId + ") as numAnswers FROM localGamePlayers " +
  345. "LEFT JOIN localUsers ON localGamePlayers.playerId = localUsers.id " +
  346. "WHERE gameId = " + gameId;
  347. IDbConnection conn = new SqliteConnection(databaseUrl);
  348. conn.Open();
  349. IDbCommand cmd = conn.CreateCommand();
  350. cmd.CommandText = sql;
  351. IDataReader reader = cmd.ExecuteReader();
  352. List<KeyValuePair<string, int>> returnList = new List<KeyValuePair<string, int>>();
  353. while (reader.Read()) {
  354. KeyValuePair<string, int> player = new KeyValuePair<string, int>(reader.GetString(0), reader.GetInt32(1));
  355. returnList.Add(player);
  356. }
  357. return returnList;
  358. }
  359. public string AnswerString { get => answerString; set => answerString = value; }
  360. public string IdString { get => idString; set => idString = value; }
  361. public string CategoryString { get => categoryString; set => categoryString = value; }
  362. internal int GetQuestionsLost(int gameId, string playerName) {
  363. string sql = "SELECT questionsLost FROM localGamePlayers WHERE gameId = " + gameId + " AND playerId = (SELECT id from localUsers WHERE name = '" + playerName + "')";
  364. IDbConnection conn = new SqliteConnection(databaseUrl);
  365. conn.Open();
  366. IDbCommand cmd = conn.CreateCommand();
  367. cmd.CommandText = sql;
  368. IDataReader reader = cmd.ExecuteReader();
  369. int returnValue = 0;
  370. while (reader.Read()) {
  371. returnValue = reader.GetInt32(0);
  372. }
  373. reader.Close();
  374. cmd.Dispose();
  375. conn.Close();
  376. return returnValue;
  377. }
  378. public string GetGameMode(int gameId) {
  379. if (this.gameMode == null) {
  380. string sql = "SELECT gameMode FROM game WHERE id = " + gameId;
  381. IDbConnection conn = new SqliteConnection(databaseUrl);
  382. conn.Open();
  383. IDbCommand cmd = conn.CreateCommand();
  384. cmd.CommandText = sql;
  385. IDataReader reader = cmd.ExecuteReader();
  386. while (reader.Read()) {
  387. this.gameMode = reader.GetString(0);
  388. }
  389. reader.Close();
  390. cmd.Dispose();
  391. conn.Close();
  392. }
  393. return this.gameMode;
  394. }
  395. public void LinkPlayersToLocalGame(List<string> playerNames, int gameId) {
  396. IDbConnection conn = new SqliteConnection(databaseUrl);
  397. conn.Open();
  398. string questionSql = "SELECT id FROM questions order by random() limit 1";
  399. IDbCommand cmd = conn.CreateCommand();
  400. cmd.CommandText = questionSql;
  401. IDataReader reader = cmd.ExecuteReader();
  402. int questionId = -1;
  403. while (reader.Read()) {
  404. questionId = reader.GetInt32(0);
  405. }
  406. foreach (string player in playerNames) {
  407. string sql = "SELECT id FROM localUsers WHERE name = '" + player + "'";
  408. int playerId;
  409. cmd = conn.CreateCommand();
  410. cmd.CommandText = sql;
  411. reader = cmd.ExecuteReader();
  412. if (reader.Read()) {
  413. playerId = reader.GetInt32(0);
  414. } else {
  415. reader.Close();
  416. sql = "INSERT INTO localUsers (name) VALUES ('" + player + "')";
  417. cmd.CommandText = sql;
  418. cmd.ExecuteNonQuery();
  419. cmd.CommandText = "SELECT last_insert_rowid()";
  420. Int64 lastInsert64 = (Int64)cmd.ExecuteScalar();
  421. playerId = (int)lastInsert64;
  422. }
  423. cmd.Dispose();
  424. reader.Close();
  425. LinkPlayerToGame(playerId, gameId);
  426. SavePlayersQuestion(questionId.ToString(), player, gameId);
  427. }
  428. IDbCommand cmd2 = conn.CreateCommand();
  429. cmd2.CommandText = "UPDATE game SET currentPlayer = '" + playerNames[0] + "' WHERE id = " + gameId;
  430. cmd2.ExecuteNonQuery();
  431. cmd2.Dispose();
  432. conn.Close();
  433. }
  434. private void LinkPlayerToGame(int playerId, int gameId) {
  435. string sql = "INSERT INTO localGamePlayers (gameId, playerId) VALUES (" + gameId + ", " + playerId + ")";
  436. SqliteConnection conn2 = new SqliteConnection(databaseUrl);
  437. conn2.Open();
  438. IDbCommand cmd = conn2.CreateCommand();
  439. cmd.CommandText = sql;
  440. cmd.ExecuteNonQuery();
  441. cmd.Dispose();
  442. conn2.Close();
  443. }
  444. internal int GetWinCondition(int gameId) {
  445. if (winAmount == -1) {
  446. string sql = "SELECT winNumber FROM game WHERE id = " + gameId;
  447. IDbConnection conn = new SqliteConnection(databaseUrl);
  448. conn.Open();
  449. IDbCommand cmd = conn.CreateCommand();
  450. cmd.CommandText = sql;
  451. IDataReader reader = cmd.ExecuteReader();
  452. while (reader.Read()) {
  453. this.winAmount = reader.GetInt32(0);
  454. }
  455. reader.Close();
  456. cmd.Dispose();
  457. conn.Close();
  458. }
  459. return this.winAmount;
  460. }
  461. public NewQuestion GetNewQuestion(List<int> userAnsweredQuestions, string userName) {
  462. int gameId = GameObject.Find("GameManager").GetComponent<GameManagerScript>().GameId;
  463. Color32 questionCategoryColor = new Color32(0, 0, 20, 20);
  464. StartCoroutine(GetQuestionData(false));
  465. NewQuestion q = NewQuestion.Instance();
  466. q.questionString = questionString;
  467. q.answerString = answerString;
  468. q.categoryString = categoryString;
  469. q.idString = idString;
  470. q.SetQuestionCategoryColor(questionCategoryColor);
  471. return q;
  472. }
  473. public void SavePlayersQuestion(string questionId, string playerNameValue, int gameId) {
  474. string gameMode = PlayerPrefs.GetString("GameMode");
  475. Int32.TryParse(questionId, out int qId);
  476. if (gameMode.Equals("Local")) {
  477. string sql = "INSERT OR IGNORE INTO usersLockedQuestions (playerName, questionId, gameId) VALUES ('" + playerNameValue + "'," + qId + "," + gameId + ")";
  478. IDbConnection conn = new SqliteConnection(databaseUrl);
  479. conn.Open();
  480. IDbCommand cmd = conn.CreateCommand();
  481. cmd.CommandText = sql;
  482. cmd.ExecuteReader();
  483. cmd.Dispose();
  484. conn.Close();
  485. } else {
  486. // TODO Save question to database online;
  487. }
  488. }
  489. public List<QuestionCard> GetPlayerQuestions(int gameId, string playerNameValue) {
  490. List<QuestionCard> questions = new List<QuestionCard>();
  491. /*
  492. if (connectionType.Equals("Local")) {
  493. 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";
  494. IDbConnection conn = new SqliteConnection(databaseUrl);
  495. conn.Open();
  496. IDbCommand cmd = conn.CreateCommand();
  497. cmd.CommandText = sql;
  498. IDataReader reader = cmd.ExecuteReader();
  499. while (reader.Read()) {
  500. GameObject question = Instantiate(questionCardPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  501. QuestionCard q = question.GetComponent<QuestionCard>();
  502. q.SetAnswerText(reader.GetInt32(2).ToString());
  503. q.SetQuestionText(reader.GetString(1));
  504. q.idString = reader.GetInt32(0).ToString();
  505. Color32 questionCategoryColor = new Color32((byte)reader.GetInt32(7), (byte)reader.GetInt32(8), (byte)reader.GetInt32(9), (byte)reader.GetInt32(10));
  506. q.SetQuestionCategoryColor(questionCategoryColor);
  507. questions.Add(q);
  508. }
  509. cmd.Dispose();
  510. conn.Close();
  511. }
  512. */
  513. // TODO Fix online call to question
  514. return questions;
  515. }
  516. private IEnumerator GetQuestionData(bool showAnswer) {
  517. UnityWebRequest www = UnityWebRequest.Get("nordh.xyz/narKampen/dbFiles/Question.php");
  518. yield return www.SendWebRequest();
  519. if (www.isNetworkError || www.isHttpError) {
  520. Debug.Log(www.error);
  521. } else {
  522. while (!www.isDone) {
  523. yield return null;
  524. }
  525. // Show result
  526. string jsonData = www.downloadHandler.text;
  527. jsonData = "{\"questionsList\" : [ " + jsonData + " ]}";
  528. Questions qe = new Questions();
  529. JsonUtility.FromJsonOverwrite(jsonData, qe);
  530. questionString = qe.questionsList[0].question;
  531. answerString = qe.questionsList[0].answer;
  532. idString = qe.questionsList[0].id;
  533. categoryString = qe.questionsList[0].category;
  534. }
  535. }
  536. public List<UserName> GetUsersToInvite(string searchString) {
  537. string postUrl = "nordh.xyz/narKampen/dbFiles/PlayerSearch.php?";
  538. postUrl += "search=" + UnityWebRequest.EscapeURL(searchString);
  539. UserNames uNames = new UserNames();
  540. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  541. www.SendWebRequest();
  542. if (www.isNetworkError || www.isHttpError) {
  543. Debug.Log(www.error);
  544. } else {
  545. while (!www.isDone) {
  546. }
  547. // Show result
  548. string jsonData = www.downloadHandler.text;
  549. jsonData = "{\"usernamesList\" : " + jsonData + " }";
  550. JsonUtility.FromJsonOverwrite(jsonData, uNames);
  551. }
  552. // TODO handle empty
  553. return uNames.usernamesList;
  554. }
  555. }