OnlineDatabase.cs 27 KB

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