OnlineDatabase.cs 26 KB

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