OnlineDatabase.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mono.Data.Sqlite;
  5. using System.Data;
  6. using System;
  7. using UnityEngine.Networking;
  8. using System.IO;
  9. public class OnlineDatabase : MonoBehaviour {
  10. private const string onlineQuestionsUrl = "nordh.xyz/narKampen/dbFiles/Question.php";
  11. private const string serverUrl = "nordh.xyz/narKampen/dbFiles/";
  12. string gameMode;
  13. int winAmount = -1;
  14. public GameObject questionCardPrefab;
  15. private static OnlineDatabase instance;
  16. public static OnlineDatabase Instance { get { return instance; } }
  17. private void Awake() {
  18. if (instance == null) {
  19. instance = this;
  20. }
  21. }
  22. internal List<CategoryPanel.Category> GetCategories(List<CategoryPanel.Category> list) {
  23. string response = CallOnlineDatabaseWithResponse("Categories.php", null);
  24. response = "{\"categoryList\" : " + response + " }";
  25. Categories categories = new Categories();
  26. JsonUtility.FromJsonOverwrite(response, categories);
  27. foreach (Category c in categories.categoryList) {
  28. CategoryPanel.Category cat = new CategoryPanel.Category();
  29. cat.color = new Color32((byte)c.r, (byte)c.g, (byte)c.b, (byte)c.a);
  30. cat.name = c.name;
  31. cat.id = c.id;
  32. list.Add(cat);
  33. }
  34. return list;
  35. }
  36. internal void SetupNewOnlineGame(int limitPerQuestion, int limitPerPlayer, int toWin, List<InviteSearchResult> inviteUsers) {
  37. List<int> playerIds = new List<int>();
  38. inviteUsers.ForEach(i => playerIds.Add(i.GetId()));
  39. int currentUser = Database.Instance.GetSignedInUser().Key;
  40. playerIds.Add(currentUser);
  41. var form = new WWWForm();
  42. form.AddField("currentUser", currentUser);
  43. form.AddField("winNumber", toWin);
  44. form.AddField("limitPerQuestion", limitPerQuestion);
  45. form.AddField("limitPerPlayer", limitPerPlayer);
  46. form.AddField("playerIds", String.Join(",", playerIds));
  47. string response = CallOnlineDatabaseWithResponse("NewOnlineGame.php", form);
  48. if (!response.Equals("")) {
  49. Debug.Log(response);
  50. }
  51. }
  52. [Serializable]
  53. public class Question {
  54. public string question;
  55. public string answer;
  56. public string id;
  57. public string category;
  58. public string categoryName;
  59. public int r;
  60. public int g;
  61. public int b;
  62. public int a;
  63. }
  64. [Serializable]
  65. public class Questions {
  66. public List<Question> questionsList = new List<Question>();
  67. }
  68. [Serializable]
  69. public class PlayerInfo {
  70. public string username;
  71. public string status;
  72. }
  73. [Serializable]
  74. public class PlayerInfos {
  75. public List<PlayerInfo> playerInfoList = new List<PlayerInfo>();
  76. }
  77. [Serializable]
  78. public class GamePlayerInfo {
  79. public string username;
  80. public string userLockedQuestions;
  81. }
  82. [Serializable]
  83. public class GamePlayerInfos {
  84. public List<GamePlayerInfo> gamePlayerInfoList = new List<GamePlayerInfo>();
  85. }
  86. [Serializable]
  87. public class Category {
  88. public int r;
  89. public int g;
  90. public int b;
  91. public int a;
  92. public int id;
  93. public string name;
  94. }
  95. [Serializable]
  96. public class Categories {
  97. public List<Category> categoryList = new List<Category>();
  98. }
  99. [Serializable]
  100. public class OnlineGame {
  101. public string id;
  102. public string winNumber;
  103. public string answerTimer;
  104. public string roundTimeLimit;
  105. public string currentPlayer;
  106. public string round;
  107. public string startDate;
  108. public string LastPlayedDate;
  109. public string finishedDate;
  110. public string status;
  111. public string playerToAct;
  112. }
  113. [Serializable]
  114. public class OnlineGames {
  115. public List<OnlineGame> onlineGamesList = new List<OnlineGame>();
  116. }
  117. [Serializable]
  118. public class UserName {
  119. public string id;
  120. public string username;
  121. }
  122. [Serializable]
  123. public class UserNames {
  124. public List<UserName> usernamesList = new List<UserName>();
  125. }
  126. private void CallDatabase(string filename, WWWForm formData) {
  127. string postUrl = serverUrl + filename;
  128. UnityWebRequest www = UnityWebRequest.Post(postUrl, formData);
  129. www.SendWebRequest();
  130. if (www.isNetworkError || www.isHttpError) {
  131. Debug.Log(www.error);
  132. } else {
  133. while (!www.isDone) {
  134. }
  135. }
  136. }
  137. private string CallOnlineDatabaseWithResponse(string filename, WWWForm formData) {
  138. string postUrl = serverUrl + filename;
  139. UnityWebRequest www = UnityWebRequest.Post(postUrl, formData);
  140. www.SendWebRequest();
  141. if (www.isNetworkError || www.isHttpError) {
  142. Debug.Log(www.error);
  143. } else {
  144. while (!www.isDone) {
  145. }
  146. }
  147. return www.downloadHandler.text;
  148. }
  149. string questionString = "";
  150. string answerString = "";
  151. string idString = "";
  152. string categoryString = "";
  153. public string QuestionString { get => questionString; set => questionString = value; }
  154. private void Start() {
  155. if (instance == null) {
  156. instance = this;
  157. }
  158. }
  159. internal void SetLastPlayedDate(int gameId) {
  160. WWWForm form = new WWWForm();
  161. form.AddField("gameId", gameId);
  162. form.AddField("f", "SetLastPlayed");
  163. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  164. if (!response.Equals("")) {
  165. Debug.Log(response);
  166. }
  167. }
  168. internal void DeclineOnlineGame(string userName, int gameId) {
  169. WWWForm formData = new WWWForm();
  170. formData.AddField("userId", -1);
  171. formData.AddField("f", "decline");
  172. formData.AddField("gameId", gameId);
  173. formData.AddField("userName", userName);
  174. CallDatabase("OnlineGames.php", formData);
  175. }
  176. internal void AcceptOnlineGame(string userName, int gameId) {
  177. WWWForm formData = new WWWForm();
  178. formData.AddField("userId", -1);
  179. formData.AddField("f", "accept");
  180. formData.AddField("gameId", gameId);
  181. formData.AddField("userName", userName);
  182. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", formData);
  183. if (!response.Equals("")) {
  184. Debug.Log(response);
  185. }
  186. }
  187. internal List<OnlineGameScript> GetOnlineGames(int userId, string userName, GameObject prefab) {
  188. WWWForm formData = new WWWForm();
  189. formData.AddField("userId", userId);
  190. formData.AddField("f", "list");
  191. formData.AddField("gameId", -1);
  192. formData.AddField("userName", userName);
  193. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", formData);
  194. if (response.Equals("No games found for user") || response.Equals("")) {
  195. return null;
  196. }
  197. response = "{\"onlineGamesList\" : " + response + " }";
  198. OnlineGames og = new OnlineGames();
  199. JsonUtility.FromJsonOverwrite(response, og);
  200. GameObject onlineGameObject;
  201. OnlineGameScript ogs = null;
  202. List<OnlineGameScript> games = new List<OnlineGameScript>();
  203. foreach (OnlineGame game in og.onlineGamesList) {
  204. onlineGameObject = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  205. ogs = onlineGameObject.GetComponent<OnlineGameScript>();
  206. ogs.CurrentPlayer = game.currentPlayer;
  207. ogs.SetGameStatus(game.status);
  208. Int32.TryParse(game.id, out int gameId);
  209. List<KeyValuePair<string, string>> playerInfos = GetGameInfo(gameId);
  210. if (game.status.Equals("PENDING")) {
  211. string extraInfo = "";
  212. foreach (KeyValuePair<string, string> s in playerInfos) {
  213. if (s.Value.Equals("WAITING") && s.Key.Equals(userName, StringComparison.InvariantCultureIgnoreCase)) {
  214. ogs.SetGameStatus("INVITED");
  215. extraInfo += s.Key + ",";
  216. } else if (s.Value.EndsWith("WAITING")) {
  217. extraInfo += s.Key + ",";
  218. }
  219. }
  220. extraInfo = extraInfo.TrimEnd(',');
  221. ogs.SetGameStatusText(extraInfo);
  222. } else if (game.status.Equals("OTHERS_TURN")) {
  223. ogs.SetGameStatusText(game.playerToAct);
  224. } else if (game.status.Equals("ACTIVE")) {
  225. ogs.SetGameStatusText(game.playerToAct);
  226. } else {
  227. ogs.SetGameStatusText();
  228. }
  229. ogs.SetId(game.id);
  230. ogs.SetWinNumber(game.winNumber);
  231. ogs.SetAnswerTimer(game.answerTimer);
  232. ogs.SetRoundTimeLimit(game.roundTimeLimit);
  233. ogs.SetRound(game.round);
  234. ogs.StartDate = game.startDate;
  235. games.Add(ogs);
  236. ogs.PlayerInfos = playerInfos;
  237. }
  238. return games;
  239. }
  240. internal List<KeyValuePair<string, string>> GetGameInfo(int gameId) {
  241. List<KeyValuePair<string, string>> returnList = new List<KeyValuePair<string, string>>();
  242. WWWForm form = new WWWForm();
  243. form.AddField("f", "GetGameInfo");
  244. form.AddField("gameId", gameId);
  245. string response = CallOnlineDatabaseWithResponse("OnlineGameInfo.php", form);
  246. response = "{\"playerInfoList\" : " + response + " }";
  247. PlayerInfos pi = new PlayerInfos();
  248. JsonUtility.FromJsonOverwrite(response, pi);
  249. foreach (PlayerInfo p in pi.playerInfoList) {
  250. KeyValuePair<string, string> player = new KeyValuePair<string, string>(p.username, p.status);
  251. returnList.Add(player);
  252. }
  253. return returnList;
  254. }
  255. internal void SetQuestionsLost(int gameId, string playerName, int questionsLost) {
  256. WWWForm form = new WWWForm();
  257. form.AddField("f", "SetQuestionsLost");
  258. form.AddField("questionsLost", questionsLost);
  259. form.AddField("userName", playerName);
  260. form.AddField("gameId", gameId);
  261. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  262. if (!response.Equals("")) {
  263. Debug.Log(response);
  264. }
  265. }
  266. internal void RemoveGame(int gameId) {
  267. WWWForm form = new WWWForm();
  268. form.AddField("f", "DeleteGame");
  269. form.AddField("gameId", gameId);
  270. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  271. if (!response.Equals("")) {
  272. Debug.Log(response);
  273. }
  274. }
  275. public string GetCurrentPlayer(int gameId) {
  276. WWWForm form = new WWWForm();
  277. form.AddField("f", "CurrentPlayer");
  278. form.AddField("gameId", gameId);
  279. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  280. if (response.Equals("")) {
  281. Debug.Log("Something wrong with current player for game with id: " + gameId);
  282. }
  283. return response;
  284. }
  285. public void SetCurrentPlayer(int gameId, string currentPlayer) {
  286. WWWForm form = new WWWForm();
  287. form.AddField("f", "SetCurrentPlayer");
  288. form.AddField("gameId", gameId);
  289. form.AddField("userName", currentPlayer);
  290. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  291. if (!response.Equals("")) {
  292. Debug.Log(response);
  293. }
  294. }
  295. internal int GetPlayerPoints(int gameId, string playerName) {
  296. WWWForm form = new WWWForm();
  297. form.AddField("f", "GetPlayerPoints");
  298. form.AddField("gameId", gameId);
  299. form.AddField("userName", playerName);
  300. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  301. if (response.Equals("")) {
  302. Debug.Log("Something wrong with current player for game with id: " + gameId);
  303. }
  304. Int32.TryParse(response, out int playerPoints);
  305. return playerPoints;
  306. }
  307. internal void SetFinishedDate(int gameId, string finishedDate) {
  308. WWWForm form = new WWWForm();
  309. form.AddField("f", "SetFinishedDate");
  310. form.AddField("gameId", gameId);
  311. form.AddField("finishedDate", finishedDate);
  312. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  313. if (!response.Equals("")) {
  314. Debug.Log(response);
  315. }
  316. }
  317. internal void SetRoundValue(int gameId, int round) {
  318. WWWForm form = new WWWForm();
  319. form.AddField("f", "SetRound");
  320. form.AddField("gameId", gameId);
  321. form.AddField("round", round);
  322. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  323. if (!response.Equals("")) {
  324. Debug.Log(response);
  325. }
  326. }
  327. internal int GetRoundValue(int gameId) {
  328. WWWForm form = new WWWForm();
  329. form.AddField("f", "GetRound");
  330. form.AddField("gameId", gameId);
  331. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  332. if (response.Equals("")) {
  333. Debug.Log("Something wrong with getting round for game with id: " + gameId);
  334. }
  335. Int32.TryParse(response, out int round);
  336. return round;
  337. }
  338. internal List<KeyValuePair<string, int>> GetPlayersForGame(int gameId) {
  339. WWWForm form = new WWWForm();
  340. form.AddField("f", "GetPlayers");
  341. form.AddField("gameId", gameId);
  342. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  343. if (response.Equals("")) {
  344. Debug.Log("Something wrong with getting players from game with id: " + gameId);
  345. }
  346. response = response = "{\"gamePlayerInfoList\" : " + response + " }";
  347. GamePlayerInfos gpi = new GamePlayerInfos();
  348. JsonUtility.FromJsonOverwrite(response, gpi);
  349. List<KeyValuePair<string, int>> returnList = new List<KeyValuePair<string, int>>();
  350. foreach (GamePlayerInfo p in gpi.gamePlayerInfoList) {
  351. Int32.TryParse(p.userLockedQuestions, out int points);
  352. KeyValuePair<string, int> player = new KeyValuePair<string, int>(p.username, points);
  353. returnList.Add(player);
  354. }
  355. return returnList;
  356. }
  357. public string AnswerString { get => answerString; set => answerString = value; }
  358. public string IdString { get => idString; set => idString = value; }
  359. public string CategoryString { get => categoryString; set => categoryString = value; }
  360. internal int GetQuestionsLost(int gameId, string playerName) {
  361. WWWForm form = new WWWForm();
  362. form.AddField("f", "GetQuestionsLost");
  363. form.AddField("userName", playerName);
  364. form.AddField("gameId", gameId);
  365. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  366. if (response.Equals("")) {
  367. Debug.Log("Something wrong with getting questions lost from game with id: " + gameId + " and playername " + playerName);
  368. }
  369. Int32.TryParse(response, out int questionsLost);
  370. return questionsLost;
  371. }
  372. internal int GetWinCondition(int gameId) {
  373. if (winAmount == -1) {
  374. WWWForm form = new WWWForm();
  375. form.AddField("gameId", gameId);
  376. form.AddField("f", "GetWinCondition");
  377. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  378. Int32.TryParse(response, out this.winAmount);
  379. }
  380. return this.winAmount;
  381. }
  382. public NewQuestionData newQuestionData;
  383. public NewQuestionData GetNewQuestion(List<int> userAnsweredQuestions, string userName) {
  384. int gameId = GameObject.Find("GameManager").GetComponent<GameManagerScript>().GameId;
  385. Question q = GetQuestionData(false);
  386. Color32 categoryColor = new Color32((byte)q.r, (byte)q.g, (byte)q.b, (byte)q.a);
  387. // Color32 questionCategoryColor = new Color32((byte)q.r, (byte)q.g, (byte)q.b, (byte)q.a);
  388. Int32.TryParse(q.category, out int categoryId);
  389. Int32.TryParse(q.id, out int questionId);
  390. NewQuestionData questionData = new NewQuestionData(q.answer, q.question, q.categoryName, categoryId, questionId, categoryColor);
  391. return questionData;
  392. }
  393. public void SavePlayersQuestion(List<int> questionsToSave, string playerNameValue, int gameId) {
  394. WWWForm form = new WWWForm();
  395. form.AddField("f", "SavePlayerQuestions");
  396. form.AddField("gameId", gameId);
  397. form.AddField("questionsToSave", String.Join(",",questionsToSave));
  398. form.AddField("userName", playerNameValue);
  399. string response = CallOnlineDatabaseWithResponse("OnlineGames.php", form);
  400. if (!response.Equals("")) {
  401. Debug.Log(response);
  402. }
  403. }
  404. public List<NewQuestionData> GetPlayerQuestions(int gameId, string playerName) {
  405. List<NewQuestionData> questions = new List<NewQuestionData>();
  406. WWWForm form = new WWWForm();
  407. form.AddField("f", "PlayerQuestions");
  408. form.AddField("gameId", gameId);
  409. form.AddField("userName", playerName);
  410. string response = CallOnlineDatabaseWithResponse("OnlineGameInfo.php", form);
  411. response = "{\"questionsList\" : " + response + "}";
  412. Questions ql = new Questions();
  413. JsonUtility.FromJsonOverwrite(response, ql);
  414. foreach (Question q in ql.questionsList) {
  415. /*GameObject question = Instantiate(questionCardPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  416. NewQuestionData qc = question.GetComponent<NewQuestionData>();
  417. */
  418. Color categoryColor = new Color(q.r, q.g, q.b, q.a);
  419. // Color32 questionCategoryColor = new Color32((byte)q.r, (byte)q.g, (byte)q.b, (byte)q.a);
  420. Int32.TryParse(q.category, out int categoryId);
  421. Int32.TryParse(q.id, out int questionId);
  422. NewQuestionData questionData = new NewQuestionData(q.answer, q.question, q.categoryName, categoryId, questionId, categoryColor);
  423. questions.Add(questionData);
  424. }
  425. return questions;
  426. }
  427. private Question GetQuestionData(bool showAnswer) {
  428. WWWForm form = new WWWForm();
  429. string response = CallOnlineDatabaseWithResponse("Question.php", form);
  430. // Show result
  431. response = "{\"questionsList\" : [ " + response + " ]}";
  432. Questions qe = new Questions();
  433. JsonUtility.FromJsonOverwrite(response, qe);
  434. return qe.questionsList[0];
  435. }
  436. public List<UserName> GetUsersToInvite(string searchString) {
  437. string postUrl = "nordh.xyz/narKampen/dbFiles/PlayerSearch.php?";
  438. postUrl += "search=" + UnityWebRequest.EscapeURL(searchString);
  439. UserNames uNames = new UserNames();
  440. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  441. www.SendWebRequest();
  442. if (www.isNetworkError || www.isHttpError) {
  443. Debug.Log(www.error);
  444. } else {
  445. while (!www.isDone) {
  446. }
  447. // Show result
  448. string jsonData = www.downloadHandler.text;
  449. if (!jsonData.Equals("")) {
  450. jsonData = "{\"usernamesList\" : " + jsonData + " }";
  451. JsonUtility.FromJsonOverwrite(jsonData, uNames);
  452. }
  453. }
  454. // TODO handle empty
  455. return uNames.usernamesList;
  456. }
  457. }