OnlineDatabase.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. using System.Text;
  10. public class OnlineDatabase : MonoBehaviour {
  11. private const string onlineQuestionsUrl = "nordh.xyz/narKampen/dbFiles/Question.php";
  12. private const string serverUrl = "nordh.xyz/narKampen/dbFiles/";
  13. string gameMode;
  14. int winAmount = -1;
  15. public GameObject questionCardPrefab;
  16. private static OnlineDatabase instance;
  17. public static OnlineDatabase Instance { get { return instance; } }
  18. private void Awake() {
  19. if (instance == null) {
  20. instance = this;
  21. }
  22. }
  23. internal List<CategoryPanel.Category> GetCategories(List<CategoryPanel.Category> list, int gameId) {
  24. WWWForm form = new WWWForm();
  25. form.AddField(Constants.GAME_ID, gameId);
  26. string response = CallOnlineDatabaseWithResponse("Categories.php", form);
  27. response = "{\"categoryList\" : " + response + " }";
  28. Categories categories = new Categories();
  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. cat.questionCount = c.count;
  36. list.Add(cat);
  37. }
  38. return list;
  39. }
  40. internal int SetupNewOnlineGame(int limitPerQuestion, int limitPerPlayer, int toWin, List<InviteSearchResult> inviteUsers, List<int> selectedCategories) {
  41. List<int> playerIds = new List<int>();
  42. inviteUsers.ForEach(i => playerIds.Add(i.GetId()));
  43. List<string> playerNames = new List<String>();
  44. inviteUsers.ForEach(i => playerNames.Add(i.GetName()));
  45. int currentUser = Database.Instance.GetSignedInUser().Key;
  46. playerIds.Add(currentUser);
  47. var form = new WWWForm();
  48. form.AddField("currentUser", currentUser);
  49. form.AddField("winNumber", toWin);
  50. form.AddField("limitPerQuestion", limitPerQuestion);
  51. form.AddField("limitPerPlayer", limitPerPlayer);
  52. form.AddField("playerIds", String.Join(",", playerIds));
  53. form.AddField("categoryIds", String.Join(",", selectedCategories));
  54. string response = CallOnlineDatabaseWithResponse("NewOnlineGame.php", form);
  55. if (response.Equals("")) {
  56. Debug.Log("Expected gameId in response for creating new game but did not get one");
  57. }
  58. if (Int32.TryParse(response, out int newGameId)) {
  59. SendInviteForNewGame(newGameId, playerNames, Database.Instance.GetSignedInUser().Value);
  60. return newGameId;
  61. } else {
  62. Debug.Log("Failed to get new game id with response " + response);
  63. return -1;
  64. }
  65. }
  66. [Serializable]
  67. public class Question {
  68. public string question;
  69. public string answer;
  70. public string id;
  71. public string category;
  72. public string categoryName;
  73. public int r;
  74. public int g;
  75. public int b;
  76. public int a;
  77. }
  78. [Serializable]
  79. public class Questions {
  80. public List<Question> questionsList = new List<Question>();
  81. }
  82. [Serializable]
  83. public class PlayerInfo {
  84. public string username;
  85. public string status;
  86. public int playerRound;
  87. }
  88. [Serializable]
  89. public class PlayerInfos {
  90. public List<PlayerInfo> playerInfoList = new List<PlayerInfo>();
  91. }
  92. [Serializable]
  93. public class GamePlayerInfo {
  94. public string username;
  95. public string userLockedQuestions;
  96. public string playerRound;
  97. }
  98. [Serializable]
  99. public class GamePlayerInfos {
  100. public List<GamePlayerInfo> gamePlayerInfoList = new List<GamePlayerInfo>();
  101. }
  102. [Serializable]
  103. public class Category {
  104. public int r;
  105. public int g;
  106. public int b;
  107. public int a;
  108. public int id;
  109. public string name;
  110. public int count;
  111. }
  112. [Serializable]
  113. public class Categories {
  114. public List<Category> categoryList = new List<Category>();
  115. }
  116. [Serializable]
  117. public class OnlineGame {
  118. public string id;
  119. public string winNumber;
  120. public string answerTimer;
  121. public string roundTimeLimit;
  122. public string currentPlayer;
  123. public string round;
  124. public string startDate;
  125. public string lastPlayedDate;
  126. public string finishedDate;
  127. public string status;
  128. public string userId;
  129. public string username;
  130. public string userLockedQuestions;
  131. public string playerStatus;
  132. public string playerRound;
  133. }
  134. [Serializable]
  135. public class OnlineGames {
  136. public List<OnlineGame> onlineGamesList = new List<OnlineGame>();
  137. }
  138. [Serializable]
  139. public class UserName {
  140. public string id;
  141. public string username;
  142. }
  143. [Serializable]
  144. public class UserNames {
  145. public List<UserName> usernamesList = new List<UserName>();
  146. }
  147. private void CallDatabase(string filename, WWWForm formData) {
  148. string postUrl = serverUrl + filename;
  149. UnityWebRequest www = UnityWebRequest.Post(postUrl, formData);
  150. www.SendWebRequest();
  151. if (www.isNetworkError || www.isHttpError) {
  152. Debug.Log(www.error);
  153. } else {
  154. while (!www.isDone) {
  155. }
  156. }
  157. }
  158. private string CallOnlineDatabaseWithResponse(string filename, WWWForm formData) {
  159. string postUrl = serverUrl + filename;
  160. UnityWebRequest www = UnityWebRequest.Post(postUrl, formData);
  161. www.SendWebRequest();
  162. if (www.isNetworkError || www.isHttpError) {
  163. Debug.Log(www.error);
  164. } else {
  165. while (!www.isDone) {
  166. }
  167. }
  168. return www.downloadHandler.text;
  169. }
  170. string questionString = "";
  171. string answerString = "";
  172. string idString = "";
  173. string categoryString = "";
  174. public string QuestionString { get => questionString; set => questionString = value; }
  175. internal void SendGameOverMessage(int gameId, List<KeyValuePair<string, int>> players, string currentPlayer, int rounds)
  176. {
  177. string message = String.Format(LocalizationManager.Instance.GetText("GAME_OVER_MESSAGE"), currentPlayer, rounds.ToString());
  178. string title = LocalizationManager.Instance.GetText("GAME_OVER_TITLE");
  179. WWWForm form = new WWWForm();
  180. form.AddField(Constants.GAME_ID, gameId);
  181. form.AddField("message", message);
  182. form.AddField("title", title);
  183. form.AddField("winningPlayer", currentPlayer);
  184. form.AddField("type", "gameFinishedMessage");
  185. int index = 0;
  186. foreach (KeyValuePair<String, int> player in players) {
  187. form.AddField("player" + index++, player.Key);
  188. }
  189. CallOnlineDatabaseWithResponse("FirebaseCaller.php", form);
  190. }
  191. private void Start() {
  192. if (instance == null) {
  193. instance = this;
  194. }
  195. }
  196. internal void SetLastPlayedDate(int gameId) {
  197. WWWForm form = new WWWForm();
  198. form.AddField(Constants.GAME_ID, gameId);
  199. form.AddField("f", "SetLastPlayed");
  200. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  201. if (!response.Equals("")) {
  202. Debug.Log(response);
  203. }
  204. }
  205. internal void SetGameFinished(int gameId, string gameMode)
  206. {
  207. WWWForm formData = new WWWForm();
  208. formData.AddField(Constants.GAME_ID, gameId);
  209. formData.AddField("f", "GameFinished");
  210. CallDatabase("OnlineGames.php", formData);
  211. }
  212. internal void DeclineOnlineGame(string userName, int gameId) {
  213. WWWForm formData = new WWWForm();
  214. formData.AddField("userId", -1);
  215. formData.AddField("f", "decline");
  216. formData.AddField(Constants.GAME_ID, gameId);
  217. formData.AddField("userName", userName);
  218. CallDatabase("OnlineGames.php", formData);
  219. }
  220. internal void AcceptOnlineGame(string userName, int gameId) {
  221. WWWForm formData = new WWWForm();
  222. formData.AddField("userId", -1);
  223. formData.AddField("f", "accept");
  224. formData.AddField(Constants.GAME_ID, gameId);
  225. formData.AddField("userName", userName);
  226. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", formData);
  227. if (!response.Equals("")) {
  228. Debug.Log(response);
  229. }
  230. }
  231. //TODO Försök att ta bort alla utom 1 databas anrop i denna
  232. internal List<OnlineGameScript> GetOnlineGames(int userId, string userName, GameObject prefab) {
  233. WWWForm formData = new WWWForm();
  234. formData.AddField("userId", userId);
  235. formData.AddField("f", "list");
  236. formData.AddField(Constants.GAME_ID, -1);
  237. formData.AddField("userName", userName);
  238. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", formData);
  239. if (response.Equals("No games found for user") || response.Equals("")) {
  240. return null;
  241. }
  242. response = "{\"onlineGamesList\" : " + response + " }";
  243. OnlineGames og = new OnlineGames();
  244. JsonUtility.FromJsonOverwrite(response, og);
  245. GameObject onlineGameObject;
  246. OnlineGameScript ogs = null;
  247. List<OnlineGameScript> games = new List<OnlineGameScript>();
  248. int gameId = -1;
  249. OnlineGame prevGame = null;
  250. KeyValuePair<int, string> signedInUser = Database.Instance.GetSignedInUser();
  251. foreach (OnlineGame game in og.onlineGamesList) {
  252. Int32.TryParse(game.id, out int currentGameId);
  253. if (gameId != currentGameId) { // Spel ej i listan
  254. if (ogs != null) { // lägg till spel i listan, inte första gången
  255. SetGlobalGameInfo(userName, ogs, gameId, prevGame);
  256. if (ogs.GameStatus == null || ogs.GameStatus.Equals("")) {
  257. ogs.SetGameStatus(prevGame.status);
  258. }
  259. games.Add(ogs);
  260. }
  261. onlineGameObject = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  262. ogs = onlineGameObject.GetComponent<OnlineGameScript>();
  263. gameId = currentGameId;
  264. if (game.currentPlayer.Equals(game.userId)) { // om första raden i spelinfo är annan spelare och det är dennes tur att agera
  265. ogs.CurrentPlayer = game.username;
  266. }
  267. ogs.addPlayer(game.username);
  268. ogs.addPlayerInfo(game.username, game.playerStatus);
  269. } else { // Spel redan i listan, fyll på med info
  270. ogs.addPlayer(game.username);
  271. ogs.addPlayerInfo(game.username, game.playerStatus);
  272. if (game.currentPlayer.Equals(game.userId)) {
  273. ogs.CurrentPlayer = game.username;
  274. }
  275. }
  276. SetGameStatus(ogs, signedInUser, game);
  277. prevGame = game;
  278. }
  279. SetGameStatus(ogs, signedInUser, og.onlineGamesList[og.onlineGamesList.Count - 1]);
  280. SetGlobalGameInfo(userName, ogs, gameId, og.onlineGamesList[og.onlineGamesList.Count - 1]);
  281. if (ogs.GameStatus == null || ogs.GameStatus.Equals("")) {
  282. ogs.SetGameStatus(og.onlineGamesList[og.onlineGamesList.Count - 1].status);
  283. }
  284. games.Add(ogs);
  285. return games;
  286. }
  287. private void SetGameStatus(OnlineGameScript ogs, KeyValuePair<int, string> signedInUser, OnlineGame game) {
  288. if (game.currentPlayer.Equals(signedInUser.Key.ToString())) {
  289. if (game.status.Equals(OnlineGameScript.GAME_STATUS_ACTIVE)) {
  290. ogs.SetGameStatus(OnlineGameScript.GAME_STATUS_YOUR_TURN);
  291. } else if (game.status.Equals(OnlineGameScript.GAME_STATUS_PENDING)) {
  292. if (game.userId.Equals(signedInUser.Key.ToString()) && game.playerStatus.Equals("WAITING")) {
  293. ogs.SetGameStatus(OnlineGameScript.GAME_STATUS_INVITED);
  294. }
  295. }
  296. } else {
  297. if (game.status.Equals("ACTIVE")) {
  298. if (game.currentPlayer.Equals(game.userId)) {
  299. ogs.CurrentPlayer = game.username;
  300. }
  301. ogs.SetGameStatus(OnlineGameScript.GAME_STATUS_OTHERS_TURN);
  302. } else if (game.status.Equals(OnlineGameScript.GAME_STATUS_PENDING)) {
  303. if (game.userId.Equals(signedInUser.Key.ToString()) && game.playerStatus.Equals("WAITING")) {
  304. ogs.SetGameStatus(OnlineGameScript.GAME_STATUS_INVITED);
  305. }
  306. }
  307. }
  308. }
  309. private void SetGlobalGameInfo(string userName, OnlineGameScript ogs, int gameId, OnlineGame game) {
  310. ogs.SetId(game.id);
  311. ogs.SetWinNumber(game.winNumber);
  312. ogs.SetAnswerTimer(game.answerTimer);
  313. ogs.SetRoundTimeLimit(game.roundTimeLimit);
  314. ogs.SetRound(game.round);
  315. ogs.StartDate = game.startDate;
  316. ogs.LastPlayedDate = game.lastPlayedDate;
  317. }
  318. internal List<KeyValuePair<string, string>> GetGameInfo(int gameId) { // TODO Return new class GameInfo
  319. List<KeyValuePair<string, string>> returnList = new List<KeyValuePair<string, string>>();
  320. WWWForm form = new WWWForm();
  321. form.AddField("f", "GetGameInfo");
  322. form.AddField(Constants.GAME_ID, gameId);
  323. string response = CallOnlineDatabaseWithResponse("OnlineGameInfo.php", form);
  324. response = "{\"playerInfoList\" : " + response + " }";
  325. PlayerInfos pi = new PlayerInfos();
  326. JsonUtility.FromJsonOverwrite(response, pi);
  327. foreach (PlayerInfo p in pi.playerInfoList) {
  328. KeyValuePair<string, string> player = new KeyValuePair<string, string>(p.username, p.status);
  329. returnList.Add(player);
  330. }
  331. return returnList;
  332. }
  333. internal void SetQuestionsLost(int gameId, string playerName, int questionsLost) {
  334. WWWForm form = new WWWForm();
  335. form.AddField("f", "SetQuestionsLost");
  336. form.AddField("questionsLost", questionsLost);
  337. form.AddField("userName", playerName);
  338. form.AddField(Constants.GAME_ID, gameId);
  339. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  340. if (!response.Equals("")) {
  341. Debug.Log(response);
  342. }
  343. }
  344. internal void IncreasePlayerRound(int gameId, string currentPlayer) {
  345. WWWForm form = new WWWForm();
  346. form.AddField("player",currentPlayer);
  347. form.AddField(Constants.GAME_ID, gameId);
  348. form.AddField("f", "IncreasePlayerRound");
  349. CallDatabase("OnlineGames.php", form);
  350. }
  351. internal void RemoveGame(int gameId) {
  352. WWWForm form = new WWWForm();
  353. form.AddField("f", "DeleteGame");
  354. form.AddField(Constants.GAME_ID, gameId);
  355. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  356. if (!response.Equals("")) {
  357. Debug.Log(response);
  358. }
  359. }
  360. public string GetCurrentPlayer(int gameId) {
  361. WWWForm form = new WWWForm();
  362. form.AddField("f", "CurrentPlayer");
  363. form.AddField(Constants.GAME_ID, gameId);
  364. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  365. if (response.Equals("")) {
  366. Debug.Log("Something wrong with current player for game with id: " + gameId);
  367. }
  368. return response;
  369. }
  370. public void SetCurrentPlayer(int gameId, string currentPlayer) {
  371. WWWForm form = new WWWForm();
  372. form.AddField("f", "SetCurrentPlayer");
  373. form.AddField(Constants.GAME_ID, gameId);
  374. form.AddField("userName", currentPlayer);
  375. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  376. if (!response.Equals("")) {
  377. Debug.Log(response);
  378. }
  379. }
  380. internal int GetPlayerPoints(int gameId, string playerName) {
  381. WWWForm form = new WWWForm();
  382. form.AddField("f", "GetPlayerPoints");
  383. form.AddField(Constants.GAME_ID, gameId);
  384. form.AddField("userName", playerName);
  385. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  386. if (response.Equals("")) {
  387. Debug.Log("Something wrong with current player for game with id: " + gameId);
  388. }
  389. Int32.TryParse(response, out int playerPoints);
  390. return playerPoints;
  391. }
  392. internal void SetFinishedDate(int gameId, string finishedDate) {
  393. WWWForm form = new WWWForm();
  394. form.AddField("f", "SetFinishedDate");
  395. form.AddField(Constants.GAME_ID, gameId);
  396. form.AddField("finishedDate", finishedDate);
  397. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  398. if (!response.Equals("")) {
  399. Debug.Log(response);
  400. }
  401. }
  402. internal void SetRoundValue(int gameId, int round) {
  403. WWWForm form = new WWWForm();
  404. form.AddField("f", "SetRound");
  405. form.AddField(Constants.GAME_ID, gameId);
  406. form.AddField("round", round);
  407. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  408. if (!response.Equals("")) {
  409. Debug.Log(response);
  410. }
  411. }
  412. internal int GetRoundValue(int gameId) {
  413. WWWForm form = new WWWForm();
  414. form.AddField("f", "GetRound");
  415. form.AddField(Constants.GAME_ID, gameId);
  416. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  417. if (response.Equals("")) {
  418. Debug.Log("Something wrong with getting round for game with id: " + gameId);
  419. }
  420. Int32.TryParse(response, out int round);
  421. return round;
  422. }
  423. internal List<KeyValuePair<string, int>> GetPlayersForGame(int gameId) { // TODO update with new class GamePlayers as return
  424. WWWForm form = new WWWForm();
  425. form.AddField("f", "GetPlayers");
  426. form.AddField(Constants.GAME_ID, gameId);
  427. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  428. if (response.Equals("")) {
  429. Debug.Log("Something wrong with getting players from game with id: " + gameId);
  430. }
  431. response = response = "{\"gamePlayerInfoList\" : " + response + " }";
  432. GamePlayerInfos gpi = new GamePlayerInfos();
  433. JsonUtility.FromJsonOverwrite(response, gpi);
  434. List<KeyValuePair<string, int>> returnList = new List<KeyValuePair<string, int>>();
  435. foreach (GamePlayerInfo p in gpi.gamePlayerInfoList) {
  436. Int32.TryParse(p.userLockedQuestions, out int points);
  437. KeyValuePair<string, int> player = new KeyValuePair<string, int>(p.username, points);
  438. returnList.Add(player);
  439. }
  440. return returnList;
  441. }
  442. public string AnswerString { get => answerString; set => answerString = value; }
  443. public string IdString { get => idString; set => idString = value; }
  444. public string CategoryString { get => categoryString; set => categoryString = value; }
  445. internal int GetQuestionsLost(int gameId, string playerName) {
  446. WWWForm form = new WWWForm();
  447. form.AddField("f", "GetQuestionsLost");
  448. form.AddField("userName", playerName);
  449. form.AddField(Constants.GAME_ID, gameId);
  450. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  451. if (response.Equals("")) {
  452. Debug.Log("Something wrong with getting questions lost from game with id: " + gameId + " and playername " + playerName);
  453. }
  454. Int32.TryParse(response, out int questionsLost);
  455. return questionsLost;
  456. }
  457. internal int GetWinCondition(int gameId) {
  458. if (winAmount == -1) {
  459. WWWForm form = new WWWForm();
  460. form.AddField(Constants.GAME_ID, gameId);
  461. form.AddField("f", "GetWinCondition");
  462. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  463. Int32.TryParse(response, out this.winAmount);
  464. }
  465. return this.winAmount;
  466. }
  467. public NewQuestionData newQuestionData;
  468. public NewQuestionData GetNewQuestion(List<int> userAnsweredQuestions, int userId) {
  469. int gameId = GameObject.Find("GameManager").GetComponent<GameManagerScript>().GameId;
  470. Question q = GetQuestionData(gameId, userId);
  471. Color32 categoryColor = new Color32((byte)q.r, (byte)q.g, (byte)q.b, (byte)q.a);
  472. Int32.TryParse(q.category, out int categoryId);
  473. Int32.TryParse(q.id, out int questionId);
  474. NewQuestionData questionData = new NewQuestionData(q.answer, q.question, q.categoryName, categoryId, questionId, categoryColor, false);
  475. return questionData;
  476. }
  477. public void SavePlayersQuestion(List<int> questionsToSave, string playerNameValue, int gameId) {
  478. WWWForm form = new WWWForm();
  479. form.AddField("f", "SavePlayerQuestions");
  480. form.AddField(Constants.GAME_ID, gameId);
  481. form.AddField("questionsToSave", String.Join(",",questionsToSave));
  482. form.AddField("userName", playerNameValue);
  483. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  484. if (!response.Equals("")) {
  485. Debug.Log(response);
  486. }
  487. }
  488. public List<NewQuestionData> GetPlayerQuestions(int gameId, string playerName) {
  489. List<NewQuestionData> questions = new List<NewQuestionData>();
  490. WWWForm form = new WWWForm();
  491. form.AddField("f", "PlayerQuestions");
  492. form.AddField(Constants.GAME_ID, gameId);
  493. form.AddField("userName", playerName);
  494. string response = CallOnlineDatabaseWithResponse("OnlineGameInfo.php", form);
  495. response = "{\"questionsList\" : " + response + "}";
  496. Questions ql = new Questions();
  497. JsonUtility.FromJsonOverwrite(response, ql);
  498. foreach (Question q in ql.questionsList) {
  499. /*GameObject question = Instantiate(questionCardPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  500. NewQuestionData qc = question.GetComponent<NewQuestionData>();
  501. */
  502. Color32 categoryColor = new Color32((byte)q.r, (byte)q.g, (byte)q.b, (byte)q.a);
  503. Int32.TryParse(q.category, out int categoryId);
  504. Int32.TryParse(q.id, out int questionId);
  505. NewQuestionData questionData = new NewQuestionData(q.answer, q.question, q.categoryName, categoryId, questionId, categoryColor, true);
  506. questions.Add(questionData);
  507. }
  508. return questions;
  509. }
  510. private Question GetQuestionData(int gameId, int playerId) {
  511. WWWForm form = new WWWForm();
  512. form.AddField(Constants.GAME_ID, gameId);
  513. form.AddField("playerId", playerId);
  514. string response = CallOnlineDatabaseWithResponse("Question.php", form);
  515. // Show result
  516. response = "{\"questionsList\" : [ " + response + " ]}";
  517. Questions qe = new Questions();
  518. JsonUtility.FromJsonOverwrite(response, qe);
  519. return qe.questionsList[0];
  520. }
  521. public List<UserName> GetUsersToInvite(string searchString) {
  522. WWWForm form = new WWWForm();
  523. form.AddField("SearchString", UnityWebRequest.EscapeURL(searchString));
  524. form.AddField("f", "PlayerSearch");
  525. String response = CallOnlineDatabaseWithResponse("PlayerSearch.php", form);
  526. response = "{\"usernamesList\" : " + response + " }";
  527. UserNames uNames = new UserNames();
  528. JsonUtility.FromJsonOverwrite(response, uNames);
  529. return uNames.usernamesList;
  530. }
  531. internal void SendNextPlayerMessage(int gameId, String nextPlayer)
  532. {
  533. WWWForm form = new WWWForm();
  534. form.AddField(Constants.GAME_ID, gameId);
  535. form.AddField("playerName", nextPlayer);
  536. form.AddField("title", LocalizationManager.Instance.GetText("FCM_NEXT_PLAYER_TITLE"));
  537. List<KeyValuePair<string, int>> players = OnlineDatabase.Instance.GetPlayersForGame(gameId);
  538. StringBuilder sb = new StringBuilder();
  539. foreach (KeyValuePair<String, int> player in players)
  540. {
  541. sb.AppendLine(player.Key + " (" + player.Value + ")");
  542. }
  543. String message = String.Format(LocalizationManager.Instance.GetText("FCM_NEXT_PLAYER_MESSAGE"), sb.ToString());
  544. form.AddField("message", message);
  545. form.AddField("type", "FCMNextPlayer");
  546. CallOnlineDatabaseWithResponse("FirebaseCaller.php", form);
  547. }
  548. internal void SendInviteForNewGame(int gameId, List<String> Players, String inviter) {
  549. WWWForm form = new WWWForm();
  550. form.AddField(Constants.GAME_ID, gameId);
  551. int index = 0;
  552. foreach(String player in Players) {
  553. form.AddField("player" + index++, player);
  554. }
  555. form.AddField("title", LocalizationManager.Instance.GetText("FCM_NEW_GAME_TITLE"));
  556. form.AddField("message", String.Format(LocalizationManager.Instance.GetText("FCM_NEW_GAME_MESSAGE"), inviter));
  557. form.AddField("type", "InviteMessage");
  558. CallOnlineDatabaseWithResponse("FirebaseCaller.php", form);
  559. }
  560. internal void UpdatePlayerToken(int userId, string myToken)
  561. {
  562. WWWForm form = new WWWForm();
  563. form.AddField("userId", userId);
  564. form.AddField("token", myToken);
  565. form.AddField("f", "UpdatePlayerToken");
  566. CallDatabase("OnlineGames.php", form);
  567. }
  568. internal List<UserName> FindRandomPlayer(int playerId) {
  569. List<string> returnValue = new List<string>();
  570. WWWForm form = new WWWForm();
  571. form.AddField("playerId", playerId);
  572. form.AddField("f", "FindRandomPlayers");
  573. String response = CallOnlineDatabaseWithResponse("PlayerSearch.php", form);
  574. response = "{\"usernamesList\" : " + response + " }";
  575. UserNames uNames = new UserNames();
  576. JsonUtility.FromJsonOverwrite(response, uNames);
  577. return uNames.usernamesList;
  578. }
  579. }