OnlineDatabase.cs 25 KB

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